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 | /* |
| 222 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 223 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 224 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 225 | */ |
| 226 | struct loaded_vmcs { |
| 227 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 228 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 229 | int cpu; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 230 | bool launched; |
| 231 | bool nmi_known_unmasked; |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 232 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
| 233 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 234 | /* Support for vnmi-less CPUs */ |
| 235 | int soft_vnmi_blocked; |
| 236 | ktime_t entry_time; |
| 237 | s64 vnmi_blocked_time; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 238 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 239 | struct list_head loaded_vmcss_on_cpu_link; |
| 240 | }; |
| 241 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 242 | struct shared_msr_entry { |
| 243 | unsigned index; |
| 244 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 245 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 246 | }; |
| 247 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 248 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 249 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 250 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 251 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 252 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 253 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 254 | * 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] | 255 | * 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] | 256 | * underlying hardware which will be used to run L2. |
| 257 | * This structure is packed to ensure that its layout is identical across |
| 258 | * machines (necessary for live migration). |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 259 | * |
| 260 | * IMPORTANT: Changing the layout of existing fields in this structure |
| 261 | * will break save/restore compatibility with older kvm releases. When |
| 262 | * adding new fields, either use space in the reserved padding* arrays |
| 263 | * or add the new fields to the end of the structure. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 264 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 265 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 266 | struct __packed vmcs12 { |
| 267 | /* According to the Intel spec, a VMCS region must start with the |
| 268 | * following two fields. Then follow implementation-specific data. |
| 269 | */ |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 270 | struct vmcs_hdr hdr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 271 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 272 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 273 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 274 | u32 padding[7]; /* room for future expansion */ |
| 275 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 276 | u64 io_bitmap_a; |
| 277 | u64 io_bitmap_b; |
| 278 | u64 msr_bitmap; |
| 279 | u64 vm_exit_msr_store_addr; |
| 280 | u64 vm_exit_msr_load_addr; |
| 281 | u64 vm_entry_msr_load_addr; |
| 282 | u64 tsc_offset; |
| 283 | u64 virtual_apic_page_addr; |
| 284 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 285 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 286 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 287 | u64 eoi_exit_bitmap0; |
| 288 | u64 eoi_exit_bitmap1; |
| 289 | u64 eoi_exit_bitmap2; |
| 290 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 291 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 292 | u64 guest_physical_address; |
| 293 | u64 vmcs_link_pointer; |
| 294 | u64 guest_ia32_debugctl; |
| 295 | u64 guest_ia32_pat; |
| 296 | u64 guest_ia32_efer; |
| 297 | u64 guest_ia32_perf_global_ctrl; |
| 298 | u64 guest_pdptr0; |
| 299 | u64 guest_pdptr1; |
| 300 | u64 guest_pdptr2; |
| 301 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 302 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 303 | u64 host_ia32_pat; |
| 304 | u64 host_ia32_efer; |
| 305 | u64 host_ia32_perf_global_ctrl; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 306 | u64 vmread_bitmap; |
| 307 | u64 vmwrite_bitmap; |
| 308 | u64 vm_function_control; |
| 309 | u64 eptp_list_address; |
| 310 | u64 pml_address; |
| 311 | u64 padding64[3]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 312 | /* |
| 313 | * To allow migration of L1 (complete with its L2 guests) between |
| 314 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 315 | * unsigned long fields with no explict size. We use u64 (aliased |
| 316 | * natural_width) instead. Luckily, x86 is little-endian. |
| 317 | */ |
| 318 | natural_width cr0_guest_host_mask; |
| 319 | natural_width cr4_guest_host_mask; |
| 320 | natural_width cr0_read_shadow; |
| 321 | natural_width cr4_read_shadow; |
| 322 | natural_width cr3_target_value0; |
| 323 | natural_width cr3_target_value1; |
| 324 | natural_width cr3_target_value2; |
| 325 | natural_width cr3_target_value3; |
| 326 | natural_width exit_qualification; |
| 327 | natural_width guest_linear_address; |
| 328 | natural_width guest_cr0; |
| 329 | natural_width guest_cr3; |
| 330 | natural_width guest_cr4; |
| 331 | natural_width guest_es_base; |
| 332 | natural_width guest_cs_base; |
| 333 | natural_width guest_ss_base; |
| 334 | natural_width guest_ds_base; |
| 335 | natural_width guest_fs_base; |
| 336 | natural_width guest_gs_base; |
| 337 | natural_width guest_ldtr_base; |
| 338 | natural_width guest_tr_base; |
| 339 | natural_width guest_gdtr_base; |
| 340 | natural_width guest_idtr_base; |
| 341 | natural_width guest_dr7; |
| 342 | natural_width guest_rsp; |
| 343 | natural_width guest_rip; |
| 344 | natural_width guest_rflags; |
| 345 | natural_width guest_pending_dbg_exceptions; |
| 346 | natural_width guest_sysenter_esp; |
| 347 | natural_width guest_sysenter_eip; |
| 348 | natural_width host_cr0; |
| 349 | natural_width host_cr3; |
| 350 | natural_width host_cr4; |
| 351 | natural_width host_fs_base; |
| 352 | natural_width host_gs_base; |
| 353 | natural_width host_tr_base; |
| 354 | natural_width host_gdtr_base; |
| 355 | natural_width host_idtr_base; |
| 356 | natural_width host_ia32_sysenter_esp; |
| 357 | natural_width host_ia32_sysenter_eip; |
| 358 | natural_width host_rsp; |
| 359 | natural_width host_rip; |
| 360 | natural_width paddingl[8]; /* room for future expansion */ |
| 361 | u32 pin_based_vm_exec_control; |
| 362 | u32 cpu_based_vm_exec_control; |
| 363 | u32 exception_bitmap; |
| 364 | u32 page_fault_error_code_mask; |
| 365 | u32 page_fault_error_code_match; |
| 366 | u32 cr3_target_count; |
| 367 | u32 vm_exit_controls; |
| 368 | u32 vm_exit_msr_store_count; |
| 369 | u32 vm_exit_msr_load_count; |
| 370 | u32 vm_entry_controls; |
| 371 | u32 vm_entry_msr_load_count; |
| 372 | u32 vm_entry_intr_info_field; |
| 373 | u32 vm_entry_exception_error_code; |
| 374 | u32 vm_entry_instruction_len; |
| 375 | u32 tpr_threshold; |
| 376 | u32 secondary_vm_exec_control; |
| 377 | u32 vm_instruction_error; |
| 378 | u32 vm_exit_reason; |
| 379 | u32 vm_exit_intr_info; |
| 380 | u32 vm_exit_intr_error_code; |
| 381 | u32 idt_vectoring_info_field; |
| 382 | u32 idt_vectoring_error_code; |
| 383 | u32 vm_exit_instruction_len; |
| 384 | u32 vmx_instruction_info; |
| 385 | u32 guest_es_limit; |
| 386 | u32 guest_cs_limit; |
| 387 | u32 guest_ss_limit; |
| 388 | u32 guest_ds_limit; |
| 389 | u32 guest_fs_limit; |
| 390 | u32 guest_gs_limit; |
| 391 | u32 guest_ldtr_limit; |
| 392 | u32 guest_tr_limit; |
| 393 | u32 guest_gdtr_limit; |
| 394 | u32 guest_idtr_limit; |
| 395 | u32 guest_es_ar_bytes; |
| 396 | u32 guest_cs_ar_bytes; |
| 397 | u32 guest_ss_ar_bytes; |
| 398 | u32 guest_ds_ar_bytes; |
| 399 | u32 guest_fs_ar_bytes; |
| 400 | u32 guest_gs_ar_bytes; |
| 401 | u32 guest_ldtr_ar_bytes; |
| 402 | u32 guest_tr_ar_bytes; |
| 403 | u32 guest_interruptibility_info; |
| 404 | u32 guest_activity_state; |
| 405 | u32 guest_sysenter_cs; |
| 406 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 407 | u32 vmx_preemption_timer_value; |
| 408 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 409 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 410 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 411 | u16 guest_es_selector; |
| 412 | u16 guest_cs_selector; |
| 413 | u16 guest_ss_selector; |
| 414 | u16 guest_ds_selector; |
| 415 | u16 guest_fs_selector; |
| 416 | u16 guest_gs_selector; |
| 417 | u16 guest_ldtr_selector; |
| 418 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 419 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 420 | u16 host_es_selector; |
| 421 | u16 host_cs_selector; |
| 422 | u16 host_ss_selector; |
| 423 | u16 host_ds_selector; |
| 424 | u16 host_fs_selector; |
| 425 | u16 host_gs_selector; |
| 426 | u16 host_tr_selector; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 427 | u16 guest_pml_index; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | /* |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 431 | * For save/restore compatibility, the vmcs12 field offsets must not change. |
| 432 | */ |
| 433 | #define CHECK_OFFSET(field, loc) \ |
| 434 | BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \ |
| 435 | "Offset of " #field " in struct vmcs12 has changed.") |
| 436 | |
| 437 | static inline void vmx_check_vmcs12_offsets(void) { |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 438 | CHECK_OFFSET(hdr, 0); |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 439 | CHECK_OFFSET(abort, 4); |
| 440 | CHECK_OFFSET(launch_state, 8); |
| 441 | CHECK_OFFSET(io_bitmap_a, 40); |
| 442 | CHECK_OFFSET(io_bitmap_b, 48); |
| 443 | CHECK_OFFSET(msr_bitmap, 56); |
| 444 | CHECK_OFFSET(vm_exit_msr_store_addr, 64); |
| 445 | CHECK_OFFSET(vm_exit_msr_load_addr, 72); |
| 446 | CHECK_OFFSET(vm_entry_msr_load_addr, 80); |
| 447 | CHECK_OFFSET(tsc_offset, 88); |
| 448 | CHECK_OFFSET(virtual_apic_page_addr, 96); |
| 449 | CHECK_OFFSET(apic_access_addr, 104); |
| 450 | CHECK_OFFSET(posted_intr_desc_addr, 112); |
| 451 | CHECK_OFFSET(ept_pointer, 120); |
| 452 | CHECK_OFFSET(eoi_exit_bitmap0, 128); |
| 453 | CHECK_OFFSET(eoi_exit_bitmap1, 136); |
| 454 | CHECK_OFFSET(eoi_exit_bitmap2, 144); |
| 455 | CHECK_OFFSET(eoi_exit_bitmap3, 152); |
| 456 | CHECK_OFFSET(xss_exit_bitmap, 160); |
| 457 | CHECK_OFFSET(guest_physical_address, 168); |
| 458 | CHECK_OFFSET(vmcs_link_pointer, 176); |
| 459 | CHECK_OFFSET(guest_ia32_debugctl, 184); |
| 460 | CHECK_OFFSET(guest_ia32_pat, 192); |
| 461 | CHECK_OFFSET(guest_ia32_efer, 200); |
| 462 | CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208); |
| 463 | CHECK_OFFSET(guest_pdptr0, 216); |
| 464 | CHECK_OFFSET(guest_pdptr1, 224); |
| 465 | CHECK_OFFSET(guest_pdptr2, 232); |
| 466 | CHECK_OFFSET(guest_pdptr3, 240); |
| 467 | CHECK_OFFSET(guest_bndcfgs, 248); |
| 468 | CHECK_OFFSET(host_ia32_pat, 256); |
| 469 | CHECK_OFFSET(host_ia32_efer, 264); |
| 470 | CHECK_OFFSET(host_ia32_perf_global_ctrl, 272); |
| 471 | CHECK_OFFSET(vmread_bitmap, 280); |
| 472 | CHECK_OFFSET(vmwrite_bitmap, 288); |
| 473 | CHECK_OFFSET(vm_function_control, 296); |
| 474 | CHECK_OFFSET(eptp_list_address, 304); |
| 475 | CHECK_OFFSET(pml_address, 312); |
| 476 | CHECK_OFFSET(cr0_guest_host_mask, 344); |
| 477 | CHECK_OFFSET(cr4_guest_host_mask, 352); |
| 478 | CHECK_OFFSET(cr0_read_shadow, 360); |
| 479 | CHECK_OFFSET(cr4_read_shadow, 368); |
| 480 | CHECK_OFFSET(cr3_target_value0, 376); |
| 481 | CHECK_OFFSET(cr3_target_value1, 384); |
| 482 | CHECK_OFFSET(cr3_target_value2, 392); |
| 483 | CHECK_OFFSET(cr3_target_value3, 400); |
| 484 | CHECK_OFFSET(exit_qualification, 408); |
| 485 | CHECK_OFFSET(guest_linear_address, 416); |
| 486 | CHECK_OFFSET(guest_cr0, 424); |
| 487 | CHECK_OFFSET(guest_cr3, 432); |
| 488 | CHECK_OFFSET(guest_cr4, 440); |
| 489 | CHECK_OFFSET(guest_es_base, 448); |
| 490 | CHECK_OFFSET(guest_cs_base, 456); |
| 491 | CHECK_OFFSET(guest_ss_base, 464); |
| 492 | CHECK_OFFSET(guest_ds_base, 472); |
| 493 | CHECK_OFFSET(guest_fs_base, 480); |
| 494 | CHECK_OFFSET(guest_gs_base, 488); |
| 495 | CHECK_OFFSET(guest_ldtr_base, 496); |
| 496 | CHECK_OFFSET(guest_tr_base, 504); |
| 497 | CHECK_OFFSET(guest_gdtr_base, 512); |
| 498 | CHECK_OFFSET(guest_idtr_base, 520); |
| 499 | CHECK_OFFSET(guest_dr7, 528); |
| 500 | CHECK_OFFSET(guest_rsp, 536); |
| 501 | CHECK_OFFSET(guest_rip, 544); |
| 502 | CHECK_OFFSET(guest_rflags, 552); |
| 503 | CHECK_OFFSET(guest_pending_dbg_exceptions, 560); |
| 504 | CHECK_OFFSET(guest_sysenter_esp, 568); |
| 505 | CHECK_OFFSET(guest_sysenter_eip, 576); |
| 506 | CHECK_OFFSET(host_cr0, 584); |
| 507 | CHECK_OFFSET(host_cr3, 592); |
| 508 | CHECK_OFFSET(host_cr4, 600); |
| 509 | CHECK_OFFSET(host_fs_base, 608); |
| 510 | CHECK_OFFSET(host_gs_base, 616); |
| 511 | CHECK_OFFSET(host_tr_base, 624); |
| 512 | CHECK_OFFSET(host_gdtr_base, 632); |
| 513 | CHECK_OFFSET(host_idtr_base, 640); |
| 514 | CHECK_OFFSET(host_ia32_sysenter_esp, 648); |
| 515 | CHECK_OFFSET(host_ia32_sysenter_eip, 656); |
| 516 | CHECK_OFFSET(host_rsp, 664); |
| 517 | CHECK_OFFSET(host_rip, 672); |
| 518 | CHECK_OFFSET(pin_based_vm_exec_control, 744); |
| 519 | CHECK_OFFSET(cpu_based_vm_exec_control, 748); |
| 520 | CHECK_OFFSET(exception_bitmap, 752); |
| 521 | CHECK_OFFSET(page_fault_error_code_mask, 756); |
| 522 | CHECK_OFFSET(page_fault_error_code_match, 760); |
| 523 | CHECK_OFFSET(cr3_target_count, 764); |
| 524 | CHECK_OFFSET(vm_exit_controls, 768); |
| 525 | CHECK_OFFSET(vm_exit_msr_store_count, 772); |
| 526 | CHECK_OFFSET(vm_exit_msr_load_count, 776); |
| 527 | CHECK_OFFSET(vm_entry_controls, 780); |
| 528 | CHECK_OFFSET(vm_entry_msr_load_count, 784); |
| 529 | CHECK_OFFSET(vm_entry_intr_info_field, 788); |
| 530 | CHECK_OFFSET(vm_entry_exception_error_code, 792); |
| 531 | CHECK_OFFSET(vm_entry_instruction_len, 796); |
| 532 | CHECK_OFFSET(tpr_threshold, 800); |
| 533 | CHECK_OFFSET(secondary_vm_exec_control, 804); |
| 534 | CHECK_OFFSET(vm_instruction_error, 808); |
| 535 | CHECK_OFFSET(vm_exit_reason, 812); |
| 536 | CHECK_OFFSET(vm_exit_intr_info, 816); |
| 537 | CHECK_OFFSET(vm_exit_intr_error_code, 820); |
| 538 | CHECK_OFFSET(idt_vectoring_info_field, 824); |
| 539 | CHECK_OFFSET(idt_vectoring_error_code, 828); |
| 540 | CHECK_OFFSET(vm_exit_instruction_len, 832); |
| 541 | CHECK_OFFSET(vmx_instruction_info, 836); |
| 542 | CHECK_OFFSET(guest_es_limit, 840); |
| 543 | CHECK_OFFSET(guest_cs_limit, 844); |
| 544 | CHECK_OFFSET(guest_ss_limit, 848); |
| 545 | CHECK_OFFSET(guest_ds_limit, 852); |
| 546 | CHECK_OFFSET(guest_fs_limit, 856); |
| 547 | CHECK_OFFSET(guest_gs_limit, 860); |
| 548 | CHECK_OFFSET(guest_ldtr_limit, 864); |
| 549 | CHECK_OFFSET(guest_tr_limit, 868); |
| 550 | CHECK_OFFSET(guest_gdtr_limit, 872); |
| 551 | CHECK_OFFSET(guest_idtr_limit, 876); |
| 552 | CHECK_OFFSET(guest_es_ar_bytes, 880); |
| 553 | CHECK_OFFSET(guest_cs_ar_bytes, 884); |
| 554 | CHECK_OFFSET(guest_ss_ar_bytes, 888); |
| 555 | CHECK_OFFSET(guest_ds_ar_bytes, 892); |
| 556 | CHECK_OFFSET(guest_fs_ar_bytes, 896); |
| 557 | CHECK_OFFSET(guest_gs_ar_bytes, 900); |
| 558 | CHECK_OFFSET(guest_ldtr_ar_bytes, 904); |
| 559 | CHECK_OFFSET(guest_tr_ar_bytes, 908); |
| 560 | CHECK_OFFSET(guest_interruptibility_info, 912); |
| 561 | CHECK_OFFSET(guest_activity_state, 916); |
| 562 | CHECK_OFFSET(guest_sysenter_cs, 920); |
| 563 | CHECK_OFFSET(host_ia32_sysenter_cs, 924); |
| 564 | CHECK_OFFSET(vmx_preemption_timer_value, 928); |
| 565 | CHECK_OFFSET(virtual_processor_id, 960); |
| 566 | CHECK_OFFSET(posted_intr_nv, 962); |
| 567 | CHECK_OFFSET(guest_es_selector, 964); |
| 568 | CHECK_OFFSET(guest_cs_selector, 966); |
| 569 | CHECK_OFFSET(guest_ss_selector, 968); |
| 570 | CHECK_OFFSET(guest_ds_selector, 970); |
| 571 | CHECK_OFFSET(guest_fs_selector, 972); |
| 572 | CHECK_OFFSET(guest_gs_selector, 974); |
| 573 | CHECK_OFFSET(guest_ldtr_selector, 976); |
| 574 | CHECK_OFFSET(guest_tr_selector, 978); |
| 575 | CHECK_OFFSET(guest_intr_status, 980); |
| 576 | CHECK_OFFSET(host_es_selector, 982); |
| 577 | CHECK_OFFSET(host_cs_selector, 984); |
| 578 | CHECK_OFFSET(host_ss_selector, 986); |
| 579 | CHECK_OFFSET(host_ds_selector, 988); |
| 580 | CHECK_OFFSET(host_fs_selector, 990); |
| 581 | CHECK_OFFSET(host_gs_selector, 992); |
| 582 | CHECK_OFFSET(host_tr_selector, 994); |
| 583 | CHECK_OFFSET(guest_pml_index, 996); |
| 584 | } |
| 585 | |
| 586 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 587 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 588 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 589 | * 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] | 590 | * |
| 591 | * IMPORTANT: Changing this value will break save/restore compatibility with |
| 592 | * older kvm releases. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 593 | */ |
| 594 | #define VMCS12_REVISION 0x11e57ed0 |
| 595 | |
| 596 | /* |
| 597 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 598 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 599 | * current implementation, 4K are reserved to avoid future complications. |
| 600 | */ |
| 601 | #define VMCS12_SIZE 0x1000 |
| 602 | |
| 603 | /* |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 604 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any |
| 605 | * supported VMCS12 field encoding. |
| 606 | */ |
| 607 | #define VMCS12_MAX_FIELD_INDEX 0x17 |
| 608 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 609 | struct nested_vmx_msrs { |
| 610 | /* |
| 611 | * We only store the "true" versions of the VMX capability MSRs. We |
| 612 | * generate the "non-true" versions by setting the must-be-1 bits |
| 613 | * according to the SDM. |
| 614 | */ |
| 615 | u32 procbased_ctls_low; |
| 616 | u32 procbased_ctls_high; |
| 617 | u32 secondary_ctls_low; |
| 618 | u32 secondary_ctls_high; |
| 619 | u32 pinbased_ctls_low; |
| 620 | u32 pinbased_ctls_high; |
| 621 | u32 exit_ctls_low; |
| 622 | u32 exit_ctls_high; |
| 623 | u32 entry_ctls_low; |
| 624 | u32 entry_ctls_high; |
| 625 | u32 misc_low; |
| 626 | u32 misc_high; |
| 627 | u32 ept_caps; |
| 628 | u32 vpid_caps; |
| 629 | u64 basic; |
| 630 | u64 cr0_fixed0; |
| 631 | u64 cr0_fixed1; |
| 632 | u64 cr4_fixed0; |
| 633 | u64 cr4_fixed1; |
| 634 | u64 vmcs_enum; |
| 635 | u64 vmfunc_controls; |
| 636 | }; |
| 637 | |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 638 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 639 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 640 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 641 | */ |
| 642 | struct nested_vmx { |
| 643 | /* Has the level1 guest done vmxon? */ |
| 644 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 645 | gpa_t vmxon_ptr; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 646 | bool pml_full; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 647 | |
| 648 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 649 | gpa_t current_vmptr; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 650 | /* |
| 651 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 652 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 653 | * memory during VMCLEAR and VMPTRLD. |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 654 | */ |
| 655 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 656 | /* |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 657 | * Cache of the guest's shadow VMCS, existing outside of guest |
| 658 | * memory. Loaded from guest memory during VM entry. Flushed |
| 659 | * to guest memory during VM exit. |
| 660 | */ |
| 661 | struct vmcs12 *cached_shadow_vmcs12; |
| 662 | /* |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 663 | * Indicates if the shadow vmcs must be updated with the |
| 664 | * data hold by vmcs12 |
| 665 | */ |
| 666 | bool sync_shadow_vmcs; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 667 | bool dirty_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 668 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 669 | bool change_vmcs01_virtual_apic_mode; |
| 670 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 671 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 672 | bool nested_run_pending; |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 673 | |
| 674 | struct loaded_vmcs vmcs02; |
| 675 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 676 | /* |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 677 | * Guest pages referred to in the vmcs02 with host-physical |
| 678 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 679 | */ |
| 680 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 681 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 682 | struct page *pi_desc_page; |
| 683 | struct pi_desc *pi_desc; |
| 684 | bool pi_pending; |
| 685 | u16 posted_intr_nv; |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 686 | |
| 687 | struct hrtimer preemption_timer; |
| 688 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 689 | |
| 690 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 691 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 692 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 693 | u16 vpid02; |
| 694 | u16 last_vpid; |
| 695 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 696 | struct nested_vmx_msrs msrs; |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 697 | |
| 698 | /* SMM related state */ |
| 699 | struct { |
| 700 | /* in VMX operation on SMM entry? */ |
| 701 | bool vmxon; |
| 702 | /* in guest mode on SMM entry? */ |
| 703 | bool guest_mode; |
| 704 | } smm; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 705 | }; |
| 706 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 707 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 708 | #define POSTED_INTR_SN 1 |
| 709 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 710 | /* Posted-Interrupt Descriptor */ |
| 711 | struct pi_desc { |
| 712 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 713 | union { |
| 714 | struct { |
| 715 | /* bit 256 - Outstanding Notification */ |
| 716 | u16 on : 1, |
| 717 | /* bit 257 - Suppress Notification */ |
| 718 | sn : 1, |
| 719 | /* bit 271:258 - Reserved */ |
| 720 | rsvd_1 : 14; |
| 721 | /* bit 279:272 - Notification Vector */ |
| 722 | u8 nv; |
| 723 | /* bit 287:280 - Reserved */ |
| 724 | u8 rsvd_2; |
| 725 | /* bit 319:288 - Notification Destination */ |
| 726 | u32 ndst; |
| 727 | }; |
| 728 | u64 control; |
| 729 | }; |
| 730 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 731 | } __aligned(64); |
| 732 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 733 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 734 | { |
| 735 | return test_and_set_bit(POSTED_INTR_ON, |
| 736 | (unsigned long *)&pi_desc->control); |
| 737 | } |
| 738 | |
| 739 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 740 | { |
| 741 | return test_and_clear_bit(POSTED_INTR_ON, |
| 742 | (unsigned long *)&pi_desc->control); |
| 743 | } |
| 744 | |
| 745 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 746 | { |
| 747 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 748 | } |
| 749 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 750 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 751 | { |
| 752 | return clear_bit(POSTED_INTR_SN, |
| 753 | (unsigned long *)&pi_desc->control); |
| 754 | } |
| 755 | |
| 756 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 757 | { |
| 758 | return set_bit(POSTED_INTR_SN, |
| 759 | (unsigned long *)&pi_desc->control); |
| 760 | } |
| 761 | |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 762 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
| 763 | { |
| 764 | clear_bit(POSTED_INTR_ON, |
| 765 | (unsigned long *)&pi_desc->control); |
| 766 | } |
| 767 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 768 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 769 | { |
| 770 | return test_bit(POSTED_INTR_ON, |
| 771 | (unsigned long *)&pi_desc->control); |
| 772 | } |
| 773 | |
| 774 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 775 | { |
| 776 | return test_bit(POSTED_INTR_SN, |
| 777 | (unsigned long *)&pi_desc->control); |
| 778 | } |
| 779 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 780 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 781 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 782 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 783 | u8 fail; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 784 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 785 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 786 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 787 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 788 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 789 | int nmsrs; |
| 790 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 791 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 792 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 793 | u64 msr_host_kernel_gs_base; |
| 794 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 795 | #endif |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 796 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 797 | u64 arch_capabilities; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 798 | u64 spec_ctrl; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 799 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 800 | u32 vm_entry_controls_shadow; |
| 801 | u32 vm_exit_controls_shadow; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 802 | u32 secondary_exec_control; |
| 803 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 804 | /* |
| 805 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 806 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 807 | * guest (L2), it points to a different VMCS. loaded_cpu_state points |
| 808 | * to the VMCS whose state is loaded into the CPU registers that only |
| 809 | * need to be switched when transitioning to/from the kernel; a NULL |
| 810 | * value indicates that host state is loaded. |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 811 | */ |
| 812 | struct loaded_vmcs vmcs01; |
| 813 | struct loaded_vmcs *loaded_vmcs; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 814 | struct loaded_vmcs *loaded_cpu_state; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 815 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 816 | struct msr_autoload { |
| 817 | unsigned nr; |
| 818 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 819 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 820 | } msr_autoload; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 821 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 822 | struct { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 823 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 824 | #ifdef CONFIG_X86_64 |
| 825 | u16 ds_sel, es_sel; |
| 826 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 827 | int gs_ldt_reload_needed; |
| 828 | int fs_reload_needed; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 829 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 830 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 831 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 832 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 833 | struct kvm_segment segs[8]; |
| 834 | } rmode; |
| 835 | struct { |
| 836 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 837 | struct kvm_save_segment { |
| 838 | u16 selector; |
| 839 | unsigned long base; |
| 840 | u32 limit; |
| 841 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 842 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 843 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 844 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 845 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 846 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 847 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 848 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 849 | /* Posted interrupt descriptor */ |
| 850 | struct pi_desc pi_desc; |
| 851 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 852 | /* Support for a guest hypervisor (nested VMX) */ |
| 853 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 854 | |
| 855 | /* Dynamic PLE window. */ |
| 856 | int ple_window; |
| 857 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 858 | |
| 859 | /* Support for PML */ |
| 860 | #define PML_ENTITY_NUM 512 |
| 861 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 862 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 863 | /* apic deadline value in host tsc */ |
| 864 | u64 hv_deadline_tsc; |
| 865 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 866 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 867 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 868 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 869 | |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 870 | unsigned long host_debugctlmsr; |
| 871 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 872 | /* |
| 873 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 874 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 875 | * in msr_ia32_feature_control_valid_bits. |
| 876 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 877 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 878 | u64 msr_ia32_feature_control_valid_bits; |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 879 | u64 ept_pointer; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 880 | }; |
| 881 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 882 | enum segment_cache_field { |
| 883 | SEG_FIELD_SEL = 0, |
| 884 | SEG_FIELD_BASE = 1, |
| 885 | SEG_FIELD_LIMIT = 2, |
| 886 | SEG_FIELD_AR = 3, |
| 887 | |
| 888 | SEG_FIELD_NR = 4 |
| 889 | }; |
| 890 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 891 | static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) |
| 892 | { |
| 893 | return container_of(kvm, struct kvm_vmx, kvm); |
| 894 | } |
| 895 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 896 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 897 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 898 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 899 | } |
| 900 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 901 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 902 | { |
| 903 | return &(to_vmx(vcpu)->pi_desc); |
| 904 | } |
| 905 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 906 | #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] | 907 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 908 | #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) |
| 909 | #define FIELD64(number, name) \ |
| 910 | FIELD(number, name), \ |
| 911 | [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 912 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 913 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 914 | static u16 shadow_read_only_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 915 | #define SHADOW_FIELD_RO(x) x, |
| 916 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 917 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 918 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 919 | ARRAY_SIZE(shadow_read_only_fields); |
| 920 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 921 | static u16 shadow_read_write_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 922 | #define SHADOW_FIELD_RW(x) x, |
| 923 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 924 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 925 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 926 | ARRAY_SIZE(shadow_read_write_fields); |
| 927 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 928 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 929 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 930 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 931 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 932 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 933 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 934 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 935 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 936 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 937 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 938 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 939 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 940 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 941 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 942 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 943 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 944 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 945 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 946 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 947 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 948 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 949 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 950 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 951 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 952 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 953 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 954 | FIELD64(PML_ADDRESS, pml_address), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 955 | FIELD64(TSC_OFFSET, tsc_offset), |
| 956 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 957 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 958 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 959 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 960 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 961 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 962 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 963 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 964 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 965 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 966 | FIELD64(VMREAD_BITMAP, vmread_bitmap), |
| 967 | FIELD64(VMWRITE_BITMAP, vmwrite_bitmap), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 968 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 969 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 970 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 971 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 972 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 973 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 974 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 975 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 976 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 977 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 978 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 979 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 980 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 981 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 982 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 983 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 984 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 985 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 986 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 987 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 988 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 989 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 990 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 991 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 992 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 993 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 994 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 995 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 996 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 997 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 998 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 999 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 1000 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 1001 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 1002 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 1003 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 1004 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 1005 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 1006 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 1007 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 1008 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 1009 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 1010 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 1011 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 1012 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 1013 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 1014 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 1015 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 1016 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 1017 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 1018 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 1019 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 1020 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 1021 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 1022 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 1023 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 1024 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 1025 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 1026 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 1027 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 1028 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 1029 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1030 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 1031 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 1032 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 1033 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 1034 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 1035 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 1036 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 1037 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 1038 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 1039 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 1040 | FIELD(GUEST_CR0, guest_cr0), |
| 1041 | FIELD(GUEST_CR3, guest_cr3), |
| 1042 | FIELD(GUEST_CR4, guest_cr4), |
| 1043 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 1044 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 1045 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 1046 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 1047 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 1048 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 1049 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 1050 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 1051 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 1052 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 1053 | FIELD(GUEST_DR7, guest_dr7), |
| 1054 | FIELD(GUEST_RSP, guest_rsp), |
| 1055 | FIELD(GUEST_RIP, guest_rip), |
| 1056 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 1057 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 1058 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 1059 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 1060 | FIELD(HOST_CR0, host_cr0), |
| 1061 | FIELD(HOST_CR3, host_cr3), |
| 1062 | FIELD(HOST_CR4, host_cr4), |
| 1063 | FIELD(HOST_FS_BASE, host_fs_base), |
| 1064 | FIELD(HOST_GS_BASE, host_gs_base), |
| 1065 | FIELD(HOST_TR_BASE, host_tr_base), |
| 1066 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 1067 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 1068 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 1069 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 1070 | FIELD(HOST_RSP, host_rsp), |
| 1071 | FIELD(HOST_RIP, host_rip), |
| 1072 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1073 | |
| 1074 | static inline short vmcs_field_to_offset(unsigned long field) |
| 1075 | { |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1076 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 1077 | unsigned short offset; |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1078 | unsigned index; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1079 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1080 | if (field >> 15) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1081 | return -ENOENT; |
| 1082 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1083 | index = ROL16(field, 6); |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1084 | if (index >= size) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1085 | return -ENOENT; |
| 1086 | |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1087 | index = array_index_nospec(index, size); |
| 1088 | offset = vmcs_field_to_offset_table[index]; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1089 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1090 | return -ENOENT; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1091 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1092 | } |
| 1093 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1094 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 1095 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 1096 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1097 | } |
| 1098 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 1099 | static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu) |
| 1100 | { |
| 1101 | return to_vmx(vcpu)->nested.cached_shadow_vmcs12; |
| 1102 | } |
| 1103 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1104 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 1105 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1106 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 1107 | static bool vmx_xsaves_supported(void); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 1108 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1109 | struct kvm_segment *var, int seg); |
| 1110 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1111 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 1112 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 1113 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 1114 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 1115 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
| 1116 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); |
| 1117 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 1118 | u16 error_code); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 1119 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1120 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 1121 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1122 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1123 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 1124 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1125 | /* |
| 1126 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 1127 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 1128 | */ |
| 1129 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1130 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 1131 | /* |
| 1132 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 1133 | * can find which vCPU should be waken up. |
| 1134 | */ |
| 1135 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 1136 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 1137 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1138 | enum { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1139 | VMX_VMREAD_BITMAP, |
| 1140 | VMX_VMWRITE_BITMAP, |
| 1141 | VMX_BITMAP_NR |
| 1142 | }; |
| 1143 | |
| 1144 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 1145 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1146 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 1147 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1148 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1149 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1150 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1151 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1152 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 1153 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 1154 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1155 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1156 | int size; |
| 1157 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1158 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1159 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1160 | u32 pin_based_exec_ctrl; |
| 1161 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1162 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1163 | u32 vmexit_ctrl; |
| 1164 | u32 vmentry_ctrl; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1165 | struct nested_vmx_msrs nested; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1166 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1167 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 1168 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1169 | u32 ept; |
| 1170 | u32 vpid; |
| 1171 | } vmx_capability; |
| 1172 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1173 | #define VMX_SEGMENT_FIELD(seg) \ |
| 1174 | [VCPU_SREG_##seg] = { \ |
| 1175 | .selector = GUEST_##seg##_SELECTOR, \ |
| 1176 | .base = GUEST_##seg##_BASE, \ |
| 1177 | .limit = GUEST_##seg##_LIMIT, \ |
| 1178 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 1179 | } |
| 1180 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 1181 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1182 | unsigned selector; |
| 1183 | unsigned base; |
| 1184 | unsigned limit; |
| 1185 | unsigned ar_bytes; |
| 1186 | } kvm_vmx_segment_fields[] = { |
| 1187 | VMX_SEGMENT_FIELD(CS), |
| 1188 | VMX_SEGMENT_FIELD(DS), |
| 1189 | VMX_SEGMENT_FIELD(ES), |
| 1190 | VMX_SEGMENT_FIELD(FS), |
| 1191 | VMX_SEGMENT_FIELD(GS), |
| 1192 | VMX_SEGMENT_FIELD(SS), |
| 1193 | VMX_SEGMENT_FIELD(TR), |
| 1194 | VMX_SEGMENT_FIELD(LDTR), |
| 1195 | }; |
| 1196 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1197 | static u64 host_efer; |
| 1198 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1199 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1200 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1201 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1202 | * 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] | 1203 | * away by decrementing the array size. |
| 1204 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1205 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1206 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1207 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1208 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1209 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1210 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1211 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1212 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
| 1213 | |
| 1214 | #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) |
| 1215 | |
| 1216 | #define KVM_EVMCS_VERSION 1 |
| 1217 | |
| 1218 | #if IS_ENABLED(CONFIG_HYPERV) |
| 1219 | static bool __read_mostly enlightened_vmcs = true; |
| 1220 | module_param(enlightened_vmcs, bool, 0444); |
| 1221 | |
| 1222 | static inline void evmcs_write64(unsigned long field, u64 value) |
| 1223 | { |
| 1224 | u16 clean_field; |
| 1225 | int offset = get_evmcs_offset(field, &clean_field); |
| 1226 | |
| 1227 | if (offset < 0) |
| 1228 | return; |
| 1229 | |
| 1230 | *(u64 *)((char *)current_evmcs + offset) = value; |
| 1231 | |
| 1232 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1233 | } |
| 1234 | |
| 1235 | static inline void evmcs_write32(unsigned long field, u32 value) |
| 1236 | { |
| 1237 | u16 clean_field; |
| 1238 | int offset = get_evmcs_offset(field, &clean_field); |
| 1239 | |
| 1240 | if (offset < 0) |
| 1241 | return; |
| 1242 | |
| 1243 | *(u32 *)((char *)current_evmcs + offset) = value; |
| 1244 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1245 | } |
| 1246 | |
| 1247 | static inline void evmcs_write16(unsigned long field, u16 value) |
| 1248 | { |
| 1249 | u16 clean_field; |
| 1250 | int offset = get_evmcs_offset(field, &clean_field); |
| 1251 | |
| 1252 | if (offset < 0) |
| 1253 | return; |
| 1254 | |
| 1255 | *(u16 *)((char *)current_evmcs + offset) = value; |
| 1256 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1257 | } |
| 1258 | |
| 1259 | static inline u64 evmcs_read64(unsigned long field) |
| 1260 | { |
| 1261 | int offset = get_evmcs_offset(field, NULL); |
| 1262 | |
| 1263 | if (offset < 0) |
| 1264 | return 0; |
| 1265 | |
| 1266 | return *(u64 *)((char *)current_evmcs + offset); |
| 1267 | } |
| 1268 | |
| 1269 | static inline u32 evmcs_read32(unsigned long field) |
| 1270 | { |
| 1271 | int offset = get_evmcs_offset(field, NULL); |
| 1272 | |
| 1273 | if (offset < 0) |
| 1274 | return 0; |
| 1275 | |
| 1276 | return *(u32 *)((char *)current_evmcs + offset); |
| 1277 | } |
| 1278 | |
| 1279 | static inline u16 evmcs_read16(unsigned long field) |
| 1280 | { |
| 1281 | int offset = get_evmcs_offset(field, NULL); |
| 1282 | |
| 1283 | if (offset < 0) |
| 1284 | return 0; |
| 1285 | |
| 1286 | return *(u16 *)((char *)current_evmcs + offset); |
| 1287 | } |
| 1288 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1289 | static inline void evmcs_touch_msr_bitmap(void) |
| 1290 | { |
| 1291 | if (unlikely(!current_evmcs)) |
| 1292 | return; |
| 1293 | |
| 1294 | if (current_evmcs->hv_enlightenments_control.msr_bitmap) |
| 1295 | current_evmcs->hv_clean_fields &= |
| 1296 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP; |
| 1297 | } |
| 1298 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1299 | static void evmcs_load(u64 phys_addr) |
| 1300 | { |
| 1301 | struct hv_vp_assist_page *vp_ap = |
| 1302 | hv_get_vp_assist_page(smp_processor_id()); |
| 1303 | |
| 1304 | vp_ap->current_nested_vmcs = phys_addr; |
| 1305 | vp_ap->enlighten_vmentry = 1; |
| 1306 | } |
| 1307 | |
| 1308 | static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) |
| 1309 | { |
| 1310 | /* |
| 1311 | * Enlightened VMCSv1 doesn't support these: |
| 1312 | * |
| 1313 | * POSTED_INTR_NV = 0x00000002, |
| 1314 | * GUEST_INTR_STATUS = 0x00000810, |
| 1315 | * APIC_ACCESS_ADDR = 0x00002014, |
| 1316 | * POSTED_INTR_DESC_ADDR = 0x00002016, |
| 1317 | * EOI_EXIT_BITMAP0 = 0x0000201c, |
| 1318 | * EOI_EXIT_BITMAP1 = 0x0000201e, |
| 1319 | * EOI_EXIT_BITMAP2 = 0x00002020, |
| 1320 | * EOI_EXIT_BITMAP3 = 0x00002022, |
| 1321 | */ |
| 1322 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
| 1323 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1324 | ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1325 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1326 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1327 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1328 | ~SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1329 | |
| 1330 | /* |
| 1331 | * GUEST_PML_INDEX = 0x00000812, |
| 1332 | * PML_ADDRESS = 0x0000200e, |
| 1333 | */ |
| 1334 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML; |
| 1335 | |
| 1336 | /* VM_FUNCTION_CONTROL = 0x00002018, */ |
| 1337 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1338 | |
| 1339 | /* |
| 1340 | * EPTP_LIST_ADDRESS = 0x00002024, |
| 1341 | * VMREAD_BITMAP = 0x00002026, |
| 1342 | * VMWRITE_BITMAP = 0x00002028, |
| 1343 | */ |
| 1344 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 1345 | |
| 1346 | /* |
| 1347 | * TSC_MULTIPLIER = 0x00002032, |
| 1348 | */ |
| 1349 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING; |
| 1350 | |
| 1351 | /* |
| 1352 | * PLE_GAP = 0x00004020, |
| 1353 | * PLE_WINDOW = 0x00004022, |
| 1354 | */ |
| 1355 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1356 | |
| 1357 | /* |
| 1358 | * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, |
| 1359 | */ |
| 1360 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1361 | |
| 1362 | /* |
| 1363 | * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, |
| 1364 | * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, |
| 1365 | */ |
| 1366 | vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1367 | vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1368 | |
| 1369 | /* |
| 1370 | * Currently unsupported in KVM: |
| 1371 | * GUEST_IA32_RTIT_CTL = 0x00002814, |
| 1372 | */ |
| 1373 | } |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1374 | |
| 1375 | /* check_ept_pointer() should be under protection of ept_pointer_lock. */ |
| 1376 | static void check_ept_pointer_match(struct kvm *kvm) |
| 1377 | { |
| 1378 | struct kvm_vcpu *vcpu; |
| 1379 | u64 tmp_eptp = INVALID_PAGE; |
| 1380 | int i; |
| 1381 | |
| 1382 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1383 | if (!VALID_PAGE(tmp_eptp)) { |
| 1384 | tmp_eptp = to_vmx(vcpu)->ept_pointer; |
| 1385 | } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) { |
| 1386 | to_kvm_vmx(kvm)->ept_pointers_match |
| 1387 | = EPT_POINTERS_MISMATCH; |
| 1388 | return; |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH; |
| 1393 | } |
| 1394 | |
| 1395 | static int vmx_hv_remote_flush_tlb(struct kvm *kvm) |
| 1396 | { |
| 1397 | int ret; |
| 1398 | |
| 1399 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 1400 | |
| 1401 | if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK) |
| 1402 | check_ept_pointer_match(kvm); |
| 1403 | |
| 1404 | if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) { |
| 1405 | ret = -ENOTSUPP; |
| 1406 | goto out; |
| 1407 | } |
| 1408 | |
| 1409 | ret = hyperv_flush_guest_mapping( |
| 1410 | to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer); |
| 1411 | |
| 1412 | out: |
| 1413 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 1414 | return ret; |
| 1415 | } |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1416 | #else /* !IS_ENABLED(CONFIG_HYPERV) */ |
| 1417 | static inline void evmcs_write64(unsigned long field, u64 value) {} |
| 1418 | static inline void evmcs_write32(unsigned long field, u32 value) {} |
| 1419 | static inline void evmcs_write16(unsigned long field, u16 value) {} |
| 1420 | static inline u64 evmcs_read64(unsigned long field) { return 0; } |
| 1421 | static inline u32 evmcs_read32(unsigned long field) { return 0; } |
| 1422 | static inline u16 evmcs_read16(unsigned long field) { return 0; } |
| 1423 | static inline void evmcs_load(u64 phys_addr) {} |
| 1424 | static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {} |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1425 | static inline void evmcs_touch_msr_bitmap(void) {} |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1426 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 1427 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1428 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1429 | { |
| 1430 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1431 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1432 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1433 | } |
| 1434 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1435 | static inline bool is_debug(u32 intr_info) |
| 1436 | { |
| 1437 | return is_exception_n(intr_info, DB_VECTOR); |
| 1438 | } |
| 1439 | |
| 1440 | static inline bool is_breakpoint(u32 intr_info) |
| 1441 | { |
| 1442 | return is_exception_n(intr_info, BP_VECTOR); |
| 1443 | } |
| 1444 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1445 | static inline bool is_page_fault(u32 intr_info) |
| 1446 | { |
| 1447 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1448 | } |
| 1449 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1450 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1451 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1452 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1453 | } |
| 1454 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1455 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1456 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1457 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1458 | } |
| 1459 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 1460 | static inline bool is_gp_fault(u32 intr_info) |
| 1461 | { |
| 1462 | return is_exception_n(intr_info, GP_VECTOR); |
| 1463 | } |
| 1464 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1465 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1466 | { |
| 1467 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1468 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1469 | } |
| 1470 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1471 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1472 | { |
| 1473 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1474 | INTR_INFO_VALID_MASK)) == |
| 1475 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1476 | } |
| 1477 | |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 1478 | /* Undocumented: icebp/int1 */ |
| 1479 | static inline bool is_icebp(u32 intr_info) |
| 1480 | { |
| 1481 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1482 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); |
| 1483 | } |
| 1484 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1485 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1486 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1487 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1488 | } |
| 1489 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1490 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1491 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1492 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1493 | } |
| 1494 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1495 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1496 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1497 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1498 | } |
| 1499 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1500 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1501 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1502 | return vmcs_config.cpu_based_exec_ctrl & |
| 1503 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1504 | } |
| 1505 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1506 | static inline bool cpu_has_vmx_virtualize_apic_accesses(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_2nd_exec_ctrl & |
| 1509 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1510 | } |
| 1511 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1512 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1513 | { |
| 1514 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1515 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1516 | } |
| 1517 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1518 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1519 | { |
| 1520 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1521 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1522 | } |
| 1523 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1524 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1525 | { |
| 1526 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1527 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1528 | } |
| 1529 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1530 | /* |
| 1531 | * Comment's format: document - errata name - stepping - processor name. |
| 1532 | * Refer from |
| 1533 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1534 | */ |
| 1535 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1536 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1537 | 0x000206E6, |
| 1538 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1539 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1540 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1541 | 0x00020652, |
| 1542 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1543 | 0x00020655, |
| 1544 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1545 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1546 | /* |
| 1547 | * 320767.pdf - AAP86 - B1 - |
| 1548 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1549 | */ |
| 1550 | 0x000106E5, |
| 1551 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1552 | 0x000106A0, |
| 1553 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1554 | 0x000106A1, |
| 1555 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1556 | 0x000106A4, |
| 1557 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1558 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1559 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1560 | 0x000106A5, |
| 1561 | }; |
| 1562 | |
| 1563 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1564 | { |
| 1565 | u32 eax = cpuid_eax(0x00000001), i; |
| 1566 | |
| 1567 | /* Clear the reserved bits */ |
| 1568 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1569 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1570 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1571 | return true; |
| 1572 | |
| 1573 | return false; |
| 1574 | } |
| 1575 | |
| 1576 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1577 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1578 | return vmcs_config.pin_based_exec_ctrl & |
| 1579 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1580 | } |
| 1581 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1582 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1583 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1584 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1585 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | static inline bool cpu_has_vmx_apicv(void) |
| 1589 | { |
| 1590 | return cpu_has_vmx_apic_register_virt() && |
| 1591 | cpu_has_vmx_virtual_intr_delivery() && |
| 1592 | cpu_has_vmx_posted_intr(); |
| 1593 | } |
| 1594 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1595 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1596 | { |
| 1597 | return cpu_has_vmx_tpr_shadow() && |
| 1598 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1599 | } |
| 1600 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1601 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1602 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1603 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1604 | } |
| 1605 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1606 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1607 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1608 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1609 | } |
| 1610 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1611 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1612 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1613 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1614 | } |
| 1615 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1616 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1617 | { |
| 1618 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1619 | } |
| 1620 | |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 1621 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
| 1622 | { |
| 1623 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
| 1624 | } |
| 1625 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 1626 | static inline bool cpu_has_vmx_ept_5levels(void) |
| 1627 | { |
| 1628 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; |
| 1629 | } |
| 1630 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1631 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1632 | { |
| 1633 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1634 | } |
| 1635 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1636 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1637 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1638 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1639 | } |
| 1640 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1641 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1642 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1643 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 1646 | static inline bool cpu_has_vmx_invvpid_individual_addr(void) |
| 1647 | { |
| 1648 | return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT; |
| 1649 | } |
| 1650 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1651 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1652 | { |
| 1653 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1654 | } |
| 1655 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1656 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1657 | { |
| 1658 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1659 | } |
| 1660 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1661 | static inline bool cpu_has_vmx_invvpid(void) |
| 1662 | { |
| 1663 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1664 | } |
| 1665 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1666 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1667 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1668 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1669 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1670 | } |
| 1671 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1672 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1673 | { |
| 1674 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1675 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1676 | } |
| 1677 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1678 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1679 | { |
| 1680 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1681 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1682 | } |
| 1683 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1684 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1685 | { |
| 1686 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1687 | } |
| 1688 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1689 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1690 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1691 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1692 | } |
| 1693 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1694 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1695 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1696 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1697 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1698 | } |
| 1699 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1700 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1701 | { |
| 1702 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1703 | SECONDARY_EXEC_RDTSCP; |
| 1704 | } |
| 1705 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1706 | static inline bool cpu_has_vmx_invpcid(void) |
| 1707 | { |
| 1708 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1709 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1710 | } |
| 1711 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 1712 | static inline bool cpu_has_virtual_nmis(void) |
| 1713 | { |
| 1714 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1715 | } |
| 1716 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1717 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1718 | { |
| 1719 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1720 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1721 | } |
| 1722 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1723 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1724 | { |
| 1725 | u64 vmx_msr; |
| 1726 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1727 | /* check if the cpu supports writing r/o exit information fields */ |
| 1728 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1729 | return false; |
| 1730 | |
| 1731 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1732 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1733 | } |
| 1734 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1735 | static inline bool cpu_has_vmx_pml(void) |
| 1736 | { |
| 1737 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1738 | } |
| 1739 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1740 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1741 | { |
| 1742 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1743 | SECONDARY_EXEC_TSC_SCALING; |
| 1744 | } |
| 1745 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1746 | static inline bool cpu_has_vmx_vmfunc(void) |
| 1747 | { |
| 1748 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1749 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1750 | } |
| 1751 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 1752 | static bool vmx_umip_emulated(void) |
| 1753 | { |
| 1754 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1755 | SECONDARY_EXEC_DESC; |
| 1756 | } |
| 1757 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1758 | static inline bool report_flexpriority(void) |
| 1759 | { |
| 1760 | return flexpriority_enabled; |
| 1761 | } |
| 1762 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1763 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
| 1764 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1765 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low); |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 1768 | /* |
| 1769 | * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE |
| 1770 | * to modify any valid field of the VMCS, or are the VM-exit |
| 1771 | * information fields read-only? |
| 1772 | */ |
| 1773 | static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu) |
| 1774 | { |
| 1775 | return to_vmx(vcpu)->nested.msrs.misc_low & |
| 1776 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS; |
| 1777 | } |
| 1778 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 1779 | static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu) |
| 1780 | { |
| 1781 | return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS; |
| 1782 | } |
| 1783 | |
| 1784 | static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu) |
| 1785 | { |
| 1786 | return to_vmx(vcpu)->nested.msrs.procbased_ctls_high & |
| 1787 | CPU_BASED_MONITOR_TRAP_FLAG; |
| 1788 | } |
| 1789 | |
Liran Alon | fa97d7d | 2018-07-18 14:07:59 +0200 | [diff] [blame] | 1790 | static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 1791 | { |
| 1792 | return to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
| 1793 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1794 | } |
| 1795 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1796 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1797 | { |
| 1798 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1799 | } |
| 1800 | |
| 1801 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1802 | { |
| 1803 | return (vmcs12->cpu_based_vm_exec_control & |
| 1804 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1805 | (vmcs12->secondary_vm_exec_control & bit); |
| 1806 | } |
| 1807 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1808 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1809 | { |
| 1810 | return vmcs12->pin_based_vm_exec_control & |
| 1811 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1812 | } |
| 1813 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 1814 | static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12) |
| 1815 | { |
| 1816 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING; |
| 1817 | } |
| 1818 | |
| 1819 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
| 1820 | { |
| 1821 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1822 | } |
| 1823 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1824 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1825 | { |
| 1826 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1827 | } |
| 1828 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1829 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1830 | { |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 1831 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1832 | } |
| 1833 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 1834 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
| 1835 | { |
| 1836 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); |
| 1837 | } |
| 1838 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1839 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1840 | { |
| 1841 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1842 | } |
| 1843 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1844 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1845 | { |
| 1846 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1847 | } |
| 1848 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1849 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1850 | { |
| 1851 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1852 | } |
| 1853 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1854 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1855 | { |
| 1856 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1857 | } |
| 1858 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1859 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1860 | { |
| 1861 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1862 | } |
| 1863 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 1864 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
| 1865 | { |
| 1866 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); |
| 1867 | } |
| 1868 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 1869 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
| 1870 | { |
| 1871 | return nested_cpu_has_vmfunc(vmcs12) && |
| 1872 | (vmcs12->vm_function_control & |
| 1873 | VMX_VMFUNC_EPTP_SWITCHING); |
| 1874 | } |
| 1875 | |
Liran Alon | f792d27 | 2018-06-23 02:35:05 +0300 | [diff] [blame] | 1876 | static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12) |
| 1877 | { |
| 1878 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS); |
| 1879 | } |
| 1880 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1881 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1882 | { |
| 1883 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1884 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1885 | } |
| 1886 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1887 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1888 | u32 exit_intr_info, |
| 1889 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1890 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1891 | struct vmcs12 *vmcs12, |
| 1892 | u32 reason, unsigned long qualification); |
| 1893 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1894 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1895 | { |
| 1896 | int i; |
| 1897 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1898 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1899 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1900 | return i; |
| 1901 | return -1; |
| 1902 | } |
| 1903 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1904 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1905 | { |
| 1906 | struct { |
| 1907 | u64 vpid : 16; |
| 1908 | u64 rsvd : 48; |
| 1909 | u64 gva; |
| 1910 | } operand = { vpid, 0, gva }; |
| 1911 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1912 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1913 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1914 | "; ja 1f ; ud2 ; 1:" |
| 1915 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1916 | } |
| 1917 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1918 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1919 | { |
| 1920 | struct { |
| 1921 | u64 eptp, gpa; |
| 1922 | } operand = {eptp, gpa}; |
| 1923 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1924 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1925 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1926 | "; ja 1f ; ud2 ; 1:\n" |
| 1927 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1928 | } |
| 1929 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1930 | 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] | 1931 | { |
| 1932 | int i; |
| 1933 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1934 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1935 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1936 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1937 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1938 | } |
| 1939 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1940 | static void vmcs_clear(struct vmcs *vmcs) |
| 1941 | { |
| 1942 | u64 phys_addr = __pa(vmcs); |
| 1943 | u8 error; |
| 1944 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1945 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1946 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1947 | : "cc", "memory"); |
| 1948 | if (error) |
| 1949 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1950 | vmcs, phys_addr); |
| 1951 | } |
| 1952 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1953 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1954 | { |
| 1955 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1956 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1957 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1958 | loaded_vmcs->cpu = -1; |
| 1959 | loaded_vmcs->launched = 0; |
| 1960 | } |
| 1961 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1962 | static void vmcs_load(struct vmcs *vmcs) |
| 1963 | { |
| 1964 | u64 phys_addr = __pa(vmcs); |
| 1965 | u8 error; |
| 1966 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1967 | if (static_branch_unlikely(&enable_evmcs)) |
| 1968 | return evmcs_load(phys_addr); |
| 1969 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1970 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1971 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1972 | : "cc", "memory"); |
| 1973 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1974 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1975 | vmcs, phys_addr); |
| 1976 | } |
| 1977 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1978 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1979 | /* |
| 1980 | * This bitmap is used to indicate whether the vmclear |
| 1981 | * operation is enabled on all cpus. All disabled by |
| 1982 | * default. |
| 1983 | */ |
| 1984 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1985 | |
| 1986 | static inline void crash_enable_local_vmclear(int cpu) |
| 1987 | { |
| 1988 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1989 | } |
| 1990 | |
| 1991 | static inline void crash_disable_local_vmclear(int cpu) |
| 1992 | { |
| 1993 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1994 | } |
| 1995 | |
| 1996 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1997 | { |
| 1998 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1999 | } |
| 2000 | |
| 2001 | static void crash_vmclear_local_loaded_vmcss(void) |
| 2002 | { |
| 2003 | int cpu = raw_smp_processor_id(); |
| 2004 | struct loaded_vmcs *v; |
| 2005 | |
| 2006 | if (!crash_local_vmclear_enabled(cpu)) |
| 2007 | return; |
| 2008 | |
| 2009 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2010 | loaded_vmcss_on_cpu_link) |
| 2011 | vmcs_clear(v->vmcs); |
| 2012 | } |
| 2013 | #else |
| 2014 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 2015 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 2016 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2017 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2018 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2019 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2020 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2021 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2022 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2023 | if (loaded_vmcs->cpu != cpu) |
| 2024 | return; /* vcpu migration can race with cpu offline */ |
| 2025 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2026 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2027 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2028 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2029 | |
| 2030 | /* |
| 2031 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 2032 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 2033 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 2034 | * then adds the vmcs into percpu list before it is deleted. |
| 2035 | */ |
| 2036 | smp_wmb(); |
| 2037 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2038 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2039 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2040 | } |
| 2041 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2042 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 2043 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 2044 | int cpu = loaded_vmcs->cpu; |
| 2045 | |
| 2046 | if (cpu != -1) |
| 2047 | smp_call_function_single(cpu, |
| 2048 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 2049 | } |
| 2050 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 2051 | static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr) |
| 2052 | { |
| 2053 | if (vpid == 0) |
| 2054 | return true; |
| 2055 | |
| 2056 | if (cpu_has_vmx_invvpid_individual_addr()) { |
| 2057 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr); |
| 2058 | return true; |
| 2059 | } |
| 2060 | |
| 2061 | return false; |
| 2062 | } |
| 2063 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2064 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2065 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2066 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2067 | return; |
| 2068 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 2069 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2070 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2071 | } |
| 2072 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2073 | static inline void vpid_sync_vcpu_global(void) |
| 2074 | { |
| 2075 | if (cpu_has_vmx_invvpid_global()) |
| 2076 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 2077 | } |
| 2078 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2079 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2080 | { |
| 2081 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2082 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2083 | else |
| 2084 | vpid_sync_vcpu_global(); |
| 2085 | } |
| 2086 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2087 | static inline void ept_sync_global(void) |
| 2088 | { |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 2089 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | static inline void ept_sync_context(u64 eptp) |
| 2093 | { |
David Hildenbrand | 0e1252d | 2017-08-24 20:51:28 +0200 | [diff] [blame] | 2094 | if (cpu_has_vmx_invept_context()) |
| 2095 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 2096 | else |
| 2097 | ept_sync_global(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2098 | } |
| 2099 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2100 | static __always_inline void vmcs_check16(unsigned long field) |
| 2101 | { |
| 2102 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2103 | "16-bit accessor invalid for 64-bit field"); |
| 2104 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2105 | "16-bit accessor invalid for 64-bit high field"); |
| 2106 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2107 | "16-bit accessor invalid for 32-bit high field"); |
| 2108 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2109 | "16-bit accessor invalid for natural width field"); |
| 2110 | } |
| 2111 | |
| 2112 | static __always_inline void vmcs_check32(unsigned long field) |
| 2113 | { |
| 2114 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2115 | "32-bit accessor invalid for 16-bit field"); |
| 2116 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2117 | "32-bit accessor invalid for natural width field"); |
| 2118 | } |
| 2119 | |
| 2120 | static __always_inline void vmcs_check64(unsigned long field) |
| 2121 | { |
| 2122 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2123 | "64-bit accessor invalid for 16-bit field"); |
| 2124 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2125 | "64-bit accessor invalid for 64-bit high field"); |
| 2126 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2127 | "64-bit accessor invalid for 32-bit field"); |
| 2128 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2129 | "64-bit accessor invalid for natural width field"); |
| 2130 | } |
| 2131 | |
| 2132 | static __always_inline void vmcs_checkl(unsigned long field) |
| 2133 | { |
| 2134 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2135 | "Natural width accessor invalid for 16-bit field"); |
| 2136 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2137 | "Natural width accessor invalid for 64-bit field"); |
| 2138 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2139 | "Natural width accessor invalid for 64-bit high field"); |
| 2140 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2141 | "Natural width accessor invalid for 32-bit field"); |
| 2142 | } |
| 2143 | |
| 2144 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2145 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2146 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2147 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2148 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 2149 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2150 | return value; |
| 2151 | } |
| 2152 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2153 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2154 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2155 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2156 | if (static_branch_unlikely(&enable_evmcs)) |
| 2157 | return evmcs_read16(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2158 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2159 | } |
| 2160 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2161 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2162 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2163 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2164 | if (static_branch_unlikely(&enable_evmcs)) |
| 2165 | return evmcs_read32(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2166 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2167 | } |
| 2168 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2169 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2170 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2171 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2172 | if (static_branch_unlikely(&enable_evmcs)) |
| 2173 | return evmcs_read64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2174 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2175 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2176 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2177 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2178 | #endif |
| 2179 | } |
| 2180 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2181 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 2182 | { |
| 2183 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2184 | if (static_branch_unlikely(&enable_evmcs)) |
| 2185 | return evmcs_read64(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2186 | return __vmcs_readl(field); |
| 2187 | } |
| 2188 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2189 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 2190 | { |
| 2191 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 2192 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 2193 | dump_stack(); |
| 2194 | } |
| 2195 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2196 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2197 | { |
| 2198 | u8 error; |
| 2199 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2200 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2201 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2202 | if (unlikely(error)) |
| 2203 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2204 | } |
| 2205 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2206 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2207 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2208 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2209 | if (static_branch_unlikely(&enable_evmcs)) |
| 2210 | return evmcs_write16(field, value); |
| 2211 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2212 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2213 | } |
| 2214 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2215 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2216 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2217 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2218 | if (static_branch_unlikely(&enable_evmcs)) |
| 2219 | return evmcs_write32(field, value); |
| 2220 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2221 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2224 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2225 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2226 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2227 | if (static_branch_unlikely(&enable_evmcs)) |
| 2228 | return evmcs_write64(field, value); |
| 2229 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2230 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 2231 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2232 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2233 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2234 | #endif |
| 2235 | } |
| 2236 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2237 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2238 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2239 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2240 | if (static_branch_unlikely(&enable_evmcs)) |
| 2241 | return evmcs_write64(field, value); |
| 2242 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2243 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2244 | } |
| 2245 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2246 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2247 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2248 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2249 | "vmcs_clear_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2250 | if (static_branch_unlikely(&enable_evmcs)) |
| 2251 | return evmcs_write32(field, evmcs_read32(field) & ~mask); |
| 2252 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2253 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 2254 | } |
| 2255 | |
| 2256 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 2257 | { |
| 2258 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2259 | "vmcs_set_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2260 | if (static_branch_unlikely(&enable_evmcs)) |
| 2261 | return evmcs_write32(field, evmcs_read32(field) | mask); |
| 2262 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2263 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2264 | } |
| 2265 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2266 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2267 | { |
| 2268 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 2269 | } |
| 2270 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2271 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2272 | { |
| 2273 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 2274 | vmx->vm_entry_controls_shadow = val; |
| 2275 | } |
| 2276 | |
| 2277 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2278 | { |
| 2279 | if (vmx->vm_entry_controls_shadow != val) |
| 2280 | vm_entry_controls_init(vmx, val); |
| 2281 | } |
| 2282 | |
| 2283 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 2284 | { |
| 2285 | return vmx->vm_entry_controls_shadow; |
| 2286 | } |
| 2287 | |
| 2288 | |
| 2289 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2290 | { |
| 2291 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 2292 | } |
| 2293 | |
| 2294 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2295 | { |
| 2296 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 2297 | } |
| 2298 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2299 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2300 | { |
| 2301 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 2302 | } |
| 2303 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2304 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2305 | { |
| 2306 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 2307 | vmx->vm_exit_controls_shadow = val; |
| 2308 | } |
| 2309 | |
| 2310 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2311 | { |
| 2312 | if (vmx->vm_exit_controls_shadow != val) |
| 2313 | vm_exit_controls_init(vmx, val); |
| 2314 | } |
| 2315 | |
| 2316 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 2317 | { |
| 2318 | return vmx->vm_exit_controls_shadow; |
| 2319 | } |
| 2320 | |
| 2321 | |
| 2322 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2323 | { |
| 2324 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 2325 | } |
| 2326 | |
| 2327 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2328 | { |
| 2329 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 2330 | } |
| 2331 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2332 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 2333 | { |
| 2334 | vmx->segment_cache.bitmask = 0; |
| 2335 | } |
| 2336 | |
| 2337 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 2338 | unsigned field) |
| 2339 | { |
| 2340 | bool ret; |
| 2341 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 2342 | |
| 2343 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 2344 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 2345 | vmx->segment_cache.bitmask = 0; |
| 2346 | } |
| 2347 | ret = vmx->segment_cache.bitmask & mask; |
| 2348 | vmx->segment_cache.bitmask |= mask; |
| 2349 | return ret; |
| 2350 | } |
| 2351 | |
| 2352 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 2353 | { |
| 2354 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 2355 | |
| 2356 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 2357 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 2358 | return *p; |
| 2359 | } |
| 2360 | |
| 2361 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 2362 | { |
| 2363 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 2364 | |
| 2365 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 2366 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 2367 | return *p; |
| 2368 | } |
| 2369 | |
| 2370 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 2371 | { |
| 2372 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 2373 | |
| 2374 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 2375 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 2376 | return *p; |
| 2377 | } |
| 2378 | |
| 2379 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 2380 | { |
| 2381 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 2382 | |
| 2383 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 2384 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 2385 | return *p; |
| 2386 | } |
| 2387 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2388 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 2389 | { |
| 2390 | u32 eb; |
| 2391 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2392 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2393 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 2394 | /* |
| 2395 | * Guest access to VMware backdoor ports could legitimately |
| 2396 | * trigger #GP because of TSS I/O permission bitmap. |
| 2397 | * We intercept those #GP and allow access to them anyway |
| 2398 | * as VMware does. |
| 2399 | */ |
| 2400 | if (enable_vmware_backdoor) |
| 2401 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2402 | if ((vcpu->guest_debug & |
| 2403 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 2404 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 2405 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2406 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2407 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2408 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2409 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2410 | |
| 2411 | /* When we are running a nested L2 guest and L1 specified for it a |
| 2412 | * certain exception bitmap, we must trap the same exceptions and pass |
| 2413 | * them to L1. When running L2, we will only handle the exceptions |
| 2414 | * specified above if L1 did not want them. |
| 2415 | */ |
| 2416 | if (is_guest_mode(vcpu)) |
| 2417 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 2418 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2419 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 2420 | } |
| 2421 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2422 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 2423 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 2424 | */ |
| 2425 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 2426 | { |
| 2427 | unsigned long *msr_bitmap; |
| 2428 | int f = sizeof(unsigned long); |
| 2429 | |
| 2430 | if (!cpu_has_vmx_msr_bitmap()) |
| 2431 | return true; |
| 2432 | |
| 2433 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 2434 | |
| 2435 | if (msr <= 0x1fff) { |
| 2436 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2437 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2438 | msr &= 0x1fff; |
| 2439 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2440 | } |
| 2441 | |
| 2442 | return true; |
| 2443 | } |
| 2444 | |
| 2445 | /* |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2446 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 2447 | */ |
| 2448 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 2449 | { |
| 2450 | unsigned long *msr_bitmap; |
| 2451 | int f = sizeof(unsigned long); |
| 2452 | |
| 2453 | if (!cpu_has_vmx_msr_bitmap()) |
| 2454 | return true; |
| 2455 | |
| 2456 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 2457 | |
| 2458 | if (msr <= 0x1fff) { |
| 2459 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2460 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2461 | msr &= 0x1fff; |
| 2462 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2463 | } |
| 2464 | |
| 2465 | return true; |
| 2466 | } |
| 2467 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2468 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2469 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2470 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2471 | vm_entry_controls_clearbit(vmx, entry); |
| 2472 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2473 | } |
| 2474 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2475 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 2476 | { |
| 2477 | unsigned i; |
| 2478 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2479 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2480 | switch (msr) { |
| 2481 | case MSR_EFER: |
| 2482 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2483 | clear_atomic_switch_msr_special(vmx, |
| 2484 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2485 | VM_EXIT_LOAD_IA32_EFER); |
| 2486 | return; |
| 2487 | } |
| 2488 | break; |
| 2489 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2490 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2491 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2492 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2493 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2494 | return; |
| 2495 | } |
| 2496 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2497 | } |
| 2498 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2499 | for (i = 0; i < m->nr; ++i) |
| 2500 | if (m->guest[i].index == msr) |
| 2501 | break; |
| 2502 | |
| 2503 | if (i == m->nr) |
| 2504 | return; |
| 2505 | --m->nr; |
| 2506 | m->guest[i] = m->guest[m->nr]; |
| 2507 | m->host[i] = m->host[m->nr]; |
| 2508 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2509 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2510 | } |
| 2511 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2512 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2513 | unsigned long entry, unsigned long exit, |
| 2514 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 2515 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2516 | { |
| 2517 | vmcs_write64(guest_val_vmcs, guest_val); |
| 2518 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2519 | vm_entry_controls_setbit(vmx, entry); |
| 2520 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2521 | } |
| 2522 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2523 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 2524 | u64 guest_val, u64 host_val) |
| 2525 | { |
| 2526 | unsigned i; |
| 2527 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2528 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2529 | switch (msr) { |
| 2530 | case MSR_EFER: |
| 2531 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2532 | add_atomic_switch_msr_special(vmx, |
| 2533 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2534 | VM_EXIT_LOAD_IA32_EFER, |
| 2535 | GUEST_IA32_EFER, |
| 2536 | HOST_IA32_EFER, |
| 2537 | guest_val, host_val); |
| 2538 | return; |
| 2539 | } |
| 2540 | break; |
| 2541 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2542 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2543 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2544 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2545 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2546 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 2547 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 2548 | guest_val, host_val); |
| 2549 | return; |
| 2550 | } |
| 2551 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 2552 | case MSR_IA32_PEBS_ENABLE: |
| 2553 | /* PEBS needs a quiescent period after being disabled (to write |
| 2554 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 2555 | * provide that period, so a CPU could write host's record into |
| 2556 | * guest's memory. |
| 2557 | */ |
| 2558 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2559 | } |
| 2560 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2561 | for (i = 0; i < m->nr; ++i) |
| 2562 | if (m->guest[i].index == msr) |
| 2563 | break; |
| 2564 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2565 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 2566 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2567 | "Can't add msr %x\n", msr); |
| 2568 | return; |
| 2569 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2570 | ++m->nr; |
| 2571 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2572 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2573 | } |
| 2574 | |
| 2575 | m->guest[i].index = msr; |
| 2576 | m->guest[i].value = guest_val; |
| 2577 | m->host[i].index = msr; |
| 2578 | m->host[i].value = host_val; |
| 2579 | } |
| 2580 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2581 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2582 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2583 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2584 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2585 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2586 | if (!enable_ept) { |
| 2587 | /* |
| 2588 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2589 | * host CPUID is more efficient than testing guest CPUID |
| 2590 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2591 | */ |
| 2592 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2593 | guest_efer |= EFER_NX; |
| 2594 | else if (!(guest_efer & EFER_NX)) |
| 2595 | ignore_bits |= EFER_NX; |
| 2596 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2597 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2598 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2599 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2600 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2601 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2602 | #ifdef CONFIG_X86_64 |
| 2603 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2604 | /* SCE is meaningful only in long mode on Intel */ |
| 2605 | if (guest_efer & EFER_LMA) |
| 2606 | ignore_bits &= ~(u64)EFER_SCE; |
| 2607 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2608 | |
| 2609 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2610 | |
| 2611 | /* |
| 2612 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2613 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2614 | * atomically, since it's faster than switching it manually. |
| 2615 | */ |
| 2616 | if (cpu_has_load_ia32_efer || |
| 2617 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2618 | if (!(guest_efer & EFER_LMA)) |
| 2619 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2620 | if (guest_efer != host_efer) |
| 2621 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2622 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2623 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2624 | } else { |
| 2625 | guest_efer &= ~ignore_bits; |
| 2626 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2627 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2628 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2629 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2630 | |
| 2631 | return true; |
| 2632 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2633 | } |
| 2634 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2635 | #ifdef CONFIG_X86_32 |
| 2636 | /* |
| 2637 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 2638 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 2639 | * out the current bases to poke them into the VMCS before entry. |
| 2640 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2641 | static unsigned long segment_base(u16 selector) |
| 2642 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2643 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2644 | unsigned long v; |
| 2645 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2646 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2647 | return 0; |
| 2648 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2649 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2650 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2651 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2652 | u16 ldt_selector = kvm_read_ldt(); |
| 2653 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2654 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2655 | return 0; |
| 2656 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2657 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2658 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2659 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2660 | return v; |
| 2661 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2662 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2663 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2664 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2665 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2666 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2667 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2668 | int cpu = raw_smp_processor_id(); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2669 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2670 | unsigned long fs_base, gs_base; |
| 2671 | u16 fs_sel, gs_sel; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2672 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2673 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 2674 | if (vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2675 | return; |
| 2676 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 2677 | vmx->loaded_cpu_state = vmx->loaded_vmcs; |
| 2678 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2679 | /* |
| 2680 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2681 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2682 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2683 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2684 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2685 | |
| 2686 | #ifdef CONFIG_X86_64 |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2687 | savesegment(ds, vmx->host_state.ds_sel); |
| 2688 | savesegment(es, vmx->host_state.es_sel); |
| 2689 | |
| 2690 | gs_base = cpu_kernelmode_gs_base(cpu); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2691 | if (likely(is_64bit_mm(current->mm))) { |
| 2692 | save_fsgs_for_kvm(); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2693 | fs_sel = current->thread.fsindex; |
| 2694 | gs_sel = current->thread.gsindex; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2695 | fs_base = current->thread.fsbase; |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2696 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2697 | } else { |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2698 | savesegment(fs, fs_sel); |
| 2699 | savesegment(gs, gs_sel); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2700 | fs_base = read_msr(MSR_FS_BASE); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2701 | vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2702 | } |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2703 | |
| 2704 | if (is_long_mode(&vmx->vcpu)) |
| 2705 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2706 | #else |
| 2707 | savesegment(fs, fs_sel); |
| 2708 | savesegment(gs, gs_sel); |
| 2709 | fs_base = segment_base(fs_sel); |
| 2710 | gs_base = segment_base(gs_sel); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2711 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2712 | |
| 2713 | vmx->host_state.fs_sel = fs_sel; |
| 2714 | if (!(fs_sel & 7)) { |
| 2715 | vmcs_write16(HOST_FS_SELECTOR, fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2716 | vmx->host_state.fs_reload_needed = 0; |
| 2717 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2718 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2719 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2720 | } |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2721 | vmx->host_state.gs_sel = gs_sel; |
| 2722 | if (!(gs_sel & 7)) |
| 2723 | vmcs_write16(HOST_GS_SELECTOR, gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2724 | else { |
| 2725 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2726 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2727 | } |
| 2728 | |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2729 | vmcs_writel(HOST_FS_BASE, fs_base); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2730 | vmcs_writel(HOST_GS_BASE, gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2731 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2732 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2733 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2734 | vmx->guest_msrs[i].data, |
| 2735 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2736 | } |
| 2737 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2738 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2739 | { |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 2740 | if (!vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2741 | return; |
| 2742 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 2743 | WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs); |
| 2744 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2745 | ++vmx->vcpu.stat.host_state_reload; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 2746 | vmx->loaded_cpu_state = NULL; |
| 2747 | |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2748 | #ifdef CONFIG_X86_64 |
| 2749 | if (is_long_mode(&vmx->vcpu)) |
| 2750 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2751 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2752 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2753 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2754 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2755 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2756 | #else |
| 2757 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2758 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2759 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2760 | if (vmx->host_state.fs_reload_needed) |
| 2761 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2762 | #ifdef CONFIG_X86_64 |
| 2763 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2764 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2765 | loadsegment(es, vmx->host_state.es_sel); |
| 2766 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2767 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2768 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2769 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2770 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2771 | #endif |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2772 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2773 | } |
| 2774 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2775 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2776 | { |
| 2777 | preempt_disable(); |
| 2778 | __vmx_load_host_state(vmx); |
| 2779 | preempt_enable(); |
| 2780 | } |
| 2781 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2782 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2783 | { |
| 2784 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2785 | struct pi_desc old, new; |
| 2786 | unsigned int dest; |
| 2787 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2788 | /* |
| 2789 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2790 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2791 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2792 | * code easier, and CPU migration is not a fast path. |
| 2793 | */ |
| 2794 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2795 | return; |
| 2796 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2797 | /* |
| 2798 | * First handle the simple case where no cmpxchg is necessary; just |
| 2799 | * allow posting non-urgent interrupts. |
| 2800 | * |
| 2801 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2802 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2803 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2804 | * PI.NDST. |
| 2805 | */ |
| 2806 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2807 | vcpu->cpu == cpu) { |
| 2808 | pi_clear_sn(pi_desc); |
| 2809 | return; |
| 2810 | } |
| 2811 | |
| 2812 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2813 | do { |
| 2814 | old.control = new.control = pi_desc->control; |
| 2815 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2816 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2817 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2818 | if (x2apic_enabled()) |
| 2819 | new.ndst = dest; |
| 2820 | else |
| 2821 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2822 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2823 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2824 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2825 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2826 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2827 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2828 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2829 | { |
| 2830 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2831 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2832 | } |
| 2833 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2834 | /* |
| 2835 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2836 | * vcpu mutex is already taken. |
| 2837 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2838 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2839 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2840 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2841 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2842 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2843 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2844 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2845 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2846 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2847 | |
| 2848 | /* |
| 2849 | * Read loaded_vmcs->cpu should be before fetching |
| 2850 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2851 | * See the comments in __loaded_vmcs_clear(). |
| 2852 | */ |
| 2853 | smp_rmb(); |
| 2854 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2855 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2856 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2857 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2858 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2862 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2863 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2864 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2868 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2869 | unsigned long sysenter_esp; |
| 2870 | |
| 2871 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2872 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2873 | /* |
| 2874 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2875 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2876 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2877 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 2878 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2879 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2880 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2881 | /* |
| 2882 | * VM exits change the host TR limit to 0x67 after a VM |
| 2883 | * exit. This is okay, since 0x67 covers everything except |
| 2884 | * the IO bitmap and have have code to handle the IO bitmap |
| 2885 | * being lost after a VM exit. |
| 2886 | */ |
| 2887 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 2888 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2889 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2890 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2891 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2892 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2893 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2894 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2895 | /* Setup TSC multiplier */ |
| 2896 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2897 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2898 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2899 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2900 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2901 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 2902 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2906 | { |
| 2907 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2908 | |
| 2909 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2910 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2911 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2912 | return; |
| 2913 | |
| 2914 | /* Set SN when the vCPU is preempted */ |
| 2915 | if (vcpu->preempted) |
| 2916 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2920 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2921 | vmx_vcpu_pi_put(vcpu); |
| 2922 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2923 | __vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2924 | } |
| 2925 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2926 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 2927 | { |
| 2928 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 2929 | } |
| 2930 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2931 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2932 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2933 | /* |
| 2934 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2935 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2936 | * its hypervisor (cr0_read_shadow). |
| 2937 | */ |
| 2938 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2939 | { |
| 2940 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2941 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2942 | } |
| 2943 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2944 | { |
| 2945 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2946 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2947 | } |
| 2948 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2949 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2950 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2951 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2952 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2953 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2954 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2955 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2956 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2957 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2958 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2959 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2960 | } |
| 2961 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2962 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2963 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2967 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2968 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 2969 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2970 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2971 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2972 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2973 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2974 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2975 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2976 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2977 | |
| 2978 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 2979 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2980 | } |
| 2981 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2982 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2983 | { |
| 2984 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2985 | int ret = 0; |
| 2986 | |
| 2987 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2988 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2989 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2990 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2991 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2992 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2996 | { |
| 2997 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2998 | u32 interruptibility = interruptibility_old; |
| 2999 | |
| 3000 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 3001 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3002 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3003 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3004 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3005 | interruptibility |= GUEST_INTR_STATE_STI; |
| 3006 | |
| 3007 | if ((interruptibility != interruptibility_old)) |
| 3008 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 3009 | } |
| 3010 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3011 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 3012 | { |
| 3013 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3014 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3015 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3016 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3017 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3018 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3019 | /* skipping an emulated instruction also counts */ |
| 3020 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3021 | } |
| 3022 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3023 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3024 | unsigned long exit_qual) |
| 3025 | { |
| 3026 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3027 | unsigned int nr = vcpu->arch.exception.nr; |
| 3028 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3029 | |
| 3030 | if (vcpu->arch.exception.has_error_code) { |
| 3031 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3032 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3033 | } |
| 3034 | |
| 3035 | if (kvm_exception_is_soft(nr)) |
| 3036 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3037 | else |
| 3038 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3039 | |
| 3040 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3041 | vmx_get_nmi_mask(vcpu)) |
| 3042 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3043 | |
| 3044 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3045 | } |
| 3046 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3047 | /* |
| 3048 | * KVM wants to inject page-faults which it got to the guest. This function |
| 3049 | * 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] | 3050 | */ |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3051 | 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] | 3052 | { |
| 3053 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3054 | unsigned int nr = vcpu->arch.exception.nr; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3055 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3056 | if (nr == PF_VECTOR) { |
| 3057 | if (vcpu->arch.exception.nested_apf) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3058 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3059 | return 1; |
| 3060 | } |
| 3061 | /* |
| 3062 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. |
| 3063 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 3064 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 |
| 3065 | * can be written only when inject_pending_event runs. This should be |
| 3066 | * conditional on a new capability---if the capability is disabled, |
| 3067 | * kvm_multiple_exception would write the ancillary information to |
| 3068 | * CR2 or DR6, for backwards ABI-compatibility. |
| 3069 | */ |
| 3070 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 3071 | vcpu->arch.exception.error_code)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3072 | *exit_qual = vcpu->arch.cr2; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3073 | return 1; |
| 3074 | } |
| 3075 | } else { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3076 | if (vmcs12->exception_bitmap & (1u << nr)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3077 | if (nr == DB_VECTOR) |
| 3078 | *exit_qual = vcpu->arch.dr6; |
| 3079 | else |
| 3080 | *exit_qual = 0; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3081 | return 1; |
| 3082 | } |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3085 | return 0; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3086 | } |
| 3087 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3088 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 3089 | { |
| 3090 | /* |
| 3091 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 3092 | * explicitly skip the instruction because if the HLT state is set, |
| 3093 | * then the instruction is already executing and RIP has already been |
| 3094 | * advanced. |
| 3095 | */ |
| 3096 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 3097 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 3098 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 3099 | } |
| 3100 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3101 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3102 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3103 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3104 | unsigned nr = vcpu->arch.exception.nr; |
| 3105 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3106 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3107 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3108 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3109 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3110 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3111 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3112 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3113 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3114 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3115 | int inc_eip = 0; |
| 3116 | if (kvm_exception_is_soft(nr)) |
| 3117 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3118 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3119 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3120 | return; |
| 3121 | } |
| 3122 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 3123 | WARN_ON_ONCE(vmx->emulation_required); |
| 3124 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3125 | if (kvm_exception_is_soft(nr)) { |
| 3126 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3127 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3128 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3129 | } else |
| 3130 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3131 | |
| 3132 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3133 | |
| 3134 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3135 | } |
| 3136 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3137 | static bool vmx_rdtscp_supported(void) |
| 3138 | { |
| 3139 | return cpu_has_vmx_rdtscp(); |
| 3140 | } |
| 3141 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3142 | static bool vmx_invpcid_supported(void) |
| 3143 | { |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 3144 | return cpu_has_vmx_invpcid(); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3147 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3148 | * Swap MSR entry in host/guest MSR entry array. |
| 3149 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3150 | 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] | 3151 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3152 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3153 | |
| 3154 | tmp = vmx->guest_msrs[to]; |
| 3155 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 3156 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3160 | * Set up the vmcs to automatically save and restore system |
| 3161 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 3162 | * mode, as fiddling with msrs is very expensive. |
| 3163 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3164 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3165 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3166 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3167 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3168 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3169 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3170 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3171 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3172 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3173 | move_msr_up(vmx, index, save_nmsrs++); |
| 3174 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3175 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3176 | move_msr_up(vmx, index, save_nmsrs++); |
| 3177 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3178 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3179 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3180 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3181 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3182 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3183 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3184 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3185 | * if efer.sce is enabled. |
| 3186 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3187 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3188 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3189 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3190 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3191 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 3192 | index = __find_msr_index(vmx, MSR_EFER); |
| 3193 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3194 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3195 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3196 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3197 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3198 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3199 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3200 | } |
| 3201 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3202 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3203 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3204 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3205 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3206 | if (is_guest_mode(vcpu) && |
| 3207 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 3208 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; |
| 3209 | |
| 3210 | return vcpu->arch.tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3214 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3215 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3216 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3217 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3218 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3219 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3220 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 3221 | * to the spec, this should set L1's TSC; The offset that L1 |
| 3222 | * set for L2 remains unchanged, and still needs to be added |
| 3223 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3224 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3225 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3226 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 3227 | vmcs12 = get_vmcs12(vcpu); |
| 3228 | vmcs_write64(TSC_OFFSET, offset + |
| 3229 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 3230 | vmcs12->tsc_offset : 0)); |
| 3231 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 3232 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 3233 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3234 | vmcs_write64(TSC_OFFSET, offset); |
| 3235 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3236 | } |
| 3237 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3238 | /* |
| 3239 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 3240 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 3241 | * all guests if the "nested" module option is off, and can also be disabled |
| 3242 | * for a single guest by disabling its VMX cpuid bit. |
| 3243 | */ |
| 3244 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 3245 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3246 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3247 | } |
| 3248 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3249 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3250 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 3251 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 3252 | * The same values should also be used to verify that vmcs12 control fields are |
| 3253 | * valid during nested entry from L1 to L2. |
| 3254 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 3255 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 3256 | * bit in the high half is on if the corresponding bit in the control field |
| 3257 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3258 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3259 | 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] | 3260 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3261 | if (!nested) { |
| 3262 | memset(msrs, 0, sizeof(*msrs)); |
| 3263 | return; |
| 3264 | } |
| 3265 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3266 | /* |
| 3267 | * Note that as a general rule, the high half of the MSRs (bits in |
| 3268 | * the control fields which may be 1) should be initialized by the |
| 3269 | * intersection of the underlying hardware's MSR (i.e., features which |
| 3270 | * can be supported) and the list of features we want to expose - |
| 3271 | * because they are known to be properly supported in our code. |
| 3272 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 3273 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 3274 | * reason is that if one of these bits is necessary, it will appear |
| 3275 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 3276 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 3277 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3278 | * These rules have exceptions below. |
| 3279 | */ |
| 3280 | |
| 3281 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 3282 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3283 | msrs->pinbased_ctls_low, |
| 3284 | msrs->pinbased_ctls_high); |
| 3285 | msrs->pinbased_ctls_low |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3286 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3287 | msrs->pinbased_ctls_high &= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3288 | PIN_BASED_EXT_INTR_MASK | |
| 3289 | PIN_BASED_NMI_EXITING | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3290 | PIN_BASED_VIRTUAL_NMIS | |
| 3291 | (apicv ? PIN_BASED_POSTED_INTR : 0); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3292 | msrs->pinbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3293 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 3294 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3295 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 3296 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3297 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3298 | msrs->exit_ctls_low, |
| 3299 | msrs->exit_ctls_high); |
| 3300 | msrs->exit_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3301 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3302 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3303 | msrs->exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3304 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3305 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3306 | #endif |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3307 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3308 | msrs->exit_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3309 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3310 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3311 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 3312 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3313 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3314 | msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3315 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3316 | /* We support free control of debug control saving. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3317 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3318 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3319 | /* entry controls */ |
| 3320 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3321 | msrs->entry_ctls_low, |
| 3322 | msrs->entry_ctls_high); |
| 3323 | msrs->entry_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3324 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3325 | msrs->entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3326 | #ifdef CONFIG_X86_64 |
| 3327 | VM_ENTRY_IA32E_MODE | |
| 3328 | #endif |
| 3329 | VM_ENTRY_LOAD_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3330 | msrs->entry_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3331 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3332 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3333 | msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3334 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3335 | /* We support free control of debug control loading. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3336 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3337 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3338 | /* cpu-based controls */ |
| 3339 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3340 | msrs->procbased_ctls_low, |
| 3341 | msrs->procbased_ctls_high); |
| 3342 | msrs->procbased_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3343 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3344 | msrs->procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 3345 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 3346 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3347 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 3348 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 3349 | CPU_BASED_CR3_STORE_EXITING | |
| 3350 | #ifdef CONFIG_X86_64 |
| 3351 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 3352 | #endif |
| 3353 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 3354 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 3355 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 3356 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 3357 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3358 | /* |
| 3359 | * We can allow some features even when not supported by the |
| 3360 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 3361 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 3362 | * without MSR bitmaps. |
| 3363 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3364 | msrs->procbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3365 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 3366 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3367 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3368 | /* We support free control of CR3 access interception. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3369 | msrs->procbased_ctls_low &= |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3370 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 3371 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3372 | /* |
| 3373 | * secondary cpu-based controls. Do not include those that |
| 3374 | * depend on CPUID bits, they are added later by vmx_cpuid_update. |
| 3375 | */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3376 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3377 | msrs->secondary_ctls_low, |
| 3378 | msrs->secondary_ctls_high); |
| 3379 | msrs->secondary_ctls_low = 0; |
| 3380 | msrs->secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 3381 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 3382 | SECONDARY_EXEC_DESC | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 3383 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 3384 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 3385 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3386 | SECONDARY_EXEC_WBINVD_EXITING; |
Liran Alon | 32c7acf | 2018-06-23 02:35:11 +0300 | [diff] [blame] | 3387 | /* |
| 3388 | * We can emulate "VMCS shadowing," even if the hardware |
| 3389 | * doesn't support it. |
| 3390 | */ |
| 3391 | msrs->secondary_ctls_high |= |
| 3392 | SECONDARY_EXEC_SHADOW_VMCS; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3393 | |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3394 | if (enable_ept) { |
| 3395 | /* nested EPT: emulate EPT also to L1 */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3396 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3397 | SECONDARY_EXEC_ENABLE_EPT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3398 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3399 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3400 | if (cpu_has_vmx_ept_execute_only()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3401 | msrs->ept_caps |= |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3402 | VMX_EPT_EXECUTE_ONLY_BIT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3403 | msrs->ept_caps &= vmx_capability.ept; |
| 3404 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3405 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 3406 | VMX_EPT_1GB_PAGE_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3407 | if (enable_ept_ad_bits) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3408 | msrs->secondary_ctls_high |= |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3409 | SECONDARY_EXEC_ENABLE_PML; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3410 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3411 | } |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3412 | } |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3413 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3414 | if (cpu_has_vmx_vmfunc()) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3415 | msrs->secondary_ctls_high |= |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3416 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 3417 | /* |
| 3418 | * Advertise EPTP switching unconditionally |
| 3419 | * since we emulate it |
| 3420 | */ |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3421 | if (enable_ept) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3422 | msrs->vmfunc_controls = |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3423 | VMX_VMFUNC_EPTP_SWITCHING; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3424 | } |
| 3425 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 3426 | /* |
| 3427 | * Old versions of KVM use the single-context version without |
| 3428 | * checking for support, so declare that it is supported even |
| 3429 | * though it is treated as global context. The alternative is |
| 3430 | * not failing the single-context invvpid, and it is worse. |
| 3431 | */ |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3432 | if (enable_vpid) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3433 | msrs->secondary_ctls_high |= |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3434 | SECONDARY_EXEC_ENABLE_VPID; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3435 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 3436 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3437 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 3438 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3439 | if (enable_unrestricted_guest) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3440 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3441 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3442 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3443 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3444 | rdmsr(MSR_IA32_VMX_MISC, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3445 | msrs->misc_low, |
| 3446 | msrs->misc_high); |
| 3447 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 3448 | msrs->misc_low |= |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3449 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3450 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3451 | VMX_MISC_ACTIVITY_HLT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3452 | msrs->misc_high = 0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3453 | |
| 3454 | /* |
| 3455 | * This MSR reports some information about VMX support. We |
| 3456 | * should return information about the VMX we emulate for the |
| 3457 | * guest, and the VMCS structure we give it - not about the |
| 3458 | * VMX support of the underlying hardware. |
| 3459 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3460 | msrs->basic = |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3461 | VMCS12_REVISION | |
| 3462 | VMX_BASIC_TRUE_CTLS | |
| 3463 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 3464 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 3465 | |
| 3466 | if (cpu_has_vmx_basic_inout()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3467 | msrs->basic |= VMX_BASIC_INOUT; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3468 | |
| 3469 | /* |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3470 | * These MSRs specify bits which the guest must keep fixed on |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3471 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 3472 | * We picked the standard core2 setting. |
| 3473 | */ |
| 3474 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 3475 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3476 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 3477 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3478 | |
| 3479 | /* These MSRs specify bits which the guest must keep fixed off. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3480 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 3481 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3482 | |
| 3483 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3484 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3485 | } |
| 3486 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3487 | /* |
| 3488 | * if fixed0[i] == 1: val[i] must be 1 |
| 3489 | * if fixed1[i] == 0: val[i] must be 0 |
| 3490 | */ |
| 3491 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) |
| 3492 | { |
| 3493 | return ((val & fixed1) | fixed0) == val; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 3497 | { |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3498 | return fixed_bits_valid(control, low, high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 3502 | { |
| 3503 | return low | ((u64)high << 32); |
| 3504 | } |
| 3505 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3506 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 3507 | { |
| 3508 | superset &= mask; |
| 3509 | subset &= mask; |
| 3510 | |
| 3511 | return (superset | subset) == superset; |
| 3512 | } |
| 3513 | |
| 3514 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 3515 | { |
| 3516 | const u64 feature_and_reserved = |
| 3517 | /* feature (except bit 48; see below) */ |
| 3518 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 3519 | /* reserved */ |
| 3520 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3521 | u64 vmx_basic = vmx->nested.msrs.basic; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3522 | |
| 3523 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 3524 | return -EINVAL; |
| 3525 | |
| 3526 | /* |
| 3527 | * KVM does not emulate a version of VMX that constrains physical |
| 3528 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 3529 | */ |
| 3530 | if (data & BIT_ULL(48)) |
| 3531 | return -EINVAL; |
| 3532 | |
| 3533 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 3534 | vmx_basic_vmcs_revision_id(data)) |
| 3535 | return -EINVAL; |
| 3536 | |
| 3537 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 3538 | return -EINVAL; |
| 3539 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3540 | vmx->nested.msrs.basic = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3541 | return 0; |
| 3542 | } |
| 3543 | |
| 3544 | static int |
| 3545 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3546 | { |
| 3547 | u64 supported; |
| 3548 | u32 *lowp, *highp; |
| 3549 | |
| 3550 | switch (msr_index) { |
| 3551 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3552 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 3553 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3554 | break; |
| 3555 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3556 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 3557 | highp = &vmx->nested.msrs.procbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3558 | break; |
| 3559 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3560 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 3561 | highp = &vmx->nested.msrs.exit_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3562 | break; |
| 3563 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3564 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 3565 | highp = &vmx->nested.msrs.entry_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3566 | break; |
| 3567 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3568 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 3569 | highp = &vmx->nested.msrs.secondary_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3570 | break; |
| 3571 | default: |
| 3572 | BUG(); |
| 3573 | } |
| 3574 | |
| 3575 | supported = vmx_control_msr(*lowp, *highp); |
| 3576 | |
| 3577 | /* Check must-be-1 bits are still 1. */ |
| 3578 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 3579 | return -EINVAL; |
| 3580 | |
| 3581 | /* Check must-be-0 bits are still 0. */ |
| 3582 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 3583 | return -EINVAL; |
| 3584 | |
| 3585 | *lowp = data; |
| 3586 | *highp = data >> 32; |
| 3587 | return 0; |
| 3588 | } |
| 3589 | |
| 3590 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 3591 | { |
| 3592 | const u64 feature_and_reserved_bits = |
| 3593 | /* feature */ |
| 3594 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 3595 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 3596 | /* reserved */ |
| 3597 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 3598 | u64 vmx_misc; |
| 3599 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3600 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 3601 | vmx->nested.msrs.misc_high); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3602 | |
| 3603 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 3604 | return -EINVAL; |
| 3605 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3606 | if ((vmx->nested.msrs.pinbased_ctls_high & |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3607 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 3608 | vmx_misc_preemption_timer_rate(data) != |
| 3609 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 3610 | return -EINVAL; |
| 3611 | |
| 3612 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 3613 | return -EINVAL; |
| 3614 | |
| 3615 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 3616 | return -EINVAL; |
| 3617 | |
| 3618 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 3619 | return -EINVAL; |
| 3620 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3621 | vmx->nested.msrs.misc_low = data; |
| 3622 | vmx->nested.msrs.misc_high = data >> 32; |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3623 | |
| 3624 | /* |
| 3625 | * If L1 has read-only VM-exit information fields, use the |
| 3626 | * less permissive vmx_vmwrite_bitmap to specify write |
| 3627 | * permissions for the shadow VMCS. |
| 3628 | */ |
| 3629 | if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 3630 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 3631 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3632 | return 0; |
| 3633 | } |
| 3634 | |
| 3635 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 3636 | { |
| 3637 | u64 vmx_ept_vpid_cap; |
| 3638 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3639 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 3640 | vmx->nested.msrs.vpid_caps); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3641 | |
| 3642 | /* Every bit is either reserved or a feature bit. */ |
| 3643 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 3644 | return -EINVAL; |
| 3645 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3646 | vmx->nested.msrs.ept_caps = data; |
| 3647 | vmx->nested.msrs.vpid_caps = data >> 32; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3648 | return 0; |
| 3649 | } |
| 3650 | |
| 3651 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3652 | { |
| 3653 | u64 *msr; |
| 3654 | |
| 3655 | switch (msr_index) { |
| 3656 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3657 | msr = &vmx->nested.msrs.cr0_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3658 | break; |
| 3659 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3660 | msr = &vmx->nested.msrs.cr4_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3661 | break; |
| 3662 | default: |
| 3663 | BUG(); |
| 3664 | } |
| 3665 | |
| 3666 | /* |
| 3667 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 3668 | * must be 1 in the restored value. |
| 3669 | */ |
| 3670 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 3671 | return -EINVAL; |
| 3672 | |
| 3673 | *msr = data; |
| 3674 | return 0; |
| 3675 | } |
| 3676 | |
| 3677 | /* |
| 3678 | * Called when userspace is restoring VMX MSRs. |
| 3679 | * |
| 3680 | * Returns 0 on success, non-0 otherwise. |
| 3681 | */ |
| 3682 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 3683 | { |
| 3684 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3685 | |
Jim Mattson | a943ac5 | 2018-05-29 09:11:32 -0700 | [diff] [blame] | 3686 | /* |
| 3687 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 3688 | * is in VMX operation. |
| 3689 | */ |
| 3690 | if (vmx->nested.vmxon) |
| 3691 | return -EBUSY; |
| 3692 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3693 | switch (msr_index) { |
| 3694 | case MSR_IA32_VMX_BASIC: |
| 3695 | return vmx_restore_vmx_basic(vmx, data); |
| 3696 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 3697 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 3698 | case MSR_IA32_VMX_EXIT_CTLS: |
| 3699 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 3700 | /* |
| 3701 | * The "non-true" VMX capability MSRs are generated from the |
| 3702 | * "true" MSRs, so we do not support restoring them directly. |
| 3703 | * |
| 3704 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 3705 | * should restore the "true" MSRs with the must-be-1 bits |
| 3706 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 3707 | * DEFAULT SETTINGS". |
| 3708 | */ |
| 3709 | return -EINVAL; |
| 3710 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3711 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3712 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3713 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3714 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 3715 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 3716 | case MSR_IA32_VMX_MISC: |
| 3717 | return vmx_restore_vmx_misc(vmx, data); |
| 3718 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3719 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3720 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 3721 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3722 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3723 | /* |
| 3724 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 3725 | * do not support restoring them directly. |
| 3726 | */ |
| 3727 | return -EINVAL; |
| 3728 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 3729 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 3730 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3731 | vmx->nested.msrs.vmcs_enum = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3732 | return 0; |
| 3733 | default: |
| 3734 | /* |
| 3735 | * The rest of the VMX capability MSRs do not support restore. |
| 3736 | */ |
| 3737 | return -EINVAL; |
| 3738 | } |
| 3739 | } |
| 3740 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3741 | /* Returns 0 on success, non-0 otherwise. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3742 | 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] | 3743 | { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3744 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3745 | case MSR_IA32_VMX_BASIC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3746 | *pdata = msrs->basic; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3747 | break; |
| 3748 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3749 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3750 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3751 | msrs->pinbased_ctls_low, |
| 3752 | msrs->pinbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3753 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 3754 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3755 | break; |
| 3756 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3757 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3758 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3759 | msrs->procbased_ctls_low, |
| 3760 | msrs->procbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3761 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 3762 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3763 | break; |
| 3764 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3765 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3766 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3767 | msrs->exit_ctls_low, |
| 3768 | msrs->exit_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3769 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 3770 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3771 | break; |
| 3772 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3773 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3774 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3775 | msrs->entry_ctls_low, |
| 3776 | msrs->entry_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3777 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 3778 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3779 | break; |
| 3780 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3781 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3782 | msrs->misc_low, |
| 3783 | msrs->misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3784 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3785 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3786 | *pdata = msrs->cr0_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3787 | break; |
| 3788 | case MSR_IA32_VMX_CR0_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3789 | *pdata = msrs->cr0_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3790 | break; |
| 3791 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3792 | *pdata = msrs->cr4_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3793 | break; |
| 3794 | case MSR_IA32_VMX_CR4_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3795 | *pdata = msrs->cr4_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3796 | break; |
| 3797 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3798 | *pdata = msrs->vmcs_enum; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3799 | break; |
| 3800 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
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->secondary_ctls_low, |
| 3803 | msrs->secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3804 | break; |
| 3805 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3806 | *pdata = msrs->ept_caps | |
| 3807 | ((u64)msrs->vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3808 | break; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3809 | case MSR_IA32_VMX_VMFUNC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3810 | *pdata = msrs->vmfunc_controls; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3811 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3812 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3813 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 3814 | } |
| 3815 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3816 | return 0; |
| 3817 | } |
| 3818 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3819 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 3820 | uint64_t val) |
| 3821 | { |
| 3822 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 3823 | |
| 3824 | return !(val & ~valid_bits); |
| 3825 | } |
| 3826 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3827 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 3828 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3829 | switch (msr->index) { |
| 3830 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3831 | if (!nested) |
| 3832 | return 1; |
| 3833 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 3834 | default: |
| 3835 | return 1; |
| 3836 | } |
| 3837 | |
| 3838 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3839 | } |
| 3840 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3841 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3842 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 3843 | * Returns 0 on success, non-0 otherwise. |
| 3844 | * Assumes vcpu_load() was already called. |
| 3845 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3846 | 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] | 3847 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3848 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3849 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3850 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3851 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3852 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3853 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3854 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3855 | break; |
| 3856 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3857 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3858 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3859 | case MSR_KERNEL_GS_BASE: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3860 | vmx_load_host_state(vmx); |
| 3861 | msr_info->data = vmx->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3862 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3863 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3864 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3865 | return kvm_get_msr_common(vcpu, msr_info); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3866 | case MSR_IA32_SPEC_CTRL: |
| 3867 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3868 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3869 | return 1; |
| 3870 | |
| 3871 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 3872 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3873 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3874 | if (!msr_info->host_initiated && |
| 3875 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 3876 | return 1; |
| 3877 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 3878 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3879 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3880 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3881 | break; |
| 3882 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3883 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3884 | break; |
| 3885 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3886 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3887 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3888 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3889 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3890 | (!msr_info->host_initiated && |
| 3891 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3892 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3893 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3894 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3895 | case MSR_IA32_MCG_EXT_CTL: |
| 3896 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3897 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3898 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3899 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3900 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3901 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3902 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3903 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3904 | break; |
| 3905 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3906 | if (!nested_vmx_allowed(vcpu)) |
| 3907 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3908 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 3909 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3910 | case MSR_IA32_XSS: |
| 3911 | if (!vmx_xsaves_supported()) |
| 3912 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3913 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3914 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3915 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3916 | if (!msr_info->host_initiated && |
| 3917 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3918 | return 1; |
| 3919 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3920 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3921 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3922 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3923 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3924 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3925 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3926 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3927 | } |
| 3928 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3929 | return 0; |
| 3930 | } |
| 3931 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3932 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3933 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3934 | /* |
| 3935 | * Writes msr value into into the appropriate "register". |
| 3936 | * Returns 0 on success, non-0 otherwise. |
| 3937 | * Assumes vcpu_load() was already called. |
| 3938 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3939 | 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] | 3940 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3941 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3942 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3943 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3944 | u32 msr_index = msr_info->index; |
| 3945 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3946 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3947 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3948 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3949 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3950 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3951 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3952 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3953 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3954 | vmcs_writel(GUEST_FS_BASE, data); |
| 3955 | break; |
| 3956 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3957 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3958 | vmcs_writel(GUEST_GS_BASE, data); |
| 3959 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3960 | case MSR_KERNEL_GS_BASE: |
| 3961 | vmx_load_host_state(vmx); |
| 3962 | vmx->msr_guest_kernel_gs_base = data; |
| 3963 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3964 | #endif |
| 3965 | case MSR_IA32_SYSENTER_CS: |
| 3966 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3967 | break; |
| 3968 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3969 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3970 | break; |
| 3971 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3972 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3973 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3974 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3975 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3976 | (!msr_info->host_initiated && |
| 3977 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3978 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 3979 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3980 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3981 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3982 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3983 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3984 | case MSR_IA32_SPEC_CTRL: |
| 3985 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3986 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3987 | return 1; |
| 3988 | |
| 3989 | /* 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] | 3990 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3991 | return 1; |
| 3992 | |
| 3993 | vmx->spec_ctrl = data; |
| 3994 | |
| 3995 | if (!data) |
| 3996 | break; |
| 3997 | |
| 3998 | /* |
| 3999 | * For non-nested: |
| 4000 | * When it's written (to non-zero) for the first time, pass |
| 4001 | * it through. |
| 4002 | * |
| 4003 | * For nested: |
| 4004 | * The handling of the MSR bitmap for L2 guests is done in |
| 4005 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 4006 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 4007 | * in the merging. We update the vmcs01 here for L1 as well |
| 4008 | * since it will end up touching the MSR anyway now. |
| 4009 | */ |
| 4010 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 4011 | MSR_IA32_SPEC_CTRL, |
| 4012 | MSR_TYPE_RW); |
| 4013 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4014 | case MSR_IA32_PRED_CMD: |
| 4015 | if (!msr_info->host_initiated && |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4016 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 4017 | return 1; |
| 4018 | |
| 4019 | if (data & ~PRED_CMD_IBPB) |
| 4020 | return 1; |
| 4021 | |
| 4022 | if (!data) |
| 4023 | break; |
| 4024 | |
| 4025 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 4026 | |
| 4027 | /* |
| 4028 | * For non-nested: |
| 4029 | * When it's written (to non-zero) for the first time, pass |
| 4030 | * it through. |
| 4031 | * |
| 4032 | * For nested: |
| 4033 | * The handling of the MSR bitmap for L2 guests is done in |
| 4034 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 4035 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 4036 | * in the merging. |
| 4037 | */ |
| 4038 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 4039 | MSR_TYPE_W); |
| 4040 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 4041 | case MSR_IA32_ARCH_CAPABILITIES: |
| 4042 | if (!msr_info->host_initiated) |
| 4043 | return 1; |
| 4044 | vmx->arch_capabilities = data; |
| 4045 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4046 | case MSR_IA32_CR_PAT: |
| 4047 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 4048 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 4049 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4050 | vmcs_write64(GUEST_IA32_PAT, data); |
| 4051 | vcpu->arch.pat = data; |
| 4052 | break; |
| 4053 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4054 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4055 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 4056 | case MSR_IA32_TSC_ADJUST: |
| 4057 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4058 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 4059 | case MSR_IA32_MCG_EXT_CTL: |
| 4060 | if ((!msr_info->host_initiated && |
| 4061 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 4062 | FEATURE_CONTROL_LMCE)) || |
| 4063 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 4064 | return 1; |
| 4065 | vcpu->arch.mcg_ext_ctl = data; |
| 4066 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4067 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 4068 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 4069 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4070 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 4071 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 4072 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4073 | if (msr_info->host_initiated && data == 0) |
| 4074 | vmx_leave_nested(vcpu); |
| 4075 | break; |
| 4076 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 4077 | if (!msr_info->host_initiated) |
| 4078 | return 1; /* they are read-only */ |
| 4079 | if (!nested_vmx_allowed(vcpu)) |
| 4080 | return 1; |
| 4081 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4082 | case MSR_IA32_XSS: |
| 4083 | if (!vmx_xsaves_supported()) |
| 4084 | return 1; |
| 4085 | /* |
| 4086 | * The only supported bit as of Skylake is bit 8, but |
| 4087 | * it is not supported on KVM. |
| 4088 | */ |
| 4089 | if (data != 0) |
| 4090 | return 1; |
| 4091 | vcpu->arch.ia32_xss = data; |
| 4092 | if (vcpu->arch.ia32_xss != host_xss) |
| 4093 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 4094 | vcpu->arch.ia32_xss, host_xss); |
| 4095 | else |
| 4096 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 4097 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4098 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4099 | if (!msr_info->host_initiated && |
| 4100 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4101 | return 1; |
| 4102 | /* Check reserved bit, higher 32 bits should be zero */ |
| 4103 | if ((data >> 32) != 0) |
| 4104 | return 1; |
| 4105 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4106 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4107 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4108 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4109 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4110 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4111 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 4112 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4113 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 4114 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4115 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4116 | if (ret) |
| 4117 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4118 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4119 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4120 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4121 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4122 | } |
| 4123 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4124 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4125 | } |
| 4126 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4127 | 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] | 4128 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4129 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 4130 | switch (reg) { |
| 4131 | case VCPU_REGS_RSP: |
| 4132 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 4133 | break; |
| 4134 | case VCPU_REGS_RIP: |
| 4135 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 4136 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4137 | case VCPU_EXREG_PDPTR: |
| 4138 | if (enable_ept) |
| 4139 | ept_save_pdptrs(vcpu); |
| 4140 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4141 | default: |
| 4142 | break; |
| 4143 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4144 | } |
| 4145 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4146 | static __init int cpu_has_kvm_support(void) |
| 4147 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 4148 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | static __init int vmx_disabled_by_bios(void) |
| 4152 | { |
| 4153 | u64 msr; |
| 4154 | |
| 4155 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4156 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4157 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4158 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 4159 | && tboot_enabled()) |
| 4160 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4161 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4162 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4163 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4164 | && !tboot_enabled()) { |
| 4165 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4166 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4167 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4168 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4169 | /* launched w/o TXT and VMX disabled */ |
| 4170 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 4171 | && !tboot_enabled()) |
| 4172 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4176 | } |
| 4177 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4178 | static void kvm_cpu_vmxon(u64 addr) |
| 4179 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4180 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4181 | intel_pt_handle_vmx(1); |
| 4182 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4183 | asm volatile (ASM_VMX_VMXON_RAX |
| 4184 | : : "a"(&addr), "m"(addr) |
| 4185 | : "memory", "cc"); |
| 4186 | } |
| 4187 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4188 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4189 | { |
| 4190 | int cpu = raw_smp_processor_id(); |
| 4191 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4192 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4193 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 4194 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4195 | return -EBUSY; |
| 4196 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4197 | /* |
| 4198 | * This can happen if we hot-added a CPU but failed to allocate |
| 4199 | * VP assist page for it. |
| 4200 | */ |
| 4201 | if (static_branch_unlikely(&enable_evmcs) && |
| 4202 | !hv_get_vp_assist_page(cpu)) |
| 4203 | return -EFAULT; |
| 4204 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4205 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 4206 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 4207 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 4208 | |
| 4209 | /* |
| 4210 | * Now we can enable the vmclear operation in kdump |
| 4211 | * since the loaded_vmcss_on_cpu list on this cpu |
| 4212 | * has been initialized. |
| 4213 | * |
| 4214 | * Though the cpu is not in VMX operation now, there |
| 4215 | * is no problem to enable the vmclear operation |
| 4216 | * for the loaded_vmcss_on_cpu list is empty! |
| 4217 | */ |
| 4218 | crash_enable_local_vmclear(cpu); |
| 4219 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4220 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4221 | |
| 4222 | test_bits = FEATURE_CONTROL_LOCKED; |
| 4223 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 4224 | if (tboot_enabled()) |
| 4225 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 4226 | |
| 4227 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4228 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4229 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 4230 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4231 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 4232 | if (enable_ept) |
| 4233 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4234 | |
| 4235 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4236 | } |
| 4237 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4238 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4239 | { |
| 4240 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4241 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4242 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4243 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 4244 | loaded_vmcss_on_cpu_link) |
| 4245 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4246 | } |
| 4247 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4248 | |
| 4249 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 4250 | * tricks. |
| 4251 | */ |
| 4252 | static void kvm_cpu_vmxoff(void) |
| 4253 | { |
| 4254 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4255 | |
| 4256 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4257 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4258 | } |
| 4259 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4260 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4261 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4262 | vmclear_local_loaded_vmcss(); |
| 4263 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4264 | } |
| 4265 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4266 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4267 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4268 | { |
| 4269 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4270 | u32 ctl = ctl_min | ctl_opt; |
| 4271 | |
| 4272 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4273 | |
| 4274 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 4275 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 4276 | |
| 4277 | /* Ensure minimum (required) set of control bits are supported. */ |
| 4278 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4279 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4280 | |
| 4281 | *result = ctl; |
| 4282 | return 0; |
| 4283 | } |
| 4284 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4285 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 4286 | { |
| 4287 | u32 vmx_msr_low, vmx_msr_high; |
| 4288 | |
| 4289 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4290 | return vmx_msr_high & ctl; |
| 4291 | } |
| 4292 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4293 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4294 | { |
| 4295 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4296 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4297 | u32 _pin_based_exec_control = 0; |
| 4298 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4299 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4300 | u32 _vmexit_control = 0; |
| 4301 | u32 _vmentry_control = 0; |
| 4302 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 4303 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 4304 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4305 | #ifdef CONFIG_X86_64 |
| 4306 | CPU_BASED_CR8_LOAD_EXITING | |
| 4307 | CPU_BASED_CR8_STORE_EXITING | |
| 4308 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4309 | CPU_BASED_CR3_LOAD_EXITING | |
| 4310 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 4311 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4312 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4313 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 4314 | CPU_BASED_MWAIT_EXITING | |
| 4315 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4316 | CPU_BASED_INVLPG_EXITING | |
| 4317 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4318 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4319 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4320 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4321 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4322 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 4323 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4324 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4325 | #ifdef CONFIG_X86_64 |
| 4326 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4327 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 4328 | ~CPU_BASED_CR8_STORE_EXITING; |
| 4329 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4330 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4331 | min2 = 0; |
| 4332 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4333 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4334 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4335 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4336 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4337 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4338 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4339 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4340 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4341 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4342 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4343 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4344 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 4345 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 4346 | SECONDARY_EXEC_RDSEED_EXITING | |
| 4347 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 4348 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 4349 | SECONDARY_EXEC_TSC_SCALING | |
| 4350 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4351 | if (adjust_vmx_controls(min2, opt2, |
| 4352 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4353 | &_cpu_based_2nd_exec_control) < 0) |
| 4354 | return -EIO; |
| 4355 | } |
| 4356 | #ifndef CONFIG_X86_64 |
| 4357 | if (!(_cpu_based_2nd_exec_control & |
| 4358 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 4359 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4360 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4361 | |
| 4362 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4363 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4364 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4365 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 4366 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4367 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4368 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
| 4369 | &vmx_capability.ept, &vmx_capability.vpid); |
| 4370 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4371 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4372 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 4373 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 4374 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4375 | CPU_BASED_CR3_STORE_EXITING | |
| 4376 | CPU_BASED_INVLPG_EXITING); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4377 | } else if (vmx_capability.ept) { |
| 4378 | vmx_capability.ept = 0; |
| 4379 | pr_warn_once("EPT CAP should not exist if not support " |
| 4380 | "1-setting enable EPT VM-execution control\n"); |
| 4381 | } |
| 4382 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
| 4383 | vmx_capability.vpid) { |
| 4384 | vmx_capability.vpid = 0; |
| 4385 | pr_warn_once("VPID CAP should not exist if not support " |
| 4386 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4387 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4388 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4389 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4390 | #ifdef CONFIG_X86_64 |
| 4391 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 4392 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4393 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4394 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4395 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 4396 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4397 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4398 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4399 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 4400 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 4401 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4402 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 4403 | &_pin_based_exec_control) < 0) |
| 4404 | return -EIO; |
| 4405 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 4406 | if (cpu_has_broken_vmx_preemption_timer()) |
| 4407 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4408 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4409 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4410 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 4411 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 4412 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 4413 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4414 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 4415 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4416 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4417 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4418 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4419 | |
| 4420 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 4421 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
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 | |
| 4424 | #ifdef CONFIG_X86_64 |
| 4425 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 4426 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4427 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4428 | #endif |
| 4429 | |
| 4430 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 4431 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4432 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4433 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4434 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 4435 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 4436 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4437 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4438 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4439 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4440 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 4441 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4442 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4443 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 4444 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4445 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4446 | if (static_branch_unlikely(&enable_evmcs)) |
| 4447 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 4448 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4449 | cpu_has_load_ia32_efer = |
| 4450 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4451 | VM_ENTRY_LOAD_IA32_EFER) |
| 4452 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4453 | VM_EXIT_LOAD_IA32_EFER); |
| 4454 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 4455 | cpu_has_load_perf_global_ctrl = |
| 4456 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4457 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 4458 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4459 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 4460 | |
| 4461 | /* |
| 4462 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 4463 | * 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] | 4464 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 4465 | * |
| 4466 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 4467 | * |
| 4468 | * AAK155 (model 26) |
| 4469 | * AAP115 (model 30) |
| 4470 | * AAT100 (model 37) |
| 4471 | * BC86,AAY89,BD102 (model 44) |
| 4472 | * BA97 (model 46) |
| 4473 | * |
| 4474 | */ |
| 4475 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 4476 | switch (boot_cpu_data.x86_model) { |
| 4477 | case 26: |
| 4478 | case 30: |
| 4479 | case 37: |
| 4480 | case 44: |
| 4481 | case 46: |
| 4482 | cpu_has_load_perf_global_ctrl = false; |
| 4483 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 4484 | "does not work properly. Using workaround\n"); |
| 4485 | break; |
| 4486 | default: |
| 4487 | break; |
| 4488 | } |
| 4489 | } |
| 4490 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 4491 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4492 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 4493 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4494 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4495 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4496 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4497 | static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4498 | { |
| 4499 | int node = cpu_to_node(cpu); |
| 4500 | struct page *pages; |
| 4501 | struct vmcs *vmcs; |
| 4502 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 4503 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4504 | if (!pages) |
| 4505 | return NULL; |
| 4506 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4507 | memset(vmcs, 0, vmcs_config.size); |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4508 | |
| 4509 | /* KVM supports Enlightened VMCS v1 only */ |
| 4510 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4511 | vmcs->hdr.revision_id = KVM_EVMCS_VERSION; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4512 | else |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4513 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4514 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4515 | if (shadow) |
| 4516 | vmcs->hdr.shadow_vmcs = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4517 | return vmcs; |
| 4518 | } |
| 4519 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4520 | static void free_vmcs(struct vmcs *vmcs) |
| 4521 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4522 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4523 | } |
| 4524 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4525 | /* |
| 4526 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 4527 | */ |
| 4528 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4529 | { |
| 4530 | if (!loaded_vmcs->vmcs) |
| 4531 | return; |
| 4532 | loaded_vmcs_clear(loaded_vmcs); |
| 4533 | free_vmcs(loaded_vmcs->vmcs); |
| 4534 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4535 | if (loaded_vmcs->msr_bitmap) |
| 4536 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 4537 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4538 | } |
| 4539 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4540 | static struct vmcs *alloc_vmcs(bool shadow) |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4541 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4542 | return alloc_vmcs_cpu(shadow, raw_smp_processor_id()); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4546 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4547 | loaded_vmcs->vmcs = alloc_vmcs(false); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4548 | if (!loaded_vmcs->vmcs) |
| 4549 | return -ENOMEM; |
| 4550 | |
| 4551 | loaded_vmcs->shadow_vmcs = NULL; |
| 4552 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4553 | |
| 4554 | if (cpu_has_vmx_msr_bitmap()) { |
| 4555 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4556 | if (!loaded_vmcs->msr_bitmap) |
| 4557 | goto out_vmcs; |
| 4558 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4559 | |
Arnd Bergmann | 1f008e1 | 2018-05-25 17:36:17 +0200 | [diff] [blame] | 4560 | if (IS_ENABLED(CONFIG_HYPERV) && |
| 4561 | static_branch_unlikely(&enable_evmcs) && |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4562 | (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { |
| 4563 | struct hv_enlightened_vmcs *evmcs = |
| 4564 | (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; |
| 4565 | |
| 4566 | evmcs->hv_enlightenments_control.msr_bitmap = 1; |
| 4567 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4568 | } |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4569 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4570 | |
| 4571 | out_vmcs: |
| 4572 | free_loaded_vmcs(loaded_vmcs); |
| 4573 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4574 | } |
| 4575 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 4576 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4577 | { |
| 4578 | int cpu; |
| 4579 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4580 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4581 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4582 | per_cpu(vmxarea, cpu) = NULL; |
| 4583 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4584 | } |
| 4585 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4586 | enum vmcs_field_width { |
| 4587 | VMCS_FIELD_WIDTH_U16 = 0, |
| 4588 | VMCS_FIELD_WIDTH_U64 = 1, |
| 4589 | VMCS_FIELD_WIDTH_U32 = 2, |
| 4590 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4591 | }; |
| 4592 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4593 | static inline int vmcs_field_width(unsigned long field) |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4594 | { |
| 4595 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4596 | return VMCS_FIELD_WIDTH_U32; |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4597 | return (field >> 13) & 0x3 ; |
| 4598 | } |
| 4599 | |
| 4600 | static inline int vmcs_field_readonly(unsigned long field) |
| 4601 | { |
| 4602 | return (((field >> 10) & 0x3) == 1); |
| 4603 | } |
| 4604 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4605 | static void init_vmcs_shadow_fields(void) |
| 4606 | { |
| 4607 | int i, j; |
| 4608 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4609 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
| 4610 | u16 field = shadow_read_only_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4611 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4612 | (i + 1 == max_shadow_read_only_fields || |
| 4613 | shadow_read_only_fields[i + 1] != field + 1)) |
| 4614 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 4615 | field + 1); |
| 4616 | |
| 4617 | clear_bit(field, vmx_vmread_bitmap); |
| 4618 | #ifdef CONFIG_X86_64 |
| 4619 | if (field & 1) |
| 4620 | continue; |
| 4621 | #endif |
| 4622 | if (j < i) |
| 4623 | shadow_read_only_fields[j] = field; |
| 4624 | j++; |
| 4625 | } |
| 4626 | max_shadow_read_only_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4627 | |
| 4628 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4629 | u16 field = shadow_read_write_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4630 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4631 | (i + 1 == max_shadow_read_write_fields || |
| 4632 | shadow_read_write_fields[i + 1] != field + 1)) |
| 4633 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 4634 | field + 1); |
| 4635 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4636 | /* |
| 4637 | * PML and the preemption timer can be emulated, but the |
| 4638 | * processor cannot vmwrite to fields that don't exist |
| 4639 | * on bare metal. |
| 4640 | */ |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4641 | switch (field) { |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4642 | case GUEST_PML_INDEX: |
| 4643 | if (!cpu_has_vmx_pml()) |
| 4644 | continue; |
| 4645 | break; |
| 4646 | case VMX_PREEMPTION_TIMER_VALUE: |
| 4647 | if (!cpu_has_vmx_preemption_timer()) |
| 4648 | continue; |
| 4649 | break; |
| 4650 | case GUEST_INTR_STATUS: |
| 4651 | if (!cpu_has_vmx_apicv()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4652 | continue; |
| 4653 | break; |
| 4654 | default: |
| 4655 | break; |
| 4656 | } |
| 4657 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4658 | clear_bit(field, vmx_vmwrite_bitmap); |
| 4659 | clear_bit(field, vmx_vmread_bitmap); |
| 4660 | #ifdef CONFIG_X86_64 |
| 4661 | if (field & 1) |
| 4662 | continue; |
| 4663 | #endif |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4664 | if (j < i) |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4665 | shadow_read_write_fields[j] = field; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4666 | j++; |
| 4667 | } |
| 4668 | max_shadow_read_write_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4669 | } |
| 4670 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4671 | static __init int alloc_kvm_area(void) |
| 4672 | { |
| 4673 | int cpu; |
| 4674 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4675 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4676 | struct vmcs *vmcs; |
| 4677 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4678 | vmcs = alloc_vmcs_cpu(false, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4679 | if (!vmcs) { |
| 4680 | free_kvm_area(); |
| 4681 | return -ENOMEM; |
| 4682 | } |
| 4683 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4684 | /* |
| 4685 | * When eVMCS is enabled, alloc_vmcs_cpu() sets |
| 4686 | * vmcs->revision_id to KVM_EVMCS_VERSION instead of |
| 4687 | * revision_id reported by MSR_IA32_VMX_BASIC. |
| 4688 | * |
| 4689 | * However, even though not explictly documented by |
| 4690 | * TLFS, VMXArea passed as VMXON argument should |
| 4691 | * still be marked with revision_id reported by |
| 4692 | * physical CPU. |
| 4693 | */ |
| 4694 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4695 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4696 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4697 | per_cpu(vmxarea, cpu) = vmcs; |
| 4698 | } |
| 4699 | return 0; |
| 4700 | } |
| 4701 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4702 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4703 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4704 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4705 | if (!emulate_invalid_guest_state) { |
| 4706 | /* |
| 4707 | * CS and SS RPL should be equal during guest entry according |
| 4708 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 4709 | * is in the middle of the transition from real mode to |
| 4710 | * protected mode it is safe to assume that RPL 0 is a good |
| 4711 | * default value. |
| 4712 | */ |
| 4713 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4714 | save->selector &= ~SEGMENT_RPL_MASK; |
| 4715 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4716 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4717 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4718 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 4722 | { |
| 4723 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4724 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4725 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4726 | /* |
| 4727 | * Update real mode segment cache. It may be not up-to-date if sement |
| 4728 | * register was written while vcpu was in a guest mode. |
| 4729 | */ |
| 4730 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4731 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4732 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4733 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 4734 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4735 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 4736 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4737 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4738 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4739 | vmx_segment_cache_clear(vmx); |
| 4740 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4741 | 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] | 4742 | |
| 4743 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4744 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 4745 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4746 | vmcs_writel(GUEST_RFLAGS, flags); |
| 4747 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4748 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 4749 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4750 | |
| 4751 | update_exception_bitmap(vcpu); |
| 4752 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4753 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4754 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4755 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4756 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4757 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 4758 | 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] | 4759 | } |
| 4760 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4761 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4762 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4763 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4764 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4765 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4766 | var.dpl = 0x3; |
| 4767 | if (seg == VCPU_SREG_CS) |
| 4768 | var.type = 0x3; |
| 4769 | |
| 4770 | if (!emulate_invalid_guest_state) { |
| 4771 | var.selector = var.base >> 4; |
| 4772 | var.base = var.base & 0xffff0; |
| 4773 | var.limit = 0xffff; |
| 4774 | var.g = 0; |
| 4775 | var.db = 0; |
| 4776 | var.present = 1; |
| 4777 | var.s = 1; |
| 4778 | var.l = 0; |
| 4779 | var.unusable = 0; |
| 4780 | var.type = 0x3; |
| 4781 | var.avl = 0; |
| 4782 | if (save->base & 0xf) |
| 4783 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 4784 | "paragraph aligned when entering " |
| 4785 | "protected mode (seg=%d)", seg); |
| 4786 | } |
| 4787 | |
| 4788 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 4789 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4790 | vmcs_write32(sf->limit, var.limit); |
| 4791 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 4795 | { |
| 4796 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4797 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4798 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4799 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4800 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 4801 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4802 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4803 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4804 | 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] | 4805 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4806 | 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] | 4807 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4808 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4809 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4810 | /* |
| 4811 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4812 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4813 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4814 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4815 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 4816 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4817 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4818 | vmx_segment_cache_clear(vmx); |
| 4819 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4820 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4821 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4822 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4823 | |
| 4824 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4825 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4826 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 4827 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4828 | |
| 4829 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4830 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4831 | update_exception_bitmap(vcpu); |
| 4832 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4833 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4834 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4835 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4836 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4837 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 4838 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4839 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 4840 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4841 | } |
| 4842 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4843 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 4844 | { |
| 4845 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4846 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 4847 | |
| 4848 | if (!msr) |
| 4849 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4850 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4851 | /* |
| 4852 | * Force kernel_gs_base reloading before EFER changes, as control |
| 4853 | * of this msr depends on is_long_mode(). |
| 4854 | */ |
| 4855 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4856 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4857 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4858 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4859 | msr->data = efer; |
| 4860 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4861 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4862 | |
| 4863 | msr->data = efer & ~EFER_LME; |
| 4864 | } |
| 4865 | setup_msrs(vmx); |
| 4866 | } |
| 4867 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4868 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4869 | |
| 4870 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 4871 | { |
| 4872 | u32 guest_tr_ar; |
| 4873 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4874 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 4875 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4876 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4877 | 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] | 4878 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 4879 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4880 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4881 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 4882 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4883 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4884 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 4888 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4889 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4890 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | #endif |
| 4894 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4895 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
| 4896 | bool invalidate_gpa) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4897 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4898 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4899 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 4900 | return; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4901 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
Jim Mattson | f0b98c0 | 2017-03-15 07:56:11 -0700 | [diff] [blame] | 4902 | } else { |
| 4903 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4904 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4905 | } |
| 4906 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4907 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4908 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4909 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4910 | } |
| 4911 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 4912 | static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr) |
| 4913 | { |
| 4914 | int vpid = to_vmx(vcpu)->vpid; |
| 4915 | |
| 4916 | if (!vpid_sync_vcpu_addr(vpid, addr)) |
| 4917 | vpid_sync_context(vpid); |
| 4918 | |
| 4919 | /* |
| 4920 | * If VPIDs are not supported or enabled, then the above is a no-op. |
| 4921 | * But we don't really need a TLB flush in that case anyway, because |
| 4922 | * each VM entry/exit includes an implicit flush when VPID is 0. |
| 4923 | */ |
| 4924 | } |
| 4925 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4926 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 4927 | { |
| 4928 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 4929 | |
| 4930 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 4931 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 4932 | } |
| 4933 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4934 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 4935 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4936 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4937 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 4938 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 4939 | } |
| 4940 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4941 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4942 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4943 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 4944 | |
| 4945 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 4946 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4947 | } |
| 4948 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4949 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 4950 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4951 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4952 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4953 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 4954 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 4955 | return; |
| 4956 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4957 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4958 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 4959 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 4960 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 4961 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4962 | } |
| 4963 | } |
| 4964 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4965 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 4966 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4967 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4968 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4969 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4970 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 4971 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 4972 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 4973 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4974 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4975 | |
| 4976 | __set_bit(VCPU_EXREG_PDPTR, |
| 4977 | (unsigned long *)&vcpu->arch.regs_avail); |
| 4978 | __set_bit(VCPU_EXREG_PDPTR, |
| 4979 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4980 | } |
| 4981 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4982 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4983 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4984 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4985 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4986 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4987 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4988 | if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4989 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 4990 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 4991 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); |
| 4992 | |
| 4993 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4994 | } |
| 4995 | |
| 4996 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4997 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4998 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4999 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5000 | |
| 5001 | return fixed_bits_valid(val, fixed0, fixed1); |
| 5002 | } |
| 5003 | |
| 5004 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 5005 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5006 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0; |
| 5007 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5008 | |
| 5009 | return fixed_bits_valid(val, fixed0, fixed1); |
| 5010 | } |
| 5011 | |
| 5012 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ |
| 5013 | #define nested_guest_cr4_valid nested_cr4_valid |
| 5014 | #define nested_host_cr4_valid nested_cr4_valid |
| 5015 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5016 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5017 | |
| 5018 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 5019 | unsigned long cr0, |
| 5020 | struct kvm_vcpu *vcpu) |
| 5021 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 5022 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 5023 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5024 | if (!(cr0 & X86_CR0_PG)) { |
| 5025 | /* From paging/starting to nonpaging */ |
| 5026 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 5027 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5028 | (CPU_BASED_CR3_LOAD_EXITING | |
| 5029 | CPU_BASED_CR3_STORE_EXITING)); |
| 5030 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5031 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5032 | } else if (!is_paging(vcpu)) { |
| 5033 | /* From nonpaging to paging */ |
| 5034 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 5035 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5036 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 5037 | CPU_BASED_CR3_STORE_EXITING)); |
| 5038 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5039 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5040 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 5041 | |
| 5042 | if (!(cr0 & X86_CR0_WP)) |
| 5043 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5044 | } |
| 5045 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5046 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 5047 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5048 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5049 | unsigned long hw_cr0; |
| 5050 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 5051 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5052 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 5053 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5054 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 5055 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5056 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5057 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 5058 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5059 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5060 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 5061 | enter_rmode(vcpu); |
| 5062 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5063 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5064 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 5065 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 5066 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5067 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 5068 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5069 | exit_lmode(vcpu); |
| 5070 | } |
| 5071 | #endif |
| 5072 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 5073 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5074 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 5075 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5076 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5077 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5078 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 5079 | |
| 5080 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 5081 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5082 | } |
| 5083 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5084 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 5085 | { |
| 5086 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 5087 | return 5; |
| 5088 | return 4; |
| 5089 | } |
| 5090 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5091 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5092 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5093 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5094 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5095 | 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] | 5096 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5097 | if (enable_ept_ad_bits && |
| 5098 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 5099 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5100 | eptp |= (root_hpa & PAGE_MASK); |
| 5101 | |
| 5102 | return eptp; |
| 5103 | } |
| 5104 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5105 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 5106 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5107 | struct kvm *kvm = vcpu->kvm; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5108 | unsigned long guest_cr3; |
| 5109 | u64 eptp; |
| 5110 | |
| 5111 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 5112 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5113 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5114 | vmcs_write64(EPT_POINTER, eptp); |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5115 | |
| 5116 | if (kvm_x86_ops->tlb_remote_flush) { |
| 5117 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 5118 | to_vmx(vcpu)->ept_pointer = eptp; |
| 5119 | to_kvm_vmx(kvm)->ept_pointers_match |
| 5120 | = EPT_POINTERS_CHECK; |
| 5121 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 5122 | } |
| 5123 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 5124 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 5125 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 5126 | guest_cr3 = kvm_read_cr3(vcpu); |
| 5127 | else |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5128 | guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5129 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5130 | } |
| 5131 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5132 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5133 | } |
| 5134 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5135 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5136 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 5137 | /* |
| 5138 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 5139 | * is in force while we are in guest mode. Do not let guests control |
| 5140 | * this bit, even if host CR4.MCE == 0. |
| 5141 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5142 | unsigned long hw_cr4; |
| 5143 | |
| 5144 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 5145 | if (enable_unrestricted_guest) |
| 5146 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 5147 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 5148 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 5149 | else |
| 5150 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5151 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5152 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { |
| 5153 | if (cr4 & X86_CR4_UMIP) { |
| 5154 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5155 | SECONDARY_EXEC_DESC); |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5156 | hw_cr4 &= ~X86_CR4_UMIP; |
| 5157 | } else if (!is_guest_mode(vcpu) || |
| 5158 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
| 5159 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5160 | SECONDARY_EXEC_DESC); |
| 5161 | } |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5162 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5163 | if (cr4 & X86_CR4_VMXE) { |
| 5164 | /* |
| 5165 | * To use VMXON (and later other VMX instructions), a guest |
| 5166 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 5167 | * So basically the check on whether to allow nested VMX |
| 5168 | * is here. |
| 5169 | */ |
| 5170 | if (!nested_vmx_allowed(vcpu)) |
| 5171 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5172 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5173 | |
| 5174 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5175 | return 1; |
| 5176 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5177 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5178 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5179 | if (!enable_unrestricted_guest) { |
| 5180 | if (enable_ept) { |
| 5181 | if (!is_paging(vcpu)) { |
| 5182 | hw_cr4 &= ~X86_CR4_PAE; |
| 5183 | hw_cr4 |= X86_CR4_PSE; |
| 5184 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 5185 | hw_cr4 &= ~X86_CR4_PAE; |
| 5186 | } |
| 5187 | } |
| 5188 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5189 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 5190 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 5191 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 5192 | * to be manually disabled when guest switches to non-paging |
| 5193 | * mode. |
| 5194 | * |
| 5195 | * If !enable_unrestricted_guest, the CPU is always running |
| 5196 | * with CR0.PG=1 and CR4 needs to be modified. |
| 5197 | * If enable_unrestricted_guest, the CPU automatically |
| 5198 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5199 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5200 | if (!is_paging(vcpu)) |
| 5201 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 5202 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5203 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5204 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 5205 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5206 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5207 | } |
| 5208 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5209 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 5210 | struct kvm_segment *var, int seg) |
| 5211 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5212 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5213 | u32 ar; |
| 5214 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 5215 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5216 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5217 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5218 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5219 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 5220 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5221 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5222 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5223 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5224 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5225 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 5226 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5227 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5228 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5229 | var->type = ar & 15; |
| 5230 | var->s = (ar >> 4) & 1; |
| 5231 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5232 | /* |
| 5233 | * Some userspaces do not preserve unusable property. Since usable |
| 5234 | * segment has to be present according to VMX spec we can use present |
| 5235 | * property to amend userspace bug by making unusable segment always |
| 5236 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 5237 | * segment as unusable. |
| 5238 | */ |
| 5239 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5240 | var->avl = (ar >> 12) & 1; |
| 5241 | var->l = (ar >> 13) & 1; |
| 5242 | var->db = (ar >> 14) & 1; |
| 5243 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5244 | } |
| 5245 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5246 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 5247 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5248 | struct kvm_segment s; |
| 5249 | |
| 5250 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 5251 | vmx_get_segment(vcpu, &s, seg); |
| 5252 | return s.base; |
| 5253 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5254 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5255 | } |
| 5256 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5257 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5258 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5259 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5260 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5261 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5262 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5263 | else { |
| 5264 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5265 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5266 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5267 | } |
| 5268 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5269 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5270 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5271 | u32 ar; |
| 5272 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 5273 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5274 | ar = 1 << 16; |
| 5275 | else { |
| 5276 | ar = var->type & 15; |
| 5277 | ar |= (var->s & 1) << 4; |
| 5278 | ar |= (var->dpl & 3) << 5; |
| 5279 | ar |= (var->present & 1) << 7; |
| 5280 | ar |= (var->avl & 1) << 12; |
| 5281 | ar |= (var->l & 1) << 13; |
| 5282 | ar |= (var->db & 1) << 14; |
| 5283 | ar |= (var->g & 1) << 15; |
| 5284 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5285 | |
| 5286 | return ar; |
| 5287 | } |
| 5288 | |
| 5289 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 5290 | struct kvm_segment *var, int seg) |
| 5291 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5292 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5293 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5294 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5295 | vmx_segment_cache_clear(vmx); |
| 5296 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5297 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 5298 | vmx->rmode.segs[seg] = *var; |
| 5299 | if (seg == VCPU_SREG_TR) |
| 5300 | vmcs_write16(sf->selector, var->selector); |
| 5301 | else if (var->s) |
| 5302 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5303 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5304 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5305 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5306 | vmcs_writel(sf->base, var->base); |
| 5307 | vmcs_write32(sf->limit, var->limit); |
| 5308 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5309 | |
| 5310 | /* |
| 5311 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 5312 | * qemu binaries. |
| 5313 | * IA32 arch specifies that at the time of processor reset the |
| 5314 | * "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] | 5315 | * 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] | 5316 | * state vmexit when "unrestricted guest" mode is turned on. |
| 5317 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 5318 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 5319 | * kvm hack. |
| 5320 | */ |
| 5321 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5322 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5323 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5324 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5325 | |
| 5326 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 5327 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5328 | } |
| 5329 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5330 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 5331 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5332 | 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] | 5333 | |
| 5334 | *db = (ar >> 14) & 1; |
| 5335 | *l = (ar >> 13) & 1; |
| 5336 | } |
| 5337 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5338 | 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] | 5339 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5340 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 5341 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5342 | } |
| 5343 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5344 | 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] | 5345 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5346 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 5347 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5348 | } |
| 5349 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5350 | 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] | 5351 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5352 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 5353 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5354 | } |
| 5355 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5356 | 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] | 5357 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5358 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 5359 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5360 | } |
| 5361 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5362 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5363 | { |
| 5364 | struct kvm_segment var; |
| 5365 | u32 ar; |
| 5366 | |
| 5367 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5368 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 5369 | if (seg == VCPU_SREG_CS) |
| 5370 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5371 | ar = vmx_segment_access_rights(&var); |
| 5372 | |
| 5373 | if (var.base != (var.selector << 4)) |
| 5374 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 5375 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5376 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5377 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5378 | return false; |
| 5379 | |
| 5380 | return true; |
| 5381 | } |
| 5382 | |
| 5383 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 5384 | { |
| 5385 | struct kvm_segment cs; |
| 5386 | unsigned int cs_rpl; |
| 5387 | |
| 5388 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5389 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5390 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5391 | if (cs.unusable) |
| 5392 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5393 | 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] | 5394 | return false; |
| 5395 | if (!cs.s) |
| 5396 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5397 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5398 | if (cs.dpl > cs_rpl) |
| 5399 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5400 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5401 | if (cs.dpl != cs_rpl) |
| 5402 | return false; |
| 5403 | } |
| 5404 | if (!cs.present) |
| 5405 | return false; |
| 5406 | |
| 5407 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 5408 | return true; |
| 5409 | } |
| 5410 | |
| 5411 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 5412 | { |
| 5413 | struct kvm_segment ss; |
| 5414 | unsigned int ss_rpl; |
| 5415 | |
| 5416 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5417 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5418 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5419 | if (ss.unusable) |
| 5420 | return true; |
| 5421 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5422 | return false; |
| 5423 | if (!ss.s) |
| 5424 | return false; |
| 5425 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 5426 | return false; |
| 5427 | if (!ss.present) |
| 5428 | return false; |
| 5429 | |
| 5430 | return true; |
| 5431 | } |
| 5432 | |
| 5433 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5434 | { |
| 5435 | struct kvm_segment var; |
| 5436 | unsigned int rpl; |
| 5437 | |
| 5438 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5439 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5440 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5441 | if (var.unusable) |
| 5442 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5443 | if (!var.s) |
| 5444 | return false; |
| 5445 | if (!var.present) |
| 5446 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5447 | 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] | 5448 | if (var.dpl < rpl) /* DPL < RPL */ |
| 5449 | return false; |
| 5450 | } |
| 5451 | |
| 5452 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 5453 | * rights flags |
| 5454 | */ |
| 5455 | return true; |
| 5456 | } |
| 5457 | |
| 5458 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 5459 | { |
| 5460 | struct kvm_segment tr; |
| 5461 | |
| 5462 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 5463 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5464 | if (tr.unusable) |
| 5465 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5466 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5467 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5468 | 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] | 5469 | return false; |
| 5470 | if (!tr.present) |
| 5471 | return false; |
| 5472 | |
| 5473 | return true; |
| 5474 | } |
| 5475 | |
| 5476 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 5477 | { |
| 5478 | struct kvm_segment ldtr; |
| 5479 | |
| 5480 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 5481 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5482 | if (ldtr.unusable) |
| 5483 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5484 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5485 | return false; |
| 5486 | if (ldtr.type != 2) |
| 5487 | return false; |
| 5488 | if (!ldtr.present) |
| 5489 | return false; |
| 5490 | |
| 5491 | return true; |
| 5492 | } |
| 5493 | |
| 5494 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 5495 | { |
| 5496 | struct kvm_segment cs, ss; |
| 5497 | |
| 5498 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5499 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 5500 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5501 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 5502 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | /* |
| 5506 | * Check if guest state is valid. Returns true if valid, false if |
| 5507 | * not. |
| 5508 | * We assume that registers are always usable |
| 5509 | */ |
| 5510 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 5511 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 5512 | if (enable_unrestricted_guest) |
| 5513 | return true; |
| 5514 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5515 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 5516 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5517 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 5518 | return false; |
| 5519 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 5520 | return false; |
| 5521 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5522 | return false; |
| 5523 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5524 | return false; |
| 5525 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5526 | return false; |
| 5527 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5528 | return false; |
| 5529 | } else { |
| 5530 | /* protected mode guest state checks */ |
| 5531 | if (!cs_ss_rpl_check(vcpu)) |
| 5532 | return false; |
| 5533 | if (!code_segment_valid(vcpu)) |
| 5534 | return false; |
| 5535 | if (!stack_segment_valid(vcpu)) |
| 5536 | return false; |
| 5537 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5538 | return false; |
| 5539 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5540 | return false; |
| 5541 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5542 | return false; |
| 5543 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5544 | return false; |
| 5545 | if (!tr_valid(vcpu)) |
| 5546 | return false; |
| 5547 | if (!ldtr_valid(vcpu)) |
| 5548 | return false; |
| 5549 | } |
| 5550 | /* TODO: |
| 5551 | * - Add checks on RIP |
| 5552 | * - Add checks on RFLAGS |
| 5553 | */ |
| 5554 | |
| 5555 | return true; |
| 5556 | } |
| 5557 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 5558 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 5559 | { |
| 5560 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 5561 | } |
| 5562 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5563 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5564 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5565 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5566 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5567 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5568 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5569 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5570 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5571 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5572 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5573 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5574 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 5575 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 5576 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5577 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5578 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5579 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 5580 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5581 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5582 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5583 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5584 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5585 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5586 | r = kvm_write_guest_page(kvm, fn, &data, |
| 5587 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 5588 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5589 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5590 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5591 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5592 | } |
| 5593 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5594 | static int init_rmode_identity_map(struct kvm *kvm) |
| 5595 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5596 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5597 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 5598 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5599 | u32 tmp; |
| 5600 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5601 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5602 | mutex_lock(&kvm->slots_lock); |
| 5603 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5604 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5605 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5606 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5607 | if (!kvm_vmx->ept_identity_map_addr) |
| 5608 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 5609 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5610 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 5611 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5612 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5613 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5614 | goto out2; |
| 5615 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5616 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5617 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 5618 | if (r < 0) |
| 5619 | goto out; |
| 5620 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 5621 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 5622 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 5623 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 5624 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 5625 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 5626 | if (r < 0) |
| 5627 | goto out; |
| 5628 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5629 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5630 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5631 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5632 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5633 | |
| 5634 | out2: |
| 5635 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5636 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5637 | } |
| 5638 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5639 | static void seg_setup(int seg) |
| 5640 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5641 | 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] | 5642 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5643 | |
| 5644 | vmcs_write16(sf->selector, 0); |
| 5645 | vmcs_writel(sf->base, 0); |
| 5646 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 5647 | ar = 0x93; |
| 5648 | if (seg == VCPU_SREG_CS) |
| 5649 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5650 | |
| 5651 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5652 | } |
| 5653 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5654 | static int alloc_apic_access_page(struct kvm *kvm) |
| 5655 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5656 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5657 | int r = 0; |
| 5658 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5659 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5660 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5661 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5662 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 5663 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5664 | if (r) |
| 5665 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5666 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 5667 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5668 | if (is_error_page(page)) { |
| 5669 | r = -EFAULT; |
| 5670 | goto out; |
| 5671 | } |
| 5672 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5673 | /* |
| 5674 | * Do not pin the page in memory, so that memory hot-unplug |
| 5675 | * is able to migrate it. |
| 5676 | */ |
| 5677 | put_page(page); |
| 5678 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5679 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5680 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5681 | return r; |
| 5682 | } |
| 5683 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5684 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5685 | { |
| 5686 | int vpid; |
| 5687 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 5688 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5689 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5690 | spin_lock(&vmx_vpid_lock); |
| 5691 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5692 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5693 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5694 | else |
| 5695 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5696 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5697 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5698 | } |
| 5699 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5700 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5701 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5702 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5703 | return; |
| 5704 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5705 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5706 | spin_unlock(&vmx_vpid_lock); |
| 5707 | } |
| 5708 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5709 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5710 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5711 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5712 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5713 | |
| 5714 | if (!cpu_has_vmx_msr_bitmap()) |
| 5715 | return; |
| 5716 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5717 | if (static_branch_unlikely(&enable_evmcs)) |
| 5718 | evmcs_touch_msr_bitmap(); |
| 5719 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5720 | /* |
| 5721 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5722 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5723 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5724 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5725 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5726 | if (type & MSR_TYPE_R) |
| 5727 | /* read-low */ |
| 5728 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 5729 | |
| 5730 | if (type & MSR_TYPE_W) |
| 5731 | /* write-low */ |
| 5732 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 5733 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5734 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5735 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5736 | if (type & MSR_TYPE_R) |
| 5737 | /* read-high */ |
| 5738 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 5739 | |
| 5740 | if (type & MSR_TYPE_W) |
| 5741 | /* write-high */ |
| 5742 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 5743 | |
| 5744 | } |
| 5745 | } |
| 5746 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5747 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5748 | u32 msr, int type) |
| 5749 | { |
| 5750 | int f = sizeof(unsigned long); |
| 5751 | |
| 5752 | if (!cpu_has_vmx_msr_bitmap()) |
| 5753 | return; |
| 5754 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5755 | if (static_branch_unlikely(&enable_evmcs)) |
| 5756 | evmcs_touch_msr_bitmap(); |
| 5757 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5758 | /* |
| 5759 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5760 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5761 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5762 | */ |
| 5763 | if (msr <= 0x1fff) { |
| 5764 | if (type & MSR_TYPE_R) |
| 5765 | /* read-low */ |
| 5766 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 5767 | |
| 5768 | if (type & MSR_TYPE_W) |
| 5769 | /* write-low */ |
| 5770 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 5771 | |
| 5772 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5773 | msr &= 0x1fff; |
| 5774 | if (type & MSR_TYPE_R) |
| 5775 | /* read-high */ |
| 5776 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 5777 | |
| 5778 | if (type & MSR_TYPE_W) |
| 5779 | /* write-high */ |
| 5780 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 5781 | |
| 5782 | } |
| 5783 | } |
| 5784 | |
| 5785 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
| 5786 | u32 msr, int type, bool value) |
| 5787 | { |
| 5788 | if (value) |
| 5789 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 5790 | else |
| 5791 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 5792 | } |
| 5793 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5794 | /* |
| 5795 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 5796 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 5797 | */ |
| 5798 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 5799 | unsigned long *msr_bitmap_nested, |
| 5800 | u32 msr, int type) |
| 5801 | { |
| 5802 | int f = sizeof(unsigned long); |
| 5803 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5804 | /* |
| 5805 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5806 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5807 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5808 | */ |
| 5809 | if (msr <= 0x1fff) { |
| 5810 | if (type & MSR_TYPE_R && |
| 5811 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 5812 | /* read-low */ |
| 5813 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 5814 | |
| 5815 | if (type & MSR_TYPE_W && |
| 5816 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 5817 | /* write-low */ |
| 5818 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 5819 | |
| 5820 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5821 | msr &= 0x1fff; |
| 5822 | if (type & MSR_TYPE_R && |
| 5823 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 5824 | /* read-high */ |
| 5825 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 5826 | |
| 5827 | if (type & MSR_TYPE_W && |
| 5828 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 5829 | /* write-high */ |
| 5830 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 5831 | |
| 5832 | } |
| 5833 | } |
| 5834 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5835 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5836 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5837 | u8 mode = 0; |
| 5838 | |
| 5839 | if (cpu_has_secondary_exec_ctrls() && |
| 5840 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 5841 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 5842 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 5843 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 5844 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 5845 | } |
| 5846 | |
| 5847 | if (is_long_mode(vcpu)) |
| 5848 | mode |= MSR_BITMAP_MODE_LM; |
| 5849 | |
| 5850 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5851 | } |
| 5852 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5853 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 5854 | |
| 5855 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 5856 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5857 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5858 | int msr; |
| 5859 | |
| 5860 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 5861 | unsigned word = msr / BITS_PER_LONG; |
| 5862 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 5863 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 5864 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5865 | |
| 5866 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 5867 | /* |
| 5868 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 5869 | * delivery is not in use. |
| 5870 | */ |
| 5871 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 5872 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 5873 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 5874 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 5875 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 5876 | } |
| 5877 | } |
| 5878 | } |
| 5879 | |
| 5880 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
| 5881 | { |
| 5882 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5883 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 5884 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 5885 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 5886 | |
| 5887 | if (!changed) |
| 5888 | return; |
| 5889 | |
| 5890 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, |
| 5891 | !(mode & MSR_BITMAP_MODE_LM)); |
| 5892 | |
| 5893 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 5894 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 5895 | |
| 5896 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5897 | } |
| 5898 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 5899 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5900 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5901 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5902 | } |
| 5903 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5904 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 5905 | { |
| 5906 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5907 | gfn_t gfn; |
| 5908 | |
| 5909 | /* |
| 5910 | * Don't need to mark the APIC access page dirty; it is never |
| 5911 | * written to by the CPU during APIC virtualization. |
| 5912 | */ |
| 5913 | |
| 5914 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 5915 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 5916 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5917 | } |
| 5918 | |
| 5919 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 5920 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 5921 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5922 | } |
| 5923 | } |
| 5924 | |
| 5925 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 5926 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5927 | { |
| 5928 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5929 | int max_irr; |
| 5930 | void *vapic_page; |
| 5931 | u16 status; |
| 5932 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5933 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 5934 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5935 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5936 | vmx->nested.pi_pending = false; |
| 5937 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 5938 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5939 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5940 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 5941 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5942 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 5943 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 5944 | vapic_page, &max_irr); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5945 | kunmap(vmx->nested.virtual_apic_page); |
| 5946 | |
| 5947 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5948 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 5949 | status &= ~0xff; |
| 5950 | status |= (u8)max_irr; |
| 5951 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5952 | } |
| 5953 | } |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5954 | |
| 5955 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5956 | } |
| 5957 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5958 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5959 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5960 | { |
| 5961 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5962 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 5963 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5964 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5965 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5966 | * The vector of interrupt to be delivered to vcpu had |
| 5967 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5968 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5969 | * Following cases will be reached in this block, and |
| 5970 | * we always send a notification event in all cases as |
| 5971 | * explained below. |
| 5972 | * |
| 5973 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 5974 | * notification event posts the interrupt to vcpu. |
| 5975 | * |
| 5976 | * Case 2: vcpu exits to root mode and is still |
| 5977 | * runnable. PIR will be synced to vIRR before the |
| 5978 | * next vcpu entry. Sending a notification event in |
| 5979 | * this case has no effect, as vcpu is not in root |
| 5980 | * mode. |
| 5981 | * |
| 5982 | * Case 3: vcpu exits to root mode and is blocked. |
| 5983 | * vcpu_block() has already synced PIR to vIRR and |
| 5984 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 5985 | * a blocked vcpu here does not wait for any requested |
| 5986 | * interrupts in PIR, and sending a notification event |
| 5987 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5988 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5989 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5990 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5991 | return true; |
| 5992 | } |
| 5993 | #endif |
| 5994 | return false; |
| 5995 | } |
| 5996 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5997 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5998 | int vector) |
| 5999 | { |
| 6000 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6001 | |
| 6002 | if (is_guest_mode(vcpu) && |
| 6003 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6004 | /* |
| 6005 | * If a posted intr is not recognized by hardware, |
| 6006 | * we will accomplish it in the next vmentry. |
| 6007 | */ |
| 6008 | vmx->nested.pi_pending = true; |
| 6009 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 6010 | /* the PIR and ON have been set by L1. */ |
| 6011 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 6012 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6013 | return 0; |
| 6014 | } |
| 6015 | return -1; |
| 6016 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6017 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6018 | * Send interrupt to vcpu via posted interrupt way. |
| 6019 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 6020 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 6021 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 6022 | * interrupt from PIR in next vmentry. |
| 6023 | */ |
| 6024 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 6025 | { |
| 6026 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6027 | int r; |
| 6028 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6029 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 6030 | if (!r) |
| 6031 | return; |
| 6032 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6033 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 6034 | return; |
| 6035 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 6036 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 6037 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 6038 | return; |
| 6039 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 6040 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6041 | kvm_vcpu_kick(vcpu); |
| 6042 | } |
| 6043 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6044 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6045 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 6046 | * will not change in the lifetime of the guest. |
| 6047 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 6048 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 6049 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6050 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6051 | { |
| 6052 | u32 low32, high32; |
| 6053 | unsigned long tmpl; |
| 6054 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6055 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6056 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 6057 | cr0 = read_cr0(); |
| 6058 | WARN_ON(cr0 & X86_CR0_TS); |
| 6059 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6060 | |
| 6061 | /* |
| 6062 | * Save the most likely value for this task's CR3 in the VMCS. |
| 6063 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 6064 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 6065 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6066 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 6067 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6068 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6069 | /* 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] | 6070 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6071 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 6072 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6073 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6074 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 6075 | #ifdef CONFIG_X86_64 |
| 6076 | /* |
| 6077 | * Load null selectors, so we can avoid reloading them in |
| 6078 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 6079 | * too (the expected case). |
| 6080 | */ |
| 6081 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 6082 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 6083 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6084 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 6085 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 6086 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6087 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 6088 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 6089 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 6090 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6091 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6092 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6093 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6094 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6095 | |
| 6096 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 6097 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 6098 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 6099 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 6100 | |
| 6101 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 6102 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 6103 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 6104 | } |
| 6105 | } |
| 6106 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6107 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 6108 | { |
| 6109 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 6110 | if (enable_ept) |
| 6111 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6112 | if (is_guest_mode(&vmx->vcpu)) |
| 6113 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 6114 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6115 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 6116 | } |
| 6117 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6118 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 6119 | { |
| 6120 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 6121 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6122 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6123 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6124 | |
| 6125 | if (!enable_vnmi) |
| 6126 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 6127 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6128 | /* Enable the preemption timer dynamically */ |
| 6129 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6130 | return pin_based_exec_ctrl; |
| 6131 | } |
| 6132 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6133 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 6134 | { |
| 6135 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6136 | |
| 6137 | 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] | 6138 | if (cpu_has_secondary_exec_ctrls()) { |
| 6139 | if (kvm_vcpu_apicv_active(vcpu)) |
| 6140 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6141 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6142 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6143 | else |
| 6144 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6145 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6146 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6147 | } |
| 6148 | |
| 6149 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6150 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6151 | } |
| 6152 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6153 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 6154 | { |
| 6155 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 6156 | |
| 6157 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 6158 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 6159 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6160 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6161 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6162 | #ifdef CONFIG_X86_64 |
| 6163 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 6164 | CPU_BASED_CR8_LOAD_EXITING; |
| 6165 | #endif |
| 6166 | } |
| 6167 | if (!enable_ept) |
| 6168 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 6169 | CPU_BASED_CR3_LOAD_EXITING | |
| 6170 | CPU_BASED_INVLPG_EXITING; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 6171 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 6172 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 6173 | CPU_BASED_MONITOR_EXITING); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6174 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 6175 | exec_control &= ~CPU_BASED_HLT_EXITING; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6176 | return exec_control; |
| 6177 | } |
| 6178 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6179 | static bool vmx_rdrand_supported(void) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6180 | { |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6181 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6182 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6183 | } |
| 6184 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6185 | static bool vmx_rdseed_supported(void) |
| 6186 | { |
| 6187 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6188 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6189 | } |
| 6190 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6191 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6192 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6193 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 6194 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6195 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6196 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6197 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6198 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6199 | if (vmx->vpid == 0) |
| 6200 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 6201 | if (!enable_ept) { |
| 6202 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 6203 | enable_unrestricted_guest = 0; |
| 6204 | } |
| 6205 | if (!enable_unrestricted_guest) |
| 6206 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6207 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6208 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6209 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6210 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6211 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6212 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6213 | |
| 6214 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 6215 | * in vmx_set_cr4. */ |
| 6216 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 6217 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 6218 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 6219 | (handle_vmptrld). |
| 6220 | We can NOT enable shadow_vmcs here because we don't have yet |
| 6221 | a current VMCS12 |
| 6222 | */ |
| 6223 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 6224 | |
| 6225 | if (!enable_pml) |
| 6226 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6227 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6228 | if (vmx_xsaves_supported()) { |
| 6229 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 6230 | bool xsaves_enabled = |
| 6231 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 6232 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 6233 | |
| 6234 | if (!xsaves_enabled) |
| 6235 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 6236 | |
| 6237 | if (nested) { |
| 6238 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6239 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6240 | SECONDARY_EXEC_XSAVES; |
| 6241 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6242 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6243 | ~SECONDARY_EXEC_XSAVES; |
| 6244 | } |
| 6245 | } |
| 6246 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6247 | if (vmx_rdtscp_supported()) { |
| 6248 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 6249 | if (!rdtscp_enabled) |
| 6250 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6251 | |
| 6252 | if (nested) { |
| 6253 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6254 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6255 | SECONDARY_EXEC_RDTSCP; |
| 6256 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6257 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6258 | ~SECONDARY_EXEC_RDTSCP; |
| 6259 | } |
| 6260 | } |
| 6261 | |
| 6262 | if (vmx_invpcid_supported()) { |
| 6263 | /* Exposing INVPCID only when PCID is exposed */ |
| 6264 | bool invpcid_enabled = |
| 6265 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 6266 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 6267 | |
| 6268 | if (!invpcid_enabled) { |
| 6269 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6270 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 6271 | } |
| 6272 | |
| 6273 | if (nested) { |
| 6274 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6275 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6276 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 6277 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6278 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6279 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6280 | } |
| 6281 | } |
| 6282 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6283 | if (vmx_rdrand_supported()) { |
| 6284 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 6285 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6286 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6287 | |
| 6288 | if (nested) { |
| 6289 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6290 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6291 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6292 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6293 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6294 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6295 | } |
| 6296 | } |
| 6297 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6298 | if (vmx_rdseed_supported()) { |
| 6299 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 6300 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6301 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6302 | |
| 6303 | if (nested) { |
| 6304 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6305 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6306 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6307 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6308 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6309 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6310 | } |
| 6311 | } |
| 6312 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6313 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6314 | } |
| 6315 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6316 | static void ept_set_mmio_spte_mask(void) |
| 6317 | { |
| 6318 | /* |
| 6319 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 6320 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6321 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 6322 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 6323 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6324 | } |
| 6325 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6326 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6327 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6328 | * Sets up the vmcs for emulated real mode. |
| 6329 | */ |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 6330 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6331 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 6332 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6333 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 6334 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6335 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6336 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6337 | if (enable_shadow_vmcs) { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6338 | /* |
| 6339 | * At vCPU creation, "VMWRITE to any supported field |
| 6340 | * in the VMCS" is supported, so use the more |
| 6341 | * permissive vmx_vmread_bitmap to specify both read |
| 6342 | * and write permissions for the shadow VMCS. |
| 6343 | */ |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6344 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6345 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap)); |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6346 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6347 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6348 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6349 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6350 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 6351 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6352 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6353 | 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] | 6354 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6355 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6356 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6357 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6358 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6359 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6360 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6361 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6362 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6363 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6364 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6365 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 6366 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 6367 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 6368 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 6369 | |
| 6370 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6371 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 6372 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6373 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6374 | } |
| 6375 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6376 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6377 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 6378 | vmx->ple_window = ple_window; |
| 6379 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6380 | } |
| 6381 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 6382 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 6383 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6384 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 6385 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 6386 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 6387 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6388 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6389 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6390 | rdmsrl(MSR_FS_BASE, a); |
| 6391 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 6392 | rdmsrl(MSR_GS_BASE, a); |
| 6393 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 6394 | #else |
| 6395 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 6396 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 6397 | #endif |
| 6398 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 6399 | if (cpu_has_vmx_vmfunc()) |
| 6400 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 6401 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6402 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 6403 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6404 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6405 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6406 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6407 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 6408 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6409 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 6410 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6411 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6412 | u32 index = vmx_msr_index[i]; |
| 6413 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6414 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6415 | |
| 6416 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 6417 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 6418 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 6419 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 6420 | vmx->guest_msrs[j].index = i; |
| 6421 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 6422 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6423 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6424 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6425 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 6426 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 6427 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6428 | |
| 6429 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6430 | |
| 6431 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6432 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 6433 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6434 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 6435 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 6436 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6437 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6438 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6439 | if (vmx_xsaves_supported()) |
| 6440 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 6441 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6442 | if (enable_pml) { |
| 6443 | ASSERT(vmx->pml_pg); |
| 6444 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 6445 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 6446 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6447 | } |
| 6448 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6449 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6450 | { |
| 6451 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 6452 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6453 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6454 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6455 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6456 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6457 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 6458 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6459 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6460 | kvm_set_cr8(vcpu, 0); |
| 6461 | |
| 6462 | if (!init_event) { |
| 6463 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 6464 | MSR_IA32_APICBASE_ENABLE; |
| 6465 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 6466 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 6467 | apic_base_msr.host_initiated = true; |
| 6468 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 6469 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6470 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6471 | vmx_segment_cache_clear(vmx); |
| 6472 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 6473 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6474 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6475 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6476 | |
| 6477 | seg_setup(VCPU_SREG_DS); |
| 6478 | seg_setup(VCPU_SREG_ES); |
| 6479 | seg_setup(VCPU_SREG_FS); |
| 6480 | seg_setup(VCPU_SREG_GS); |
| 6481 | seg_setup(VCPU_SREG_SS); |
| 6482 | |
| 6483 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 6484 | vmcs_writel(GUEST_TR_BASE, 0); |
| 6485 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 6486 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 6487 | |
| 6488 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 6489 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 6490 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 6491 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 6492 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6493 | if (!init_event) { |
| 6494 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 6495 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 6496 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 6497 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 6498 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6499 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 6500 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6501 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6502 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6503 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 6504 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 6505 | |
| 6506 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 6507 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 6508 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 6509 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6510 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6511 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 6512 | if (kvm_mpx_supported()) |
| 6513 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6514 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6515 | setup_msrs(vmx); |
| 6516 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6517 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 6518 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6519 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6520 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6521 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6522 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6523 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6524 | vmcs_write32(TPR_THRESHOLD, 0); |
| 6525 | } |
| 6526 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 6527 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6528 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6529 | if (vmx->vpid != 0) |
| 6530 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6531 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6532 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6533 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 6534 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6535 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 6536 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6537 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6538 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6539 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 6540 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6541 | if (init_event) |
| 6542 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6543 | } |
| 6544 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 6545 | /* |
| 6546 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 6547 | * For most existing hypervisors, this will always return true. |
| 6548 | */ |
| 6549 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 6550 | { |
| 6551 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 6552 | PIN_BASED_EXT_INTR_MASK; |
| 6553 | } |
| 6554 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 6555 | /* |
| 6556 | * In nested virtualization, check if L1 has set |
| 6557 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 6558 | */ |
| 6559 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 6560 | { |
| 6561 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 6562 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6563 | } |
| 6564 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6565 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 6566 | { |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 6567 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6568 | } |
| 6569 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6570 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6571 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6572 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6573 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6574 | } |
| 6575 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6576 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6577 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6578 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6579 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6580 | enable_irq_window(vcpu); |
| 6581 | return; |
| 6582 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 6583 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6584 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6585 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6586 | } |
| 6587 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6588 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6589 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6590 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6591 | uint32_t intr; |
| 6592 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6593 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6594 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6595 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 6596 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6597 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6598 | int inc_eip = 0; |
| 6599 | if (vcpu->arch.interrupt.soft) |
| 6600 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 6601 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6602 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6603 | return; |
| 6604 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6605 | intr = irq | INTR_INFO_VALID_MASK; |
| 6606 | if (vcpu->arch.interrupt.soft) { |
| 6607 | intr |= INTR_TYPE_SOFT_INTR; |
| 6608 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6609 | vmx->vcpu.arch.event_exit_inst_len); |
| 6610 | } else |
| 6611 | intr |= INTR_TYPE_EXT_INTR; |
| 6612 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6613 | |
| 6614 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6615 | } |
| 6616 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6617 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 6618 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6619 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6620 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6621 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6622 | /* |
| 6623 | * Tracking the NMI-blocked state in software is built upon |
| 6624 | * finding the next open IRQ window. This, in turn, depends on |
| 6625 | * well-behaving guests: They have to keep IRQs disabled at |
| 6626 | * least as long as the NMI handler runs. Otherwise we may |
| 6627 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 6628 | * highly unlikely, we can live with the residual risk. |
| 6629 | */ |
| 6630 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 6631 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6632 | } |
| 6633 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6634 | ++vcpu->stat.nmi_injections; |
| 6635 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6636 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6637 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6638 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6639 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6640 | return; |
| 6641 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 6642 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6643 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6644 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6645 | |
| 6646 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6647 | } |
| 6648 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6649 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 6650 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6651 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6652 | bool masked; |
| 6653 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6654 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6655 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6656 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6657 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6658 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 6659 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6660 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 6664 | { |
| 6665 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6666 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6667 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6668 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 6669 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 6670 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6671 | } |
| 6672 | } else { |
| 6673 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6674 | if (masked) |
| 6675 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6676 | GUEST_INTR_STATE_NMI); |
| 6677 | else |
| 6678 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6679 | GUEST_INTR_STATE_NMI); |
| 6680 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6681 | } |
| 6682 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6683 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 6684 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6685 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 6686 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6687 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6688 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6689 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 6690 | return 0; |
| 6691 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6692 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6693 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 6694 | | GUEST_INTR_STATE_NMI)); |
| 6695 | } |
| 6696 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6697 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 6698 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6699 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 6700 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 6701 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6702 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6703 | } |
| 6704 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6705 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 6706 | { |
| 6707 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6708 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 6709 | if (enable_unrestricted_guest) |
| 6710 | return 0; |
| 6711 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 6712 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 6713 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6714 | if (ret) |
| 6715 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6716 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 6717 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6718 | } |
| 6719 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6720 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 6721 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6722 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6723 | return 0; |
| 6724 | } |
| 6725 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6726 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6727 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6728 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6729 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6730 | /* |
| 6731 | * Update instruction length as we may reinject the exception |
| 6732 | * from user space while in guest debugging mode. |
| 6733 | */ |
| 6734 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 6735 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6736 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6737 | return false; |
| 6738 | /* fall through */ |
| 6739 | case DB_VECTOR: |
| 6740 | if (vcpu->guest_debug & |
| 6741 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 6742 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6743 | /* fall through */ |
| 6744 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6745 | case OF_VECTOR: |
| 6746 | case BR_VECTOR: |
| 6747 | case UD_VECTOR: |
| 6748 | case DF_VECTOR: |
| 6749 | case SS_VECTOR: |
| 6750 | case GP_VECTOR: |
| 6751 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6752 | return true; |
| 6753 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6754 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6755 | return false; |
| 6756 | } |
| 6757 | |
| 6758 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 6759 | int vec, u32 err_code) |
| 6760 | { |
| 6761 | /* |
| 6762 | * Instruction with address size override prefix opcode 0x67 |
| 6763 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 6764 | */ |
| 6765 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 6766 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 6767 | if (vcpu->arch.halt_request) { |
| 6768 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6769 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6770 | } |
| 6771 | return 1; |
| 6772 | } |
| 6773 | return 0; |
| 6774 | } |
| 6775 | |
| 6776 | /* |
| 6777 | * Forward all other exceptions that are valid in real mode. |
| 6778 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 6779 | * the required debugging infrastructure rework. |
| 6780 | */ |
| 6781 | kvm_queue_exception(vcpu, vec); |
| 6782 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6783 | } |
| 6784 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6785 | /* |
| 6786 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 6787 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 6788 | * We pass a fake environment to the machine check handler because we want |
| 6789 | * the guest to be always treated like user space, no matter what context |
| 6790 | * it used internally. |
| 6791 | */ |
| 6792 | static void kvm_machine_check(void) |
| 6793 | { |
| 6794 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 6795 | struct pt_regs regs = { |
| 6796 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 6797 | .flags = X86_EFLAGS_IF, |
| 6798 | }; |
| 6799 | |
| 6800 | do_machine_check(®s, 0); |
| 6801 | #endif |
| 6802 | } |
| 6803 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6804 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6805 | { |
| 6806 | /* already handled by vcpu_run */ |
| 6807 | return 1; |
| 6808 | } |
| 6809 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6810 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6811 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6812 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6813 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6814 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6815 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6816 | u32 vect_info; |
| 6817 | enum emulation_result er; |
| 6818 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6819 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 6820 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6821 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6822 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6823 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6824 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 6825 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6826 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 6827 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 6828 | if (is_invalid_opcode(intr_info)) |
| 6829 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 6830 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6831 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 6832 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6833 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6834 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 6835 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 6836 | WARN_ON_ONCE(!enable_vmware_backdoor); |
| 6837 | er = emulate_instruction(vcpu, |
| 6838 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 6839 | if (er == EMULATE_USER_EXIT) |
| 6840 | return 0; |
| 6841 | else if (er != EMULATE_DONE) |
| 6842 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 6843 | return 1; |
| 6844 | } |
| 6845 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6846 | /* |
| 6847 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 6848 | * MMIO, it is better to report an internal error. |
| 6849 | * See the comments in vmx_handle_exit. |
| 6850 | */ |
| 6851 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 6852 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 6853 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6854 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6855 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6856 | vcpu->run->internal.data[0] = vect_info; |
| 6857 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6858 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6859 | return 0; |
| 6860 | } |
| 6861 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6862 | if (is_page_fault(intr_info)) { |
| 6863 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 6864 | /* EPT won't cause page fault directly */ |
| 6865 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 6866 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6867 | } |
| 6868 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6869 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6870 | |
| 6871 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 6872 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 6873 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6874 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 6875 | case AC_VECTOR: |
| 6876 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 6877 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6878 | case DB_VECTOR: |
| 6879 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 6880 | if (!(vcpu->guest_debug & |
| 6881 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 6882 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6883 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 6884 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 6885 | skip_emulated_instruction(vcpu); |
| 6886 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6887 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6888 | return 1; |
| 6889 | } |
| 6890 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 6891 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6892 | /* fall through */ |
| 6893 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6894 | /* |
| 6895 | * Update instruction length as we may reinject #BP from |
| 6896 | * user space while in guest debugging mode. Reading it for |
| 6897 | * #DB as well causes no harm, it is not used in that case. |
| 6898 | */ |
| 6899 | vmx->vcpu.arch.event_exit_inst_len = |
| 6900 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6901 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 6902 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6903 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 6904 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6905 | break; |
| 6906 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6907 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 6908 | kvm_run->ex.exception = ex_no; |
| 6909 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6910 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6911 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6912 | return 0; |
| 6913 | } |
| 6914 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6915 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6916 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 6917 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6918 | return 1; |
| 6919 | } |
| 6920 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6921 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6922 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6923 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 6924 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6925 | return 0; |
| 6926 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6927 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6928 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6929 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6930 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6931 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6932 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6933 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6934 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6935 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 6936 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6937 | ++vcpu->stat.io_exits; |
| 6938 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6939 | if (string) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6940 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6941 | |
| 6942 | port = exit_qualification >> 16; |
| 6943 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6944 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6945 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6946 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6947 | } |
| 6948 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6949 | static void |
| 6950 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 6951 | { |
| 6952 | /* |
| 6953 | * Patch in the VMCALL instruction: |
| 6954 | */ |
| 6955 | hypercall[0] = 0x0f; |
| 6956 | hypercall[1] = 0x01; |
| 6957 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6958 | } |
| 6959 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 6960 | /* 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] | 6961 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 6962 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6963 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6964 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6965 | unsigned long orig_val = val; |
| 6966 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6967 | /* |
| 6968 | * We get here when L2 changed cr0 in a way that did not change |
| 6969 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6970 | * but did change L0 shadowed bits. So we first calculate the |
| 6971 | * effective cr0 value that L1 would like to write into the |
| 6972 | * hardware. It consists of the L2-owned bits from the new |
| 6973 | * 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] | 6974 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6975 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 6976 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 6977 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6978 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6979 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6980 | |
| 6981 | if (kvm_set_cr0(vcpu, val)) |
| 6982 | return 1; |
| 6983 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6984 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6985 | } else { |
| 6986 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6987 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6988 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6989 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6990 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6991 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6992 | } |
| 6993 | |
| 6994 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 6995 | { |
| 6996 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6997 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6998 | unsigned long orig_val = val; |
| 6999 | |
| 7000 | /* analogously to handle_set_cr0 */ |
| 7001 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 7002 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 7003 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7004 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7005 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7006 | return 0; |
| 7007 | } else |
| 7008 | return kvm_set_cr4(vcpu, val); |
| 7009 | } |
| 7010 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 7011 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 7012 | { |
| 7013 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
| 7014 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
| 7015 | } |
| 7016 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7017 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7018 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7019 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7020 | int cr; |
| 7021 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 7022 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7023 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7024 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7025 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7026 | cr = exit_qualification & 15; |
| 7027 | reg = (exit_qualification >> 8) & 15; |
| 7028 | switch ((exit_qualification >> 4) & 3) { |
| 7029 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7030 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7031 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7032 | switch (cr) { |
| 7033 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7034 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7035 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7036 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 7037 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 7038 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7039 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7040 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7041 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7042 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7043 | case 8: { |
| 7044 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7045 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 7046 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7047 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 7048 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7049 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7050 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7051 | return ret; |
| 7052 | /* |
| 7053 | * TODO: we might be squashing a |
| 7054 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 7055 | * KVM_EXIT_DEBUG here. |
| 7056 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7057 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7058 | return 0; |
| 7059 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 7060 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7061 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7062 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 7063 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 7064 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 7065 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7066 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7067 | case 1: /*mov from cr*/ |
| 7068 | switch (cr) { |
| 7069 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 7070 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 7071 | val = kvm_read_cr3(vcpu); |
| 7072 | kvm_register_write(vcpu, reg, val); |
| 7073 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7074 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7075 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7076 | val = kvm_get_cr8(vcpu); |
| 7077 | kvm_register_write(vcpu, reg, val); |
| 7078 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7079 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7080 | } |
| 7081 | break; |
| 7082 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 7083 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 7084 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 7085 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7086 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7087 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7088 | default: |
| 7089 | break; |
| 7090 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7091 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 7092 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7093 | (int)(exit_qualification >> 4) & 3, cr); |
| 7094 | return 0; |
| 7095 | } |
| 7096 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7097 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7098 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7099 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7100 | int dr, dr7, reg; |
| 7101 | |
| 7102 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7103 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 7104 | |
| 7105 | /* First, if DR does not exist, trigger UD */ |
| 7106 | if (!kvm_require_dr(vcpu, dr)) |
| 7107 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7108 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 7109 | /* 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] | 7110 | if (!kvm_require_cpl(vcpu, 0)) |
| 7111 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7112 | dr7 = vmcs_readl(GUEST_DR7); |
| 7113 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7114 | /* |
| 7115 | * As the vm-exit takes precedence over the debug trap, we |
| 7116 | * need to emulate the latter, either for the host or the |
| 7117 | * guest debugging itself. |
| 7118 | */ |
| 7119 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7120 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7121 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 7122 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7123 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 7124 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7125 | return 0; |
| 7126 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 7127 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 7128 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7129 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 7130 | return 1; |
| 7131 | } |
| 7132 | } |
| 7133 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7134 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7135 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7136 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7137 | |
| 7138 | /* |
| 7139 | * No more DR vmexits; force a reload of the debug registers |
| 7140 | * and reenter on this instruction. The next vmexit will |
| 7141 | * retrieve the full state of the debug registers. |
| 7142 | */ |
| 7143 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 7144 | return 1; |
| 7145 | } |
| 7146 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7147 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 7148 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7149 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7150 | |
| 7151 | if (kvm_get_dr(vcpu, dr, &val)) |
| 7152 | return 1; |
| 7153 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7154 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 7155 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7156 | return 1; |
| 7157 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7158 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7159 | } |
| 7160 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7161 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 7162 | { |
| 7163 | return vcpu->arch.dr6; |
| 7164 | } |
| 7165 | |
| 7166 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 7167 | { |
| 7168 | } |
| 7169 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7170 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 7171 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7172 | get_debugreg(vcpu->arch.db[0], 0); |
| 7173 | get_debugreg(vcpu->arch.db[1], 1); |
| 7174 | get_debugreg(vcpu->arch.db[2], 2); |
| 7175 | get_debugreg(vcpu->arch.db[3], 3); |
| 7176 | get_debugreg(vcpu->arch.dr6, 6); |
| 7177 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 7178 | |
| 7179 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7180 | 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] | 7181 | } |
| 7182 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7183 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 7184 | { |
| 7185 | vmcs_writel(GUEST_DR7, val); |
| 7186 | } |
| 7187 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7188 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7189 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 7190 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7191 | } |
| 7192 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7193 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7194 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7195 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7196 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7197 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7198 | msr_info.index = ecx; |
| 7199 | msr_info.host_initiated = false; |
| 7200 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7201 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7202 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7203 | return 1; |
| 7204 | } |
| 7205 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7206 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7207 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7208 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7209 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 7210 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7211 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7212 | } |
| 7213 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7214 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7215 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7216 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7217 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7218 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 7219 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7220 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7221 | msr.data = data; |
| 7222 | msr.index = ecx; |
| 7223 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 7224 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7225 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7226 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7227 | return 1; |
| 7228 | } |
| 7229 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7230 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7231 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7232 | } |
| 7233 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7234 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7235 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 7236 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7237 | return 1; |
| 7238 | } |
| 7239 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7240 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7241 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7242 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7243 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7244 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7245 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7246 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 7247 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7248 | return 1; |
| 7249 | } |
| 7250 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7251 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7252 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 7253 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7254 | } |
| 7255 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7256 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7257 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 7258 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7259 | } |
| 7260 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7261 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 7262 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7263 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7264 | } |
| 7265 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7266 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7267 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7268 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7269 | |
| 7270 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7271 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7272 | } |
| 7273 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7274 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 7275 | { |
| 7276 | int err; |
| 7277 | |
| 7278 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7279 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7280 | } |
| 7281 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7282 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7283 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7284 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7285 | } |
| 7286 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7287 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 7288 | { |
| 7289 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 7290 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 7291 | |
| 7292 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7293 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7294 | return 1; |
| 7295 | } |
| 7296 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7297 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 7298 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7299 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7300 | WARN(1, "this should never happen\n"); |
| 7301 | return 1; |
| 7302 | } |
| 7303 | |
| 7304 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 7305 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7306 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7307 | WARN(1, "this should never happen\n"); |
| 7308 | return 1; |
| 7309 | } |
| 7310 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7311 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7312 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7313 | if (likely(fasteoi)) { |
| 7314 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7315 | int access_type, offset; |
| 7316 | |
| 7317 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 7318 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 7319 | /* |
| 7320 | * Sane guest uses MOV to write EOI, with written value |
| 7321 | * not cared. So make a short-circuit here by avoiding |
| 7322 | * heavy instruction emulation. |
| 7323 | */ |
| 7324 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 7325 | (offset == APIC_EOI)) { |
| 7326 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7327 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7328 | } |
| 7329 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7330 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7331 | } |
| 7332 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7333 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 7334 | { |
| 7335 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7336 | int vector = exit_qualification & 0xff; |
| 7337 | |
| 7338 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 7339 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 7340 | return 1; |
| 7341 | } |
| 7342 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7343 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 7344 | { |
| 7345 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7346 | u32 offset = exit_qualification & 0xfff; |
| 7347 | |
| 7348 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 7349 | kvm_apic_write_nodecode(vcpu, offset); |
| 7350 | return 1; |
| 7351 | } |
| 7352 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7353 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7354 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7355 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7356 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7357 | bool has_error_code = false; |
| 7358 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7359 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7360 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7361 | |
| 7362 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7363 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7364 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7365 | |
| 7366 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7367 | |
| 7368 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7369 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 7370 | switch (type) { |
| 7371 | case INTR_TYPE_NMI_INTR: |
| 7372 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 7373 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7374 | break; |
| 7375 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7376 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7377 | kvm_clear_interrupt_queue(vcpu); |
| 7378 | break; |
| 7379 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7380 | if (vmx->idt_vectoring_info & |
| 7381 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 7382 | has_error_code = true; |
| 7383 | error_code = |
| 7384 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 7385 | } |
| 7386 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7387 | case INTR_TYPE_SOFT_EXCEPTION: |
| 7388 | kvm_clear_exception_queue(vcpu); |
| 7389 | break; |
| 7390 | default: |
| 7391 | break; |
| 7392 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7393 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7394 | tss_selector = exit_qualification; |
| 7395 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7396 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 7397 | type != INTR_TYPE_EXT_INTR && |
| 7398 | type != INTR_TYPE_NMI_INTR)) |
| 7399 | skip_emulated_instruction(vcpu); |
| 7400 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7401 | if (kvm_task_switch(vcpu, tss_selector, |
| 7402 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 7403 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7404 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7405 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7406 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7407 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7408 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7409 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7410 | /* |
| 7411 | * TODO: What about debug traps on tss switch? |
| 7412 | * Are we supposed to inject them and update dr6? |
| 7413 | */ |
| 7414 | |
| 7415 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7416 | } |
| 7417 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7418 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7419 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7420 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7421 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7422 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7423 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7424 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7425 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7426 | /* |
| 7427 | * EPT violation happened while executing iret from NMI, |
| 7428 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7429 | * There are errata that may cause this bit to not be set: |
| 7430 | * AAK134, BY25. |
| 7431 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7432 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7433 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7434 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7435 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 7436 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7437 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7438 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7439 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7440 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7441 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7442 | ? PFERR_USER_MASK : 0; |
| 7443 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7444 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7445 | ? PFERR_WRITE_MASK : 0; |
| 7446 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7447 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7448 | ? PFERR_FETCH_MASK : 0; |
| 7449 | /* ept page table entry is present? */ |
| 7450 | error_code |= (exit_qualification & |
| 7451 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 7452 | EPT_VIOLATION_EXECUTABLE)) |
| 7453 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7454 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7455 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 7456 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 7457 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7458 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7459 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7460 | } |
| 7461 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7462 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7463 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7464 | gpa_t gpa; |
| 7465 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7466 | /* |
| 7467 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 7468 | * nGPA here instead of the required GPA. |
| 7469 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7470 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7471 | if (!is_guest_mode(vcpu) && |
| 7472 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 7473 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 7474 | /* |
| 7475 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 7476 | * behavior: Intel's manual doesn't mandate |
| 7477 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 7478 | * occurs and while on real hardware it was observed to be set, |
| 7479 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 7480 | * advancing IP with some random value. Disable fast mmio when |
| 7481 | * running nested and keep it for real hardware in hope that |
| 7482 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 7483 | */ |
| 7484 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 7485 | return kvm_skip_emulated_instruction(vcpu); |
| 7486 | else |
| 7487 | return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP, |
| 7488 | NULL, 0) == EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 7489 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7490 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 7491 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7492 | } |
| 7493 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7494 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7495 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7496 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7497 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7498 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7499 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7500 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7501 | |
| 7502 | return 1; |
| 7503 | } |
| 7504 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7505 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7506 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 7507 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7508 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7509 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7510 | u32 cpu_exec_ctrl; |
| 7511 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 7512 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7513 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 7514 | /* |
| 7515 | * We should never reach the point where we are emulating L2 |
| 7516 | * due to invalid guest state as that means we incorrectly |
| 7517 | * allowed a nested VMEntry with an invalid vmcs12. |
| 7518 | */ |
| 7519 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 7520 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7521 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 7522 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7523 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 7524 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 7525 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7526 | return handle_interrupt_window(&vmx->vcpu); |
| 7527 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 7528 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 7529 | return 1; |
| 7530 | |
Liran Alon | 9b8ae63 | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 7531 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7532 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 7533 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 7534 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7535 | ret = 0; |
| 7536 | goto out; |
| 7537 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 7538 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7539 | if (err != EMULATE_DONE) |
| 7540 | goto emulation_error; |
| 7541 | |
| 7542 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 7543 | vcpu->arch.exception.pending) |
| 7544 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7545 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7546 | if (vcpu->arch.halt_request) { |
| 7547 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7548 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7549 | goto out; |
| 7550 | } |
| 7551 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7552 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7553 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7554 | if (need_resched()) |
| 7555 | schedule(); |
| 7556 | } |
| 7557 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7558 | out: |
| 7559 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7560 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7561 | emulation_error: |
| 7562 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7563 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7564 | vcpu->run->internal.ndata = 0; |
| 7565 | return 0; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7566 | } |
| 7567 | |
| 7568 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 7569 | { |
| 7570 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7571 | int old = vmx->ple_window; |
| 7572 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7573 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 7574 | ple_window_grow, |
| 7575 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7576 | |
| 7577 | if (vmx->ple_window != old) |
| 7578 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7579 | |
| 7580 | 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] | 7581 | } |
| 7582 | |
| 7583 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 7584 | { |
| 7585 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7586 | int old = vmx->ple_window; |
| 7587 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7588 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 7589 | ple_window_shrink, |
| 7590 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7591 | |
| 7592 | if (vmx->ple_window != old) |
| 7593 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7594 | |
| 7595 | 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] | 7596 | } |
| 7597 | |
| 7598 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7599 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 7600 | */ |
| 7601 | static void wakeup_handler(void) |
| 7602 | { |
| 7603 | struct kvm_vcpu *vcpu; |
| 7604 | int cpu = smp_processor_id(); |
| 7605 | |
| 7606 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7607 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 7608 | blocked_vcpu_list) { |
| 7609 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7610 | |
| 7611 | if (pi_test_on(pi_desc) == 1) |
| 7612 | kvm_vcpu_kick(vcpu); |
| 7613 | } |
| 7614 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7615 | } |
| 7616 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 7617 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7618 | { |
| 7619 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 7620 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 7621 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 7622 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 7623 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 7624 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7625 | |
| 7626 | ept_set_mmio_spte_mask(); |
| 7627 | kvm_enable_tdp(); |
| 7628 | } |
| 7629 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7630 | static __init int hardware_setup(void) |
| 7631 | { |
Sean Christopherson | cf81a7e | 2018-07-11 09:54:30 -0700 | [diff] [blame] | 7632 | unsigned long host_bndcfgs; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 7633 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7634 | |
| 7635 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7636 | |
| 7637 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7638 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7639 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7640 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 7641 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7642 | if (!vmx_bitmap[i]) |
| 7643 | goto out; |
| 7644 | } |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7645 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7646 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 7647 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 7648 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7649 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 7650 | r = -EIO; |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7651 | goto out; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7652 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7653 | |
| 7654 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7655 | kvm_enable_efer_bits(EFER_NX); |
| 7656 | |
Sean Christopherson | cf81a7e | 2018-07-11 09:54:30 -0700 | [diff] [blame] | 7657 | if (boot_cpu_has(X86_FEATURE_MPX)) { |
| 7658 | rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs); |
| 7659 | WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost"); |
| 7660 | } |
| 7661 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7662 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7663 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7664 | enable_vpid = 0; |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7665 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7666 | if (!cpu_has_vmx_ept() || |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 7667 | !cpu_has_vmx_ept_4levels() || |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 7668 | !cpu_has_vmx_ept_mt_wb() || |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7669 | !cpu_has_vmx_invept_global()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7670 | enable_ept = 0; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7671 | |
Wanpeng Li | fce6ac4 | 2017-05-11 02:58:56 -0700 | [diff] [blame] | 7672 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7673 | enable_ept_ad_bits = 0; |
| 7674 | |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7675 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7676 | enable_unrestricted_guest = 0; |
| 7677 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7678 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7679 | flexpriority_enabled = 0; |
| 7680 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7681 | if (!cpu_has_virtual_nmis()) |
| 7682 | enable_vnmi = 0; |
| 7683 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7684 | /* |
| 7685 | * set_apic_access_page_addr() is used to reload apic access |
| 7686 | * page upon invalidation. No need to do anything if not |
| 7687 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7688 | */ |
| 7689 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7690 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7691 | |
| 7692 | if (!cpu_has_vmx_tpr_shadow()) |
| 7693 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7694 | |
| 7695 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7696 | kvm_disable_largepages(); |
| 7697 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 7698 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7699 | if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH |
| 7700 | && enable_ept) |
| 7701 | kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb; |
| 7702 | #endif |
| 7703 | |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7704 | if (!cpu_has_vmx_ple()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7705 | ple_gap = 0; |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7706 | ple_window = 0; |
| 7707 | ple_window_grow = 0; |
| 7708 | ple_window_max = 0; |
| 7709 | ple_window_shrink = 0; |
| 7710 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7711 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7712 | if (!cpu_has_vmx_apicv()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7713 | enable_apicv = 0; |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7714 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7715 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7716 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 7717 | if (cpu_has_vmx_tsc_scaling()) { |
| 7718 | kvm_has_tsc_control = true; |
| 7719 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7720 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7721 | } |
| 7722 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 7723 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7724 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7725 | if (enable_ept) |
| 7726 | vmx_enable_tdp(); |
| 7727 | else |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7728 | kvm_disable_tdp(); |
| 7729 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 7730 | if (!nested) { |
| 7731 | kvm_x86_ops->get_nested_state = NULL; |
| 7732 | kvm_x86_ops->set_nested_state = NULL; |
| 7733 | } |
| 7734 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7735 | /* |
| 7736 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7737 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7738 | */ |
| 7739 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7740 | enable_pml = 0; |
| 7741 | |
| 7742 | if (!enable_pml) { |
| 7743 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 7744 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 7745 | kvm_x86_ops->flush_log_dirty = NULL; |
| 7746 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 7747 | } |
| 7748 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7749 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 7750 | u64 vmx_msr; |
| 7751 | |
| 7752 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 7753 | cpu_preemption_timer_multi = |
| 7754 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 7755 | } else { |
| 7756 | kvm_x86_ops->set_hv_timer = NULL; |
| 7757 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 7758 | } |
| 7759 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 7760 | if (!cpu_has_vmx_shadow_vmcs()) |
| 7761 | enable_shadow_vmcs = 0; |
| 7762 | if (enable_shadow_vmcs) |
| 7763 | init_vmcs_shadow_fields(); |
| 7764 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7765 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 7766 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7767 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7768 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 7769 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7770 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7771 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7772 | out: |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7773 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7774 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7775 | |
| 7776 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | static __exit void hardware_unsetup(void) |
| 7780 | { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7781 | int i; |
| 7782 | |
| 7783 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7784 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7785 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7786 | free_kvm_area(); |
| 7787 | } |
| 7788 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7789 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7790 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 7791 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 7792 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 7793 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7794 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7795 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7796 | grow_ple_window(vcpu); |
| 7797 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 7798 | /* |
| 7799 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 7800 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 7801 | * never set PAUSE_EXITING and just set PLE if supported, |
| 7802 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 7803 | */ |
| 7804 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7805 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7806 | } |
| 7807 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7808 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7809 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7810 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7811 | } |
| 7812 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7813 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 7814 | { |
| 7815 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 7816 | return handle_nop(vcpu); |
| 7817 | } |
| 7818 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 7819 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 7820 | { |
| 7821 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7822 | return 1; |
| 7823 | } |
| 7824 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7825 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 7826 | { |
| 7827 | return 1; |
| 7828 | } |
| 7829 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7830 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 7831 | { |
| 7832 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 7833 | return handle_nop(vcpu); |
| 7834 | } |
| 7835 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7836 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7837 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 7838 | * set the success or error code of an emulated VMX instruction, as specified |
| 7839 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 7840 | */ |
| 7841 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 7842 | { |
| 7843 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 7844 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7845 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 7846 | } |
| 7847 | |
| 7848 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 7849 | { |
| 7850 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7851 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 7852 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7853 | | X86_EFLAGS_CF); |
| 7854 | } |
| 7855 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7856 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7857 | u32 vm_instruction_error) |
| 7858 | { |
| 7859 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 7860 | /* |
| 7861 | * failValid writes the error number to the current VMCS, which |
| 7862 | * can't be done there isn't a current VMCS. |
| 7863 | */ |
| 7864 | nested_vmx_failInvalid(vcpu); |
| 7865 | return; |
| 7866 | } |
| 7867 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7868 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7869 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7870 | | X86_EFLAGS_ZF); |
| 7871 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 7872 | /* |
| 7873 | * We don't need to force a shadow sync because |
| 7874 | * VM_INSTRUCTION_ERROR is not shadowed |
| 7875 | */ |
| 7876 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7877 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7878 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 7879 | { |
| 7880 | /* TODO: not to reset guest simply here. */ |
| 7881 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 7882 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7883 | } |
| 7884 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7885 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 7886 | { |
| 7887 | struct vcpu_vmx *vmx = |
| 7888 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 7889 | |
| 7890 | vmx->nested.preemption_timer_expired = true; |
| 7891 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 7892 | kvm_vcpu_kick(&vmx->vcpu); |
| 7893 | |
| 7894 | return HRTIMER_NORESTART; |
| 7895 | } |
| 7896 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7897 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7898 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 7899 | * exit caused by such an instruction (run by a guest hypervisor). |
| 7900 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 7901 | * #UD or #GP. |
| 7902 | */ |
| 7903 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 7904 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7905 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7906 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7907 | gva_t off; |
| 7908 | bool exn; |
| 7909 | struct kvm_segment s; |
| 7910 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7911 | /* |
| 7912 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 7913 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 7914 | * addressing components of the operand. Only the displacement part |
| 7915 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 7916 | * For how an actual address is calculated from all these components, |
| 7917 | * refer to Vol. 1, "Operand Addressing". |
| 7918 | */ |
| 7919 | int scaling = vmx_instruction_info & 3; |
| 7920 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 7921 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 7922 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 7923 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 7924 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 7925 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 7926 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 7927 | |
| 7928 | if (is_reg) { |
| 7929 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7930 | return 1; |
| 7931 | } |
| 7932 | |
| 7933 | /* Addr = segment_base + offset */ |
| 7934 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7935 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7936 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7937 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7938 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7939 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 7940 | vmx_get_segment(vcpu, &s, seg_reg); |
| 7941 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7942 | |
| 7943 | if (addr_size == 1) /* 32 bit */ |
| 7944 | *ret &= 0xffffffff; |
| 7945 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7946 | /* Checks for #GP/#SS exceptions. */ |
| 7947 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7948 | if (is_long_mode(vcpu)) { |
| 7949 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 7950 | * non-canonical form. This is the only check on the memory |
| 7951 | * destination for long mode! |
| 7952 | */ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 7953 | exn = is_noncanonical_address(*ret, vcpu); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7954 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7955 | /* Protected mode: apply checks for segment validity in the |
| 7956 | * following order: |
| 7957 | * - segment type check (#GP(0) may be thrown) |
| 7958 | * - usability check (#GP(0)/#SS(0)) |
| 7959 | * - limit check (#GP(0)/#SS(0)) |
| 7960 | */ |
| 7961 | if (wr) |
| 7962 | /* #GP(0) if the destination operand is located in a |
| 7963 | * read-only data segment or any code segment. |
| 7964 | */ |
| 7965 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 7966 | else |
| 7967 | /* #GP(0) if the source operand is located in an |
| 7968 | * execute-only code segment |
| 7969 | */ |
| 7970 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7971 | if (exn) { |
| 7972 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7973 | return 1; |
| 7974 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7975 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 7976 | */ |
| 7977 | exn = (s.unusable != 0); |
| 7978 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 7979 | * operand is outside the segment limit. |
| 7980 | */ |
| 7981 | exn = exn || (off + sizeof(u64) > s.limit); |
| 7982 | } |
| 7983 | if (exn) { |
| 7984 | kvm_queue_exception_e(vcpu, |
| 7985 | seg_reg == VCPU_SREG_SS ? |
| 7986 | SS_VECTOR : GP_VECTOR, |
| 7987 | 0); |
| 7988 | return 1; |
| 7989 | } |
| 7990 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7991 | return 0; |
| 7992 | } |
| 7993 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7994 | 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] | 7995 | { |
| 7996 | gva_t gva; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7997 | struct x86_exception e; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7998 | |
| 7999 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8000 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8001 | return 1; |
| 8002 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8003 | if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8004 | kvm_inject_page_fault(vcpu, &e); |
| 8005 | return 1; |
| 8006 | } |
| 8007 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8008 | return 0; |
| 8009 | } |
| 8010 | |
Liran Alon | abfc52c | 2018-06-23 02:35:13 +0300 | [diff] [blame] | 8011 | /* |
| 8012 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 8013 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 8014 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 8015 | */ |
| 8016 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 8017 | { |
| 8018 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8019 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 8020 | |
| 8021 | /* |
| 8022 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 8023 | * executes VMXON and free it when L1 executes VMXOFF. |
| 8024 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 8025 | * here when vmcs01 already have an allocated shadow vmcs. |
| 8026 | */ |
| 8027 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 8028 | |
| 8029 | if (!loaded_vmcs->shadow_vmcs) { |
| 8030 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 8031 | if (loaded_vmcs->shadow_vmcs) |
| 8032 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 8033 | } |
| 8034 | return loaded_vmcs->shadow_vmcs; |
| 8035 | } |
| 8036 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8037 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 8038 | { |
| 8039 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 8040 | int r; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8041 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 8042 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 8043 | if (r < 0) |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8044 | goto out_vmcs02; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8045 | |
| 8046 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 8047 | if (!vmx->nested.cached_vmcs12) |
| 8048 | goto out_cached_vmcs12; |
| 8049 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8050 | vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 8051 | if (!vmx->nested.cached_shadow_vmcs12) |
| 8052 | goto out_cached_shadow_vmcs12; |
| 8053 | |
Liran Alon | abfc52c | 2018-06-23 02:35:13 +0300 | [diff] [blame] | 8054 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 8055 | goto out_shadow_vmcs; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8056 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8057 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 8058 | HRTIMER_MODE_REL_PINNED); |
| 8059 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 8060 | |
| 8061 | vmx->nested.vmxon = true; |
| 8062 | return 0; |
| 8063 | |
| 8064 | out_shadow_vmcs: |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8065 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 8066 | |
| 8067 | out_cached_shadow_vmcs12: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8068 | kfree(vmx->nested.cached_vmcs12); |
| 8069 | |
| 8070 | out_cached_vmcs12: |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8071 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8072 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8073 | out_vmcs02: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8074 | return -ENOMEM; |
| 8075 | } |
| 8076 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8077 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8078 | * Emulate the VMXON instruction. |
| 8079 | * Currently, we just remember that VMX is active, and do not save or even |
| 8080 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 8081 | * do not currently need to store anything in that guest-allocated memory |
| 8082 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 8083 | * argument is different from the VMXON pointer (which the spec says they do). |
| 8084 | */ |
| 8085 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 8086 | { |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8087 | int ret; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8088 | gpa_t vmptr; |
| 8089 | struct page *page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8090 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8091 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 8092 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8093 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8094 | /* |
| 8095 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 8096 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 8097 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 8098 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 8099 | * have already been checked by hardware, prior to the VM-exit for |
| 8100 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 8101 | * that bit set to 1 in non-root mode. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8102 | */ |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8103 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8104 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8105 | return 1; |
| 8106 | } |
| 8107 | |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8108 | /* CPL=0 must be checked manually. */ |
| 8109 | if (vmx_get_cpl(vcpu)) { |
Jim Mattson | 36090bf | 2018-07-27 09:18:50 -0700 | [diff] [blame] | 8110 | kvm_inject_gp(vcpu, 0); |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8111 | return 1; |
| 8112 | } |
| 8113 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 8114 | if (vmx->nested.vmxon) { |
| 8115 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8116 | return kvm_skip_emulated_instruction(vcpu); |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 8117 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8118 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 8119 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8120 | != VMXON_NEEDED_FEATURES) { |
| 8121 | kvm_inject_gp(vcpu, 0); |
| 8122 | return 1; |
| 8123 | } |
| 8124 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8125 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Jim Mattson | 21e7fbe | 2016-12-22 15:49:55 -0800 | [diff] [blame] | 8126 | return 1; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8127 | |
| 8128 | /* |
| 8129 | * SDM 3: 24.11.5 |
| 8130 | * The first 4 bytes of VMXON region contain the supported |
| 8131 | * VMCS revision identifier |
| 8132 | * |
| 8133 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 8134 | * which replaces physical address width with 32 |
| 8135 | */ |
| 8136 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8137 | nested_vmx_failInvalid(vcpu); |
| 8138 | return kvm_skip_emulated_instruction(vcpu); |
| 8139 | } |
| 8140 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8141 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8142 | if (is_error_page(page)) { |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8143 | nested_vmx_failInvalid(vcpu); |
| 8144 | return kvm_skip_emulated_instruction(vcpu); |
| 8145 | } |
| 8146 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 8147 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8148 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8149 | nested_vmx_failInvalid(vcpu); |
| 8150 | return kvm_skip_emulated_instruction(vcpu); |
| 8151 | } |
| 8152 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8153 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8154 | |
| 8155 | vmx->nested.vmxon_ptr = vmptr; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8156 | ret = enter_vmx_operation(vcpu); |
| 8157 | if (ret) |
| 8158 | return ret; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8159 | |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8160 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8161 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8162 | } |
| 8163 | |
| 8164 | /* |
| 8165 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 8166 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 8167 | * slightly different). It also specifies what exception to inject otherwise. |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8168 | * Note that many of these exceptions have priority over VM exits, so they |
| 8169 | * don't have to be checked again here. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8170 | */ |
| 8171 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 8172 | { |
Jim Mattson | e49fcb8 | 2018-07-27 13:44:45 -0700 | [diff] [blame] | 8173 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 8174 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8175 | return 0; |
| 8176 | } |
| 8177 | |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8178 | if (vmx_get_cpl(vcpu)) { |
Jim Mattson | 36090bf | 2018-07-27 09:18:50 -0700 | [diff] [blame] | 8179 | kvm_inject_gp(vcpu, 0); |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8180 | return 0; |
| 8181 | } |
| 8182 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8183 | return 1; |
| 8184 | } |
| 8185 | |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8186 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 8187 | { |
| 8188 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 8189 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 8190 | } |
| 8191 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8192 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 8193 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8194 | if (vmx->nested.current_vmptr == -1ull) |
| 8195 | return; |
| 8196 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8197 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8198 | /* copy to memory all shadowed fields in case |
| 8199 | they were modified */ |
| 8200 | copy_shadow_to_vmcs12(vmx); |
| 8201 | vmx->nested.sync_shadow_vmcs = false; |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8202 | vmx_disable_shadow_vmcs(vmx); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8203 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8204 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8205 | |
| 8206 | /* Flush VMCS12 to guest memory */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8207 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
| 8208 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 8209 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8210 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8211 | vmx->nested.current_vmptr = -1ull; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8212 | } |
| 8213 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8214 | /* |
| 8215 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 8216 | * just stops using VMX. |
| 8217 | */ |
| 8218 | static void free_nested(struct vcpu_vmx *vmx) |
| 8219 | { |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8220 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8221 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8222 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8223 | vmx->nested.vmxon = false; |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8224 | vmx->nested.smm.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 8225 | free_vpid(vmx->nested.vpid02); |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8226 | vmx->nested.posted_intr_nv = -1; |
| 8227 | vmx->nested.current_vmptr = -1ull; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8228 | if (enable_shadow_vmcs) { |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8229 | vmx_disable_shadow_vmcs(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8230 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 8231 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 8232 | vmx->vmcs01.shadow_vmcs = NULL; |
| 8233 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8234 | kfree(vmx->nested.cached_vmcs12); |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8235 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8236 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8237 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8238 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8239 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8240 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8241 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8242 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8243 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8244 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8245 | if (vmx->nested.pi_desc_page) { |
| 8246 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8247 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8248 | vmx->nested.pi_desc_page = NULL; |
| 8249 | vmx->nested.pi_desc = NULL; |
| 8250 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 8251 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8252 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8253 | } |
| 8254 | |
| 8255 | /* Emulate the VMXOFF instruction */ |
| 8256 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 8257 | { |
| 8258 | if (!nested_vmx_check_permission(vcpu)) |
| 8259 | return 1; |
| 8260 | free_nested(to_vmx(vcpu)); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8261 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8262 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8263 | } |
| 8264 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8265 | /* Emulate the VMCLEAR instruction */ |
| 8266 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 8267 | { |
| 8268 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8269 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8270 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8271 | |
| 8272 | if (!nested_vmx_check_permission(vcpu)) |
| 8273 | return 1; |
| 8274 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8275 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8276 | return 1; |
| 8277 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8278 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8279 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 8280 | return kvm_skip_emulated_instruction(vcpu); |
| 8281 | } |
| 8282 | |
| 8283 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8284 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
| 8285 | return kvm_skip_emulated_instruction(vcpu); |
| 8286 | } |
| 8287 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8288 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8289 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8290 | |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8291 | kvm_vcpu_write_guest(vcpu, |
| 8292 | vmptr + offsetof(struct vmcs12, launch_state), |
| 8293 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8294 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8295 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8296 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8297 | } |
| 8298 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8299 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 8300 | |
| 8301 | /* Emulate the VMLAUNCH instruction */ |
| 8302 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 8303 | { |
| 8304 | return nested_vmx_run(vcpu, true); |
| 8305 | } |
| 8306 | |
| 8307 | /* Emulate the VMRESUME instruction */ |
| 8308 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 8309 | { |
| 8310 | |
| 8311 | return nested_vmx_run(vcpu, false); |
| 8312 | } |
| 8313 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8314 | /* |
| 8315 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 8316 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 8317 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 8318 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 8319 | * 64-bit fields are to be returned). |
| 8320 | */ |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8321 | static inline int vmcs12_read_any(struct vmcs12 *vmcs12, |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8322 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8323 | { |
| 8324 | short offset = vmcs_field_to_offset(field); |
| 8325 | char *p; |
| 8326 | |
| 8327 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8328 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8329 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8330 | p = (char *)vmcs12 + offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8331 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8332 | switch (vmcs_field_width(field)) { |
| 8333 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8334 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8335 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8336 | case VMCS_FIELD_WIDTH_U16: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8337 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8338 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8339 | case VMCS_FIELD_WIDTH_U32: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8340 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8341 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8342 | case VMCS_FIELD_WIDTH_U64: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8343 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8344 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8345 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8346 | WARN_ON(1); |
| 8347 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8348 | } |
| 8349 | } |
| 8350 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8351 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8352 | static inline int vmcs12_write_any(struct vmcs12 *vmcs12, |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8353 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8354 | short offset = vmcs_field_to_offset(field); |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8355 | char *p = (char *)vmcs12 + offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8356 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8357 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8358 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8359 | switch (vmcs_field_width(field)) { |
| 8360 | case VMCS_FIELD_WIDTH_U16: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8361 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8362 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8363 | case VMCS_FIELD_WIDTH_U32: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8364 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8365 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8366 | case VMCS_FIELD_WIDTH_U64: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8367 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8368 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8369 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8370 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8371 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8372 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8373 | WARN_ON(1); |
| 8374 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8375 | } |
| 8376 | |
| 8377 | } |
| 8378 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8379 | /* |
| 8380 | * Copy the writable VMCS shadow fields back to the VMCS12, in case |
| 8381 | * they have been modified by the L1 guest. Note that the "read-only" |
| 8382 | * VM-exit information fields are actually writable if the vCPU is |
| 8383 | * configured to support "VMWRITE to any supported field in the VMCS." |
| 8384 | */ |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8385 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 8386 | { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8387 | const u16 *fields[] = { |
| 8388 | shadow_read_write_fields, |
| 8389 | shadow_read_only_fields |
| 8390 | }; |
| 8391 | const int max_fields[] = { |
| 8392 | max_shadow_read_write_fields, |
| 8393 | max_shadow_read_only_fields |
| 8394 | }; |
| 8395 | int i, q; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8396 | unsigned long field; |
| 8397 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8398 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8399 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8400 | preempt_disable(); |
| 8401 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8402 | vmcs_load(shadow_vmcs); |
| 8403 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8404 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 8405 | for (i = 0; i < max_fields[q]; i++) { |
| 8406 | field = fields[q][i]; |
| 8407 | field_value = __vmcs_readl(field); |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8408 | vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8409 | } |
| 8410 | /* |
| 8411 | * Skip the VM-exit information fields if they are read-only. |
| 8412 | */ |
| 8413 | if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 8414 | break; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8415 | } |
| 8416 | |
| 8417 | vmcs_clear(shadow_vmcs); |
| 8418 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8419 | |
| 8420 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8421 | } |
| 8422 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8423 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 8424 | { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8425 | const u16 *fields[] = { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8426 | shadow_read_write_fields, |
| 8427 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8428 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8429 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8430 | max_shadow_read_write_fields, |
| 8431 | max_shadow_read_only_fields |
| 8432 | }; |
| 8433 | int i, q; |
| 8434 | unsigned long field; |
| 8435 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8436 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8437 | |
| 8438 | vmcs_load(shadow_vmcs); |
| 8439 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8440 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8441 | for (i = 0; i < max_fields[q]; i++) { |
| 8442 | field = fields[q][i]; |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8443 | vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value); |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8444 | __vmcs_writel(field, field_value); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8445 | } |
| 8446 | } |
| 8447 | |
| 8448 | vmcs_clear(shadow_vmcs); |
| 8449 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 8450 | } |
| 8451 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8452 | /* |
| 8453 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 8454 | * used before) all generate the same failure when it is missing. |
| 8455 | */ |
| 8456 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 8457 | { |
| 8458 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8459 | if (vmx->nested.current_vmptr == -1ull) { |
| 8460 | nested_vmx_failInvalid(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8461 | return 0; |
| 8462 | } |
| 8463 | return 1; |
| 8464 | } |
| 8465 | |
| 8466 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 8467 | { |
| 8468 | unsigned long field; |
| 8469 | u64 field_value; |
| 8470 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8471 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8472 | gva_t gva = 0; |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8473 | struct vmcs12 *vmcs12; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8474 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8475 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8476 | return 1; |
| 8477 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8478 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8479 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8480 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8481 | if (!is_guest_mode(vcpu)) |
| 8482 | vmcs12 = get_vmcs12(vcpu); |
| 8483 | else { |
| 8484 | /* |
| 8485 | * When vmcs->vmcs_link_pointer is -1ull, any VMREAD |
| 8486 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 8487 | */ |
| 8488 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) { |
| 8489 | nested_vmx_failInvalid(vcpu); |
| 8490 | return kvm_skip_emulated_instruction(vcpu); |
| 8491 | } |
| 8492 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 8493 | } |
| 8494 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8495 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8496 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8497 | /* Read the field, zero-extended to a u64 field_value */ |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8498 | if (vmcs12_read_any(vmcs12, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8499 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8500 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8501 | } |
| 8502 | /* |
| 8503 | * Now copy part of this value to register or memory, as requested. |
| 8504 | * Note that the number of bits actually copied is 32 or 64 depending |
| 8505 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 8506 | */ |
| 8507 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8508 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8509 | field_value); |
| 8510 | } else { |
| 8511 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8512 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8513 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8514 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8515 | kvm_write_guest_virt_system(vcpu, gva, &field_value, |
| 8516 | (is_long_mode(vcpu) ? 8 : 4), NULL); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8520 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8521 | } |
| 8522 | |
| 8523 | |
| 8524 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 8525 | { |
| 8526 | unsigned long field; |
| 8527 | gva_t gva; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8528 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8529 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8530 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8531 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8532 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 8533 | * mode, and eventually we need to write that into a field of several |
| 8534 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 8535 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8536 | * bits into the vmcs12 field. |
| 8537 | */ |
| 8538 | u64 field_value = 0; |
| 8539 | struct x86_exception e; |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8540 | struct vmcs12 *vmcs12; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8541 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8542 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8543 | return 1; |
| 8544 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8545 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8546 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8547 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8548 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8549 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8550 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 8551 | else { |
| 8552 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8553 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8554 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8555 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
| 8556 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8557 | kvm_inject_page_fault(vcpu, &e); |
| 8558 | return 1; |
| 8559 | } |
| 8560 | } |
| 8561 | |
| 8562 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8563 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8564 | /* |
| 8565 | * If the vCPU supports "VMWRITE to any supported field in the |
| 8566 | * VMCS," then the "read-only" fields are actually read/write. |
| 8567 | */ |
| 8568 | if (vmcs_field_readonly(field) && |
| 8569 | !nested_cpu_has_vmwrite_any_field(vcpu)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8570 | nested_vmx_failValid(vcpu, |
| 8571 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8572 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8573 | } |
| 8574 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8575 | if (!is_guest_mode(vcpu)) |
| 8576 | vmcs12 = get_vmcs12(vcpu); |
| 8577 | else { |
| 8578 | /* |
| 8579 | * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE |
| 8580 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 8581 | */ |
| 8582 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) { |
| 8583 | nested_vmx_failInvalid(vcpu); |
| 8584 | return kvm_skip_emulated_instruction(vcpu); |
| 8585 | } |
| 8586 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 8587 | |
| 8588 | } |
| 8589 | |
| 8590 | if (vmcs12_write_any(vmcs12, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8591 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8592 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8593 | } |
| 8594 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8595 | /* |
| 8596 | * Do not track vmcs12 dirty-state if in guest-mode |
| 8597 | * as we actually dirty shadow vmcs12 instead of vmcs12. |
| 8598 | */ |
| 8599 | if (!is_guest_mode(vcpu)) { |
| 8600 | switch (field) { |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8601 | #define SHADOW_FIELD_RW(x) case x: |
| 8602 | #include "vmx_shadow_fields.h" |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8603 | /* |
| 8604 | * The fields that can be updated by L1 without a vmexit are |
| 8605 | * always updated in the vmcs02, the others go down the slow |
| 8606 | * path of prepare_vmcs02. |
| 8607 | */ |
| 8608 | break; |
| 8609 | default: |
| 8610 | vmx->nested.dirty_vmcs12 = true; |
| 8611 | break; |
| 8612 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8613 | } |
| 8614 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8615 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8616 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8617 | } |
| 8618 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8619 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 8620 | { |
| 8621 | vmx->nested.current_vmptr = vmptr; |
| 8622 | if (enable_shadow_vmcs) { |
| 8623 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 8624 | SECONDARY_EXEC_SHADOW_VMCS); |
| 8625 | vmcs_write64(VMCS_LINK_POINTER, |
| 8626 | __pa(vmx->vmcs01.shadow_vmcs)); |
| 8627 | vmx->nested.sync_shadow_vmcs = true; |
| 8628 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8629 | vmx->nested.dirty_vmcs12 = true; |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8630 | } |
| 8631 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8632 | /* Emulate the VMPTRLD instruction */ |
| 8633 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 8634 | { |
| 8635 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8636 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8637 | |
| 8638 | if (!nested_vmx_check_permission(vcpu)) |
| 8639 | return 1; |
| 8640 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8641 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8642 | return 1; |
| 8643 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8644 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8645 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 8646 | return kvm_skip_emulated_instruction(vcpu); |
| 8647 | } |
| 8648 | |
| 8649 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8650 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
| 8651 | return kvm_skip_emulated_instruction(vcpu); |
| 8652 | } |
| 8653 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8654 | if (vmx->nested.current_vmptr != vmptr) { |
| 8655 | struct vmcs12 *new_vmcs12; |
| 8656 | struct page *page; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8657 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8658 | if (is_error_page(page)) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8659 | nested_vmx_failInvalid(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8660 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8661 | } |
| 8662 | new_vmcs12 = kmap(page); |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 8663 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
Liran Alon | fa97d7d | 2018-07-18 14:07:59 +0200 | [diff] [blame] | 8664 | (new_vmcs12->hdr.shadow_vmcs && |
| 8665 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8666 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8667 | kvm_release_page_clean(page); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8668 | nested_vmx_failValid(vcpu, |
| 8669 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8670 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8671 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8672 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8673 | nested_release_vmcs12(vmx); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8674 | /* |
| 8675 | * Load VMCS12 from guest memory since it is not already |
| 8676 | * cached. |
| 8677 | */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8678 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 8679 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8680 | kvm_release_page_clean(page); |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8681 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8682 | set_current_vmptr(vmx, vmptr); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8683 | } |
| 8684 | |
| 8685 | nested_vmx_succeed(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 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8689 | /* Emulate the VMPTRST instruction */ |
| 8690 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 8691 | { |
| 8692 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8693 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8694 | gva_t vmcs_gva; |
| 8695 | struct x86_exception e; |
| 8696 | |
| 8697 | if (!nested_vmx_check_permission(vcpu)) |
| 8698 | return 1; |
| 8699 | |
| 8700 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8701 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8702 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8703 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8704 | if (kvm_write_guest_virt_system(vcpu, vmcs_gva, |
| 8705 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 8706 | sizeof(u64), &e)) { |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8707 | kvm_inject_page_fault(vcpu, &e); |
| 8708 | return 1; |
| 8709 | } |
| 8710 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8711 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8712 | } |
| 8713 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8714 | /* Emulate the INVEPT instruction */ |
| 8715 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 8716 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8717 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8718 | u32 vmx_instruction_info, types; |
| 8719 | unsigned long type; |
| 8720 | gva_t gva; |
| 8721 | struct x86_exception e; |
| 8722 | struct { |
| 8723 | u64 eptp, gpa; |
| 8724 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8725 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8726 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8727 | SECONDARY_EXEC_ENABLE_EPT) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8728 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8729 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8730 | return 1; |
| 8731 | } |
| 8732 | |
| 8733 | if (!nested_vmx_check_permission(vcpu)) |
| 8734 | return 1; |
| 8735 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8736 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8737 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8738 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8739 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8740 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8741 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8742 | nested_vmx_failValid(vcpu, |
| 8743 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8744 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8745 | } |
| 8746 | |
| 8747 | /* According to the Intel VMX instruction reference, the memory |
| 8748 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8749 | */ |
| 8750 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8751 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8752 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8753 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8754 | kvm_inject_page_fault(vcpu, &e); |
| 8755 | return 1; |
| 8756 | } |
| 8757 | |
| 8758 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8759 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 8760 | /* |
| 8761 | * TODO: track mappings and invalidate |
| 8762 | * single context requests appropriately |
| 8763 | */ |
| 8764 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8765 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 8766 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8767 | nested_vmx_succeed(vcpu); |
| 8768 | break; |
| 8769 | default: |
| 8770 | BUG_ON(1); |
| 8771 | break; |
| 8772 | } |
| 8773 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8774 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8775 | } |
| 8776 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8777 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 8778 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8779 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8780 | u32 vmx_instruction_info; |
| 8781 | unsigned long type, types; |
| 8782 | gva_t gva; |
| 8783 | struct x86_exception e; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8784 | struct { |
| 8785 | u64 vpid; |
| 8786 | u64 gla; |
| 8787 | } operand; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8788 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8789 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8790 | SECONDARY_EXEC_ENABLE_VPID) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8791 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8792 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8793 | return 1; |
| 8794 | } |
| 8795 | |
| 8796 | if (!nested_vmx_check_permission(vcpu)) |
| 8797 | return 1; |
| 8798 | |
| 8799 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8800 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8801 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8802 | types = (vmx->nested.msrs.vpid_caps & |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8803 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8804 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8805 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8806 | nested_vmx_failValid(vcpu, |
| 8807 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8808 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8809 | } |
| 8810 | |
| 8811 | /* according to the intel vmx instruction reference, the memory |
| 8812 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8813 | */ |
| 8814 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8815 | vmx_instruction_info, false, &gva)) |
| 8816 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8817 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8818 | kvm_inject_page_fault(vcpu, &e); |
| 8819 | return 1; |
| 8820 | } |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8821 | if (operand.vpid >> 16) { |
| 8822 | nested_vmx_failValid(vcpu, |
| 8823 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8824 | return kvm_skip_emulated_instruction(vcpu); |
| 8825 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8826 | |
| 8827 | switch (type) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8828 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8829 | if (!operand.vpid || |
| 8830 | is_noncanonical_address(operand.gla, vcpu)) { |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8831 | nested_vmx_failValid(vcpu, |
| 8832 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8833 | return kvm_skip_emulated_instruction(vcpu); |
| 8834 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8835 | if (cpu_has_vmx_invvpid_individual_addr() && |
| 8836 | vmx->nested.vpid02) { |
| 8837 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, |
| 8838 | vmx->nested.vpid02, operand.gla); |
| 8839 | } else |
| 8840 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
| 8841 | break; |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 8842 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8843 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8844 | if (!operand.vpid) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8845 | nested_vmx_failValid(vcpu, |
| 8846 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8847 | return kvm_skip_emulated_instruction(vcpu); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8848 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8849 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8850 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8851 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8852 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8853 | break; |
| 8854 | default: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8855 | WARN_ON_ONCE(1); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8856 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8857 | } |
| 8858 | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8859 | nested_vmx_succeed(vcpu); |
| 8860 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8861 | return kvm_skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8862 | } |
| 8863 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8864 | static int handle_invpcid(struct kvm_vcpu *vcpu) |
| 8865 | { |
| 8866 | u32 vmx_instruction_info; |
| 8867 | unsigned long type; |
| 8868 | bool pcid_enabled; |
| 8869 | gva_t gva; |
| 8870 | struct x86_exception e; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 8871 | unsigned i; |
| 8872 | unsigned long roots_to_free = 0; |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8873 | struct { |
| 8874 | u64 pcid; |
| 8875 | u64 gla; |
| 8876 | } operand; |
| 8877 | |
| 8878 | if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { |
| 8879 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8880 | return 1; |
| 8881 | } |
| 8882 | |
| 8883 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8884 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8885 | |
| 8886 | if (type > 3) { |
| 8887 | kvm_inject_gp(vcpu, 0); |
| 8888 | return 1; |
| 8889 | } |
| 8890 | |
| 8891 | /* According to the Intel instruction reference, the memory operand |
| 8892 | * is read even if it isn't needed (e.g., for type==all) |
| 8893 | */ |
| 8894 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8895 | vmx_instruction_info, false, &gva)) |
| 8896 | return 1; |
| 8897 | |
| 8898 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
| 8899 | kvm_inject_page_fault(vcpu, &e); |
| 8900 | return 1; |
| 8901 | } |
| 8902 | |
| 8903 | if (operand.pcid >> 12 != 0) { |
| 8904 | kvm_inject_gp(vcpu, 0); |
| 8905 | return 1; |
| 8906 | } |
| 8907 | |
| 8908 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 8909 | |
| 8910 | switch (type) { |
| 8911 | case INVPCID_TYPE_INDIV_ADDR: |
| 8912 | if ((!pcid_enabled && (operand.pcid != 0)) || |
| 8913 | is_noncanonical_address(operand.gla, vcpu)) { |
| 8914 | kvm_inject_gp(vcpu, 0); |
| 8915 | return 1; |
| 8916 | } |
| 8917 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); |
| 8918 | return kvm_skip_emulated_instruction(vcpu); |
| 8919 | |
| 8920 | case INVPCID_TYPE_SINGLE_CTXT: |
| 8921 | if (!pcid_enabled && (operand.pcid != 0)) { |
| 8922 | kvm_inject_gp(vcpu, 0); |
| 8923 | return 1; |
| 8924 | } |
| 8925 | |
| 8926 | if (kvm_get_active_pcid(vcpu) == operand.pcid) { |
| 8927 | kvm_mmu_sync_roots(vcpu); |
| 8928 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 8929 | } |
| 8930 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 8931 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
| 8932 | if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3) |
| 8933 | == operand.pcid) |
| 8934 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 8935 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 8936 | kvm_mmu_free_roots(vcpu, roots_to_free); |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8937 | /* |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 8938 | * 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] | 8939 | * given PCID, then nothing needs to be done here because a |
| 8940 | * resync will happen anyway before switching to any other CR3. |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8941 | */ |
| 8942 | |
| 8943 | return kvm_skip_emulated_instruction(vcpu); |
| 8944 | |
| 8945 | case INVPCID_TYPE_ALL_NON_GLOBAL: |
| 8946 | /* |
| 8947 | * Currently, KVM doesn't mark global entries in the shadow |
| 8948 | * page tables, so a non-global flush just degenerates to a |
| 8949 | * global flush. If needed, we could optimize this later by |
| 8950 | * keeping track of global entries in shadow page tables. |
| 8951 | */ |
| 8952 | |
| 8953 | /* fall-through */ |
| 8954 | case INVPCID_TYPE_ALL_INCL_GLOBAL: |
| 8955 | kvm_mmu_unload(vcpu); |
| 8956 | return kvm_skip_emulated_instruction(vcpu); |
| 8957 | |
| 8958 | default: |
| 8959 | BUG(); /* We have already checked above that type <= 3 */ |
| 8960 | } |
| 8961 | } |
| 8962 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8963 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 8964 | { |
| 8965 | unsigned long exit_qualification; |
| 8966 | |
| 8967 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 8968 | |
| 8969 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8970 | |
| 8971 | /* |
| 8972 | * PML buffer FULL happened while executing iret from NMI, |
| 8973 | * "blocked by NMI" bit has to be set before next VM entry. |
| 8974 | */ |
| 8975 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 8976 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8977 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 8978 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8979 | GUEST_INTR_STATE_NMI); |
| 8980 | |
| 8981 | /* |
| 8982 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 8983 | * here.., and there's no userspace involvement needed for PML. |
| 8984 | */ |
| 8985 | return 1; |
| 8986 | } |
| 8987 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8988 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 8989 | { |
| 8990 | kvm_lapic_expired_hv_timer(vcpu); |
| 8991 | return 1; |
| 8992 | } |
| 8993 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8994 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
| 8995 | { |
| 8996 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8997 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 8998 | |
| 8999 | /* Check for memory type validity */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9000 | switch (address & VMX_EPTP_MT_MASK) { |
| 9001 | case VMX_EPTP_MT_UC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9002 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9003 | return false; |
| 9004 | break; |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9005 | case VMX_EPTP_MT_WB: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9006 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9007 | return false; |
| 9008 | break; |
| 9009 | default: |
| 9010 | return false; |
| 9011 | } |
| 9012 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9013 | /* only 4 levels page-walk length are valid */ |
| 9014 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9015 | return false; |
| 9016 | |
| 9017 | /* Reserved bits should not be set */ |
| 9018 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) |
| 9019 | return false; |
| 9020 | |
| 9021 | /* AD, if set, should be supported */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9022 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9023 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9024 | return false; |
| 9025 | } |
| 9026 | |
| 9027 | return true; |
| 9028 | } |
| 9029 | |
| 9030 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 9031 | struct vmcs12 *vmcs12) |
| 9032 | { |
| 9033 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 9034 | u64 address; |
| 9035 | bool accessed_dirty; |
| 9036 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 9037 | |
| 9038 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 9039 | !nested_cpu_has_ept(vmcs12)) |
| 9040 | return 1; |
| 9041 | |
| 9042 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 9043 | return 1; |
| 9044 | |
| 9045 | |
| 9046 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
| 9047 | &address, index * 8, 8)) |
| 9048 | return 1; |
| 9049 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9050 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9051 | |
| 9052 | /* |
| 9053 | * If the (L2) guest does a vmfunc to the currently |
| 9054 | * active ept pointer, we don't have to do anything else |
| 9055 | */ |
| 9056 | if (vmcs12->ept_pointer != address) { |
| 9057 | if (!valid_ept_address(vcpu, address)) |
| 9058 | return 1; |
| 9059 | |
| 9060 | kvm_mmu_unload(vcpu); |
| 9061 | mmu->ept_ad = accessed_dirty; |
| 9062 | mmu->base_role.ad_disabled = !accessed_dirty; |
| 9063 | vmcs12->ept_pointer = address; |
| 9064 | /* |
| 9065 | * TODO: Check what's the correct approach in case |
| 9066 | * mmu reload fails. Currently, we just let the next |
| 9067 | * reload potentially fail |
| 9068 | */ |
| 9069 | kvm_mmu_reload(vcpu); |
| 9070 | } |
| 9071 | |
| 9072 | return 0; |
| 9073 | } |
| 9074 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9075 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 9076 | { |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 9077 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9078 | struct vmcs12 *vmcs12; |
| 9079 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 9080 | |
| 9081 | /* |
| 9082 | * VMFUNC is only supported for nested guests, but we always enable the |
| 9083 | * secondary control for simplicity; for non-nested mode, fake that we |
| 9084 | * didn't by injecting #UD. |
| 9085 | */ |
| 9086 | if (!is_guest_mode(vcpu)) { |
| 9087 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9088 | return 1; |
| 9089 | } |
| 9090 | |
| 9091 | vmcs12 = get_vmcs12(vcpu); |
| 9092 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 9093 | goto fail; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9094 | |
| 9095 | switch (function) { |
| 9096 | case 0: |
| 9097 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 9098 | goto fail; |
| 9099 | break; |
| 9100 | default: |
| 9101 | goto fail; |
| 9102 | } |
| 9103 | return kvm_skip_emulated_instruction(vcpu); |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 9104 | |
| 9105 | fail: |
| 9106 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 9107 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9108 | vmcs_readl(EXIT_QUALIFICATION)); |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9109 | return 1; |
| 9110 | } |
| 9111 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 9112 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9113 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 9114 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 9115 | * to be done to userspace and return 0. |
| 9116 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 9117 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9118 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 9119 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 9120 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 9121 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9122 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9123 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 9124 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 9125 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 9126 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 9127 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 9128 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 9129 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 9130 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 9131 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 9132 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 9133 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 9134 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9135 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9136 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9137 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9138 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9139 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9140 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 9141 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 9142 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 9143 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 9144 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 9145 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9146 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 9147 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 9148 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 9149 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9150 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 9151 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 9152 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 9153 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 9154 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 9155 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 9156 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9157 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 9158 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9159 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9160 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 9161 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 9162 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 9163 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 9164 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9165 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 9166 | [EXIT_REASON_INVPCID] = handle_invpcid, |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9167 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9168 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9169 | }; |
| 9170 | |
| 9171 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 9172 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9173 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9174 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 9175 | struct vmcs12 *vmcs12) |
| 9176 | { |
| 9177 | unsigned long exit_qualification; |
| 9178 | gpa_t bitmap, last_bitmap; |
| 9179 | unsigned int port; |
| 9180 | int size; |
| 9181 | u8 b; |
| 9182 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9183 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 9184 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9185 | |
| 9186 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9187 | |
| 9188 | port = exit_qualification >> 16; |
| 9189 | size = (exit_qualification & 7) + 1; |
| 9190 | |
| 9191 | last_bitmap = (gpa_t)-1; |
| 9192 | b = -1; |
| 9193 | |
| 9194 | while (size > 0) { |
| 9195 | if (port < 0x8000) |
| 9196 | bitmap = vmcs12->io_bitmap_a; |
| 9197 | else if (port < 0x10000) |
| 9198 | bitmap = vmcs12->io_bitmap_b; |
| 9199 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9200 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9201 | bitmap += (port & 0x7fff) / 8; |
| 9202 | |
| 9203 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9204 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9205 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9206 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9207 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9208 | |
| 9209 | port++; |
| 9210 | size--; |
| 9211 | last_bitmap = bitmap; |
| 9212 | } |
| 9213 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9214 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9215 | } |
| 9216 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9217 | /* |
| 9218 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 9219 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 9220 | * disinterest in the current event (read or write a specific MSR) by using an |
| 9221 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 9222 | */ |
| 9223 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 9224 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 9225 | { |
| 9226 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 9227 | gpa_t bitmap; |
| 9228 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 9229 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9230 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9231 | |
| 9232 | /* |
| 9233 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 9234 | * for the four combinations of read/write and low/high MSR numbers. |
| 9235 | * First we need to figure out which of the four to use: |
| 9236 | */ |
| 9237 | bitmap = vmcs12->msr_bitmap; |
| 9238 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 9239 | bitmap += 2048; |
| 9240 | if (msr_index >= 0xc0000000) { |
| 9241 | msr_index -= 0xc0000000; |
| 9242 | bitmap += 1024; |
| 9243 | } |
| 9244 | |
| 9245 | /* Then read the msr_index'th bit from this bitmap: */ |
| 9246 | if (msr_index < 1024*8) { |
| 9247 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9248 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9249 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9250 | return 1 & (b >> (msr_index & 7)); |
| 9251 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9252 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9253 | } |
| 9254 | |
| 9255 | /* |
| 9256 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 9257 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 9258 | * intercept (via guest_host_mask etc.) the current event. |
| 9259 | */ |
| 9260 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 9261 | struct vmcs12 *vmcs12) |
| 9262 | { |
| 9263 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9264 | int cr = exit_qualification & 15; |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9265 | int reg; |
| 9266 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9267 | |
| 9268 | switch ((exit_qualification >> 4) & 3) { |
| 9269 | case 0: /* mov to cr */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9270 | reg = (exit_qualification >> 8) & 15; |
| 9271 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9272 | switch (cr) { |
| 9273 | case 0: |
| 9274 | if (vmcs12->cr0_guest_host_mask & |
| 9275 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9276 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9277 | break; |
| 9278 | case 3: |
| 9279 | if ((vmcs12->cr3_target_count >= 1 && |
| 9280 | vmcs12->cr3_target_value0 == val) || |
| 9281 | (vmcs12->cr3_target_count >= 2 && |
| 9282 | vmcs12->cr3_target_value1 == val) || |
| 9283 | (vmcs12->cr3_target_count >= 3 && |
| 9284 | vmcs12->cr3_target_value2 == val) || |
| 9285 | (vmcs12->cr3_target_count >= 4 && |
| 9286 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9287 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9288 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9289 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9290 | break; |
| 9291 | case 4: |
| 9292 | if (vmcs12->cr4_guest_host_mask & |
| 9293 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9294 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9295 | break; |
| 9296 | case 8: |
| 9297 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9298 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9299 | break; |
| 9300 | } |
| 9301 | break; |
| 9302 | case 2: /* clts */ |
| 9303 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 9304 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9305 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9306 | break; |
| 9307 | case 1: /* mov from cr */ |
| 9308 | switch (cr) { |
| 9309 | case 3: |
| 9310 | if (vmcs12->cpu_based_vm_exec_control & |
| 9311 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9312 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9313 | break; |
| 9314 | case 8: |
| 9315 | if (vmcs12->cpu_based_vm_exec_control & |
| 9316 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9317 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9318 | break; |
| 9319 | } |
| 9320 | break; |
| 9321 | case 3: /* lmsw */ |
| 9322 | /* |
| 9323 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 9324 | * cr0. Other attempted changes are ignored, with no exit. |
| 9325 | */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9326 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9327 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 9328 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9329 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9330 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 9331 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 9332 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9333 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9334 | break; |
| 9335 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9336 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9337 | } |
| 9338 | |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9339 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 9340 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 9341 | { |
| 9342 | u32 vmx_instruction_info; |
| 9343 | unsigned long field; |
| 9344 | u8 b; |
| 9345 | |
| 9346 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 9347 | return true; |
| 9348 | |
| 9349 | /* Decode instruction info and find the field to access */ |
| 9350 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 9351 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 9352 | |
| 9353 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 9354 | if (field >> 15) |
| 9355 | return true; |
| 9356 | |
| 9357 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 9358 | return true; |
| 9359 | |
| 9360 | return 1 & (b >> (field & 7)); |
| 9361 | } |
| 9362 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9363 | /* |
| 9364 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 9365 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 9366 | * when in is_guest_mode (L2). |
| 9367 | */ |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9368 | 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] | 9369 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9370 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9371 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9372 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9373 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9374 | if (vmx->nested.nested_run_pending) |
| 9375 | return false; |
| 9376 | |
| 9377 | if (unlikely(vmx->fail)) { |
| 9378 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 9379 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 9380 | return true; |
| 9381 | } |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 9382 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9383 | /* |
| 9384 | * The host physical addresses of some pages of guest memory |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 9385 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 9386 | * Page). The CPU may write to these pages via their host |
| 9387 | * physical address while L2 is running, bypassing any |
| 9388 | * address-translation-based dirty tracking (e.g. EPT write |
| 9389 | * protection). |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9390 | * |
| 9391 | * Mark them dirty on every exit from L2 to prevent them from |
| 9392 | * getting out of sync with dirty tracking. |
| 9393 | */ |
| 9394 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 9395 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9396 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 9397 | vmcs_readl(EXIT_QUALIFICATION), |
| 9398 | vmx->idt_vectoring_info, |
| 9399 | intr_info, |
| 9400 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9401 | KVM_ISA_VMX); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9402 | |
| 9403 | switch (exit_reason) { |
| 9404 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9405 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9406 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9407 | else if (is_page_fault(intr_info)) |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 9408 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 9409 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 9410 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9411 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 9412 | else if (is_debug(intr_info) && |
| 9413 | vcpu->guest_debug & |
| 9414 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 9415 | return false; |
| 9416 | else if (is_breakpoint(intr_info) && |
| 9417 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 9418 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9419 | return vmcs12->exception_bitmap & |
| 9420 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 9421 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9422 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9423 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9424 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9425 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9426 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9427 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9428 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9429 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9430 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9431 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9432 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9433 | case EXIT_REASON_HLT: |
| 9434 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 9435 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9436 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9437 | case EXIT_REASON_INVLPG: |
| 9438 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 9439 | case EXIT_REASON_RDPMC: |
| 9440 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9441 | case EXIT_REASON_RDRAND: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9442 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9443 | case EXIT_REASON_RDSEED: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9444 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 9445 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9446 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9447 | case EXIT_REASON_VMREAD: |
| 9448 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 9449 | vmcs12->vmread_bitmap); |
| 9450 | case EXIT_REASON_VMWRITE: |
| 9451 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 9452 | vmcs12->vmwrite_bitmap); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9453 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 9454 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9455 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9456 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9457 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9458 | /* |
| 9459 | * VMX instructions trap unconditionally. This allows L1 to |
| 9460 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 9461 | */ |
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_CR_ACCESS: |
| 9464 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 9465 | case EXIT_REASON_DR_ACCESS: |
| 9466 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 9467 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9468 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 9469 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 9470 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9471 | case EXIT_REASON_MSR_READ: |
| 9472 | case EXIT_REASON_MSR_WRITE: |
| 9473 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 9474 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9475 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9476 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 9477 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9478 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 9479 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9480 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 9481 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 9482 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 9483 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 9484 | nested_cpu_has2(vmcs12, |
| 9485 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 9486 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9487 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9488 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9489 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9490 | case EXIT_REASON_APIC_ACCESS: |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9491 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9492 | case EXIT_REASON_EOI_INDUCED: |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9493 | /* |
| 9494 | * The controls for "virtualize APIC accesses," "APIC- |
| 9495 | * register virtualization," and "virtual-interrupt |
| 9496 | * delivery" only come from vmcs12. |
| 9497 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9498 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9499 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9500 | /* |
| 9501 | * L0 always deals with the EPT violation. If nested EPT is |
| 9502 | * used, and the nested mmu code discovers that the address is |
| 9503 | * missing in the guest EPT table (EPT12), the EPT violation |
| 9504 | * will be injected with nested_ept_inject_page_fault() |
| 9505 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9506 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9507 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9508 | /* |
| 9509 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 9510 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 9511 | * (EPT on EPT). So any problems with the structure of the |
| 9512 | * table is L0's fault. |
| 9513 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9514 | return false; |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 9515 | case EXIT_REASON_INVPCID: |
| 9516 | return |
| 9517 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 9518 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9519 | case EXIT_REASON_WBINVD: |
| 9520 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 9521 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9522 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9523 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 9524 | /* |
| 9525 | * This should never happen, since it is not possible to |
| 9526 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 9527 | * If if it were, XSS would have to be checked against |
| 9528 | * the XSS exit bitmap in vmcs12. |
| 9529 | */ |
| 9530 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 9531 | case EXIT_REASON_PREEMPTION_TIMER: |
| 9532 | return false; |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9533 | case EXIT_REASON_PML_FULL: |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 9534 | /* We emulate PML support to L1. */ |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9535 | return false; |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9536 | case EXIT_REASON_VMFUNC: |
| 9537 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 9538 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9539 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9540 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9541 | } |
| 9542 | } |
| 9543 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9544 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 9545 | { |
| 9546 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9547 | |
| 9548 | /* |
| 9549 | * At this point, the exit interruption info in exit_intr_info |
| 9550 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT |
| 9551 | * we need to query the in-kernel LAPIC. |
| 9552 | */ |
| 9553 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); |
| 9554 | if ((exit_intr_info & |
| 9555 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 9556 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { |
| 9557 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9558 | vmcs12->vm_exit_intr_error_code = |
| 9559 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 9560 | } |
| 9561 | |
| 9562 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, |
| 9563 | vmcs_readl(EXIT_QUALIFICATION)); |
| 9564 | return 1; |
| 9565 | } |
| 9566 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 9567 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 9568 | { |
| 9569 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 9570 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9571 | } |
| 9572 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9573 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9574 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9575 | if (vmx->pml_pg) { |
| 9576 | __free_page(vmx->pml_pg); |
| 9577 | vmx->pml_pg = NULL; |
| 9578 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9579 | } |
| 9580 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9581 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9582 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9583 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9584 | u64 *pml_buf; |
| 9585 | u16 pml_idx; |
| 9586 | |
| 9587 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 9588 | |
| 9589 | /* Do nothing if PML buffer is empty */ |
| 9590 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 9591 | return; |
| 9592 | |
| 9593 | /* PML index always points to next available PML buffer entity */ |
| 9594 | if (pml_idx >= PML_ENTITY_NUM) |
| 9595 | pml_idx = 0; |
| 9596 | else |
| 9597 | pml_idx++; |
| 9598 | |
| 9599 | pml_buf = page_address(vmx->pml_pg); |
| 9600 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 9601 | u64 gpa; |
| 9602 | |
| 9603 | gpa = pml_buf[pml_idx]; |
| 9604 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9605 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9606 | } |
| 9607 | |
| 9608 | /* reset PML index */ |
| 9609 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 9610 | } |
| 9611 | |
| 9612 | /* |
| 9613 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 9614 | * Called before reporting dirty_bitmap to userspace. |
| 9615 | */ |
| 9616 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 9617 | { |
| 9618 | int i; |
| 9619 | struct kvm_vcpu *vcpu; |
| 9620 | /* |
| 9621 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 9622 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 9623 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 9624 | * buffer. |
| 9625 | */ |
| 9626 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 9627 | kvm_vcpu_kick(vcpu); |
| 9628 | } |
| 9629 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9630 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 9631 | { |
| 9632 | 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] | 9633 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9634 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 9635 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 9636 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 9637 | } |
| 9638 | |
| 9639 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 9640 | { |
| 9641 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 9642 | name, vmcs_read32(limit), |
| 9643 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 9644 | } |
| 9645 | |
| 9646 | static void dump_vmcs(void) |
| 9647 | { |
| 9648 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 9649 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 9650 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 9651 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 9652 | u32 secondary_exec_control = 0; |
| 9653 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 9654 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9655 | int i, n; |
| 9656 | |
| 9657 | if (cpu_has_secondary_exec_ctrls()) |
| 9658 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9659 | |
| 9660 | pr_err("*** Guest State ***\n"); |
| 9661 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9662 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 9663 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 9664 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9665 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 9666 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 9667 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 9668 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 9669 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9670 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 9671 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 9672 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 9673 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9674 | } |
| 9675 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 9676 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 9677 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 9678 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 9679 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9680 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 9681 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 9682 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 9683 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 9684 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 9685 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 9686 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 9687 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 9688 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 9689 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 9690 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 9691 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 9692 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 9693 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9694 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9695 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 9696 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 9697 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9698 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9699 | if (cpu_has_load_perf_global_ctrl && |
| 9700 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9701 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9702 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9703 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9704 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9705 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 9706 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 9707 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 9708 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 9709 | pr_err("InterruptStatus = %04x\n", |
| 9710 | vmcs_read16(GUEST_INTR_STATUS)); |
| 9711 | |
| 9712 | pr_err("*** Host State ***\n"); |
| 9713 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 9714 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 9715 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 9716 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 9717 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 9718 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 9719 | vmcs_read16(HOST_TR_SELECTOR)); |
| 9720 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 9721 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 9722 | vmcs_readl(HOST_TR_BASE)); |
| 9723 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 9724 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 9725 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 9726 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 9727 | vmcs_readl(HOST_CR4)); |
| 9728 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9729 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 9730 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 9731 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 9732 | 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] | 9733 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9734 | vmcs_read64(HOST_IA32_EFER), |
| 9735 | vmcs_read64(HOST_IA32_PAT)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9736 | if (cpu_has_load_perf_global_ctrl && |
| 9737 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9738 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9739 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9740 | |
| 9741 | pr_err("*** Control State ***\n"); |
| 9742 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 9743 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 9744 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 9745 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 9746 | vmcs_read32(EXCEPTION_BITMAP), |
| 9747 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 9748 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 9749 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9750 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 9751 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 9752 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 9753 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9754 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9755 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9756 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 9757 | pr_err(" reason=%08x qualification=%016lx\n", |
| 9758 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 9759 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 9760 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 9761 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9762 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 9763 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9764 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 9765 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9766 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 9767 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 9768 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 9769 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 9770 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9771 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9772 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 9773 | for (i = 0; i + 1 < n; i += 4) |
| 9774 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 9775 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 9776 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 9777 | if (i < n) |
| 9778 | pr_err("CR3 target%u=%016lx\n", |
| 9779 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 9780 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 9781 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 9782 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 9783 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 9784 | pr_err("Virtual processor ID = 0x%04x\n", |
| 9785 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 9786 | } |
| 9787 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9788 | /* |
| 9789 | * The guest has exited. See if we can fix it or if we need userspace |
| 9790 | * assistance. |
| 9791 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9792 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9793 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9794 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9795 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9796 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9797 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 9798 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 9799 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9800 | /* |
| 9801 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 9802 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 9803 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 9804 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 9805 | * flushed already. |
| 9806 | */ |
| 9807 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9808 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9809 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9810 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9811 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9812 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 9813 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9814 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 9815 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9816 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9817 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9818 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9819 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9820 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 9821 | = exit_reason; |
| 9822 | return 0; |
| 9823 | } |
| 9824 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9825 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9826 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9827 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9828 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 9829 | return 0; |
| 9830 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9831 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9832 | /* |
| 9833 | * Note: |
| 9834 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 9835 | * delivery event since it indicates guest is accessing MMIO. |
| 9836 | * The vm-exit can be triggered again after return to guest that |
| 9837 | * will cause infinite loop. |
| 9838 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 9839 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 9840 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 9841 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 9842 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9843 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 9844 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 9845 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9846 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9847 | vcpu->run->internal.data[0] = vectoring_info; |
| 9848 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9849 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 9850 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 9851 | vcpu->run->internal.ndata++; |
| 9852 | vcpu->run->internal.data[3] = |
| 9853 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 9854 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9855 | return 0; |
| 9856 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 9857 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9858 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9859 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 9860 | if (vmx_interrupt_allowed(vcpu)) { |
| 9861 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9862 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 9863 | vcpu->arch.nmi_pending) { |
| 9864 | /* |
| 9865 | * This CPU don't support us in finding the end of an |
| 9866 | * NMI-blocked window if the guest runs with IRQs |
| 9867 | * disabled. So we pull the trigger after 1 s of |
| 9868 | * futile waiting, but inform the user about this. |
| 9869 | */ |
| 9870 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 9871 | "state on VCPU %d after 1 s timeout\n", |
| 9872 | __func__, vcpu->vcpu_id); |
| 9873 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9874 | } |
| 9875 | } |
| 9876 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9877 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 9878 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9879 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9880 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 9881 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 9882 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 9883 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9884 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9885 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9886 | } |
| 9887 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9888 | 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] | 9889 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9890 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9891 | |
| 9892 | if (is_guest_mode(vcpu) && |
| 9893 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9894 | return; |
| 9895 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9896 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9897 | vmcs_write32(TPR_THRESHOLD, 0); |
| 9898 | return; |
| 9899 | } |
| 9900 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9901 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9902 | } |
| 9903 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9904 | static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9905 | { |
| 9906 | u32 sec_exec_control; |
| 9907 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9908 | if (!lapic_in_kernel(vcpu)) |
| 9909 | return; |
| 9910 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9911 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 9912 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9913 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9914 | return; |
| 9915 | } |
| 9916 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9917 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9918 | return; |
| 9919 | |
| 9920 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9921 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 9922 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9923 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9924 | switch (kvm_get_apic_mode(vcpu)) { |
| 9925 | case LAPIC_MODE_INVALID: |
| 9926 | WARN_ONCE(true, "Invalid local APIC state"); |
| 9927 | case LAPIC_MODE_DISABLED: |
| 9928 | break; |
| 9929 | case LAPIC_MODE_XAPIC: |
| 9930 | if (flexpriority_enabled) { |
| 9931 | sec_exec_control |= |
| 9932 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9933 | vmx_flush_tlb(vcpu, true); |
| 9934 | } |
| 9935 | break; |
| 9936 | case LAPIC_MODE_X2APIC: |
| 9937 | if (cpu_has_vmx_virtualize_x2apic_mode()) |
| 9938 | sec_exec_control |= |
| 9939 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 9940 | break; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9941 | } |
| 9942 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 9943 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9944 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9945 | } |
| 9946 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9947 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 9948 | { |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9949 | if (!is_guest_mode(vcpu)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9950 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 9951 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 9952 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9953 | } |
| 9954 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9955 | 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] | 9956 | { |
| 9957 | u16 status; |
| 9958 | u8 old; |
| 9959 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9960 | if (max_isr == -1) |
| 9961 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9962 | |
| 9963 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9964 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9965 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9966 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9967 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9968 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9969 | } |
| 9970 | } |
| 9971 | |
| 9972 | static void vmx_set_rvi(int vector) |
| 9973 | { |
| 9974 | u16 status; |
| 9975 | u8 old; |
| 9976 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 9977 | if (vector == -1) |
| 9978 | vector = 0; |
| 9979 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9980 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9981 | old = (u8)status & 0xff; |
| 9982 | if ((u8)vector != old) { |
| 9983 | status &= ~0xff; |
| 9984 | status |= (u8)vector; |
| 9985 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9986 | } |
| 9987 | } |
| 9988 | |
| 9989 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 9990 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9991 | /* |
| 9992 | * When running L2, updating RVI is only relevant when |
| 9993 | * vmcs12 virtual-interrupt-delivery enabled. |
| 9994 | * However, it can be enabled only when L1 also |
| 9995 | * intercepts external-interrupts and in that case |
| 9996 | * we should not update vmcs02 RVI but instead intercept |
| 9997 | * interrupt. Therefore, do nothing when running L2. |
| 9998 | */ |
| 9999 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 10000 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10001 | } |
| 10002 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10003 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10004 | { |
| 10005 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10006 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10007 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10008 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10009 | WARN_ON(!vcpu->arch.apicv_active); |
| 10010 | if (pi_test_on(&vmx->pi_desc)) { |
| 10011 | pi_clear_on(&vmx->pi_desc); |
| 10012 | /* |
| 10013 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 10014 | * But on x86 this is just a compiler barrier anyway. |
| 10015 | */ |
| 10016 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10017 | max_irr_updated = |
| 10018 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 10019 | |
| 10020 | /* |
| 10021 | * If we are running L2 and L1 has a new pending interrupt |
| 10022 | * which can be injected, we should re-evaluate |
| 10023 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 10024 | * If L1 intercepts external-interrupts, we should |
| 10025 | * exit from L2 to L1. Otherwise, interrupt should be |
| 10026 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10027 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 10028 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 10029 | if (nested_exit_on_intr(vcpu)) |
| 10030 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 10031 | else |
| 10032 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 10033 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10034 | } else { |
| 10035 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 10036 | } |
| 10037 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 10038 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10039 | } |
| 10040 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 10041 | 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] | 10042 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 10043 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 10044 | return; |
| 10045 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10046 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 10047 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 10048 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 10049 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 10050 | } |
| 10051 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 10052 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 10053 | { |
| 10054 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10055 | |
| 10056 | pi_clear_on(&vmx->pi_desc); |
| 10057 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 10058 | } |
| 10059 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10060 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10061 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10062 | u32 exit_intr_info = 0; |
| 10063 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 10064 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10065 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 10066 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 10067 | return; |
| 10068 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10069 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 10070 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10071 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10072 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 10073 | /* if exit due to PF check for async PF */ |
| 10074 | if (is_page_fault(exit_intr_info)) |
| 10075 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 10076 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10077 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10078 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 10079 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10080 | kvm_machine_check(); |
| 10081 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10082 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 10083 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 10084 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10085 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 10086 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 10087 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10088 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10089 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10090 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 10091 | { |
| 10092 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10093 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10094 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 10095 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 10096 | unsigned int vector; |
| 10097 | unsigned long entry; |
| 10098 | gate_desc *desc; |
| 10099 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10100 | #ifdef CONFIG_X86_64 |
| 10101 | unsigned long tmp; |
| 10102 | #endif |
| 10103 | |
| 10104 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 10105 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 10106 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10107 | asm volatile( |
| 10108 | #ifdef CONFIG_X86_64 |
| 10109 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 10110 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 10111 | "push $%c[ss]\n\t" |
| 10112 | "push %[sp]\n\t" |
| 10113 | #endif |
| 10114 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10115 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 10116 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10117 | : |
| 10118 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 10119 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10120 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 10121 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10122 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 10123 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10124 | [ss]"i"(__KERNEL_DS), |
| 10125 | [cs]"i"(__KERNEL_CS) |
| 10126 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 10127 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10128 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10129 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10130 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 10131 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 10132 | { |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 10133 | switch (index) { |
| 10134 | case MSR_IA32_SMBASE: |
| 10135 | /* |
| 10136 | * We cannot do SMM unless we can run the guest in big |
| 10137 | * real mode. |
| 10138 | */ |
| 10139 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 10140 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 10141 | /* This is AMD only. */ |
| 10142 | return false; |
| 10143 | default: |
| 10144 | return true; |
| 10145 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 10146 | } |
| 10147 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 10148 | static bool vmx_mpx_supported(void) |
| 10149 | { |
| 10150 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 10151 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 10152 | } |
| 10153 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 10154 | static bool vmx_xsaves_supported(void) |
| 10155 | { |
| 10156 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 10157 | SECONDARY_EXEC_XSAVES; |
| 10158 | } |
| 10159 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10160 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 10161 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 10162 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10163 | bool unblock_nmi; |
| 10164 | u8 vector; |
| 10165 | bool idtv_info_valid; |
| 10166 | |
| 10167 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10168 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 10169 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10170 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 10171 | return; |
| 10172 | /* |
| 10173 | * Can't use vmx->exit_intr_info since we're not sure what |
| 10174 | * the exit reason is. |
| 10175 | */ |
| 10176 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10177 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 10178 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 10179 | /* |
| 10180 | * SDM 3: 27.7.1.2 (September 2008) |
| 10181 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 10182 | * a guest IRET fault. |
| 10183 | * SDM 3: 23.2.2 (September 2008) |
| 10184 | * Bit 12 is undefined in any of the following cases: |
| 10185 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 10186 | * information field. |
| 10187 | * If the VM exit is due to a double fault. |
| 10188 | */ |
| 10189 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 10190 | vector != DF_VECTOR && !idtv_info_valid) |
| 10191 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 10192 | GUEST_INTR_STATE_NMI); |
| 10193 | else |
| 10194 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 10195 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 10196 | & GUEST_INTR_STATE_NMI); |
| 10197 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 10198 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 10199 | ktime_to_ns(ktime_sub(ktime_get(), |
| 10200 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10201 | } |
| 10202 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10203 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10204 | u32 idt_vectoring_info, |
| 10205 | int instr_len_field, |
| 10206 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10207 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10208 | u8 vector; |
| 10209 | int type; |
| 10210 | bool idtv_info_valid; |
| 10211 | |
| 10212 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10213 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10214 | vcpu->arch.nmi_injected = false; |
| 10215 | kvm_clear_exception_queue(vcpu); |
| 10216 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10217 | |
| 10218 | if (!idtv_info_valid) |
| 10219 | return; |
| 10220 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10221 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 10222 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10223 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 10224 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10225 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 10226 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10227 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10228 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10229 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 10230 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10231 | * Clear bit "block by NMI" before VM entry if a NMI |
| 10232 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10233 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10234 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10235 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10236 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10237 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10238 | /* fall through */ |
| 10239 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 10240 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10241 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10242 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 10243 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10244 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10245 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10246 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10247 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10248 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10249 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10250 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10251 | break; |
| 10252 | default: |
| 10253 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 10254 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10255 | } |
| 10256 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10257 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 10258 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10259 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10260 | VM_EXIT_INSTRUCTION_LEN, |
| 10261 | IDT_VECTORING_ERROR_CODE); |
| 10262 | } |
| 10263 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 10264 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 10265 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10266 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 10267 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 10268 | VM_ENTRY_INSTRUCTION_LEN, |
| 10269 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 10270 | |
| 10271 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 10272 | } |
| 10273 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10274 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 10275 | { |
| 10276 | int i, nr_msrs; |
| 10277 | struct perf_guest_switch_msr *msrs; |
| 10278 | |
| 10279 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 10280 | |
| 10281 | if (!msrs) |
| 10282 | return; |
| 10283 | |
| 10284 | for (i = 0; i < nr_msrs; i++) |
| 10285 | if (msrs[i].host == msrs[i].guest) |
| 10286 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 10287 | else |
| 10288 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 10289 | msrs[i].host); |
| 10290 | } |
| 10291 | |
Jiang Biao | 33365e7 | 2016-11-03 15:03:37 +0800 | [diff] [blame] | 10292 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10293 | { |
| 10294 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10295 | u64 tscl; |
| 10296 | u32 delta_tsc; |
| 10297 | |
| 10298 | if (vmx->hv_deadline_tsc == -1) |
| 10299 | return; |
| 10300 | |
| 10301 | tscl = rdtsc(); |
| 10302 | if (vmx->hv_deadline_tsc > tscl) |
| 10303 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 10304 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 10305 | cpu_preemption_timer_multi); |
| 10306 | else |
| 10307 | delta_tsc = 0; |
| 10308 | |
| 10309 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 10310 | } |
| 10311 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 10312 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10313 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10314 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10315 | unsigned long cr3, cr4, evmcs_rsp; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10316 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10317 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 10318 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10319 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 10320 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 10321 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10322 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 10323 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 10324 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10325 | return; |
| 10326 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 10327 | if (vmx->ple_window_dirty) { |
| 10328 | vmx->ple_window_dirty = false; |
| 10329 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 10330 | } |
| 10331 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10332 | if (vmx->nested.sync_shadow_vmcs) { |
| 10333 | copy_vmcs12_to_shadow(vmx); |
| 10334 | vmx->nested.sync_shadow_vmcs = false; |
| 10335 | } |
| 10336 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10337 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 10338 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 10339 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 10340 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 10341 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10342 | cr3 = __get_current_cr3_fast(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10343 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10344 | vmcs_writel(HOST_CR3, cr3); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10345 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10346 | } |
| 10347 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 10348 | cr4 = cr4_read_shadow(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10349 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 10350 | vmcs_writel(HOST_CR4, cr4); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10351 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 10352 | } |
| 10353 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10354 | /* When single-stepping over STI and MOV SS, we must clear the |
| 10355 | * corresponding interruptibility bits in the guest state. Otherwise |
| 10356 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 10357 | * exceptions being set, but that's not correct for the guest debugging |
| 10358 | * case. */ |
| 10359 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 10360 | vmx_set_interrupt_shadow(vcpu, 0); |
| 10361 | |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10362 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10363 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 10364 | vcpu->arch.pkru != vmx->host_pkru) |
| 10365 | __write_pkru(vcpu->arch.pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10366 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10367 | atomic_switch_perf_msrs(vmx); |
| 10368 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10369 | vmx_arm_hv_timer(vcpu); |
| 10370 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10371 | /* |
| 10372 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 10373 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 10374 | * is no need to worry about the conditional branch over the wrmsr |
| 10375 | * being speculatively taken. |
| 10376 | */ |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10377 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10378 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10379 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10380 | |
| 10381 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 10382 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 10383 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10384 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10385 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10386 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 10387 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 10388 | "push %%" _ASM_CX " \n\t" |
| 10389 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10390 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10391 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10392 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 10393 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 10394 | "jz 2f \n\t" |
| 10395 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 10396 | "jmp 1f \n\t" |
| 10397 | "2: \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10398 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10399 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 10400 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10401 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 10402 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 10403 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10404 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10405 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10406 | "3: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10407 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10408 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10409 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10410 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 10411 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 10412 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 10413 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 10414 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 10415 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10416 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10417 | "mov %c[r8](%0), %%r8 \n\t" |
| 10418 | "mov %c[r9](%0), %%r9 \n\t" |
| 10419 | "mov %c[r10](%0), %%r10 \n\t" |
| 10420 | "mov %c[r11](%0), %%r11 \n\t" |
| 10421 | "mov %c[r12](%0), %%r12 \n\t" |
| 10422 | "mov %c[r13](%0), %%r13 \n\t" |
| 10423 | "mov %c[r14](%0), %%r14 \n\t" |
| 10424 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10425 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10426 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10427 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10428 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10429 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10430 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10431 | "jmp 2f \n\t" |
| 10432 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 10433 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10434 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10435 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10436 | "pop %0 \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10437 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10438 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 10439 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 10440 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 10441 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 10442 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 10443 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 10444 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10445 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10446 | "mov %%r8, %c[r8](%0) \n\t" |
| 10447 | "mov %%r9, %c[r9](%0) \n\t" |
| 10448 | "mov %%r10, %c[r10](%0) \n\t" |
| 10449 | "mov %%r11, %c[r11](%0) \n\t" |
| 10450 | "mov %%r12, %c[r12](%0) \n\t" |
| 10451 | "mov %%r13, %c[r13](%0) \n\t" |
| 10452 | "mov %%r14, %c[r14](%0) \n\t" |
| 10453 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10454 | "xor %%r8d, %%r8d \n\t" |
| 10455 | "xor %%r9d, %%r9d \n\t" |
| 10456 | "xor %%r10d, %%r10d \n\t" |
| 10457 | "xor %%r11d, %%r11d \n\t" |
| 10458 | "xor %%r12d, %%r12d \n\t" |
| 10459 | "xor %%r13d, %%r13d \n\t" |
| 10460 | "xor %%r14d, %%r14d \n\t" |
| 10461 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10462 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10463 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 10464 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10465 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10466 | "xor %%eax, %%eax \n\t" |
| 10467 | "xor %%ebx, %%ebx \n\t" |
| 10468 | "xor %%esi, %%esi \n\t" |
| 10469 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10470 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10471 | ".pushsection .rodata \n\t" |
| 10472 | ".global vmx_return \n\t" |
| 10473 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 10474 | ".popsection" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10475 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10476 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10477 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10478 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10479 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 10480 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 10481 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 10482 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 10483 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 10484 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 10485 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10486 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10487 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 10488 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 10489 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 10490 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 10491 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 10492 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 10493 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 10494 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10495 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10496 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 10497 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10498 | : "cc", "memory" |
| 10499 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10500 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10501 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10502 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10503 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10504 | #endif |
| 10505 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10506 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10507 | /* |
| 10508 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 10509 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 10510 | * turn it off. This is much more efficient than blindly adding |
| 10511 | * it to the atomic save/restore list. Especially as the former |
| 10512 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 10513 | * |
| 10514 | * For non-nested case: |
| 10515 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 10516 | * save it. |
| 10517 | * |
| 10518 | * For nested case: |
| 10519 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 10520 | * save it. |
| 10521 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 10522 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 10523 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10524 | |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10525 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10526 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 10527 | /* Eliminate branch target predictions from guest mode */ |
| 10528 | vmexit_fill_RSB(); |
| 10529 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10530 | /* All fields are clean at this point */ |
| 10531 | if (static_branch_unlikely(&enable_evmcs)) |
| 10532 | current_evmcs->hv_clean_fields |= |
| 10533 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 10534 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10535 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 10536 | if (vmx->host_debugctlmsr) |
| 10537 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10538 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 10539 | #ifndef CONFIG_X86_64 |
| 10540 | /* |
| 10541 | * The sysexit path does not restore ds/es, so we must set them to |
| 10542 | * a reasonable value ourselves. |
| 10543 | * |
| 10544 | * We can't defer this to vmx_load_host_state() since that function |
| 10545 | * may be executed in interrupt context, which saves and restore segments |
| 10546 | * around it, nullifying its effect. |
| 10547 | */ |
| 10548 | loadsegment(ds, __USER_DS); |
| 10549 | loadsegment(es, __USER_DS); |
| 10550 | #endif |
| 10551 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 10552 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 10553 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10554 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 10555 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10556 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 10557 | vcpu->arch.regs_dirty = 0; |
| 10558 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10559 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10560 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 10561 | * back on host, so it is safe to read guest PKRU from current |
| 10562 | * XSAVE. |
| 10563 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10564 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10565 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 10566 | vcpu->arch.pkru = __read_pkru(); |
| 10567 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10568 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10569 | } |
| 10570 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10571 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 10572 | vmx->idt_vectoring_info = 0; |
| 10573 | |
| 10574 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 10575 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 10576 | return; |
| 10577 | |
| 10578 | vmx->loaded_vmcs->launched = 1; |
| 10579 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10580 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10581 | vmx_complete_atomic_exit(vmx); |
| 10582 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10583 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10584 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10585 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10586 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10587 | static struct kvm *vmx_vm_alloc(void) |
| 10588 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10589 | struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 10590 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10591 | } |
| 10592 | |
| 10593 | static void vmx_vm_free(struct kvm *kvm) |
| 10594 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10595 | vfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10596 | } |
| 10597 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10598 | 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] | 10599 | { |
| 10600 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10601 | int cpu; |
| 10602 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10603 | if (vmx->loaded_vmcs == vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10604 | return; |
| 10605 | |
| 10606 | cpu = get_cpu(); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10607 | vmx_vcpu_put(vcpu); |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame^] | 10608 | vmx->loaded_vmcs = vmcs; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10609 | vmx_vcpu_load(vcpu, cpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10610 | put_cpu(); |
| 10611 | } |
| 10612 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10613 | /* |
| 10614 | * Ensure that the current vmcs of the logical processor is the |
| 10615 | * vmcs01 of the vcpu before calling free_nested(). |
| 10616 | */ |
| 10617 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 10618 | { |
| 10619 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10620 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 10621 | vcpu_load(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10622 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10623 | free_nested(vmx); |
| 10624 | vcpu_put(vcpu); |
| 10625 | } |
| 10626 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10627 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 10628 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10629 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10630 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10631 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 10632 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10633 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10634 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10635 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10636 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10637 | kfree(vmx->guest_msrs); |
| 10638 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10639 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10640 | } |
| 10641 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10642 | 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] | 10643 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10644 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 10645 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10646 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10647 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10648 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10649 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10650 | return ERR_PTR(-ENOMEM); |
| 10651 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10652 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 10653 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10654 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 10655 | if (err) |
| 10656 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10657 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10658 | err = -ENOMEM; |
| 10659 | |
| 10660 | /* |
| 10661 | * If PML is turned on, failure on enabling PML just results in failure |
| 10662 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 10663 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 10664 | * for the guest, etc. |
| 10665 | */ |
| 10666 | if (enable_pml) { |
| 10667 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 10668 | if (!vmx->pml_pg) |
| 10669 | goto uninit_vcpu; |
| 10670 | } |
| 10671 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10672 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 10673 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 10674 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 10675 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10676 | if (!vmx->guest_msrs) |
| 10677 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10678 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10679 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 10680 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10681 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10682 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10683 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 10684 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 10685 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 10686 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 10687 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 10688 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 10689 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 10690 | vmx->msr_bitmap_mode = 0; |
| 10691 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10692 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10693 | cpu = get_cpu(); |
| 10694 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 10695 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 10696 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10697 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10698 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 10699 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 10700 | err = alloc_apic_access_page(kvm); |
| 10701 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 10702 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 10703 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10704 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 10705 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 10706 | err = init_rmode_identity_map(kvm); |
| 10707 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 10708 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 10709 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 10710 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10711 | if (nested) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10712 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
| 10713 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10714 | vmx->nested.vpid02 = allocate_vpid(); |
| 10715 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10716 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10717 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10718 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10719 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10720 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 10721 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 10722 | /* |
| 10723 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 10724 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 10725 | */ |
| 10726 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 10727 | vmx->pi_desc.sn = 1; |
| 10728 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10729 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10730 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10731 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10732 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 10733 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10734 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10735 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10736 | free_pml: |
| 10737 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10738 | uninit_vcpu: |
| 10739 | kvm_vcpu_uninit(&vmx->vcpu); |
| 10740 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10741 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10742 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10743 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10744 | } |
| 10745 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10746 | static int vmx_vm_init(struct kvm *kvm) |
| 10747 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 10748 | spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 10749 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10750 | if (!ple_gap) |
| 10751 | kvm->arch.pause_in_guest = true; |
| 10752 | return 0; |
| 10753 | } |
| 10754 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10755 | static void __init vmx_check_processor_compat(void *rtn) |
| 10756 | { |
| 10757 | struct vmcs_config vmcs_conf; |
| 10758 | |
| 10759 | *(int *)rtn = 0; |
| 10760 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 10761 | *(int *)rtn = -EIO; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 10762 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10763 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 10764 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 10765 | smp_processor_id()); |
| 10766 | *(int *)rtn = -EIO; |
| 10767 | } |
| 10768 | } |
| 10769 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10770 | 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] | 10771 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10772 | u8 cache; |
| 10773 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10774 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10775 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10776 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10777 | * 2. EPT with VT-d: |
| 10778 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10779 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10780 | * b. VT-d with snooping control feature: snooping control feature of |
| 10781 | * VT-d engine can guarantee the cache correctness. Just set it |
| 10782 | * 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] | 10783 | * 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] | 10784 | * consistent with host MTRR |
| 10785 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10786 | if (is_mmio) { |
| 10787 | cache = MTRR_TYPE_UNCACHABLE; |
| 10788 | goto exit; |
| 10789 | } |
| 10790 | |
| 10791 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10792 | ipat = VMX_EPT_IPAT_BIT; |
| 10793 | cache = MTRR_TYPE_WRBACK; |
| 10794 | goto exit; |
| 10795 | } |
| 10796 | |
| 10797 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 10798 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 10799 | 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] | 10800 | cache = MTRR_TYPE_WRBACK; |
| 10801 | else |
| 10802 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10803 | goto exit; |
| 10804 | } |
| 10805 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 10806 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10807 | |
| 10808 | exit: |
| 10809 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 10810 | } |
| 10811 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 10812 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10813 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 10814 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 10815 | return PT_DIRECTORY_LEVEL; |
| 10816 | else |
| 10817 | /* For shadow and EPT supported 1GB page */ |
| 10818 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10819 | } |
| 10820 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10821 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 10822 | { |
| 10823 | /* |
| 10824 | * These bits in the secondary execution controls field |
| 10825 | * are dynamic, the others are mostly based on the hypervisor |
| 10826 | * architecture and the guest's CPUID. Do not touch the |
| 10827 | * dynamic bits. |
| 10828 | */ |
| 10829 | u32 mask = |
| 10830 | SECONDARY_EXEC_SHADOW_VMCS | |
| 10831 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 10832 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 10833 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10834 | |
| 10835 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 10836 | |
| 10837 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 10838 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 10839 | } |
| 10840 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10841 | /* |
| 10842 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 10843 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 10844 | */ |
| 10845 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 10846 | { |
| 10847 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10848 | struct kvm_cpuid_entry2 *entry; |
| 10849 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10850 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 10851 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10852 | |
| 10853 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 10854 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10855 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10856 | } while (0) |
| 10857 | |
| 10858 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 10859 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 10860 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 10861 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 10862 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 10863 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 10864 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 10865 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 10866 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 10867 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 10868 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 10869 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 10870 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 10871 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 10872 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 10873 | |
| 10874 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 10875 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 10876 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 10877 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 10878 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 10879 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10880 | |
| 10881 | #undef cr4_fixed1_update |
| 10882 | } |
| 10883 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10884 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 10885 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10886 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10887 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 10888 | if (cpu_has_secondary_exec_ctrls()) { |
| 10889 | vmx_compute_secondary_exec_control(vmx); |
| 10890 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10891 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 10892 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10893 | if (nested_vmx_allowed(vcpu)) |
| 10894 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 10895 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 10896 | else |
| 10897 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 10898 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10899 | |
| 10900 | if (nested_vmx_allowed(vcpu)) |
| 10901 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10902 | } |
| 10903 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10904 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 10905 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 10906 | if (func == 1 && nested) |
| 10907 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10908 | } |
| 10909 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10910 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 10911 | struct x86_exception *fault) |
| 10912 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10913 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10914 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10915 | u32 exit_reason; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10916 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10917 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10918 | if (vmx->nested.pml_full) { |
| 10919 | exit_reason = EXIT_REASON_PML_FULL; |
| 10920 | vmx->nested.pml_full = false; |
| 10921 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 10922 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10923 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10924 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10925 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10926 | |
| 10927 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10928 | vmcs12->guest_physical_address = fault->address; |
| 10929 | } |
| 10930 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10931 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
| 10932 | { |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 10933 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10934 | } |
| 10935 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10936 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 10937 | |
| 10938 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 10939 | { |
| 10940 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 10941 | return get_vmcs12(vcpu)->ept_pointer; |
| 10942 | } |
| 10943 | |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10944 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10945 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10946 | WARN_ON(mmu_is_nested(vcpu)); |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10947 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10948 | return 1; |
| 10949 | |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10950 | kvm_init_shadow_ept_mmu(vcpu, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10951 | to_vmx(vcpu)->nested.msrs.ept_caps & |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10952 | VMX_EPT_EXECUTE_ONLY_BIT, |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 10953 | nested_ept_ad_enabled(vcpu), |
| 10954 | nested_ept_get_cr3(vcpu)); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10955 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 10956 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 10957 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 10958 | |
| 10959 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10960 | return 0; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10961 | } |
| 10962 | |
| 10963 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 10964 | { |
| 10965 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 10966 | } |
| 10967 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 10968 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 10969 | u16 error_code) |
| 10970 | { |
| 10971 | bool inequality, bit; |
| 10972 | |
| 10973 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 10974 | inequality = |
| 10975 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 10976 | vmcs12->page_fault_error_code_match; |
| 10977 | return inequality ^ bit; |
| 10978 | } |
| 10979 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10980 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 10981 | struct x86_exception *fault) |
| 10982 | { |
| 10983 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10984 | |
| 10985 | WARN_ON(!is_guest_mode(vcpu)); |
| 10986 | |
Wanpeng Li | 305d0ab | 2017-09-28 18:16:44 -0700 | [diff] [blame] | 10987 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 10988 | !to_vmx(vcpu)->nested.nested_run_pending) { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 10989 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 10990 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10991 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 10992 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 10993 | fault->address); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10994 | } else { |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10995 | kvm_inject_page_fault(vcpu, fault); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10996 | } |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10997 | } |
| 10998 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10999 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 11000 | struct vmcs12 *vmcs12); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11001 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 11002 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11003 | { |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 11004 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11005 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11006 | struct page *page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11007 | u64 hpa; |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11008 | |
| 11009 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11010 | /* |
| 11011 | * Translate L1 physical address to host physical |
| 11012 | * address for vmcs02. Keep the page pinned, so this |
| 11013 | * physical address remains valid. We keep a reference |
| 11014 | * to it so we can release it later. |
| 11015 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11016 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11017 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11018 | vmx->nested.apic_access_page = NULL; |
| 11019 | } |
| 11020 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11021 | /* |
| 11022 | * If translation failed, no matter: This feature asks |
| 11023 | * to exit when accessing the given address, and if it |
| 11024 | * can never be accessed, this feature won't do |
| 11025 | * anything anyway. |
| 11026 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11027 | if (!is_error_page(page)) { |
| 11028 | vmx->nested.apic_access_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11029 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 11030 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 11031 | } else { |
| 11032 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 11033 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 11034 | } |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11035 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11036 | |
| 11037 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11038 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11039 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11040 | vmx->nested.virtual_apic_page = NULL; |
| 11041 | } |
| 11042 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11043 | |
| 11044 | /* |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11045 | * If translation failed, VM entry will fail because |
| 11046 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 11047 | * Failing the vm entry is _not_ what the processor |
| 11048 | * does but it's basically the only possibility we |
| 11049 | * have. We could still enter the guest if CR8 load |
| 11050 | * exits are enabled, CR8 store exits are enabled, and |
| 11051 | * virtualize APIC access is disabled; in this case |
| 11052 | * the processor would never use the TPR shadow and we |
| 11053 | * could simply clear the bit from the execution |
| 11054 | * control. But such a configuration is useless, so |
| 11055 | * let's keep the code simple. |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11056 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11057 | if (!is_error_page(page)) { |
| 11058 | vmx->nested.virtual_apic_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11059 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 11060 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 11061 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11062 | } |
| 11063 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11064 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11065 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 11066 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11067 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11068 | vmx->nested.pi_desc_page = NULL; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11069 | } |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11070 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 11071 | if (is_error_page(page)) |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11072 | return; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11073 | vmx->nested.pi_desc_page = page; |
| 11074 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11075 | vmx->nested.pi_desc = |
| 11076 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 11077 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 11078 | (PAGE_SIZE - 1))); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11079 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 11080 | page_to_phys(vmx->nested.pi_desc_page) + |
| 11081 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 11082 | (PAGE_SIZE - 1))); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11083 | } |
Linus Torvalds | d4667ca | 2018-02-14 17:02:15 -0800 | [diff] [blame] | 11084 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
KarimAllah Ahmed | 3712caeb | 2018-02-10 23:39:26 +0000 | [diff] [blame] | 11085 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 11086 | CPU_BASED_USE_MSR_BITMAPS); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11087 | else |
| 11088 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 11089 | CPU_BASED_USE_MSR_BITMAPS); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11090 | } |
| 11091 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11092 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 11093 | { |
| 11094 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 11095 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11096 | |
| 11097 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 11098 | return; |
| 11099 | |
| 11100 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 11101 | * hrtimer_start does not guarantee this. */ |
| 11102 | if (preemption_timeout <= 1) { |
| 11103 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 11104 | return; |
| 11105 | } |
| 11106 | |
| 11107 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 11108 | preemption_timeout *= 1000000; |
| 11109 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 11110 | hrtimer_start(&vmx->nested.preemption_timer, |
| 11111 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 11112 | } |
| 11113 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 11114 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 11115 | struct vmcs12 *vmcs12) |
| 11116 | { |
| 11117 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 11118 | return 0; |
| 11119 | |
| 11120 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 11121 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 11122 | return -EINVAL; |
| 11123 | |
| 11124 | return 0; |
| 11125 | } |
| 11126 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11127 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 11128 | struct vmcs12 *vmcs12) |
| 11129 | { |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11130 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 11131 | return 0; |
| 11132 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 11133 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11134 | return -EINVAL; |
| 11135 | |
| 11136 | return 0; |
| 11137 | } |
| 11138 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 11139 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 11140 | struct vmcs12 *vmcs12) |
| 11141 | { |
| 11142 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 11143 | return 0; |
| 11144 | |
| 11145 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) |
| 11146 | return -EINVAL; |
| 11147 | |
| 11148 | return 0; |
| 11149 | } |
| 11150 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11151 | /* |
| 11152 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 11153 | * we do not use the hardware. |
| 11154 | */ |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11155 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 11156 | struct vmcs12 *vmcs12) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11157 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11158 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11159 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 11160 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 11161 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11162 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11163 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11164 | * |
| 11165 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 11166 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 11167 | * from the L12 MSR bitmap that is too permissive. |
| 11168 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 11169 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 11170 | * works properly because we only update the L01 MSR bitmap lazily. |
| 11171 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 11172 | * updated to reflect this when L1 (or its L2s) actually write to |
| 11173 | * the MSR. |
| 11174 | */ |
KarimAllah Ahmed | 206587a | 2018-02-10 23:39:25 +0000 | [diff] [blame] | 11175 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 11176 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11177 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11178 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 11179 | if (!cpu_has_vmx_msr_bitmap() || |
| 11180 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 11181 | return false; |
| 11182 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11183 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11184 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11185 | return false; |
| 11186 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11187 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 11188 | if (is_error_page(page)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11189 | return false; |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11190 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 11191 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11192 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 11193 | /* |
| 11194 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it |
| 11195 | * just lets the processor take the value from the virtual-APIC page; |
| 11196 | * take those 256 bits directly from the L1 bitmap. |
| 11197 | */ |
| 11198 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 11199 | unsigned word = msr / BITS_PER_LONG; |
| 11200 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 11201 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11202 | } |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11203 | } else { |
| 11204 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 11205 | unsigned word = msr / BITS_PER_LONG; |
| 11206 | msr_bitmap_l0[word] = ~0; |
| 11207 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 11208 | } |
| 11209 | } |
| 11210 | |
| 11211 | nested_vmx_disable_intercept_for_msr( |
| 11212 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11213 | X2APIC_MSR(APIC_TASKPRI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11214 | MSR_TYPE_W); |
| 11215 | |
| 11216 | if (nested_cpu_has_vid(vmcs12)) { |
| 11217 | nested_vmx_disable_intercept_for_msr( |
| 11218 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11219 | X2APIC_MSR(APIC_EOI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11220 | MSR_TYPE_W); |
| 11221 | nested_vmx_disable_intercept_for_msr( |
| 11222 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11223 | X2APIC_MSR(APIC_SELF_IPI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11224 | MSR_TYPE_W); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11225 | } |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11226 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11227 | if (spec_ctrl) |
| 11228 | nested_vmx_disable_intercept_for_msr( |
| 11229 | msr_bitmap_l1, msr_bitmap_l0, |
| 11230 | MSR_IA32_SPEC_CTRL, |
| 11231 | MSR_TYPE_R | MSR_TYPE_W); |
| 11232 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11233 | if (pred_cmd) |
| 11234 | nested_vmx_disable_intercept_for_msr( |
| 11235 | msr_bitmap_l1, msr_bitmap_l0, |
| 11236 | MSR_IA32_PRED_CMD, |
| 11237 | MSR_TYPE_W); |
| 11238 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11239 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11240 | kvm_release_page_clean(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11241 | |
| 11242 | return true; |
| 11243 | } |
| 11244 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 11245 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 11246 | struct vmcs12 *vmcs12) |
| 11247 | { |
| 11248 | struct vmcs12 *shadow; |
| 11249 | struct page *page; |
| 11250 | |
| 11251 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 11252 | vmcs12->vmcs_link_pointer == -1ull) |
| 11253 | return; |
| 11254 | |
| 11255 | shadow = get_shadow_vmcs12(vcpu); |
| 11256 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 11257 | |
| 11258 | memcpy(shadow, kmap(page), VMCS12_SIZE); |
| 11259 | |
| 11260 | kunmap(page); |
| 11261 | kvm_release_page_clean(page); |
| 11262 | } |
| 11263 | |
| 11264 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 11265 | struct vmcs12 *vmcs12) |
| 11266 | { |
| 11267 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11268 | |
| 11269 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 11270 | vmcs12->vmcs_link_pointer == -1ull) |
| 11271 | return; |
| 11272 | |
| 11273 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 11274 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 11275 | } |
| 11276 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 11277 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 11278 | struct vmcs12 *vmcs12) |
| 11279 | { |
| 11280 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 11281 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) |
| 11282 | return -EINVAL; |
| 11283 | else |
| 11284 | return 0; |
| 11285 | } |
| 11286 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11287 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 11288 | struct vmcs12 *vmcs12) |
| 11289 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11290 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11291 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11292 | !nested_cpu_has_vid(vmcs12) && |
| 11293 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11294 | return 0; |
| 11295 | |
| 11296 | /* |
| 11297 | * If virtualize x2apic mode is enabled, |
| 11298 | * virtualize apic access must be disabled. |
| 11299 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11300 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 11301 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11302 | return -EINVAL; |
| 11303 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11304 | /* |
| 11305 | * If virtual interrupt delivery is enabled, |
| 11306 | * we must exit on external interrupts. |
| 11307 | */ |
| 11308 | if (nested_cpu_has_vid(vmcs12) && |
| 11309 | !nested_exit_on_intr(vcpu)) |
| 11310 | return -EINVAL; |
| 11311 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11312 | /* |
| 11313 | * bits 15:8 should be zero in posted_intr_nv, |
| 11314 | * the descriptor address has been already checked |
| 11315 | * in nested_get_vmcs12_pages. |
| 11316 | */ |
| 11317 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 11318 | (!nested_cpu_has_vid(vmcs12) || |
| 11319 | !nested_exit_intr_ack_set(vcpu) || |
| 11320 | vmcs12->posted_intr_nv & 0xff00)) |
| 11321 | return -EINVAL; |
| 11322 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11323 | /* tpr shadow is needed by all apicv features. */ |
| 11324 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 11325 | return -EINVAL; |
| 11326 | |
| 11327 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11328 | } |
| 11329 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11330 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 11331 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11332 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11333 | { |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 11334 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11335 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11336 | u64 count, addr; |
| 11337 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 11338 | if (vmcs12_read_any(vmcs12, count_field, &count) || |
| 11339 | vmcs12_read_any(vmcs12, addr_field, &addr)) { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11340 | WARN_ON(1); |
| 11341 | return -EINVAL; |
| 11342 | } |
| 11343 | if (count == 0) |
| 11344 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11345 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11346 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 11347 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11348 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11349 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 11350 | addr_field, maxphyaddr, count, addr); |
| 11351 | return -EINVAL; |
| 11352 | } |
| 11353 | return 0; |
| 11354 | } |
| 11355 | |
| 11356 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 11357 | struct vmcs12 *vmcs12) |
| 11358 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11359 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 11360 | vmcs12->vm_exit_msr_store_count == 0 && |
| 11361 | vmcs12->vm_entry_msr_load_count == 0) |
| 11362 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11363 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11364 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11365 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11366 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11367 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11368 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11369 | return -EINVAL; |
| 11370 | return 0; |
| 11371 | } |
| 11372 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11373 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 11374 | struct vmcs12 *vmcs12) |
| 11375 | { |
| 11376 | u64 address = vmcs12->pml_address; |
| 11377 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 11378 | |
| 11379 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { |
| 11380 | if (!nested_cpu_has_ept(vmcs12) || |
| 11381 | !IS_ALIGNED(address, 4096) || |
| 11382 | address >> maxphyaddr) |
| 11383 | return -EINVAL; |
| 11384 | } |
| 11385 | |
| 11386 | return 0; |
| 11387 | } |
| 11388 | |
Liran Alon | a8a7c02 | 2018-06-23 02:35:06 +0300 | [diff] [blame] | 11389 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 11390 | struct vmcs12 *vmcs12) |
| 11391 | { |
| 11392 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 11393 | return 0; |
| 11394 | |
| 11395 | if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) || |
| 11396 | !page_address_valid(vcpu, vmcs12->vmwrite_bitmap)) |
| 11397 | return -EINVAL; |
| 11398 | |
| 11399 | return 0; |
| 11400 | } |
| 11401 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11402 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 11403 | struct vmx_msr_entry *e) |
| 11404 | { |
| 11405 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 11406 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11407 | return -EINVAL; |
| 11408 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 11409 | e->index == MSR_IA32_UCODE_REV) |
| 11410 | return -EINVAL; |
| 11411 | if (e->reserved != 0) |
| 11412 | return -EINVAL; |
| 11413 | return 0; |
| 11414 | } |
| 11415 | |
| 11416 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 11417 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11418 | { |
| 11419 | if (e->index == MSR_FS_BASE || |
| 11420 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11421 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 11422 | nested_vmx_msr_check_common(vcpu, e)) |
| 11423 | return -EINVAL; |
| 11424 | return 0; |
| 11425 | } |
| 11426 | |
| 11427 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 11428 | struct vmx_msr_entry *e) |
| 11429 | { |
| 11430 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 11431 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11432 | return -EINVAL; |
| 11433 | return 0; |
| 11434 | } |
| 11435 | |
| 11436 | /* |
| 11437 | * Load guest's/host's msr at nested entry/exit. |
| 11438 | * return 0 for success, entry index for failure. |
| 11439 | */ |
| 11440 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11441 | { |
| 11442 | u32 i; |
| 11443 | struct vmx_msr_entry e; |
| 11444 | struct msr_data msr; |
| 11445 | |
| 11446 | msr.host_initiated = false; |
| 11447 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11448 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 11449 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11450 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11451 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11452 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11453 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11454 | } |
| 11455 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11456 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11457 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11458 | __func__, i, e.index, e.reserved); |
| 11459 | goto fail; |
| 11460 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11461 | msr.index = e.index; |
| 11462 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11463 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11464 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11465 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 11466 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11467 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11468 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11469 | } |
| 11470 | return 0; |
| 11471 | fail: |
| 11472 | return i + 1; |
| 11473 | } |
| 11474 | |
| 11475 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11476 | { |
| 11477 | u32 i; |
| 11478 | struct vmx_msr_entry e; |
| 11479 | |
| 11480 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11481 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11482 | if (kvm_vcpu_read_guest(vcpu, |
| 11483 | gpa + i * sizeof(e), |
| 11484 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11485 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11486 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11487 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11488 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11489 | } |
| 11490 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11491 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11492 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11493 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11494 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11495 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11496 | msr_info.host_initiated = false; |
| 11497 | msr_info.index = e.index; |
| 11498 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11499 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11500 | "%s cannot read MSR (%u, 0x%x)\n", |
| 11501 | __func__, i, e.index); |
| 11502 | return -EINVAL; |
| 11503 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11504 | if (kvm_vcpu_write_guest(vcpu, |
| 11505 | gpa + i * sizeof(e) + |
| 11506 | offsetof(struct vmx_msr_entry, value), |
| 11507 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11508 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11509 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11510 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11511 | return -EINVAL; |
| 11512 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11513 | } |
| 11514 | return 0; |
| 11515 | } |
| 11516 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11517 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 11518 | { |
| 11519 | unsigned long invalid_mask; |
| 11520 | |
| 11521 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 11522 | return (val & invalid_mask) == 0; |
| 11523 | } |
| 11524 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11525 | /* |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11526 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 11527 | * emulating VM entry into a guest with EPT enabled. |
| 11528 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11529 | * is assigned to entry_failure_code on failure. |
| 11530 | */ |
| 11531 | 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] | 11532 | u32 *entry_failure_code) |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11533 | { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11534 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11535 | if (!nested_cr3_valid(vcpu, cr3)) { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11536 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11537 | return 1; |
| 11538 | } |
| 11539 | |
| 11540 | /* |
| 11541 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 11542 | * must not be dereferenced. |
| 11543 | */ |
| 11544 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 11545 | !nested_ept) { |
| 11546 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 11547 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 11548 | return 1; |
| 11549 | } |
| 11550 | } |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11551 | } |
| 11552 | |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 11553 | if (!nested_ept) |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 11554 | kvm_mmu_new_cr3(vcpu, cr3, false); |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 11555 | |
| 11556 | vcpu->arch.cr3 = cr3; |
| 11557 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 11558 | |
| 11559 | kvm_init_mmu(vcpu, false); |
| 11560 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11561 | return 0; |
| 11562 | } |
| 11563 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11564 | static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11565 | { |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11566 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11567 | |
| 11568 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 11569 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 11570 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 11571 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 11572 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 11573 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 11574 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 11575 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 11576 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 11577 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 11578 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 11579 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 11580 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 11581 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 11582 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 11583 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 11584 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 11585 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 11586 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 11587 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 11588 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 11589 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 11590 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 11591 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 11592 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 11593 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 11594 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 11595 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 11596 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 11597 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 11598 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11599 | |
| 11600 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 11601 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 11602 | vmcs12->guest_pending_dbg_exceptions); |
| 11603 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 11604 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 11605 | |
| 11606 | if (nested_cpu_has_xsaves(vmcs12)) |
| 11607 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 11608 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 11609 | |
| 11610 | if (cpu_has_vmx_posted_intr()) |
| 11611 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 11612 | |
| 11613 | /* |
| 11614 | * Whether page-faults are trapped is determined by a combination of |
| 11615 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 11616 | * If enable_ept, L0 doesn't care about page faults and we should |
| 11617 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 11618 | * care about (at least some) page faults, and because it is not easy |
| 11619 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 11620 | * to exit on each and every L2 page fault. This is done by setting |
| 11621 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 11622 | * Note that below we don't need special code to set EB.PF beyond the |
| 11623 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 11624 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 11625 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 11626 | */ |
| 11627 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 11628 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 11629 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 11630 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 11631 | |
| 11632 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 11633 | if (cpu_has_vmx_vmfunc()) |
| 11634 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 11635 | |
| 11636 | if (cpu_has_vmx_apicv()) { |
| 11637 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 11638 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 11639 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 11640 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 11641 | } |
| 11642 | |
| 11643 | /* |
| 11644 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 11645 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 11646 | * Other fields are different per CPU, and will be set later when |
| 11647 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 11648 | */ |
| 11649 | vmx_set_constant_host_state(vmx); |
| 11650 | |
| 11651 | /* |
| 11652 | * Set the MSR load/store lists to match L0's settings. |
| 11653 | */ |
| 11654 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 11655 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11656 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
| 11657 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11658 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
| 11659 | |
| 11660 | set_cr4_guest_host_mask(vmx); |
| 11661 | |
| 11662 | if (vmx_mpx_supported()) |
| 11663 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 11664 | |
| 11665 | if (enable_vpid) { |
| 11666 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 11667 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 11668 | else |
| 11669 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 11670 | } |
| 11671 | |
| 11672 | /* |
| 11673 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 11674 | */ |
| 11675 | if (enable_ept) { |
| 11676 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 11677 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 11678 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 11679 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 11680 | } |
Radim Krčmář | 80132f4 | 2018-02-02 18:26:58 +0100 | [diff] [blame] | 11681 | |
| 11682 | if (cpu_has_vmx_msr_bitmap()) |
| 11683 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11684 | } |
| 11685 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11686 | /* |
| 11687 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 11688 | * 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] | 11689 | * 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] | 11690 | * guest in a way that will both be appropriate to L1's requests, and our |
| 11691 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 11692 | * function also has additional necessary side-effects, like setting various |
| 11693 | * vcpu->arch fields. |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11694 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11695 | * is assigned to entry_failure_code on failure. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11696 | */ |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11697 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11698 | u32 *entry_failure_code) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11699 | { |
| 11700 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11701 | u32 exec_control, vmcs12_exec_ctrl; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11702 | |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11703 | if (vmx->nested.dirty_vmcs12) { |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11704 | prepare_vmcs02_full(vcpu, vmcs12); |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11705 | vmx->nested.dirty_vmcs12 = false; |
| 11706 | } |
| 11707 | |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11708 | /* |
| 11709 | * First, the fields that are shadowed. This must be kept in sync |
| 11710 | * with vmx_shadow_fields.h. |
| 11711 | */ |
| 11712 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11713 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11714 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11715 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11716 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 11717 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11718 | |
| 11719 | /* |
| 11720 | * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR, |
| 11721 | * HOST_FS_BASE, HOST_GS_BASE. |
| 11722 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11723 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11724 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11725 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 11726 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 11727 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 11728 | } else { |
| 11729 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 11730 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 11731 | } |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11732 | if (vmx->nested.nested_run_pending) { |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11733 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 11734 | vmcs12->vm_entry_intr_info_field); |
| 11735 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 11736 | vmcs12->vm_entry_exception_error_code); |
| 11737 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 11738 | vmcs12->vm_entry_instruction_len); |
| 11739 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 11740 | vmcs12->guest_interruptibility_info); |
Wanpeng Li | 2d6144e | 2017-07-25 03:40:46 -0700 | [diff] [blame] | 11741 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 11742 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11743 | } else { |
| 11744 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 11745 | } |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 11746 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11747 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11748 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11749 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11750 | /* Preemption timer setting is only taken from vmcs01. */ |
| 11751 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11752 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 11753 | if (vmx->hv_deadline_tsc == -1) |
| 11754 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11755 | |
| 11756 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11757 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11758 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 11759 | vmx->nested.pi_pending = false; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11760 | } else { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11761 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11762 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11763 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11764 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11765 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11766 | vmx->nested.preemption_timer_expired = false; |
| 11767 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 11768 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 11769 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11770 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 11771 | exec_control = vmx->secondary_exec_control; |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 11772 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11773 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11774 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 11775 | SECONDARY_EXEC_ENABLE_INVPCID | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 11776 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 11777 | SECONDARY_EXEC_XSAVES | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11778 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11779 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 11780 | SECONDARY_EXEC_ENABLE_VMFUNC); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11781 | if (nested_cpu_has(vmcs12, |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11782 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 11783 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 11784 | ~SECONDARY_EXEC_ENABLE_PML; |
| 11785 | exec_control |= vmcs12_exec_ctrl; |
| 11786 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11787 | |
Liran Alon | 32c7acf | 2018-06-23 02:35:11 +0300 | [diff] [blame] | 11788 | /* VMCS shadowing for L2 is emulated for now */ |
| 11789 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 11790 | |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11791 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11792 | vmcs_write16(GUEST_INTR_STATUS, |
| 11793 | vmcs12->guest_intr_status); |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11794 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11795 | /* |
| 11796 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 11797 | * nested_get_vmcs12_pages will either fix it up or |
| 11798 | * remove the VM execution control. |
| 11799 | */ |
| 11800 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 11801 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 11802 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11803 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 11804 | } |
| 11805 | |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 11806 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11807 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 11808 | * entry, but only if the current (host) sp changed from the value |
| 11809 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 11810 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 11811 | * here we just force the write to happen on entry. |
| 11812 | */ |
| 11813 | vmx->host_rsp = 0; |
| 11814 | |
| 11815 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 11816 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 11817 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 11818 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 11819 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11820 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11821 | /* |
| 11822 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 11823 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 11824 | * will result in a VM entry failure. |
| 11825 | */ |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11826 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11827 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11828 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 51aa68e | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 11829 | } else { |
| 11830 | #ifdef CONFIG_X86_64 |
| 11831 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 11832 | CPU_BASED_CR8_STORE_EXITING; |
| 11833 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11834 | } |
| 11835 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11836 | /* |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 11837 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 11838 | * for I/O port accesses. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11839 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11840 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 11841 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 11842 | |
| 11843 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 11844 | |
| 11845 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 11846 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 11847 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 11848 | */ |
| 11849 | update_exception_bitmap(vcpu); |
| 11850 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 11851 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 11852 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11853 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 11854 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 11855 | * bits are further modified by vmx_set_efer() below. |
| 11856 | */ |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11857 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11858 | |
| 11859 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 11860 | * emulated by vmx_set_efer(), below. |
| 11861 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 11862 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11863 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 11864 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11865 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 11866 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11867 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11868 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11869 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11870 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11871 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11872 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11873 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11874 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11875 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 11876 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 11877 | if (kvm_has_tsc_control) |
| 11878 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11879 | |
| 11880 | if (enable_vpid) { |
| 11881 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11882 | * There is no direct mapping between vpid02 and vpid12, the |
| 11883 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 11884 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 11885 | * The vpid12 is allocated by L1 for L2, so it will not |
| 11886 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 11887 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11888 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11889 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11890 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 11891 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
Liran Alon | 6bce30c | 2018-05-22 17:16:12 +0300 | [diff] [blame] | 11892 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11893 | } |
| 11894 | } else { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 11895 | vmx_flush_tlb(vcpu, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11896 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11897 | } |
| 11898 | |
Ladi Prosek | 1fb883b | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 11899 | if (enable_pml) { |
| 11900 | /* |
| 11901 | * Conceptually we want to copy the PML address and index from |
| 11902 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 11903 | * since we always flush the log on each vmexit, this happens |
| 11904 | * to be equivalent to simply resetting the fields in vmcs02. |
| 11905 | */ |
| 11906 | ASSERT(vmx->pml_pg); |
| 11907 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 11908 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 11909 | } |
| 11910 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11911 | if (nested_cpu_has_ept(vmcs12)) { |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 11912 | if (nested_ept_init_mmu_context(vcpu)) { |
| 11913 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11914 | return 1; |
| 11915 | } |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 11916 | } else if (nested_cpu_has2(vmcs12, |
| 11917 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 11918 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11919 | } |
| 11920 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11921 | /* |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11922 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 11923 | * bits which we consider mandatory enabled. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11924 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 11925 | * the specifications by L1; It's not enough to take |
| 11926 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 11927 | * have more bits than L1 expected. |
| 11928 | */ |
| 11929 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 11930 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 11931 | |
| 11932 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 11933 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 11934 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11935 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11936 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
David Matlack | 5a6a974 | 2016-11-29 18:14:10 -0800 | [diff] [blame] | 11937 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 11938 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 11939 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 11940 | else |
| 11941 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 11942 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 11943 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 11944 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11945 | /* |
| 11946 | * Guest state is invalid and unrestricted guest is disabled, |
| 11947 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 11948 | * Fail the VMEntry. |
| 11949 | */ |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11950 | if (vmx->emulation_required) { |
| 11951 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11952 | return 1; |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11953 | } |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11954 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11955 | /* Shadow page tables on either EPT or shadow page tables. */ |
Ladi Prosek | 7ad658b | 2017-03-23 07:18:08 +0100 | [diff] [blame] | 11956 | 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] | 11957 | entry_failure_code)) |
| 11958 | return 1; |
Ladi Prosek | 7ca29de | 2016-11-30 16:03:08 +0100 | [diff] [blame] | 11959 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11960 | if (!enable_ept) |
| 11961 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 11962 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11963 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 11964 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11965 | return 0; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11966 | } |
| 11967 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11968 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 11969 | { |
| 11970 | if (!nested_cpu_has_nmi_exiting(vmcs12) && |
| 11971 | nested_cpu_has_virtual_nmis(vmcs12)) |
| 11972 | return -EINVAL; |
| 11973 | |
| 11974 | if (!nested_cpu_has_virtual_nmis(vmcs12) && |
| 11975 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) |
| 11976 | return -EINVAL; |
| 11977 | |
| 11978 | return 0; |
| 11979 | } |
| 11980 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11981 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11982 | { |
| 11983 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11984 | |
| 11985 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 11986 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
| 11987 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11988 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 11989 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
| 11990 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11991 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11992 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
| 11993 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11994 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 11995 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
| 11996 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11997 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 11998 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
| 11999 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12000 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12001 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
| 12002 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12003 | |
| 12004 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
| 12005 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12006 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12007 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
| 12008 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12009 | |
Liran Alon | a8a7c02 | 2018-06-23 02:35:06 +0300 | [diff] [blame] | 12010 | if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12)) |
| 12011 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12012 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12013 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12014 | vmx->nested.msrs.procbased_ctls_low, |
| 12015 | vmx->nested.msrs.procbased_ctls_high) || |
Jim Mattson | 2e5b0bd | 2017-05-04 11:51:58 -0700 | [diff] [blame] | 12016 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 12017 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12018 | vmx->nested.msrs.secondary_ctls_low, |
| 12019 | vmx->nested.msrs.secondary_ctls_high)) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12020 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12021 | vmx->nested.msrs.pinbased_ctls_low, |
| 12022 | vmx->nested.msrs.pinbased_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12023 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12024 | vmx->nested.msrs.exit_ctls_low, |
| 12025 | vmx->nested.msrs.exit_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12026 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12027 | vmx->nested.msrs.entry_ctls_low, |
| 12028 | vmx->nested.msrs.entry_ctls_high)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12029 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12030 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 12031 | if (nested_vmx_check_nmi_controls(vmcs12)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12032 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12033 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 12034 | if (nested_cpu_has_vmfunc(vmcs12)) { |
| 12035 | if (vmcs12->vm_function_control & |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12036 | ~vmx->nested.msrs.vmfunc_controls) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 12037 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12038 | |
| 12039 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
| 12040 | if (!nested_cpu_has_ept(vmcs12) || |
| 12041 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) |
| 12042 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12043 | } |
| 12044 | } |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 12045 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 12046 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
| 12047 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12048 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12049 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 12050 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 12051 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
| 12052 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 12053 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 12054 | /* |
| 12055 | * From the Intel SDM, volume 3: |
| 12056 | * Fields relevant to VM-entry event injection must be set properly. |
| 12057 | * These fields are the VM-entry interruption-information field, the |
| 12058 | * VM-entry exception error code, and the VM-entry instruction length. |
| 12059 | */ |
| 12060 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 12061 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 12062 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 12063 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 12064 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 12065 | bool should_have_error_code; |
| 12066 | bool urg = nested_cpu_has2(vmcs12, |
| 12067 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 12068 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 12069 | |
| 12070 | /* VM-entry interruption-info field: interruption type */ |
| 12071 | if (intr_type == INTR_TYPE_RESERVED || |
| 12072 | (intr_type == INTR_TYPE_OTHER_EVENT && |
| 12073 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
| 12074 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12075 | |
| 12076 | /* VM-entry interruption-info field: vector */ |
| 12077 | if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 12078 | (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 12079 | (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
| 12080 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12081 | |
| 12082 | /* VM-entry interruption-info field: deliver error code */ |
| 12083 | should_have_error_code = |
| 12084 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 12085 | x86_exception_has_error_code(vector); |
| 12086 | if (has_error_code != should_have_error_code) |
| 12087 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12088 | |
| 12089 | /* VM-entry exception error code */ |
| 12090 | if (has_error_code && |
| 12091 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 15)) |
| 12092 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12093 | |
| 12094 | /* VM-entry interruption-info field: reserved bits */ |
| 12095 | if (intr_info & INTR_INFO_RESVD_BITS_MASK) |
| 12096 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12097 | |
| 12098 | /* VM-entry instruction length */ |
| 12099 | switch (intr_type) { |
| 12100 | case INTR_TYPE_SOFT_EXCEPTION: |
| 12101 | case INTR_TYPE_SOFT_INTR: |
| 12102 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
| 12103 | if ((vmcs12->vm_entry_instruction_len > 15) || |
| 12104 | (vmcs12->vm_entry_instruction_len == 0 && |
| 12105 | !nested_cpu_has_zero_length_injection(vcpu))) |
| 12106 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12107 | } |
| 12108 | } |
| 12109 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12110 | return 0; |
| 12111 | } |
| 12112 | |
Liran Alon | f145d90 | 2018-06-23 02:35:07 +0300 | [diff] [blame] | 12113 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 12114 | struct vmcs12 *vmcs12) |
| 12115 | { |
| 12116 | int r; |
| 12117 | struct page *page; |
| 12118 | struct vmcs12 *shadow; |
| 12119 | |
| 12120 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 12121 | return 0; |
| 12122 | |
| 12123 | if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)) |
| 12124 | return -EINVAL; |
| 12125 | |
| 12126 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 12127 | if (is_error_page(page)) |
| 12128 | return -EINVAL; |
| 12129 | |
| 12130 | r = 0; |
| 12131 | shadow = kmap(page); |
| 12132 | if (shadow->hdr.revision_id != VMCS12_REVISION || |
| 12133 | shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)) |
| 12134 | r = -EINVAL; |
| 12135 | kunmap(page); |
| 12136 | kvm_release_page_clean(page); |
| 12137 | return r; |
| 12138 | } |
| 12139 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12140 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12141 | u32 *exit_qual) |
| 12142 | { |
| 12143 | bool ia32e; |
| 12144 | |
| 12145 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 12146 | |
| 12147 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 12148 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 12149 | return 1; |
| 12150 | |
Liran Alon | f145d90 | 2018-06-23 02:35:07 +0300 | [diff] [blame] | 12151 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12152 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 12153 | return 1; |
| 12154 | } |
| 12155 | |
| 12156 | /* |
| 12157 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 12158 | * are performed on the field for the IA32_EFER MSR: |
| 12159 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 12160 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 12161 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 12162 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 12163 | * CR0.PG) is 1. |
| 12164 | */ |
| 12165 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 12166 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 12167 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 12168 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 12169 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 12170 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 12171 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 12172 | return 1; |
| 12173 | } |
| 12174 | |
| 12175 | /* |
| 12176 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 12177 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 12178 | * the values of the LMA and LME bits in the field must each be that of |
| 12179 | * the host address-space size VM-exit control. |
| 12180 | */ |
| 12181 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 12182 | ia32e = (vmcs12->vm_exit_controls & |
| 12183 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 12184 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 12185 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 12186 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
| 12187 | return 1; |
| 12188 | } |
| 12189 | |
Wanpeng Li | f1b026a | 2017-11-05 16:54:48 -0800 | [diff] [blame] | 12190 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
| 12191 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || |
| 12192 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) |
| 12193 | return 1; |
| 12194 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12195 | return 0; |
| 12196 | } |
| 12197 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12198 | /* |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 12199 | * If exit_qual is NULL, this is being called from state restore (either RSM |
| 12200 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12201 | */ |
| 12202 | 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] | 12203 | { |
| 12204 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12205 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12206 | bool from_vmentry = !!exit_qual; |
| 12207 | u32 dummy_exit_qual; |
| 12208 | int r = 0; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12209 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12210 | enter_guest_mode(vcpu); |
| 12211 | |
| 12212 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 12213 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 12214 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 12215 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12216 | vmx_segment_cache_clear(vmx); |
| 12217 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12218 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12219 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; |
| 12220 | |
| 12221 | r = EXIT_REASON_INVALID_STATE; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12222 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual)) |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12223 | goto fail; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12224 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12225 | if (from_vmentry) { |
| 12226 | nested_get_vmcs12_pages(vcpu); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12227 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12228 | r = EXIT_REASON_MSR_LOAD_FAIL; |
| 12229 | *exit_qual = nested_vmx_load_msr(vcpu, |
| 12230 | vmcs12->vm_entry_msr_load_addr, |
| 12231 | vmcs12->vm_entry_msr_load_count); |
| 12232 | if (*exit_qual) |
| 12233 | goto fail; |
| 12234 | } else { |
| 12235 | /* |
| 12236 | * The MMU is not initialized to point at the right entities yet and |
| 12237 | * "get pages" would need to read data from the guest (i.e. we will |
| 12238 | * need to perform gpa to hpa translation). Request a call |
| 12239 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 12240 | * have already been set at vmentry time and should not be reset. |
| 12241 | */ |
| 12242 | kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu); |
| 12243 | } |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12244 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12245 | /* |
| 12246 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 12247 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 12248 | * returned as far as L1 is concerned. It will only return (and set |
| 12249 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 12250 | */ |
| 12251 | return 0; |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12252 | |
| 12253 | fail: |
| 12254 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12255 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 12256 | leave_guest_mode(vcpu); |
| 12257 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12258 | return r; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12259 | } |
| 12260 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12261 | /* |
| 12262 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 12263 | * for running an L2 nested guest. |
| 12264 | */ |
| 12265 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 12266 | { |
| 12267 | struct vmcs12 *vmcs12; |
| 12268 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 12269 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12270 | u32 exit_qual; |
| 12271 | int ret; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12272 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12273 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12274 | return 1; |
| 12275 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12276 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 12277 | goto out; |
| 12278 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12279 | vmcs12 = get_vmcs12(vcpu); |
| 12280 | |
Liran Alon | a6192d4 | 2018-06-23 02:35:04 +0300 | [diff] [blame] | 12281 | /* |
| 12282 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 12283 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 12284 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 12285 | * VM-instruction error field. |
| 12286 | */ |
| 12287 | if (vmcs12->hdr.shadow_vmcs) { |
| 12288 | nested_vmx_failInvalid(vcpu); |
| 12289 | goto out; |
| 12290 | } |
| 12291 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12292 | if (enable_shadow_vmcs) |
| 12293 | copy_shadow_to_vmcs12(vmx); |
| 12294 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12295 | /* |
| 12296 | * The nested entry process starts with enforcing various prerequisites |
| 12297 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 12298 | * they fail: As the SDM explains, some conditions should cause the |
| 12299 | * instruction to fail, while others will cause the instruction to seem |
| 12300 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 12301 | * To speed up the normal (success) code path, we should avoid checking |
| 12302 | * for misconfigurations which will anyway be caught by the processor |
| 12303 | * when using the merged vmcs02. |
| 12304 | */ |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 12305 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
| 12306 | nested_vmx_failValid(vcpu, |
| 12307 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
| 12308 | goto out; |
| 12309 | } |
| 12310 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12311 | if (vmcs12->launch_state == launch) { |
| 12312 | nested_vmx_failValid(vcpu, |
| 12313 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 12314 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12315 | goto out; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12316 | } |
| 12317 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12318 | ret = check_vmentry_prereqs(vcpu, vmcs12); |
| 12319 | if (ret) { |
| 12320 | nested_vmx_failValid(vcpu, ret); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12321 | goto out; |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 12322 | } |
| 12323 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12324 | /* |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12325 | * After this point, the trap flag no longer triggers a singlestep trap |
| 12326 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. |
| 12327 | * This is not 100% correct; for performance reasons, we delegate most |
| 12328 | * of the checks on host state to the processor. If those fail, |
| 12329 | * the singlestep trap is missed. |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12330 | */ |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12331 | skip_emulated_instruction(vcpu); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12332 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12333 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); |
| 12334 | if (ret) { |
| 12335 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 12336 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 12337 | return 1; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12338 | } |
| 12339 | |
| 12340 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12341 | * We're finally done with prerequisite checking, and can start with |
| 12342 | * the nested entry. |
| 12343 | */ |
| 12344 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12345 | vmx->nested.nested_run_pending = 1; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12346 | ret = enter_vmx_non_root_mode(vcpu, &exit_qual); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12347 | if (ret) { |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12348 | nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12349 | vmx->nested.nested_run_pending = 0; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12350 | return 1; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12351 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12352 | |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 12353 | /* |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 12354 | * Must happen outside of enter_vmx_non_root_mode() as it will |
| 12355 | * also be used as part of restoring nVMX state for |
| 12356 | * snapshot restore (migration). |
| 12357 | * |
| 12358 | * In this flow, it is assumed that vmcs12 cache was |
| 12359 | * trasferred as part of captured nVMX state and should |
| 12360 | * therefore not be read from guest memory (which may not |
| 12361 | * exist on destination host yet). |
| 12362 | */ |
| 12363 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 12364 | |
| 12365 | /* |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 12366 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken |
| 12367 | * by event injection, halt vcpu. |
| 12368 | */ |
| 12369 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12370 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) { |
| 12371 | vmx->nested.nested_run_pending = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 12372 | return kvm_vcpu_halt(vcpu); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12373 | } |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12374 | return 1; |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12375 | |
| 12376 | out: |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 12377 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12378 | } |
| 12379 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12380 | /* |
| 12381 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 12382 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 12383 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 12384 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 12385 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 12386 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 12387 | * didn't trap the bit, because if L1 did, so would L0). |
| 12388 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 12389 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 12390 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 12391 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 12392 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 12393 | * changed these bits, and therefore they need to be updated, but L0 |
| 12394 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 12395 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 12396 | */ |
| 12397 | static inline unsigned long |
| 12398 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12399 | { |
| 12400 | return |
| 12401 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 12402 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 12403 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 12404 | vcpu->arch.cr0_guest_owned_bits)); |
| 12405 | } |
| 12406 | |
| 12407 | static inline unsigned long |
| 12408 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12409 | { |
| 12410 | return |
| 12411 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 12412 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 12413 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 12414 | vcpu->arch.cr4_guest_owned_bits)); |
| 12415 | } |
| 12416 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12417 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 12418 | struct vmcs12 *vmcs12) |
| 12419 | { |
| 12420 | u32 idt_vectoring; |
| 12421 | unsigned int nr; |
| 12422 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 12423 | if (vcpu->arch.exception.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12424 | nr = vcpu->arch.exception.nr; |
| 12425 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 12426 | |
| 12427 | if (kvm_exception_is_soft(nr)) { |
| 12428 | vmcs12->vm_exit_instruction_len = |
| 12429 | vcpu->arch.event_exit_inst_len; |
| 12430 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 12431 | } else |
| 12432 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 12433 | |
| 12434 | if (vcpu->arch.exception.has_error_code) { |
| 12435 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 12436 | vmcs12->idt_vectoring_error_code = |
| 12437 | vcpu->arch.exception.error_code; |
| 12438 | } |
| 12439 | |
| 12440 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 12441 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12442 | vmcs12->idt_vectoring_info_field = |
| 12443 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 12444 | } else if (vcpu->arch.interrupt.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12445 | nr = vcpu->arch.interrupt.nr; |
| 12446 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 12447 | |
| 12448 | if (vcpu->arch.interrupt.soft) { |
| 12449 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 12450 | vmcs12->vm_entry_instruction_len = |
| 12451 | vcpu->arch.event_exit_inst_len; |
| 12452 | } else |
| 12453 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 12454 | |
| 12455 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 12456 | } |
| 12457 | } |
| 12458 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12459 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 12460 | { |
| 12461 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12462 | unsigned long exit_qual; |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12463 | bool block_nested_events = |
| 12464 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 12465 | |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12466 | if (vcpu->arch.exception.pending && |
| 12467 | nested_vmx_check_exception(vcpu, &exit_qual)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12468 | if (block_nested_events) |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12469 | return -EBUSY; |
| 12470 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12471 | return 0; |
| 12472 | } |
| 12473 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12474 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 12475 | vmx->nested.preemption_timer_expired) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12476 | if (block_nested_events) |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12477 | return -EBUSY; |
| 12478 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 12479 | return 0; |
| 12480 | } |
| 12481 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12482 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12483 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12484 | return -EBUSY; |
| 12485 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 12486 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 12487 | INTR_INFO_VALID_MASK, 0); |
| 12488 | /* |
| 12489 | * The NMI-triggered VM exit counts as injection: |
| 12490 | * clear this one and block further NMIs. |
| 12491 | */ |
| 12492 | vcpu->arch.nmi_pending = 0; |
| 12493 | vmx_set_nmi_mask(vcpu, true); |
| 12494 | return 0; |
| 12495 | } |
| 12496 | |
| 12497 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 12498 | nested_exit_on_intr(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12499 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12500 | return -EBUSY; |
| 12501 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12502 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12503 | } |
| 12504 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 12505 | vmx_complete_nested_posted_interrupt(vcpu); |
| 12506 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12507 | } |
| 12508 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12509 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 12510 | { |
| 12511 | ktime_t remaining = |
| 12512 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 12513 | u64 value; |
| 12514 | |
| 12515 | if (ktime_to_ns(remaining) <= 0) |
| 12516 | return 0; |
| 12517 | |
| 12518 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 12519 | do_div(value, 1000000); |
| 12520 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 12521 | } |
| 12522 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12523 | /* |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12524 | * Update the guest state fields of vmcs12 to reflect changes that |
| 12525 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 12526 | * VM-entry controls is also updated, since this is really a guest |
| 12527 | * state bit.) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12528 | */ |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12529 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12530 | { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12531 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 12532 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 12533 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12534 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 12535 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 12536 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 12537 | |
| 12538 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 12539 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 12540 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 12541 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 12542 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 12543 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 12544 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 12545 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 12546 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 12547 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 12548 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 12549 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 12550 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 12551 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 12552 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 12553 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 12554 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 12555 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 12556 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 12557 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 12558 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 12559 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 12560 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 12561 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 12562 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 12563 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 12564 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 12565 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 12566 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 12567 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 12568 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 12569 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 12570 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 12571 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 12572 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 12573 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 12574 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12575 | vmcs12->guest_interruptibility_info = |
| 12576 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 12577 | vmcs12->guest_pending_dbg_exceptions = |
| 12578 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 12579 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 12580 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 12581 | else |
| 12582 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12583 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12584 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 12585 | if (vmcs12->vm_exit_controls & |
| 12586 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 12587 | vmcs12->vmx_preemption_timer_value = |
| 12588 | vmx_get_preemption_timer_value(vcpu); |
| 12589 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 12590 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 12591 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12592 | /* |
| 12593 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 12594 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 12595 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 12596 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 12597 | * |
| 12598 | * Additionally, restore L2's PDPTR to vmcs12. |
| 12599 | */ |
| 12600 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 12601 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12602 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 12603 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 12604 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 12605 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 12606 | } |
| 12607 | |
Jim Mattson | d281e13 | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 12608 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 12609 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12610 | if (nested_cpu_has_vid(vmcs12)) |
| 12611 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 12612 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12613 | vmcs12->vm_entry_controls = |
| 12614 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 12615 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12616 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 12617 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 12618 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 12619 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 12620 | } |
| 12621 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12622 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 12623 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 12624 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12625 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 12626 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 12627 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12628 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 12629 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 12630 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 12631 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12632 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12633 | } |
| 12634 | |
| 12635 | /* |
| 12636 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 12637 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 12638 | * and this function updates it to reflect the changes to the guest state while |
| 12639 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 12640 | * without going back to L1), and to reflect the exit reason. |
| 12641 | * Note that we do not have to copy here all VMCS fields, just those that |
| 12642 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 12643 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 12644 | * which already writes to vmcs12 directly. |
| 12645 | */ |
| 12646 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12647 | u32 exit_reason, u32 exit_intr_info, |
| 12648 | unsigned long exit_qualification) |
| 12649 | { |
| 12650 | /* update guest state fields: */ |
| 12651 | sync_vmcs12(vcpu, vmcs12); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12652 | |
| 12653 | /* update exit information fields: */ |
| 12654 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12655 | vmcs12->vm_exit_reason = exit_reason; |
| 12656 | vmcs12->exit_qualification = exit_qualification; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12657 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 12658 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12659 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12660 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 12661 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 12662 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12663 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
Jim Mattson | 7cdc2d6 | 2017-07-06 16:33:05 -0700 | [diff] [blame] | 12664 | vmcs12->launch_state = 1; |
| 12665 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12666 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 12667 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12668 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12669 | |
| 12670 | /* |
| 12671 | * Transfer the event that L0 or L1 may wanted to inject into |
| 12672 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 12673 | */ |
| 12674 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 12675 | } |
| 12676 | |
| 12677 | /* |
| 12678 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 12679 | * preserved above and would only end up incorrectly in L1. |
| 12680 | */ |
| 12681 | vcpu->arch.nmi_injected = false; |
| 12682 | kvm_clear_exception_queue(vcpu); |
| 12683 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12684 | } |
| 12685 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12686 | static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu, |
| 12687 | struct vmcs12 *vmcs12) |
| 12688 | { |
| 12689 | u32 entry_failure_code; |
| 12690 | |
| 12691 | nested_ept_uninit_mmu_context(vcpu); |
| 12692 | |
| 12693 | /* |
| 12694 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 12695 | * couldn't have changed. |
| 12696 | */ |
| 12697 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 12698 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 12699 | |
| 12700 | if (!enable_ept) |
| 12701 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 12702 | } |
| 12703 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12704 | /* |
| 12705 | * A part of what we need to when the nested L2 guest exits and we want to |
| 12706 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 12707 | * in vmcs12. |
| 12708 | * This function is to be called not only on normal nested exit, but also on |
| 12709 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 12710 | * Failures During or After Loading Guest State"). |
| 12711 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 12712 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 12713 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 12714 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12715 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12716 | struct kvm_segment seg; |
| 12717 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12718 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 12719 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 12720 | 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] | 12721 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 12722 | else |
| 12723 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 12724 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 12725 | |
| 12726 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 12727 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 12728 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12729 | /* |
| 12730 | * 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] | 12731 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 12732 | * |
| 12733 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 12734 | * (KVM doesn't change it); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12735 | */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12736 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Jan Kiszka | 9e3e4dbf | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 12737 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12738 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12739 | /* 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] | 12740 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 8eb3f87 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 12741 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12742 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12743 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 12744 | |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 12745 | /* |
| 12746 | * If vmcs01 don't use VPID, CPU flushes TLB on every |
| 12747 | * VMEntry/VMExit. Thus, no need to flush TLB. |
| 12748 | * |
| 12749 | * If vmcs12 uses VPID, TLB entries populated by L2 are |
| 12750 | * tagged with vmx->nested.vpid02 while L1 entries are tagged |
| 12751 | * with vmx->vpid. Thus, no need to flush TLB. |
| 12752 | * |
| 12753 | * Therefore, flush TLB only in case vmcs01 uses VPID and |
| 12754 | * vmcs12 don't use VPID as in this case L1 & L2 TLB entries |
| 12755 | * are both tagged with vmx->vpid. |
| 12756 | */ |
| 12757 | if (enable_vpid && |
| 12758 | !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 12759 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12760 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12761 | |
| 12762 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 12763 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 12764 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 12765 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 12766 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 21f2d55 | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 12767 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 12768 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12769 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12770 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 12771 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 12772 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 12773 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12774 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12775 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12776 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 12777 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12778 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 12779 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 12780 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12781 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12782 | /* Set L1 segment info according to Intel SDM |
| 12783 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 12784 | seg = (struct kvm_segment) { |
| 12785 | .base = 0, |
| 12786 | .limit = 0xFFFFFFFF, |
| 12787 | .selector = vmcs12->host_cs_selector, |
| 12788 | .type = 11, |
| 12789 | .present = 1, |
| 12790 | .s = 1, |
| 12791 | .g = 1 |
| 12792 | }; |
| 12793 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 12794 | seg.l = 1; |
| 12795 | else |
| 12796 | seg.db = 1; |
| 12797 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 12798 | seg = (struct kvm_segment) { |
| 12799 | .base = 0, |
| 12800 | .limit = 0xFFFFFFFF, |
| 12801 | .type = 3, |
| 12802 | .present = 1, |
| 12803 | .s = 1, |
| 12804 | .db = 1, |
| 12805 | .g = 1 |
| 12806 | }; |
| 12807 | seg.selector = vmcs12->host_ds_selector; |
| 12808 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 12809 | seg.selector = vmcs12->host_es_selector; |
| 12810 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 12811 | seg.selector = vmcs12->host_ss_selector; |
| 12812 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 12813 | seg.selector = vmcs12->host_fs_selector; |
| 12814 | seg.base = vmcs12->host_fs_base; |
| 12815 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 12816 | seg.selector = vmcs12->host_gs_selector; |
| 12817 | seg.base = vmcs12->host_gs_base; |
| 12818 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 12819 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 12820 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12821 | .limit = 0x67, |
| 12822 | .selector = vmcs12->host_tr_selector, |
| 12823 | .type = 11, |
| 12824 | .present = 1 |
| 12825 | }; |
| 12826 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 12827 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12828 | kvm_set_dr(vcpu, 7, 0x400); |
| 12829 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12830 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12831 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 12832 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12833 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12834 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 12835 | vmcs12->vm_exit_msr_load_count)) |
| 12836 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12837 | } |
| 12838 | |
| 12839 | /* |
| 12840 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 12841 | * and modify vmcs12 to make it see what it would expect to see there if |
| 12842 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 12843 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12844 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 12845 | u32 exit_intr_info, |
| 12846 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12847 | { |
| 12848 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12849 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12850 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12851 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 12852 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 12853 | |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12854 | /* |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12855 | * The only expected VM-instruction error is "VM entry with |
| 12856 | * invalid control field(s)." Anything else indicates a |
| 12857 | * problem with L0. |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12858 | */ |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12859 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 12860 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); |
| 12861 | |
| 12862 | leave_guest_mode(vcpu); |
| 12863 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12864 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12865 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 12866 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12867 | if (likely(!vmx->fail)) { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12868 | if (exit_reason == -1) |
| 12869 | sync_vmcs12(vcpu, vmcs12); |
| 12870 | else |
| 12871 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 12872 | exit_qualification); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12873 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 12874 | /* |
| 12875 | * Must happen outside of sync_vmcs12() as it will |
| 12876 | * also be used to capture vmcs12 cache as part of |
| 12877 | * capturing nVMX state for snapshot (migration). |
| 12878 | * |
| 12879 | * Otherwise, this flush will dirty guest memory at a |
| 12880 | * point it is already assumed by user-space to be |
| 12881 | * immutable. |
| 12882 | */ |
| 12883 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
| 12884 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12885 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 12886 | vmcs12->vm_exit_msr_store_count)) |
| 12887 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 12888 | } |
| 12889 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12890 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 12891 | vm_entry_controls_reset_shadow(vmx); |
| 12892 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 12893 | vmx_segment_cache_clear(vmx); |
| 12894 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12895 | /* Update any VMCS fields that might have changed while L2 ran */ |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 12896 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 12897 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 12898 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12899 | if (vmx->hv_deadline_tsc == -1) |
| 12900 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12901 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12902 | else |
| 12903 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12904 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 12905 | if (kvm_has_tsc_control) |
| 12906 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12907 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 12908 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 12909 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 12910 | vmx_set_virtual_apic_mode(vcpu); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 12911 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 12912 | nested_cpu_has2(vmcs12, |
| 12913 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 12914 | vmx_flush_tlb(vcpu, true); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 12915 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12916 | |
| 12917 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 12918 | vmx->host_rsp = 0; |
| 12919 | |
| 12920 | /* Unpin physical memory we referred to in vmcs02 */ |
| 12921 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12922 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12923 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12924 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12925 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12926 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12927 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12928 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12929 | if (vmx->nested.pi_desc_page) { |
| 12930 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12931 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12932 | vmx->nested.pi_desc_page = NULL; |
| 12933 | vmx->nested.pi_desc = NULL; |
| 12934 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12935 | |
| 12936 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12937 | * We are now running in L2, mmu_notifier will force to reload the |
| 12938 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 12939 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 12940 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12941 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12942 | if (enable_shadow_vmcs && exit_reason != -1) |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12943 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12944 | |
| 12945 | /* in case we halted in L2 */ |
| 12946 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12947 | |
| 12948 | if (likely(!vmx->fail)) { |
| 12949 | /* |
| 12950 | * TODO: SDM says that with acknowledge interrupt on |
| 12951 | * exit, bit 31 of the VM-exit interrupt information |
| 12952 | * (valid interrupt) is always set to 1 on |
| 12953 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't |
| 12954 | * need kvm_cpu_has_interrupt(). See the commit |
| 12955 | * message for details. |
| 12956 | */ |
| 12957 | if (nested_exit_intr_ack_set(vcpu) && |
| 12958 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 12959 | kvm_cpu_has_interrupt(vcpu)) { |
| 12960 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 12961 | WARN_ON(irq < 0); |
| 12962 | vmcs12->vm_exit_intr_info = irq | |
| 12963 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 12964 | } |
| 12965 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12966 | if (exit_reason != -1) |
| 12967 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 12968 | vmcs12->exit_qualification, |
| 12969 | vmcs12->idt_vectoring_info_field, |
| 12970 | vmcs12->vm_exit_intr_info, |
| 12971 | vmcs12->vm_exit_intr_error_code, |
| 12972 | KVM_ISA_VMX); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12973 | |
| 12974 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12975 | |
| 12976 | return; |
| 12977 | } |
| 12978 | |
| 12979 | /* |
| 12980 | * After an early L2 VM-entry failure, we're now back |
| 12981 | * in L1 which thinks it just finished a VMLAUNCH or |
| 12982 | * VMRESUME instruction, so we need to set the failure |
| 12983 | * flag and the VM-instruction error field of the VMCS |
| 12984 | * accordingly. |
| 12985 | */ |
| 12986 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12987 | |
| 12988 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
| 12989 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12990 | /* |
| 12991 | * The emulated instruction was already skipped in |
| 12992 | * nested_vmx_run, but the updated RIP was never |
| 12993 | * written back to the vmcs01. |
| 12994 | */ |
| 12995 | skip_emulated_instruction(vcpu); |
| 12996 | vmx->fail = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12997 | } |
| 12998 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12999 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 13000 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 13001 | */ |
| 13002 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 13003 | { |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 13004 | if (is_guest_mode(vcpu)) { |
| 13005 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 13006 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 13007 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 13008 | free_nested(to_vmx(vcpu)); |
| 13009 | } |
| 13010 | |
| 13011 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13012 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 13013 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 13014 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 13015 | * It should only be called before L2 actually succeeded to run, and when |
| 13016 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 13017 | */ |
| 13018 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 13019 | struct vmcs12 *vmcs12, |
| 13020 | u32 reason, unsigned long qualification) |
| 13021 | { |
| 13022 | load_vmcs12_host_state(vcpu, vmcs12); |
| 13023 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 13024 | vmcs12->exit_qualification = qualification; |
| 13025 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 13026 | if (enable_shadow_vmcs) |
| 13027 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13028 | } |
| 13029 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13030 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 13031 | struct x86_instruction_info *info, |
| 13032 | enum x86_intercept_stage stage) |
| 13033 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 13034 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 13035 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 13036 | |
| 13037 | /* |
| 13038 | * RDPID causes #UD if disabled through secondary execution controls. |
| 13039 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 13040 | */ |
| 13041 | if (info->intercept == x86_intercept_rdtscp && |
| 13042 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 13043 | ctxt->exception.vector = UD_VECTOR; |
| 13044 | ctxt->exception.error_code_valid = false; |
| 13045 | return X86EMUL_PROPAGATE_FAULT; |
| 13046 | } |
| 13047 | |
| 13048 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13049 | return X86EMUL_CONTINUE; |
| 13050 | } |
| 13051 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13052 | #ifdef CONFIG_X86_64 |
| 13053 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 13054 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 13055 | u64 divisor, u64 *result) |
| 13056 | { |
| 13057 | u64 low = a << shift, high = a >> (64 - shift); |
| 13058 | |
| 13059 | /* To avoid the overflow on divq */ |
| 13060 | if (high >= divisor) |
| 13061 | return 1; |
| 13062 | |
| 13063 | /* Low hold the result, high hold rem which is discarded */ |
| 13064 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 13065 | "rm" (divisor), "0" (low), "1" (high)); |
| 13066 | *result = low; |
| 13067 | |
| 13068 | return 0; |
| 13069 | } |
| 13070 | |
| 13071 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 13072 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 13073 | struct vcpu_vmx *vmx; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 13074 | u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 13075 | |
| 13076 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 13077 | return -EOPNOTSUPP; |
| 13078 | |
| 13079 | vmx = to_vmx(vcpu); |
| 13080 | tscl = rdtsc(); |
| 13081 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 13082 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 13083 | lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); |
| 13084 | |
| 13085 | if (delta_tsc > lapic_timer_advance_cycles) |
| 13086 | delta_tsc -= lapic_timer_advance_cycles; |
| 13087 | else |
| 13088 | delta_tsc = 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13089 | |
| 13090 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 13091 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 13092 | u64_shl_div_u64(delta_tsc, |
| 13093 | kvm_tsc_scaling_ratio_frac_bits, |
| 13094 | vcpu->arch.tsc_scaling_ratio, |
| 13095 | &delta_tsc)) |
| 13096 | return -ERANGE; |
| 13097 | |
| 13098 | /* |
| 13099 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 13100 | * we can't use the preemption timer. |
| 13101 | * It's possible that it fits on later vmentries, but checking |
| 13102 | * on every vmentry is costly so we just use an hrtimer. |
| 13103 | */ |
| 13104 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 13105 | return -ERANGE; |
| 13106 | |
| 13107 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 13108 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 13109 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 13110 | |
| 13111 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13112 | } |
| 13113 | |
| 13114 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 13115 | { |
| 13116 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13117 | vmx->hv_deadline_tsc = -1; |
| 13118 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 13119 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 13120 | } |
| 13121 | #endif |
| 13122 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 13123 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13124 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13125 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 13126 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13127 | } |
| 13128 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13129 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 13130 | struct kvm_memory_slot *slot) |
| 13131 | { |
| 13132 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 13133 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 13134 | } |
| 13135 | |
| 13136 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 13137 | struct kvm_memory_slot *slot) |
| 13138 | { |
| 13139 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 13140 | } |
| 13141 | |
| 13142 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 13143 | { |
| 13144 | kvm_flush_pml_buffers(kvm); |
| 13145 | } |
| 13146 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13147 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 13148 | { |
| 13149 | struct vmcs12 *vmcs12; |
| 13150 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13151 | gpa_t gpa; |
| 13152 | struct page *page = NULL; |
| 13153 | u64 *pml_address; |
| 13154 | |
| 13155 | if (is_guest_mode(vcpu)) { |
| 13156 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 13157 | |
| 13158 | /* |
| 13159 | * Check if PML is enabled for the nested guest. |
| 13160 | * Whether eptp bit 6 is set is already checked |
| 13161 | * as part of A/D emulation. |
| 13162 | */ |
| 13163 | vmcs12 = get_vmcs12(vcpu); |
| 13164 | if (!nested_cpu_has_pml(vmcs12)) |
| 13165 | return 0; |
| 13166 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 13167 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13168 | vmx->nested.pml_full = true; |
| 13169 | return 1; |
| 13170 | } |
| 13171 | |
| 13172 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 13173 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 13174 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 13175 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13176 | return 0; |
| 13177 | |
| 13178 | pml_address = kmap(page); |
| 13179 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 13180 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 13181 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13182 | } |
| 13183 | |
| 13184 | return 0; |
| 13185 | } |
| 13186 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13187 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 13188 | struct kvm_memory_slot *memslot, |
| 13189 | gfn_t offset, unsigned long mask) |
| 13190 | { |
| 13191 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 13192 | } |
| 13193 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13194 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 13195 | { |
| 13196 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 13197 | struct pi_desc old, new; |
| 13198 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13199 | |
| 13200 | do { |
| 13201 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13202 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 13203 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13204 | |
| 13205 | dest = cpu_physical_id(vcpu->cpu); |
| 13206 | |
| 13207 | if (x2apic_enabled()) |
| 13208 | new.ndst = dest; |
| 13209 | else |
| 13210 | new.ndst = (dest << 8) & 0xFF00; |
| 13211 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13212 | /* set 'NV' to 'notification vector' */ |
| 13213 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 13214 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 13215 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13216 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13217 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 13218 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13219 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13220 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13221 | vcpu->pre_pcpu = -1; |
| 13222 | } |
| 13223 | } |
| 13224 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13225 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13226 | * This routine does the following things for vCPU which is going |
| 13227 | * to be blocked if VT-d PI is enabled. |
| 13228 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 13229 | * we can find the right vCPU to wake up. |
| 13230 | * - Change the Posted-interrupt descriptor as below: |
| 13231 | * 'NDST' <-- vcpu->pre_pcpu |
| 13232 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 13233 | * - If 'ON' is set during this process, which means at least one |
| 13234 | * interrupt is posted for this vCPU, we cannot block it, in |
| 13235 | * this case, return 1, otherwise, return 0. |
| 13236 | * |
| 13237 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13238 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13239 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13240 | unsigned int dest; |
| 13241 | struct pi_desc old, new; |
| 13242 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 13243 | |
| 13244 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 13245 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 13246 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13247 | return 0; |
| 13248 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13249 | WARN_ON(irqs_disabled()); |
| 13250 | local_irq_disable(); |
| 13251 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 13252 | vcpu->pre_pcpu = vcpu->cpu; |
| 13253 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 13254 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 13255 | &per_cpu(blocked_vcpu_on_cpu, |
| 13256 | vcpu->pre_pcpu)); |
| 13257 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 13258 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13259 | |
| 13260 | do { |
| 13261 | old.control = new.control = pi_desc->control; |
| 13262 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13263 | WARN((pi_desc->sn == 1), |
| 13264 | "Warning: SN field of posted-interrupts " |
| 13265 | "is set before blocking\n"); |
| 13266 | |
| 13267 | /* |
| 13268 | * Since vCPU can be preempted during this process, |
| 13269 | * vcpu->cpu could be different with pre_pcpu, we |
| 13270 | * need to set pre_pcpu as the destination of wakeup |
| 13271 | * notification event, then we can find the right vCPU |
| 13272 | * to wakeup in wakeup handler if interrupts happen |
| 13273 | * when the vCPU is in blocked state. |
| 13274 | */ |
| 13275 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 13276 | |
| 13277 | if (x2apic_enabled()) |
| 13278 | new.ndst = dest; |
| 13279 | else |
| 13280 | new.ndst = (dest << 8) & 0xFF00; |
| 13281 | |
| 13282 | /* set 'NV' to 'wakeup vector' */ |
| 13283 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 13284 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 13285 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13286 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13287 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 13288 | if (pi_test_on(pi_desc) == 1) |
| 13289 | __pi_post_block(vcpu); |
| 13290 | |
| 13291 | local_irq_enable(); |
| 13292 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13293 | } |
| 13294 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13295 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 13296 | { |
| 13297 | if (pi_pre_block(vcpu)) |
| 13298 | return 1; |
| 13299 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13300 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 13301 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 13302 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13303 | return 0; |
| 13304 | } |
| 13305 | |
| 13306 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13307 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13308 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13309 | return; |
| 13310 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13311 | WARN_ON(irqs_disabled()); |
| 13312 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13313 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13314 | local_irq_enable(); |
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 void vmx_post_block(struct kvm_vcpu *vcpu) |
| 13318 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13319 | if (kvm_x86_ops->set_hv_timer) |
| 13320 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 13321 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13322 | pi_post_block(vcpu); |
| 13323 | } |
| 13324 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13325 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13326 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 13327 | * |
| 13328 | * @kvm: kvm |
| 13329 | * @host_irq: host irq of the interrupt |
| 13330 | * @guest_irq: gsi of the interrupt |
| 13331 | * @set: set or unset PI |
| 13332 | * returns 0 on success, < 0 on failure |
| 13333 | */ |
| 13334 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 13335 | uint32_t guest_irq, bool set) |
| 13336 | { |
| 13337 | struct kvm_kernel_irq_routing_entry *e; |
| 13338 | struct kvm_irq_routing_table *irq_rt; |
| 13339 | struct kvm_lapic_irq irq; |
| 13340 | struct kvm_vcpu *vcpu; |
| 13341 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 13342 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13343 | |
| 13344 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 13345 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 13346 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13347 | return 0; |
| 13348 | |
| 13349 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 13350 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 13351 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 13352 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 13353 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 13354 | guest_irq, irq_rt->nr_rt_entries); |
| 13355 | goto out; |
| 13356 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13357 | |
| 13358 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 13359 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 13360 | continue; |
| 13361 | /* |
| 13362 | * VT-d PI cannot support posting multicast/broadcast |
| 13363 | * interrupts to a vCPU, we still use interrupt remapping |
| 13364 | * for these kind of interrupts. |
| 13365 | * |
| 13366 | * For lowest-priority interrupts, we only support |
| 13367 | * those with single CPU as the destination, e.g. user |
| 13368 | * configures the interrupts via /proc/irq or uses |
| 13369 | * irqbalance to make the interrupts single-CPU. |
| 13370 | * |
| 13371 | * We will support full lowest-priority interrupt later. |
| 13372 | */ |
| 13373 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 13374 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 13375 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 13376 | /* |
| 13377 | * Make sure the IRTE is in remapped mode if |
| 13378 | * we don't handle it in posted mode. |
| 13379 | */ |
| 13380 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 13381 | if (ret < 0) { |
| 13382 | printk(KERN_INFO |
| 13383 | "failed to back to remapped mode, irq: %u\n", |
| 13384 | host_irq); |
| 13385 | goto out; |
| 13386 | } |
| 13387 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13388 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 13389 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13390 | |
| 13391 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 13392 | vcpu_info.vector = irq.vector; |
| 13393 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 13394 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13395 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 13396 | |
| 13397 | if (set) |
| 13398 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2e | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 13399 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13400 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13401 | |
| 13402 | if (ret < 0) { |
| 13403 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 13404 | __func__); |
| 13405 | goto out; |
| 13406 | } |
| 13407 | } |
| 13408 | |
| 13409 | ret = 0; |
| 13410 | out: |
| 13411 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 13412 | return ret; |
| 13413 | } |
| 13414 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13415 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 13416 | { |
| 13417 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 13418 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 13419 | FEATURE_CONTROL_LMCE; |
| 13420 | else |
| 13421 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 13422 | ~FEATURE_CONTROL_LMCE; |
| 13423 | } |
| 13424 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13425 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 13426 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13427 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 13428 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 13429 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13430 | return 1; |
| 13431 | } |
| 13432 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13433 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 13434 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13435 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13436 | |
| 13437 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 13438 | if (vmx->nested.smm.guest_mode) |
| 13439 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 13440 | |
| 13441 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 13442 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 13443 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13444 | return 0; |
| 13445 | } |
| 13446 | |
| 13447 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 13448 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13449 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13450 | int ret; |
| 13451 | |
| 13452 | if (vmx->nested.smm.vmxon) { |
| 13453 | vmx->nested.vmxon = true; |
| 13454 | vmx->nested.smm.vmxon = false; |
| 13455 | } |
| 13456 | |
| 13457 | if (vmx->nested.smm.guest_mode) { |
| 13458 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13459 | ret = enter_vmx_non_root_mode(vcpu, NULL); |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13460 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 13461 | if (ret) |
| 13462 | return ret; |
| 13463 | |
| 13464 | vmx->nested.smm.guest_mode = false; |
| 13465 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13466 | return 0; |
| 13467 | } |
| 13468 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13469 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 13470 | { |
| 13471 | return 0; |
| 13472 | } |
| 13473 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13474 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 13475 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 13476 | u32 user_data_size) |
| 13477 | { |
| 13478 | struct vcpu_vmx *vmx; |
| 13479 | struct vmcs12 *vmcs12; |
| 13480 | struct kvm_nested_state kvm_state = { |
| 13481 | .flags = 0, |
| 13482 | .format = 0, |
| 13483 | .size = sizeof(kvm_state), |
| 13484 | .vmx.vmxon_pa = -1ull, |
| 13485 | .vmx.vmcs_pa = -1ull, |
| 13486 | }; |
| 13487 | |
| 13488 | if (!vcpu) |
| 13489 | return kvm_state.size + 2 * VMCS12_SIZE; |
| 13490 | |
| 13491 | vmx = to_vmx(vcpu); |
| 13492 | vmcs12 = get_vmcs12(vcpu); |
| 13493 | if (nested_vmx_allowed(vcpu) && |
| 13494 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
| 13495 | kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 13496 | kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr; |
| 13497 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13498 | if (vmx->nested.current_vmptr != -1ull) { |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13499 | kvm_state.size += VMCS12_SIZE; |
| 13500 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13501 | if (is_guest_mode(vcpu) && |
| 13502 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13503 | vmcs12->vmcs_link_pointer != -1ull) |
| 13504 | kvm_state.size += VMCS12_SIZE; |
| 13505 | } |
| 13506 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13507 | if (vmx->nested.smm.vmxon) |
| 13508 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
| 13509 | |
| 13510 | if (vmx->nested.smm.guest_mode) |
| 13511 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
| 13512 | |
| 13513 | if (is_guest_mode(vcpu)) { |
| 13514 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 13515 | |
| 13516 | if (vmx->nested.nested_run_pending) |
| 13517 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
| 13518 | } |
| 13519 | } |
| 13520 | |
| 13521 | if (user_data_size < kvm_state.size) |
| 13522 | goto out; |
| 13523 | |
| 13524 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 13525 | return -EFAULT; |
| 13526 | |
| 13527 | if (vmx->nested.current_vmptr == -1ull) |
| 13528 | goto out; |
| 13529 | |
| 13530 | /* |
| 13531 | * When running L2, the authoritative vmcs12 state is in the |
| 13532 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 13533 | * in the shadow vmcs linked to vmcs01, unless |
| 13534 | * sync_shadow_vmcs is set, in which case, the authoritative |
| 13535 | * vmcs12 state is in the vmcs12 already. |
| 13536 | */ |
| 13537 | if (is_guest_mode(vcpu)) |
| 13538 | sync_vmcs12(vcpu, vmcs12); |
| 13539 | else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs) |
| 13540 | copy_shadow_to_vmcs12(vmx); |
| 13541 | |
| 13542 | if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12))) |
| 13543 | return -EFAULT; |
| 13544 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13545 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13546 | vmcs12->vmcs_link_pointer != -1ull) { |
| 13547 | if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE, |
| 13548 | get_shadow_vmcs12(vcpu), sizeof(*vmcs12))) |
| 13549 | return -EFAULT; |
| 13550 | } |
| 13551 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13552 | out: |
| 13553 | return kvm_state.size; |
| 13554 | } |
| 13555 | |
| 13556 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 13557 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 13558 | struct kvm_nested_state *kvm_state) |
| 13559 | { |
| 13560 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13561 | struct vmcs12 *vmcs12; |
| 13562 | u32 exit_qual; |
| 13563 | int ret; |
| 13564 | |
| 13565 | if (kvm_state->format != 0) |
| 13566 | return -EINVAL; |
| 13567 | |
| 13568 | if (!nested_vmx_allowed(vcpu)) |
| 13569 | return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL; |
| 13570 | |
| 13571 | if (kvm_state->vmx.vmxon_pa == -1ull) { |
| 13572 | if (kvm_state->vmx.smm.flags) |
| 13573 | return -EINVAL; |
| 13574 | |
| 13575 | if (kvm_state->vmx.vmcs_pa != -1ull) |
| 13576 | return -EINVAL; |
| 13577 | |
| 13578 | vmx_leave_nested(vcpu); |
| 13579 | return 0; |
| 13580 | } |
| 13581 | |
| 13582 | if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa)) |
| 13583 | return -EINVAL; |
| 13584 | |
| 13585 | if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12)) |
| 13586 | return -EINVAL; |
| 13587 | |
| 13588 | if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa || |
| 13589 | !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa)) |
| 13590 | return -EINVAL; |
| 13591 | |
| 13592 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 13593 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 13594 | return -EINVAL; |
| 13595 | |
| 13596 | if (kvm_state->vmx.smm.flags & |
| 13597 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 13598 | return -EINVAL; |
| 13599 | |
| 13600 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 13601 | !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
| 13602 | return -EINVAL; |
| 13603 | |
| 13604 | vmx_leave_nested(vcpu); |
| 13605 | if (kvm_state->vmx.vmxon_pa == -1ull) |
| 13606 | return 0; |
| 13607 | |
| 13608 | vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa; |
| 13609 | ret = enter_vmx_operation(vcpu); |
| 13610 | if (ret) |
| 13611 | return ret; |
| 13612 | |
| 13613 | set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa); |
| 13614 | |
| 13615 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
| 13616 | vmx->nested.smm.vmxon = true; |
| 13617 | vmx->nested.vmxon = false; |
| 13618 | |
| 13619 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) |
| 13620 | vmx->nested.smm.guest_mode = true; |
| 13621 | } |
| 13622 | |
| 13623 | vmcs12 = get_vmcs12(vcpu); |
| 13624 | if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12))) |
| 13625 | return -EFAULT; |
| 13626 | |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 13627 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13628 | return -EINVAL; |
| 13629 | |
| 13630 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 13631 | return 0; |
| 13632 | |
| 13633 | vmx->nested.nested_run_pending = |
| 13634 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 13635 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13636 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13637 | vmcs12->vmcs_link_pointer != -1ull) { |
| 13638 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 13639 | if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12)) |
| 13640 | return -EINVAL; |
| 13641 | |
| 13642 | if (copy_from_user(shadow_vmcs12, |
| 13643 | user_kvm_nested_state->data + VMCS12_SIZE, |
| 13644 | sizeof(*vmcs12))) |
| 13645 | return -EFAULT; |
| 13646 | |
| 13647 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 13648 | !shadow_vmcs12->hdr.shadow_vmcs) |
| 13649 | return -EINVAL; |
| 13650 | } |
| 13651 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13652 | if (check_vmentry_prereqs(vcpu, vmcs12) || |
| 13653 | check_vmentry_postreqs(vcpu, vmcs12, &exit_qual)) |
| 13654 | return -EINVAL; |
| 13655 | |
| 13656 | if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING) |
| 13657 | vmx->nested.nested_run_pending = 1; |
| 13658 | |
| 13659 | vmx->nested.dirty_vmcs12 = true; |
| 13660 | ret = enter_vmx_non_root_mode(vcpu, NULL); |
| 13661 | if (ret) |
| 13662 | return -EINVAL; |
| 13663 | |
| 13664 | return 0; |
| 13665 | } |
| 13666 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 13667 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13668 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 13669 | .disabled_by_bios = vmx_disabled_by_bios, |
| 13670 | .hardware_setup = hardware_setup, |
| 13671 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 13672 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13673 | .hardware_enable = hardware_enable, |
| 13674 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 13675 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 13676 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13677 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13678 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 13679 | .vm_alloc = vmx_vm_alloc, |
| 13680 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13681 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13682 | .vcpu_create = vmx_create_vcpu, |
| 13683 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 13684 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13685 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 13686 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13687 | .vcpu_load = vmx_vcpu_load, |
| 13688 | .vcpu_put = vmx_vcpu_put, |
| 13689 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 13690 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 13691 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13692 | .get_msr = vmx_get_msr, |
| 13693 | .set_msr = vmx_set_msr, |
| 13694 | .get_segment_base = vmx_get_segment_base, |
| 13695 | .get_segment = vmx_get_segment, |
| 13696 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 13697 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13698 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 13699 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 13700 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 13701 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13702 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13703 | .set_cr3 = vmx_set_cr3, |
| 13704 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13705 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13706 | .get_idt = vmx_get_idt, |
| 13707 | .set_idt = vmx_set_idt, |
| 13708 | .get_gdt = vmx_get_gdt, |
| 13709 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 13710 | .get_dr6 = vmx_get_dr6, |
| 13711 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 13712 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 13713 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 13714 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13715 | .get_rflags = vmx_get_rflags, |
| 13716 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 13717 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13718 | .tlb_flush = vmx_flush_tlb, |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 13719 | .tlb_flush_gva = vmx_flush_tlb_gva, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13720 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13721 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 13722 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13723 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 13724 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 13725 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 13726 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 13727 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13728 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 13729 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 13730 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 13731 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13732 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 13733 | .get_nmi_mask = vmx_get_nmi_mask, |
| 13734 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13735 | .enable_nmi_window = enable_nmi_window, |
| 13736 | .enable_irq_window = enable_irq_window, |
| 13737 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 13738 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 13739 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 13740 | .get_enable_apicv = vmx_get_enable_apicv, |
| 13741 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13742 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 13743 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13744 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 13745 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 13746 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 13747 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13748 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 13749 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 13750 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 13751 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 13752 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 13753 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13754 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13755 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 13756 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 13757 | |
| 13758 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 13759 | |
| 13760 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 13761 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 13762 | |
| 13763 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 13764 | |
| 13765 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13766 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 13767 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13768 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 13769 | |
| 13770 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13771 | |
| 13772 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 13773 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 13774 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 13775 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 13776 | .umip_emulated = vmx_umip_emulated, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13777 | |
| 13778 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13779 | |
| 13780 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13781 | |
| 13782 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 13783 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 13784 | .flush_log_dirty = vmx_flush_log_dirty, |
| 13785 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13786 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13787 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13788 | .pre_block = vmx_pre_block, |
| 13789 | .post_block = vmx_post_block, |
| 13790 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13791 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13792 | |
| 13793 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13794 | |
| 13795 | #ifdef CONFIG_X86_64 |
| 13796 | .set_hv_timer = vmx_set_hv_timer, |
| 13797 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 13798 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13799 | |
| 13800 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13801 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13802 | .get_nested_state = vmx_get_nested_state, |
| 13803 | .set_nested_state = vmx_set_nested_state, |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13804 | .get_vmcs12_pages = nested_get_vmcs12_pages, |
| 13805 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13806 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13807 | .pre_enter_smm = vmx_pre_enter_smm, |
| 13808 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13809 | .enable_smi_window = enable_smi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13810 | }; |
| 13811 | |
| 13812 | static int __init vmx_init(void) |
| 13813 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13814 | int r; |
| 13815 | |
| 13816 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13817 | /* |
| 13818 | * Enlightened VMCS usage should be recommended and the host needs |
| 13819 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 13820 | * with module parameter. |
| 13821 | */ |
| 13822 | if (enlightened_vmcs && |
| 13823 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 13824 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 13825 | KVM_EVMCS_VERSION) { |
| 13826 | int cpu; |
| 13827 | |
| 13828 | /* Check that we have assist pages on all online CPUs */ |
| 13829 | for_each_online_cpu(cpu) { |
| 13830 | if (!hv_get_vp_assist_page(cpu)) { |
| 13831 | enlightened_vmcs = false; |
| 13832 | break; |
| 13833 | } |
| 13834 | } |
| 13835 | |
| 13836 | if (enlightened_vmcs) { |
| 13837 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 13838 | static_branch_enable(&enable_evmcs); |
| 13839 | } |
| 13840 | } else { |
| 13841 | enlightened_vmcs = false; |
| 13842 | } |
| 13843 | #endif |
| 13844 | |
| 13845 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13846 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13847 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13848 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 13849 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13850 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13851 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 13852 | crash_vmclear_local_loaded_vmcss); |
| 13853 | #endif |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 13854 | vmx_check_vmcs12_offsets(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13855 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13856 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13857 | } |
| 13858 | |
| 13859 | static void __exit vmx_exit(void) |
| 13860 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13861 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 13862 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13863 | synchronize_rcu(); |
| 13864 | #endif |
| 13865 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 13866 | kvm_exit(); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13867 | |
| 13868 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13869 | if (static_branch_unlikely(&enable_evmcs)) { |
| 13870 | int cpu; |
| 13871 | struct hv_vp_assist_page *vp_ap; |
| 13872 | /* |
| 13873 | * Reset everything to support using non-enlightened VMCS |
| 13874 | * access later (e.g. when we reload the module with |
| 13875 | * enlightened_vmcs=0) |
| 13876 | */ |
| 13877 | for_each_online_cpu(cpu) { |
| 13878 | vp_ap = hv_get_vp_assist_page(cpu); |
| 13879 | |
| 13880 | if (!vp_ap) |
| 13881 | continue; |
| 13882 | |
| 13883 | vp_ap->current_nested_vmcs = 0; |
| 13884 | vp_ap->enlighten_vmentry = 0; |
| 13885 | } |
| 13886 | |
| 13887 | static_branch_disable(&enable_evmcs); |
| 13888 | } |
| 13889 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13890 | } |
| 13891 | |
| 13892 | module_init(vmx_init) |
| 13893 | module_exit(vmx_exit) |