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 | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 35 | #include <linux/hrtimer.h> |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 36 | #include <linux/nospec.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 37 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 38 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 39 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 40 | #include <asm/cpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 41 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 42 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 43 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 44 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 45 | #include <asm/mce.h> |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 46 | #include <asm/fpu/internal.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 47 | #include <asm/perf_event.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 48 | #include <asm/debugreg.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 49 | #include <asm/kexec.h> |
Radim Krčmář | dab2087 | 2015-02-09 22:44:07 +0100 | [diff] [blame] | 50 | #include <asm/apic.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 51 | #include <asm/irq_remapping.h> |
David Woodhouse | c1ddd99 | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 52 | #include <asm/nospec-branch.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 53 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 54 | #include "trace.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 55 | #include "pmu.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 56 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 57 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 58 | #define __ex_clear(x, reg) \ |
| 59 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 60 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 61 | MODULE_AUTHOR("Qumranet"); |
| 62 | MODULE_LICENSE("GPL"); |
| 63 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 64 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 65 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 66 | {} |
| 67 | }; |
| 68 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 69 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 70 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 71 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 72 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 73 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 74 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 75 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 76 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 77 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 78 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 79 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 80 | module_param_named(unrestricted_guest, |
| 81 | enable_unrestricted_guest, bool, S_IRUGO); |
| 82 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 83 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 84 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 85 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 86 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 87 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 88 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 89 | static bool __read_mostly vmm_exclusive = 1; |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 90 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 91 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 92 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 93 | module_param(fasteoi, bool, S_IRUGO); |
| 94 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 95 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 96 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 97 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 98 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 99 | 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] | 100 | /* |
| 101 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 102 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 103 | * use VMX instructions. |
| 104 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 105 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 106 | module_param(nested, bool, S_IRUGO); |
| 107 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 108 | static u64 __read_mostly host_xss; |
| 109 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 110 | static bool __read_mostly enable_pml = 1; |
| 111 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 112 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 113 | #define MSR_TYPE_R 1 |
| 114 | #define MSR_TYPE_W 2 |
| 115 | #define MSR_TYPE_RW 3 |
| 116 | |
| 117 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 118 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
| 119 | #define MSR_BITMAP_MODE_LM 4 |
| 120 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 121 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 122 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 123 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 124 | static int __read_mostly cpu_preemption_timer_multi; |
| 125 | static bool __read_mostly enable_preemption_timer = 1; |
| 126 | #ifdef CONFIG_X86_64 |
| 127 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 128 | #endif |
| 129 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 130 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
| 131 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE) |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 132 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 133 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 134 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 135 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Andy Lutomirski | 52ce3c2 | 2014-10-07 17:16:21 -0700 | [diff] [blame] | 136 | | X86_CR4_OSXMMEXCPT | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 137 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 138 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 139 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 140 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 141 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 142 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 143 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 144 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 145 | /* |
| 146 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 147 | * ple_gap: upper bound on the amount of time between two successive |
| 148 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 149 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 150 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 151 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 152 | * less than 2^12 cycles |
| 153 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 154 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 155 | */ |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 156 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
| 157 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 158 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 |
| 159 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 |
| 160 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ |
| 161 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW |
| 162 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 163 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 164 | module_param(ple_gap, int, S_IRUGO); |
| 165 | |
| 166 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 167 | module_param(ple_window, int, S_IRUGO); |
| 168 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 169 | /* Default doubles per-vcpu window every exit. */ |
| 170 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; |
| 171 | module_param(ple_window_grow, int, S_IRUGO); |
| 172 | |
| 173 | /* Default resets per-vcpu window every exit to ple_window. */ |
| 174 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; |
| 175 | module_param(ple_window_shrink, int, S_IRUGO); |
| 176 | |
| 177 | /* Default is to compute the maximum so we can never overflow. */ |
| 178 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 179 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 180 | module_param(ple_window_max, int, S_IRUGO); |
| 181 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 182 | extern const ulong vmx_return; |
| 183 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 184 | #define NR_AUTOLOAD_MSRS 8 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 185 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 186 | struct vmcs { |
| 187 | u32 revision_id; |
| 188 | u32 abort; |
| 189 | char data[0]; |
| 190 | }; |
| 191 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 192 | /* |
| 193 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 194 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 195 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 196 | */ |
| 197 | struct loaded_vmcs { |
| 198 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 199 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 200 | int cpu; |
| 201 | int launched; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 202 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 203 | struct list_head loaded_vmcss_on_cpu_link; |
| 204 | }; |
| 205 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 206 | struct shared_msr_entry { |
| 207 | unsigned index; |
| 208 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 209 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 210 | }; |
| 211 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 212 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 213 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 214 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 215 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 216 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 217 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 218 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 219 | * 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] | 220 | * underlying hardware which will be used to run L2. |
| 221 | * This structure is packed to ensure that its layout is identical across |
| 222 | * machines (necessary for live migration). |
| 223 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 224 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 225 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 226 | struct __packed vmcs12 { |
| 227 | /* According to the Intel spec, a VMCS region must start with the |
| 228 | * following two fields. Then follow implementation-specific data. |
| 229 | */ |
| 230 | u32 revision_id; |
| 231 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 232 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 233 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 234 | u32 padding[7]; /* room for future expansion */ |
| 235 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 236 | u64 io_bitmap_a; |
| 237 | u64 io_bitmap_b; |
| 238 | u64 msr_bitmap; |
| 239 | u64 vm_exit_msr_store_addr; |
| 240 | u64 vm_exit_msr_load_addr; |
| 241 | u64 vm_entry_msr_load_addr; |
| 242 | u64 tsc_offset; |
| 243 | u64 virtual_apic_page_addr; |
| 244 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 245 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 246 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 247 | u64 eoi_exit_bitmap0; |
| 248 | u64 eoi_exit_bitmap1; |
| 249 | u64 eoi_exit_bitmap2; |
| 250 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 251 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 252 | u64 guest_physical_address; |
| 253 | u64 vmcs_link_pointer; |
| 254 | u64 guest_ia32_debugctl; |
| 255 | u64 guest_ia32_pat; |
| 256 | u64 guest_ia32_efer; |
| 257 | u64 guest_ia32_perf_global_ctrl; |
| 258 | u64 guest_pdptr0; |
| 259 | u64 guest_pdptr1; |
| 260 | u64 guest_pdptr2; |
| 261 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 262 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 263 | u64 host_ia32_pat; |
| 264 | u64 host_ia32_efer; |
| 265 | u64 host_ia32_perf_global_ctrl; |
| 266 | u64 padding64[8]; /* room for future expansion */ |
| 267 | /* |
| 268 | * To allow migration of L1 (complete with its L2 guests) between |
| 269 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 270 | * unsigned long fields with no explict size. We use u64 (aliased |
| 271 | * natural_width) instead. Luckily, x86 is little-endian. |
| 272 | */ |
| 273 | natural_width cr0_guest_host_mask; |
| 274 | natural_width cr4_guest_host_mask; |
| 275 | natural_width cr0_read_shadow; |
| 276 | natural_width cr4_read_shadow; |
| 277 | natural_width cr3_target_value0; |
| 278 | natural_width cr3_target_value1; |
| 279 | natural_width cr3_target_value2; |
| 280 | natural_width cr3_target_value3; |
| 281 | natural_width exit_qualification; |
| 282 | natural_width guest_linear_address; |
| 283 | natural_width guest_cr0; |
| 284 | natural_width guest_cr3; |
| 285 | natural_width guest_cr4; |
| 286 | natural_width guest_es_base; |
| 287 | natural_width guest_cs_base; |
| 288 | natural_width guest_ss_base; |
| 289 | natural_width guest_ds_base; |
| 290 | natural_width guest_fs_base; |
| 291 | natural_width guest_gs_base; |
| 292 | natural_width guest_ldtr_base; |
| 293 | natural_width guest_tr_base; |
| 294 | natural_width guest_gdtr_base; |
| 295 | natural_width guest_idtr_base; |
| 296 | natural_width guest_dr7; |
| 297 | natural_width guest_rsp; |
| 298 | natural_width guest_rip; |
| 299 | natural_width guest_rflags; |
| 300 | natural_width guest_pending_dbg_exceptions; |
| 301 | natural_width guest_sysenter_esp; |
| 302 | natural_width guest_sysenter_eip; |
| 303 | natural_width host_cr0; |
| 304 | natural_width host_cr3; |
| 305 | natural_width host_cr4; |
| 306 | natural_width host_fs_base; |
| 307 | natural_width host_gs_base; |
| 308 | natural_width host_tr_base; |
| 309 | natural_width host_gdtr_base; |
| 310 | natural_width host_idtr_base; |
| 311 | natural_width host_ia32_sysenter_esp; |
| 312 | natural_width host_ia32_sysenter_eip; |
| 313 | natural_width host_rsp; |
| 314 | natural_width host_rip; |
| 315 | natural_width paddingl[8]; /* room for future expansion */ |
| 316 | u32 pin_based_vm_exec_control; |
| 317 | u32 cpu_based_vm_exec_control; |
| 318 | u32 exception_bitmap; |
| 319 | u32 page_fault_error_code_mask; |
| 320 | u32 page_fault_error_code_match; |
| 321 | u32 cr3_target_count; |
| 322 | u32 vm_exit_controls; |
| 323 | u32 vm_exit_msr_store_count; |
| 324 | u32 vm_exit_msr_load_count; |
| 325 | u32 vm_entry_controls; |
| 326 | u32 vm_entry_msr_load_count; |
| 327 | u32 vm_entry_intr_info_field; |
| 328 | u32 vm_entry_exception_error_code; |
| 329 | u32 vm_entry_instruction_len; |
| 330 | u32 tpr_threshold; |
| 331 | u32 secondary_vm_exec_control; |
| 332 | u32 vm_instruction_error; |
| 333 | u32 vm_exit_reason; |
| 334 | u32 vm_exit_intr_info; |
| 335 | u32 vm_exit_intr_error_code; |
| 336 | u32 idt_vectoring_info_field; |
| 337 | u32 idt_vectoring_error_code; |
| 338 | u32 vm_exit_instruction_len; |
| 339 | u32 vmx_instruction_info; |
| 340 | u32 guest_es_limit; |
| 341 | u32 guest_cs_limit; |
| 342 | u32 guest_ss_limit; |
| 343 | u32 guest_ds_limit; |
| 344 | u32 guest_fs_limit; |
| 345 | u32 guest_gs_limit; |
| 346 | u32 guest_ldtr_limit; |
| 347 | u32 guest_tr_limit; |
| 348 | u32 guest_gdtr_limit; |
| 349 | u32 guest_idtr_limit; |
| 350 | u32 guest_es_ar_bytes; |
| 351 | u32 guest_cs_ar_bytes; |
| 352 | u32 guest_ss_ar_bytes; |
| 353 | u32 guest_ds_ar_bytes; |
| 354 | u32 guest_fs_ar_bytes; |
| 355 | u32 guest_gs_ar_bytes; |
| 356 | u32 guest_ldtr_ar_bytes; |
| 357 | u32 guest_tr_ar_bytes; |
| 358 | u32 guest_interruptibility_info; |
| 359 | u32 guest_activity_state; |
| 360 | u32 guest_sysenter_cs; |
| 361 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 362 | u32 vmx_preemption_timer_value; |
| 363 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 364 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 365 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 366 | u16 guest_es_selector; |
| 367 | u16 guest_cs_selector; |
| 368 | u16 guest_ss_selector; |
| 369 | u16 guest_ds_selector; |
| 370 | u16 guest_fs_selector; |
| 371 | u16 guest_gs_selector; |
| 372 | u16 guest_ldtr_selector; |
| 373 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 374 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 375 | u16 host_es_selector; |
| 376 | u16 host_cs_selector; |
| 377 | u16 host_ss_selector; |
| 378 | u16 host_ds_selector; |
| 379 | u16 host_fs_selector; |
| 380 | u16 host_gs_selector; |
| 381 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | /* |
| 385 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 386 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 387 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 388 | */ |
| 389 | #define VMCS12_REVISION 0x11e57ed0 |
| 390 | |
| 391 | /* |
| 392 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 393 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 394 | * current implementation, 4K are reserved to avoid future complications. |
| 395 | */ |
| 396 | #define VMCS12_SIZE 0x1000 |
| 397 | |
| 398 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 399 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 400 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 401 | */ |
| 402 | struct nested_vmx { |
| 403 | /* Has the level1 guest done vmxon? */ |
| 404 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 405 | gpa_t vmxon_ptr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 406 | |
| 407 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 408 | gpa_t current_vmptr; |
| 409 | /* The host-usable pointer to the above */ |
| 410 | struct page *current_vmcs12_page; |
| 411 | struct vmcs12 *current_vmcs12; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 412 | /* |
| 413 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 414 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
| 415 | * memory during VMXOFF, VMCLEAR, VMPTRLD. |
| 416 | */ |
| 417 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 418 | /* |
| 419 | * Indicates if the shadow vmcs must be updated with the |
| 420 | * data hold by vmcs12 |
| 421 | */ |
| 422 | bool sync_shadow_vmcs; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 423 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 424 | bool change_vmcs01_virtual_x2apic_mode; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 425 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 426 | bool nested_run_pending; |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 427 | |
| 428 | struct loaded_vmcs vmcs02; |
| 429 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 430 | /* |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 431 | * Guest pages referred to in the vmcs02 with host-physical |
| 432 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 433 | */ |
| 434 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 435 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 436 | struct page *pi_desc_page; |
| 437 | struct pi_desc *pi_desc; |
| 438 | bool pi_pending; |
| 439 | u16 posted_intr_nv; |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 440 | |
| 441 | struct hrtimer preemption_timer; |
| 442 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 443 | |
| 444 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 445 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 446 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 447 | u16 vpid02; |
| 448 | u16 last_vpid; |
| 449 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 450 | u32 nested_vmx_procbased_ctls_low; |
| 451 | u32 nested_vmx_procbased_ctls_high; |
| 452 | u32 nested_vmx_true_procbased_ctls_low; |
| 453 | u32 nested_vmx_secondary_ctls_low; |
| 454 | u32 nested_vmx_secondary_ctls_high; |
| 455 | u32 nested_vmx_pinbased_ctls_low; |
| 456 | u32 nested_vmx_pinbased_ctls_high; |
| 457 | u32 nested_vmx_exit_ctls_low; |
| 458 | u32 nested_vmx_exit_ctls_high; |
| 459 | u32 nested_vmx_true_exit_ctls_low; |
| 460 | u32 nested_vmx_entry_ctls_low; |
| 461 | u32 nested_vmx_entry_ctls_high; |
| 462 | u32 nested_vmx_true_entry_ctls_low; |
| 463 | u32 nested_vmx_misc_low; |
| 464 | u32 nested_vmx_misc_high; |
| 465 | u32 nested_vmx_ept_caps; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 466 | u32 nested_vmx_vpid_caps; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 467 | }; |
| 468 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 469 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 470 | #define POSTED_INTR_SN 1 |
| 471 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 472 | /* Posted-Interrupt Descriptor */ |
| 473 | struct pi_desc { |
| 474 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 475 | union { |
| 476 | struct { |
| 477 | /* bit 256 - Outstanding Notification */ |
| 478 | u16 on : 1, |
| 479 | /* bit 257 - Suppress Notification */ |
| 480 | sn : 1, |
| 481 | /* bit 271:258 - Reserved */ |
| 482 | rsvd_1 : 14; |
| 483 | /* bit 279:272 - Notification Vector */ |
| 484 | u8 nv; |
| 485 | /* bit 287:280 - Reserved */ |
| 486 | u8 rsvd_2; |
| 487 | /* bit 319:288 - Notification Destination */ |
| 488 | u32 ndst; |
| 489 | }; |
| 490 | u64 control; |
| 491 | }; |
| 492 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 493 | } __aligned(64); |
| 494 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 495 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 496 | { |
| 497 | return test_and_set_bit(POSTED_INTR_ON, |
| 498 | (unsigned long *)&pi_desc->control); |
| 499 | } |
| 500 | |
| 501 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 502 | { |
| 503 | return test_and_clear_bit(POSTED_INTR_ON, |
| 504 | (unsigned long *)&pi_desc->control); |
| 505 | } |
| 506 | |
| 507 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 508 | { |
| 509 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 510 | } |
| 511 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 512 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 513 | { |
| 514 | return clear_bit(POSTED_INTR_SN, |
| 515 | (unsigned long *)&pi_desc->control); |
| 516 | } |
| 517 | |
| 518 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 519 | { |
| 520 | return set_bit(POSTED_INTR_SN, |
| 521 | (unsigned long *)&pi_desc->control); |
| 522 | } |
| 523 | |
| 524 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 525 | { |
| 526 | return test_bit(POSTED_INTR_ON, |
| 527 | (unsigned long *)&pi_desc->control); |
| 528 | } |
| 529 | |
| 530 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 531 | { |
| 532 | return test_bit(POSTED_INTR_SN, |
| 533 | (unsigned long *)&pi_desc->control); |
| 534 | } |
| 535 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 536 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 537 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 538 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 539 | u8 fail; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 540 | bool nmi_known_unmasked; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 541 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 542 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 543 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 544 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 545 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 546 | int nmsrs; |
| 547 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 548 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 549 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 550 | u64 msr_host_kernel_gs_base; |
| 551 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 552 | #endif |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 553 | |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 554 | u64 arch_capabilities; |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 555 | u64 spec_ctrl; |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 556 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 557 | u32 vm_entry_controls_shadow; |
| 558 | u32 vm_exit_controls_shadow; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 559 | /* |
| 560 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 561 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 562 | * guest (L2), it points to a different VMCS. |
| 563 | */ |
| 564 | struct loaded_vmcs vmcs01; |
| 565 | struct loaded_vmcs *loaded_vmcs; |
| 566 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 567 | struct msr_autoload { |
| 568 | unsigned nr; |
| 569 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 570 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 571 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 572 | struct { |
| 573 | int loaded; |
| 574 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 575 | #ifdef CONFIG_X86_64 |
| 576 | u16 ds_sel, es_sel; |
| 577 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 578 | int gs_ldt_reload_needed; |
| 579 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 580 | u64 msr_host_bndcfgs; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 581 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 582 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 583 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 584 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 585 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 586 | struct kvm_segment segs[8]; |
| 587 | } rmode; |
| 588 | struct { |
| 589 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 590 | struct kvm_save_segment { |
| 591 | u16 selector; |
| 592 | unsigned long base; |
| 593 | u32 limit; |
| 594 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 595 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 596 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 597 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 598 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 599 | |
| 600 | /* Support for vnmi-less CPUs */ |
| 601 | int soft_vnmi_blocked; |
| 602 | ktime_t entry_time; |
| 603 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 604 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 605 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 606 | /* Posted interrupt descriptor */ |
| 607 | struct pi_desc pi_desc; |
| 608 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 609 | /* Support for a guest hypervisor (nested VMX) */ |
| 610 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 611 | |
| 612 | /* Dynamic PLE window. */ |
| 613 | int ple_window; |
| 614 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 615 | |
| 616 | /* Support for PML */ |
| 617 | #define PML_ENTITY_NUM 512 |
| 618 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 619 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 620 | /* apic deadline value in host tsc */ |
| 621 | u64 hv_deadline_tsc; |
| 622 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 623 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 624 | |
| 625 | bool guest_pkru_valid; |
| 626 | u32 guest_pkru; |
| 627 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 628 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 629 | /* |
| 630 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 631 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 632 | * in msr_ia32_feature_control_valid_bits. |
| 633 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 634 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 635 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 636 | }; |
| 637 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 638 | enum segment_cache_field { |
| 639 | SEG_FIELD_SEL = 0, |
| 640 | SEG_FIELD_BASE = 1, |
| 641 | SEG_FIELD_LIMIT = 2, |
| 642 | SEG_FIELD_AR = 3, |
| 643 | |
| 644 | SEG_FIELD_NR = 4 |
| 645 | }; |
| 646 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 647 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 648 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 649 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 650 | } |
| 651 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 652 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 653 | { |
| 654 | return &(to_vmx(vcpu)->pi_desc); |
| 655 | } |
| 656 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 657 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 658 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 659 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 660 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 661 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 662 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 663 | static unsigned long shadow_read_only_fields[] = { |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 664 | /* |
| 665 | * We do NOT shadow fields that are modified when L0 |
| 666 | * traps and emulates any vmx instruction (e.g. VMPTRLD, |
| 667 | * VMXON...) executed by L1. |
| 668 | * For example, VM_INSTRUCTION_ERROR is read |
| 669 | * by L1 if a vmx instruction fails (part of the error path). |
| 670 | * Note the code assumes this logic. If for some reason |
| 671 | * we start shadowing these fields then we need to |
| 672 | * force a shadow sync when L0 emulates vmx instructions |
| 673 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified |
| 674 | * by nested_vmx_failValid) |
| 675 | */ |
| 676 | VM_EXIT_REASON, |
| 677 | VM_EXIT_INTR_INFO, |
| 678 | VM_EXIT_INSTRUCTION_LEN, |
| 679 | IDT_VECTORING_INFO_FIELD, |
| 680 | IDT_VECTORING_ERROR_CODE, |
| 681 | VM_EXIT_INTR_ERROR_CODE, |
| 682 | EXIT_QUALIFICATION, |
| 683 | GUEST_LINEAR_ADDRESS, |
| 684 | GUEST_PHYSICAL_ADDRESS |
| 685 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 686 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 687 | ARRAY_SIZE(shadow_read_only_fields); |
| 688 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 689 | static unsigned long shadow_read_write_fields[] = { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 690 | TPR_THRESHOLD, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 691 | GUEST_RIP, |
| 692 | GUEST_RSP, |
| 693 | GUEST_CR0, |
| 694 | GUEST_CR3, |
| 695 | GUEST_CR4, |
| 696 | GUEST_INTERRUPTIBILITY_INFO, |
| 697 | GUEST_RFLAGS, |
| 698 | GUEST_CS_SELECTOR, |
| 699 | GUEST_CS_AR_BYTES, |
| 700 | GUEST_CS_LIMIT, |
| 701 | GUEST_CS_BASE, |
| 702 | GUEST_ES_BASE, |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 703 | GUEST_BNDCFGS, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 704 | CR0_GUEST_HOST_MASK, |
| 705 | CR0_READ_SHADOW, |
| 706 | CR4_READ_SHADOW, |
| 707 | TSC_OFFSET, |
| 708 | EXCEPTION_BITMAP, |
| 709 | CPU_BASED_VM_EXEC_CONTROL, |
| 710 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 711 | VM_ENTRY_INTR_INFO_FIELD, |
| 712 | VM_ENTRY_INSTRUCTION_LEN, |
| 713 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 714 | HOST_FS_BASE, |
| 715 | HOST_GS_BASE, |
| 716 | HOST_FS_SELECTOR, |
| 717 | HOST_GS_SELECTOR |
| 718 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 719 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 720 | ARRAY_SIZE(shadow_read_write_fields); |
| 721 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 722 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 723 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 724 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 725 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 726 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 727 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 728 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 729 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 730 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 731 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 732 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 733 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 734 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 735 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 736 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 737 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 738 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 739 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 740 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 741 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 742 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 743 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 744 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 745 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 746 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 747 | FIELD64(TSC_OFFSET, tsc_offset), |
| 748 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 749 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 750 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 751 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 752 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 753 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 754 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 755 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 756 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 757 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 758 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 759 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 760 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 761 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 762 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 763 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 764 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 765 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 766 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 767 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 768 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 769 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 770 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 771 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 772 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 773 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 774 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 775 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 776 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 777 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 778 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 779 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 780 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 781 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 782 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 783 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 784 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 785 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 786 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 787 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 788 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 789 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 790 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 791 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 792 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 793 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 794 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 795 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 796 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 797 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 798 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 799 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 800 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 801 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 802 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 803 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 804 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 805 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 806 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 807 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 808 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 809 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 810 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 811 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 812 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 813 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 814 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 815 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 816 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 817 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 818 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 819 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 820 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 821 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 822 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 823 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 824 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 825 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 826 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 827 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 828 | FIELD(GUEST_CR0, guest_cr0), |
| 829 | FIELD(GUEST_CR3, guest_cr3), |
| 830 | FIELD(GUEST_CR4, guest_cr4), |
| 831 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 832 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 833 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 834 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 835 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 836 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 837 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 838 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 839 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 840 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 841 | FIELD(GUEST_DR7, guest_dr7), |
| 842 | FIELD(GUEST_RSP, guest_rsp), |
| 843 | FIELD(GUEST_RIP, guest_rip), |
| 844 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 845 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 846 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 847 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 848 | FIELD(HOST_CR0, host_cr0), |
| 849 | FIELD(HOST_CR3, host_cr3), |
| 850 | FIELD(HOST_CR4, host_cr4), |
| 851 | FIELD(HOST_FS_BASE, host_fs_base), |
| 852 | FIELD(HOST_GS_BASE, host_gs_base), |
| 853 | FIELD(HOST_TR_BASE, host_tr_base), |
| 854 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 855 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 856 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 857 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 858 | FIELD(HOST_RSP, host_rsp), |
| 859 | FIELD(HOST_RIP, host_rip), |
| 860 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 861 | |
| 862 | static inline short vmcs_field_to_offset(unsigned long field) |
| 863 | { |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 864 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 865 | unsigned short offset; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 866 | |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 867 | BUILD_BUG_ON(size > SHRT_MAX); |
| 868 | if (field >= size) |
Andrew Honig | 012df71 | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 869 | return -ENOENT; |
| 870 | |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 871 | field = array_index_nospec(field, size); |
| 872 | offset = vmcs_field_to_offset_table[field]; |
| 873 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 874 | return -ENOENT; |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 875 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 876 | } |
| 877 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 878 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 879 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 880 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 884 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 885 | struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT); |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 886 | if (is_error_page(page)) |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 887 | return NULL; |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 888 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 889 | return page; |
| 890 | } |
| 891 | |
| 892 | static void nested_release_page(struct page *page) |
| 893 | { |
| 894 | kvm_release_page_dirty(page); |
| 895 | } |
| 896 | |
| 897 | static void nested_release_page_clean(struct page *page) |
| 898 | { |
| 899 | kvm_release_page_clean(page); |
| 900 | } |
| 901 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 902 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 903 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 904 | static void kvm_cpu_vmxon(u64 addr); |
| 905 | static void kvm_cpu_vmxoff(void); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 906 | static bool vmx_xsaves_supported(void); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 907 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 908 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 909 | struct kvm_segment *var, int seg); |
| 910 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 911 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 912 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 913 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 914 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 915 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 916 | static int alloc_identity_pagetable(struct kvm *kvm); |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 917 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 918 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 919 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 920 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 921 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 922 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 923 | /* |
| 924 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 925 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 926 | */ |
| 927 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 928 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 929 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 930 | /* |
| 931 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 932 | * can find which vCPU should be waken up. |
| 933 | */ |
| 934 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 935 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 936 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 937 | static unsigned long *vmx_io_bitmap_a; |
| 938 | static unsigned long *vmx_io_bitmap_b; |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 939 | static unsigned long *vmx_vmread_bitmap; |
| 940 | static unsigned long *vmx_vmwrite_bitmap; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 941 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 942 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 943 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 944 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 945 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 946 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 947 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 948 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 949 | int size; |
| 950 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 951 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 952 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 953 | u32 pin_based_exec_ctrl; |
| 954 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 955 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 956 | u32 vmexit_ctrl; |
| 957 | u32 vmentry_ctrl; |
| 958 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 959 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 960 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 961 | u32 ept; |
| 962 | u32 vpid; |
| 963 | } vmx_capability; |
| 964 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 965 | #define VMX_SEGMENT_FIELD(seg) \ |
| 966 | [VCPU_SREG_##seg] = { \ |
| 967 | .selector = GUEST_##seg##_SELECTOR, \ |
| 968 | .base = GUEST_##seg##_BASE, \ |
| 969 | .limit = GUEST_##seg##_LIMIT, \ |
| 970 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 971 | } |
| 972 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 973 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 974 | unsigned selector; |
| 975 | unsigned base; |
| 976 | unsigned limit; |
| 977 | unsigned ar_bytes; |
| 978 | } kvm_vmx_segment_fields[] = { |
| 979 | VMX_SEGMENT_FIELD(CS), |
| 980 | VMX_SEGMENT_FIELD(DS), |
| 981 | VMX_SEGMENT_FIELD(ES), |
| 982 | VMX_SEGMENT_FIELD(FS), |
| 983 | VMX_SEGMENT_FIELD(GS), |
| 984 | VMX_SEGMENT_FIELD(SS), |
| 985 | VMX_SEGMENT_FIELD(TR), |
| 986 | VMX_SEGMENT_FIELD(LDTR), |
| 987 | }; |
| 988 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 989 | static u64 host_efer; |
| 990 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 991 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 992 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 993 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 994 | * 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] | 995 | * away by decrementing the array size. |
| 996 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 997 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 998 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 999 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1000 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1001 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1002 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1003 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1004 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1005 | { |
| 1006 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1007 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1008 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1009 | } |
| 1010 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1011 | static inline bool is_debug(u32 intr_info) |
| 1012 | { |
| 1013 | return is_exception_n(intr_info, DB_VECTOR); |
| 1014 | } |
| 1015 | |
| 1016 | static inline bool is_breakpoint(u32 intr_info) |
| 1017 | { |
| 1018 | return is_exception_n(intr_info, BP_VECTOR); |
| 1019 | } |
| 1020 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1021 | static inline bool is_page_fault(u32 intr_info) |
| 1022 | { |
| 1023 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1026 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1027 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1028 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1029 | } |
| 1030 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1031 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1032 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1033 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1034 | } |
| 1035 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1036 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1037 | { |
| 1038 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1039 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1040 | } |
| 1041 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1042 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1043 | { |
| 1044 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1045 | INTR_INFO_VALID_MASK)) == |
| 1046 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1047 | } |
| 1048 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1049 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1050 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1051 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1052 | } |
| 1053 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1054 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1055 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1056 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1059 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1060 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1061 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1064 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1065 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1066 | return vmcs_config.cpu_based_exec_ctrl & |
| 1067 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1070 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1071 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1072 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1073 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1074 | } |
| 1075 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1076 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1077 | { |
| 1078 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1079 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1080 | } |
| 1081 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1082 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1083 | { |
| 1084 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1085 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1086 | } |
| 1087 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1088 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1089 | { |
| 1090 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1091 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1092 | } |
| 1093 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1094 | /* |
| 1095 | * Comment's format: document - errata name - stepping - processor name. |
| 1096 | * Refer from |
| 1097 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1098 | */ |
| 1099 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1100 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1101 | 0x000206E6, |
| 1102 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1103 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1104 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1105 | 0x00020652, |
| 1106 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1107 | 0x00020655, |
| 1108 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1109 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1110 | /* |
| 1111 | * 320767.pdf - AAP86 - B1 - |
| 1112 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1113 | */ |
| 1114 | 0x000106E5, |
| 1115 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1116 | 0x000106A0, |
| 1117 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1118 | 0x000106A1, |
| 1119 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1120 | 0x000106A4, |
| 1121 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1122 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1123 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1124 | 0x000106A5, |
| 1125 | }; |
| 1126 | |
| 1127 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1128 | { |
| 1129 | u32 eax = cpuid_eax(0x00000001), i; |
| 1130 | |
| 1131 | /* Clear the reserved bits */ |
| 1132 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1133 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1134 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1135 | return true; |
| 1136 | |
| 1137 | return false; |
| 1138 | } |
| 1139 | |
| 1140 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1141 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1142 | return vmcs_config.pin_based_exec_ctrl & |
| 1143 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1144 | } |
| 1145 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1146 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1147 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1148 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1149 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static inline bool cpu_has_vmx_apicv(void) |
| 1153 | { |
| 1154 | return cpu_has_vmx_apic_register_virt() && |
| 1155 | cpu_has_vmx_virtual_intr_delivery() && |
| 1156 | cpu_has_vmx_posted_intr(); |
| 1157 | } |
| 1158 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1159 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1160 | { |
| 1161 | return cpu_has_vmx_tpr_shadow() && |
| 1162 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1163 | } |
| 1164 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1165 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1166 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1167 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1168 | } |
| 1169 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1170 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1171 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1172 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1173 | } |
| 1174 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1175 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1176 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1177 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1180 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1181 | { |
| 1182 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1183 | } |
| 1184 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1185 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1186 | { |
| 1187 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1188 | } |
| 1189 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1190 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1191 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1192 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1195 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1196 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1197 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1200 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1201 | { |
| 1202 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1203 | } |
| 1204 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1205 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1206 | { |
| 1207 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1208 | } |
| 1209 | |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1210 | static inline bool cpu_has_vmx_invvpid(void) |
| 1211 | { |
| 1212 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1213 | } |
| 1214 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1215 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1216 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1217 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1218 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1219 | } |
| 1220 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1221 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1222 | { |
| 1223 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1224 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1225 | } |
| 1226 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1227 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1228 | { |
| 1229 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1230 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1231 | } |
| 1232 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1233 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1234 | { |
| 1235 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1236 | } |
| 1237 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1238 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1239 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1240 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1243 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1244 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1245 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1246 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1247 | } |
| 1248 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1249 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1250 | { |
| 1251 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1252 | SECONDARY_EXEC_RDTSCP; |
| 1253 | } |
| 1254 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1255 | static inline bool cpu_has_vmx_invpcid(void) |
| 1256 | { |
| 1257 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1258 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1259 | } |
| 1260 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1261 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 1262 | { |
| 1263 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1264 | } |
| 1265 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1266 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1267 | { |
| 1268 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1269 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1270 | } |
| 1271 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1272 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1273 | { |
| 1274 | u64 vmx_msr; |
| 1275 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1276 | /* check if the cpu supports writing r/o exit information fields */ |
| 1277 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1278 | return false; |
| 1279 | |
| 1280 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1281 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1282 | } |
| 1283 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1284 | static inline bool cpu_has_vmx_pml(void) |
| 1285 | { |
| 1286 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1287 | } |
| 1288 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1289 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1290 | { |
| 1291 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1292 | SECONDARY_EXEC_TSC_SCALING; |
| 1293 | } |
| 1294 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1295 | static inline bool report_flexpriority(void) |
| 1296 | { |
| 1297 | return flexpriority_enabled; |
| 1298 | } |
| 1299 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1300 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1301 | { |
| 1302 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1303 | } |
| 1304 | |
| 1305 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1306 | { |
| 1307 | return (vmcs12->cpu_based_vm_exec_control & |
| 1308 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1309 | (vmcs12->secondary_vm_exec_control & bit); |
| 1310 | } |
| 1311 | |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 1312 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1313 | { |
| 1314 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1315 | } |
| 1316 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1317 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1318 | { |
| 1319 | return vmcs12->pin_based_vm_exec_control & |
| 1320 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1321 | } |
| 1322 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1323 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1324 | { |
| 1325 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1326 | } |
| 1327 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1328 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1329 | { |
| 1330 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) && |
| 1331 | vmx_xsaves_supported(); |
| 1332 | } |
| 1333 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1334 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1335 | { |
| 1336 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1337 | } |
| 1338 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1339 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1340 | { |
| 1341 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1342 | } |
| 1343 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1344 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1345 | { |
| 1346 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1347 | } |
| 1348 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1349 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1350 | { |
| 1351 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1352 | } |
| 1353 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1354 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1355 | { |
| 1356 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1357 | } |
| 1358 | |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1359 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1360 | { |
| 1361 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1362 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1363 | } |
| 1364 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1365 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1366 | u32 exit_intr_info, |
| 1367 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1368 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1369 | struct vmcs12 *vmcs12, |
| 1370 | u32 reason, unsigned long qualification); |
| 1371 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1372 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1373 | { |
| 1374 | int i; |
| 1375 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1376 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1377 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1378 | return i; |
| 1379 | return -1; |
| 1380 | } |
| 1381 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1382 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1383 | { |
| 1384 | struct { |
| 1385 | u64 vpid : 16; |
| 1386 | u64 rsvd : 48; |
| 1387 | u64 gva; |
| 1388 | } operand = { vpid, 0, gva }; |
| 1389 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1390 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1391 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1392 | "; ja 1f ; ud2 ; 1:" |
| 1393 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1394 | } |
| 1395 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1396 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1397 | { |
| 1398 | struct { |
| 1399 | u64 eptp, gpa; |
| 1400 | } operand = {eptp, gpa}; |
| 1401 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1402 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1403 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1404 | "; ja 1f ; ud2 ; 1:\n" |
| 1405 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1406 | } |
| 1407 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1408 | 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] | 1409 | { |
| 1410 | int i; |
| 1411 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1412 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1413 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1414 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1415 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1418 | static void vmcs_clear(struct vmcs *vmcs) |
| 1419 | { |
| 1420 | u64 phys_addr = __pa(vmcs); |
| 1421 | u8 error; |
| 1422 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1423 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1424 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1425 | : "cc", "memory"); |
| 1426 | if (error) |
| 1427 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1428 | vmcs, phys_addr); |
| 1429 | } |
| 1430 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1431 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1432 | { |
| 1433 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1434 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1435 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1436 | loaded_vmcs->cpu = -1; |
| 1437 | loaded_vmcs->launched = 0; |
| 1438 | } |
| 1439 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1440 | static void vmcs_load(struct vmcs *vmcs) |
| 1441 | { |
| 1442 | u64 phys_addr = __pa(vmcs); |
| 1443 | u8 error; |
| 1444 | |
| 1445 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1446 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1447 | : "cc", "memory"); |
| 1448 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1449 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1450 | vmcs, phys_addr); |
| 1451 | } |
| 1452 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1453 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1454 | /* |
| 1455 | * This bitmap is used to indicate whether the vmclear |
| 1456 | * operation is enabled on all cpus. All disabled by |
| 1457 | * default. |
| 1458 | */ |
| 1459 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1460 | |
| 1461 | static inline void crash_enable_local_vmclear(int cpu) |
| 1462 | { |
| 1463 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1464 | } |
| 1465 | |
| 1466 | static inline void crash_disable_local_vmclear(int cpu) |
| 1467 | { |
| 1468 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1469 | } |
| 1470 | |
| 1471 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1472 | { |
| 1473 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1474 | } |
| 1475 | |
| 1476 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1477 | { |
| 1478 | int cpu = raw_smp_processor_id(); |
| 1479 | struct loaded_vmcs *v; |
| 1480 | |
| 1481 | if (!crash_local_vmclear_enabled(cpu)) |
| 1482 | return; |
| 1483 | |
| 1484 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1485 | loaded_vmcss_on_cpu_link) |
| 1486 | vmcs_clear(v->vmcs); |
| 1487 | } |
| 1488 | #else |
| 1489 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1490 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1491 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1492 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1493 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1494 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1495 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1496 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1497 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1498 | if (loaded_vmcs->cpu != cpu) |
| 1499 | return; /* vcpu migration can race with cpu offline */ |
| 1500 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1501 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1502 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1503 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1504 | |
| 1505 | /* |
| 1506 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1507 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1508 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1509 | * then adds the vmcs into percpu list before it is deleted. |
| 1510 | */ |
| 1511 | smp_wmb(); |
| 1512 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1513 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1514 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1517 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1518 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1519 | int cpu = loaded_vmcs->cpu; |
| 1520 | |
| 1521 | if (cpu != -1) |
| 1522 | smp_call_function_single(cpu, |
| 1523 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1524 | } |
| 1525 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1526 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1527 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1528 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1529 | return; |
| 1530 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1531 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1532 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1533 | } |
| 1534 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1535 | static inline void vpid_sync_vcpu_global(void) |
| 1536 | { |
| 1537 | if (cpu_has_vmx_invvpid_global()) |
| 1538 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1539 | } |
| 1540 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1541 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1542 | { |
| 1543 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1544 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1545 | else |
| 1546 | vpid_sync_vcpu_global(); |
| 1547 | } |
| 1548 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1549 | static inline void ept_sync_global(void) |
| 1550 | { |
| 1551 | if (cpu_has_vmx_invept_global()) |
| 1552 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1553 | } |
| 1554 | |
| 1555 | static inline void ept_sync_context(u64 eptp) |
| 1556 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1557 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1558 | if (cpu_has_vmx_invept_context()) |
| 1559 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1560 | else |
| 1561 | ept_sync_global(); |
| 1562 | } |
| 1563 | } |
| 1564 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1565 | static __always_inline void vmcs_check16(unsigned long field) |
| 1566 | { |
| 1567 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1568 | "16-bit accessor invalid for 64-bit field"); |
| 1569 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1570 | "16-bit accessor invalid for 64-bit high field"); |
| 1571 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1572 | "16-bit accessor invalid for 32-bit high field"); |
| 1573 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1574 | "16-bit accessor invalid for natural width field"); |
| 1575 | } |
| 1576 | |
| 1577 | static __always_inline void vmcs_check32(unsigned long field) |
| 1578 | { |
| 1579 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1580 | "32-bit accessor invalid for 16-bit field"); |
| 1581 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1582 | "32-bit accessor invalid for natural width field"); |
| 1583 | } |
| 1584 | |
| 1585 | static __always_inline void vmcs_check64(unsigned long field) |
| 1586 | { |
| 1587 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1588 | "64-bit accessor invalid for 16-bit field"); |
| 1589 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1590 | "64-bit accessor invalid for 64-bit high field"); |
| 1591 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1592 | "64-bit accessor invalid for 32-bit field"); |
| 1593 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1594 | "64-bit accessor invalid for natural width field"); |
| 1595 | } |
| 1596 | |
| 1597 | static __always_inline void vmcs_checkl(unsigned long field) |
| 1598 | { |
| 1599 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1600 | "Natural width accessor invalid for 16-bit field"); |
| 1601 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1602 | "Natural width accessor invalid for 64-bit field"); |
| 1603 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1604 | "Natural width accessor invalid for 64-bit high field"); |
| 1605 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1606 | "Natural width accessor invalid for 32-bit field"); |
| 1607 | } |
| 1608 | |
| 1609 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1610 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1611 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1612 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1613 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1614 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1615 | return value; |
| 1616 | } |
| 1617 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1618 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1619 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1620 | vmcs_check16(field); |
| 1621 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1622 | } |
| 1623 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1624 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1625 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1626 | vmcs_check32(field); |
| 1627 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1628 | } |
| 1629 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1630 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1631 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1632 | vmcs_check64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1633 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1634 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1635 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1636 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1637 | #endif |
| 1638 | } |
| 1639 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1640 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 1641 | { |
| 1642 | vmcs_checkl(field); |
| 1643 | return __vmcs_readl(field); |
| 1644 | } |
| 1645 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1646 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1647 | { |
| 1648 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1649 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1650 | dump_stack(); |
| 1651 | } |
| 1652 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1653 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1654 | { |
| 1655 | u8 error; |
| 1656 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1657 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1658 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1659 | if (unlikely(error)) |
| 1660 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1661 | } |
| 1662 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1663 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1664 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1665 | vmcs_check16(field); |
| 1666 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1667 | } |
| 1668 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1669 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1670 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1671 | vmcs_check32(field); |
| 1672 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1675 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1676 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1677 | vmcs_check64(field); |
| 1678 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1679 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1680 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1681 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1682 | #endif |
| 1683 | } |
| 1684 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1685 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1686 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1687 | vmcs_checkl(field); |
| 1688 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1689 | } |
| 1690 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1691 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1692 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1693 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1694 | "vmcs_clear_bits does not support 64-bit fields"); |
| 1695 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 1696 | } |
| 1697 | |
| 1698 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 1699 | { |
| 1700 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1701 | "vmcs_set_bits does not support 64-bit fields"); |
| 1702 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1703 | } |
| 1704 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1705 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1706 | { |
| 1707 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 1708 | } |
| 1709 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1710 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1711 | { |
| 1712 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 1713 | vmx->vm_entry_controls_shadow = val; |
| 1714 | } |
| 1715 | |
| 1716 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1717 | { |
| 1718 | if (vmx->vm_entry_controls_shadow != val) |
| 1719 | vm_entry_controls_init(vmx, val); |
| 1720 | } |
| 1721 | |
| 1722 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 1723 | { |
| 1724 | return vmx->vm_entry_controls_shadow; |
| 1725 | } |
| 1726 | |
| 1727 | |
| 1728 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1729 | { |
| 1730 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 1731 | } |
| 1732 | |
| 1733 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1734 | { |
| 1735 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 1736 | } |
| 1737 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1738 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1739 | { |
| 1740 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 1741 | } |
| 1742 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1743 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1744 | { |
| 1745 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 1746 | vmx->vm_exit_controls_shadow = val; |
| 1747 | } |
| 1748 | |
| 1749 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1750 | { |
| 1751 | if (vmx->vm_exit_controls_shadow != val) |
| 1752 | vm_exit_controls_init(vmx, val); |
| 1753 | } |
| 1754 | |
| 1755 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 1756 | { |
| 1757 | return vmx->vm_exit_controls_shadow; |
| 1758 | } |
| 1759 | |
| 1760 | |
| 1761 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1762 | { |
| 1763 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 1764 | } |
| 1765 | |
| 1766 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1767 | { |
| 1768 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 1769 | } |
| 1770 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1771 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1772 | { |
| 1773 | vmx->segment_cache.bitmask = 0; |
| 1774 | } |
| 1775 | |
| 1776 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1777 | unsigned field) |
| 1778 | { |
| 1779 | bool ret; |
| 1780 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1781 | |
| 1782 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1783 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1784 | vmx->segment_cache.bitmask = 0; |
| 1785 | } |
| 1786 | ret = vmx->segment_cache.bitmask & mask; |
| 1787 | vmx->segment_cache.bitmask |= mask; |
| 1788 | return ret; |
| 1789 | } |
| 1790 | |
| 1791 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1792 | { |
| 1793 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1794 | |
| 1795 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1796 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1797 | return *p; |
| 1798 | } |
| 1799 | |
| 1800 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1801 | { |
| 1802 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1803 | |
| 1804 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1805 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1806 | return *p; |
| 1807 | } |
| 1808 | |
| 1809 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1810 | { |
| 1811 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1812 | |
| 1813 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1814 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1815 | return *p; |
| 1816 | } |
| 1817 | |
| 1818 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1819 | { |
| 1820 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1821 | |
| 1822 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1823 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1824 | return *p; |
| 1825 | } |
| 1826 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1827 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1828 | { |
| 1829 | u32 eb; |
| 1830 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1831 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1832 | (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1833 | if ((vcpu->guest_debug & |
| 1834 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1835 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1836 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1837 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1838 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1839 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1840 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1841 | if (vcpu->fpu_active) |
| 1842 | eb &= ~(1u << NM_VECTOR); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1843 | |
| 1844 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1845 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1846 | * them to L1. When running L2, we will only handle the exceptions |
| 1847 | * specified above if L1 did not want them. |
| 1848 | */ |
| 1849 | if (is_guest_mode(vcpu)) |
| 1850 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1851 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1852 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1853 | } |
| 1854 | |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1855 | /* |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1856 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 1857 | */ |
| 1858 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 1859 | { |
| 1860 | unsigned long *msr_bitmap; |
| 1861 | int f = sizeof(unsigned long); |
| 1862 | |
| 1863 | if (!cpu_has_vmx_msr_bitmap()) |
| 1864 | return true; |
| 1865 | |
| 1866 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 1867 | |
| 1868 | if (msr <= 0x1fff) { |
| 1869 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 1870 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 1871 | msr &= 0x1fff; |
| 1872 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 1873 | } |
| 1874 | |
| 1875 | return true; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1879 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 1880 | */ |
| 1881 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 1882 | { |
| 1883 | unsigned long *msr_bitmap; |
| 1884 | int f = sizeof(unsigned long); |
| 1885 | |
| 1886 | if (!cpu_has_vmx_msr_bitmap()) |
| 1887 | return true; |
| 1888 | |
| 1889 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 1890 | |
| 1891 | if (msr <= 0x1fff) { |
| 1892 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 1893 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 1894 | msr &= 0x1fff; |
| 1895 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 1896 | } |
| 1897 | |
| 1898 | return true; |
| 1899 | } |
| 1900 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1901 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1902 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1903 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1904 | vm_entry_controls_clearbit(vmx, entry); |
| 1905 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1906 | } |
| 1907 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1908 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1909 | { |
| 1910 | unsigned i; |
| 1911 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1912 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1913 | switch (msr) { |
| 1914 | case MSR_EFER: |
| 1915 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1916 | clear_atomic_switch_msr_special(vmx, |
| 1917 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1918 | VM_EXIT_LOAD_IA32_EFER); |
| 1919 | return; |
| 1920 | } |
| 1921 | break; |
| 1922 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1923 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1924 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1925 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1926 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 1927 | return; |
| 1928 | } |
| 1929 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1930 | } |
| 1931 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1932 | for (i = 0; i < m->nr; ++i) |
| 1933 | if (m->guest[i].index == msr) |
| 1934 | break; |
| 1935 | |
| 1936 | if (i == m->nr) |
| 1937 | return; |
| 1938 | --m->nr; |
| 1939 | m->guest[i] = m->guest[m->nr]; |
| 1940 | m->host[i] = m->host[m->nr]; |
| 1941 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1942 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1943 | } |
| 1944 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1945 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1946 | unsigned long entry, unsigned long exit, |
| 1947 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 1948 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1949 | { |
| 1950 | vmcs_write64(guest_val_vmcs, guest_val); |
| 1951 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1952 | vm_entry_controls_setbit(vmx, entry); |
| 1953 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1954 | } |
| 1955 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1956 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1957 | u64 guest_val, u64 host_val) |
| 1958 | { |
| 1959 | unsigned i; |
| 1960 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1961 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1962 | switch (msr) { |
| 1963 | case MSR_EFER: |
| 1964 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1965 | add_atomic_switch_msr_special(vmx, |
| 1966 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1967 | VM_EXIT_LOAD_IA32_EFER, |
| 1968 | GUEST_IA32_EFER, |
| 1969 | HOST_IA32_EFER, |
| 1970 | guest_val, host_val); |
| 1971 | return; |
| 1972 | } |
| 1973 | break; |
| 1974 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1975 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1976 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1977 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1978 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1979 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 1980 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 1981 | guest_val, host_val); |
| 1982 | return; |
| 1983 | } |
| 1984 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 1985 | case MSR_IA32_PEBS_ENABLE: |
| 1986 | /* PEBS needs a quiescent period after being disabled (to write |
| 1987 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 1988 | * provide that period, so a CPU could write host's record into |
| 1989 | * guest's memory. |
| 1990 | */ |
| 1991 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1992 | } |
| 1993 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1994 | for (i = 0; i < m->nr; ++i) |
| 1995 | if (m->guest[i].index == msr) |
| 1996 | break; |
| 1997 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1998 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 1999 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2000 | "Can't add msr %x\n", msr); |
| 2001 | return; |
| 2002 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2003 | ++m->nr; |
| 2004 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2005 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2006 | } |
| 2007 | |
| 2008 | m->guest[i].index = msr; |
| 2009 | m->guest[i].value = guest_val; |
| 2010 | m->host[i].index = msr; |
| 2011 | m->host[i].value = host_val; |
| 2012 | } |
| 2013 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2014 | static void reload_tss(void) |
| 2015 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2016 | /* |
| 2017 | * VT restores TR but not its size. Useless. |
| 2018 | */ |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2019 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 2020 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2021 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2022 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2023 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 2024 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2025 | } |
| 2026 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2027 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2028 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2029 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2030 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2031 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2032 | if (!enable_ept) { |
| 2033 | /* |
| 2034 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2035 | * host CPUID is more efficient than testing guest CPUID |
| 2036 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2037 | */ |
| 2038 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2039 | guest_efer |= EFER_NX; |
| 2040 | else if (!(guest_efer & EFER_NX)) |
| 2041 | ignore_bits |= EFER_NX; |
| 2042 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2043 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2044 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2045 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2046 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2047 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2048 | #ifdef CONFIG_X86_64 |
| 2049 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2050 | /* SCE is meaningful only in long mode on Intel */ |
| 2051 | if (guest_efer & EFER_LMA) |
| 2052 | ignore_bits &= ~(u64)EFER_SCE; |
| 2053 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2054 | |
| 2055 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2056 | |
| 2057 | /* |
| 2058 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2059 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2060 | * atomically, since it's faster than switching it manually. |
| 2061 | */ |
| 2062 | if (cpu_has_load_ia32_efer || |
| 2063 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2064 | if (!(guest_efer & EFER_LMA)) |
| 2065 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2066 | if (guest_efer != host_efer) |
| 2067 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2068 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2069 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2070 | } else { |
| 2071 | guest_efer &= ~ignore_bits; |
| 2072 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2073 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2074 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2075 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2076 | |
| 2077 | return true; |
| 2078 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2079 | } |
| 2080 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2081 | static unsigned long segment_base(u16 selector) |
| 2082 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2083 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2084 | struct desc_struct *d; |
| 2085 | unsigned long table_base; |
| 2086 | unsigned long v; |
| 2087 | |
| 2088 | if (!(selector & ~3)) |
| 2089 | return 0; |
| 2090 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2091 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2092 | |
| 2093 | if (selector & 4) { /* from ldt */ |
| 2094 | u16 ldt_selector = kvm_read_ldt(); |
| 2095 | |
| 2096 | if (!(ldt_selector & ~3)) |
| 2097 | return 0; |
| 2098 | |
| 2099 | table_base = segment_base(ldt_selector); |
| 2100 | } |
| 2101 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 2102 | v = get_desc_base(d); |
| 2103 | #ifdef CONFIG_X86_64 |
| 2104 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 2105 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 2106 | #endif |
| 2107 | return v; |
| 2108 | } |
| 2109 | |
| 2110 | static inline unsigned long kvm_read_tr_base(void) |
| 2111 | { |
| 2112 | u16 tr; |
| 2113 | asm("str %0" : "=g"(tr)); |
| 2114 | return segment_base(tr); |
| 2115 | } |
| 2116 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2117 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2118 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2119 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2120 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2121 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2122 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2123 | return; |
| 2124 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2125 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2126 | /* |
| 2127 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2128 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2129 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2130 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2131 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2132 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2133 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2134 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2135 | vmx->host_state.fs_reload_needed = 0; |
| 2136 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2137 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2138 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2139 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2140 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2141 | if (!(vmx->host_state.gs_sel & 7)) |
| 2142 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2143 | else { |
| 2144 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2145 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2149 | savesegment(ds, vmx->host_state.ds_sel); |
| 2150 | savesegment(es, vmx->host_state.es_sel); |
| 2151 | #endif |
| 2152 | |
| 2153 | #ifdef CONFIG_X86_64 |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2154 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 2155 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 2156 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2157 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2158 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2159 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2160 | |
| 2161 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2162 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 2163 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2164 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2165 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2166 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 2167 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2168 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2169 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2170 | vmx->guest_msrs[i].data, |
| 2171 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2172 | } |
| 2173 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2174 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2175 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2176 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2177 | return; |
| 2178 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2179 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2180 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2181 | #ifdef CONFIG_X86_64 |
| 2182 | if (is_long_mode(&vmx->vcpu)) |
| 2183 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2184 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2185 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2186 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2187 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2188 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2189 | #else |
| 2190 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2191 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2192 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2193 | if (vmx->host_state.fs_reload_needed) |
| 2194 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2195 | #ifdef CONFIG_X86_64 |
| 2196 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2197 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2198 | loadsegment(es, vmx->host_state.es_sel); |
| 2199 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2200 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2201 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2202 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2203 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2204 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2205 | if (vmx->host_state.msr_host_bndcfgs) |
| 2206 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 2207 | /* |
| 2208 | * If the FPU is not active (through the host task or |
| 2209 | * the guest vcpu), then restore the cr0.TS bit. |
| 2210 | */ |
Ingo Molnar | 3c6dffa | 2015-04-28 12:28:08 +0200 | [diff] [blame] | 2211 | if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded) |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 2212 | stts(); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2213 | load_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2214 | } |
| 2215 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2216 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2217 | { |
| 2218 | preempt_disable(); |
| 2219 | __vmx_load_host_state(vmx); |
| 2220 | preempt_enable(); |
| 2221 | } |
| 2222 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2223 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2224 | { |
| 2225 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2226 | struct pi_desc old, new; |
| 2227 | unsigned int dest; |
| 2228 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2229 | /* |
| 2230 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2231 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2232 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2233 | * code easier, and CPU migration is not a fast path. |
| 2234 | */ |
| 2235 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2236 | return; |
| 2237 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2238 | /* |
| 2239 | * First handle the simple case where no cmpxchg is necessary; just |
| 2240 | * allow posting non-urgent interrupts. |
| 2241 | * |
| 2242 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2243 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2244 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2245 | * PI.NDST. |
| 2246 | */ |
| 2247 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2248 | vcpu->cpu == cpu) { |
| 2249 | pi_clear_sn(pi_desc); |
| 2250 | return; |
| 2251 | } |
| 2252 | |
| 2253 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2254 | do { |
| 2255 | old.control = new.control = pi_desc->control; |
| 2256 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2257 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2258 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2259 | if (x2apic_enabled()) |
| 2260 | new.ndst = dest; |
| 2261 | else |
| 2262 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2263 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2264 | new.sn = 0; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2265 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2266 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2267 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2268 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2269 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2270 | { |
| 2271 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2272 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2273 | } |
| 2274 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2275 | /* |
| 2276 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2277 | * vcpu mutex is already taken. |
| 2278 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2279 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2280 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2281 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2282 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2283 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2284 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2285 | if (!vmm_exclusive) |
| 2286 | kvm_cpu_vmxon(phys_addr); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2287 | else if (!already_loaded) |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2288 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2289 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2290 | if (!already_loaded) { |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2291 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2292 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2293 | |
| 2294 | /* |
| 2295 | * Read loaded_vmcs->cpu should be before fetching |
| 2296 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2297 | * See the comments in __loaded_vmcs_clear(). |
| 2298 | */ |
| 2299 | smp_rmb(); |
| 2300 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2301 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2302 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2303 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2304 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2308 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2309 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2310 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | if (!already_loaded) { |
| 2314 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
| 2315 | unsigned long sysenter_esp; |
| 2316 | |
| 2317 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2318 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2319 | /* |
| 2320 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 2321 | * processors. |
| 2322 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2323 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2324 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2325 | |
| 2326 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2327 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2328 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2329 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2330 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2331 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2332 | /* Setup TSC multiplier */ |
| 2333 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2334 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2335 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2336 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2337 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2338 | vmx->host_pkru = read_pkru(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2342 | { |
| 2343 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2344 | |
| 2345 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2346 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2347 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2348 | return; |
| 2349 | |
| 2350 | /* Set SN when the vCPU is preempted */ |
| 2351 | if (vcpu->preempted) |
| 2352 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2356 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2357 | vmx_vcpu_pi_put(vcpu); |
| 2358 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2359 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2360 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2361 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 2362 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2363 | kvm_cpu_vmxoff(); |
| 2364 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2365 | } |
| 2366 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2367 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 2368 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2369 | ulong cr0; |
| 2370 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2371 | if (vcpu->fpu_active) |
| 2372 | return; |
| 2373 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2374 | cr0 = vmcs_readl(GUEST_CR0); |
| 2375 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 2376 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 2377 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2378 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2379 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2380 | if (is_guest_mode(vcpu)) |
| 2381 | vcpu->arch.cr0_guest_owned_bits &= |
| 2382 | ~get_vmcs12(vcpu)->cr0_guest_host_mask; |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2383 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2384 | } |
| 2385 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2386 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2387 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2388 | /* |
| 2389 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2390 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2391 | * its hypervisor (cr0_read_shadow). |
| 2392 | */ |
| 2393 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2394 | { |
| 2395 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2396 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2397 | } |
| 2398 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2399 | { |
| 2400 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2401 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2402 | } |
| 2403 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2404 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 2405 | { |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2406 | /* Note that there is no vcpu->fpu_active = 0 here. The caller must |
| 2407 | * set this *before* calling this function. |
| 2408 | */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2409 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2410 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2411 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2412 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 2413 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2414 | if (is_guest_mode(vcpu)) { |
| 2415 | /* |
| 2416 | * L1's specified read shadow might not contain the TS bit, |
| 2417 | * so now that we turned on shadowing of this bit, we need to |
| 2418 | * set this bit of the shadow. Like in nested_vmx_run we need |
| 2419 | * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet |
| 2420 | * up-to-date here because we just decached cr0.TS (and we'll |
| 2421 | * only update vmcs12->guest_cr0 on nested exit). |
| 2422 | */ |
| 2423 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2424 | vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) | |
| 2425 | (vcpu->arch.cr0 & X86_CR0_TS); |
| 2426 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2427 | } else |
| 2428 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2429 | } |
| 2430 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2431 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2432 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2433 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2434 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2435 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2436 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2437 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2438 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2439 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2440 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2441 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2442 | } |
| 2443 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2444 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2445 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2449 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2450 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2451 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2452 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2453 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2454 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2455 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2456 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 2457 | } |
| 2458 | |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 2459 | static u32 vmx_get_pkru(struct kvm_vcpu *vcpu) |
| 2460 | { |
| 2461 | return to_vmx(vcpu)->guest_pkru; |
| 2462 | } |
| 2463 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2464 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2465 | { |
| 2466 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2467 | int ret = 0; |
| 2468 | |
| 2469 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2470 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2471 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2472 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2473 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2474 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2478 | { |
| 2479 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2480 | u32 interruptibility = interruptibility_old; |
| 2481 | |
| 2482 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2483 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2484 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2485 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2486 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2487 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2488 | |
| 2489 | if ((interruptibility != interruptibility_old)) |
| 2490 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2491 | } |
| 2492 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2493 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2494 | { |
| 2495 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2496 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2497 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2498 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2499 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2500 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2501 | /* skipping an emulated instruction also counts */ |
| 2502 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2503 | } |
| 2504 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2505 | /* |
| 2506 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2507 | * 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] | 2508 | */ |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2509 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2510 | { |
| 2511 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2512 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2513 | if (!(vmcs12->exception_bitmap & (1u << nr))) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2514 | return 0; |
| 2515 | |
Wanpeng Li | a29fd27 | 2017-06-05 05:19:09 -0700 | [diff] [blame] | 2516 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 2517 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 2518 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2519 | return 1; |
| 2520 | } |
| 2521 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2522 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 2523 | bool has_error_code, u32 error_code, |
| 2524 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2525 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2526 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2527 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2528 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2529 | if (!reinject && is_guest_mode(vcpu) && |
| 2530 | nested_vmx_check_exception(vcpu, nr)) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2531 | return; |
| 2532 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2533 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2534 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2535 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2536 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2537 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2538 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 2539 | int inc_eip = 0; |
| 2540 | if (kvm_exception_is_soft(nr)) |
| 2541 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 2542 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2543 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2544 | return; |
| 2545 | } |
| 2546 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2547 | if (kvm_exception_is_soft(nr)) { |
| 2548 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2549 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2550 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2551 | } else |
| 2552 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2553 | |
| 2554 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2555 | } |
| 2556 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2557 | static bool vmx_rdtscp_supported(void) |
| 2558 | { |
| 2559 | return cpu_has_vmx_rdtscp(); |
| 2560 | } |
| 2561 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2562 | static bool vmx_invpcid_supported(void) |
| 2563 | { |
| 2564 | return cpu_has_vmx_invpcid() && enable_ept; |
| 2565 | } |
| 2566 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2567 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2568 | * Swap MSR entry in host/guest MSR entry array. |
| 2569 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2570 | 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] | 2571 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2572 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2573 | |
| 2574 | tmp = vmx->guest_msrs[to]; |
| 2575 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 2576 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2580 | * Set up the vmcs to automatically save and restore system |
| 2581 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 2582 | * mode, as fiddling with msrs is very expensive. |
| 2583 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2584 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2585 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2586 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2587 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2588 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2589 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2590 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2591 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2592 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2593 | move_msr_up(vmx, index, save_nmsrs++); |
| 2594 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2595 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2596 | move_msr_up(vmx, index, save_nmsrs++); |
| 2597 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2598 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2599 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2600 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 2601 | if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2602 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2603 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2604 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2605 | * if efer.sce is enabled. |
| 2606 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2607 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2608 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2609 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2610 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2611 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2612 | index = __find_msr_index(vmx, MSR_EFER); |
| 2613 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2614 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2615 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2616 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2617 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2618 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2619 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2623 | * reads and returns guest's timestamp counter "register" |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2624 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
| 2625 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2626 | */ |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2627 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2628 | { |
| 2629 | u64 host_tsc, tsc_offset; |
| 2630 | |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 2631 | host_tsc = rdtsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2632 | tsc_offset = vmcs_read64(TSC_OFFSET); |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2633 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2637 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2638 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2639 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2640 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2641 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2642 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2643 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 2644 | * to the spec, this should set L1's TSC; The offset that L1 |
| 2645 | * set for L2 remains unchanged, and still needs to be added |
| 2646 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2647 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2648 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2649 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 2650 | vmcs12 = get_vmcs12(vcpu); |
| 2651 | vmcs_write64(TSC_OFFSET, offset + |
| 2652 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 2653 | vmcs12->tsc_offset : 0)); |
| 2654 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2655 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 2656 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2657 | vmcs_write64(TSC_OFFSET, offset); |
| 2658 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2659 | } |
| 2660 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 2661 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 2662 | { |
| 2663 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 2664 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 2665 | } |
| 2666 | |
| 2667 | /* |
| 2668 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 2669 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 2670 | * all guests if the "nested" module option is off, and can also be disabled |
| 2671 | * for a single guest by disabling its VMX cpuid bit. |
| 2672 | */ |
| 2673 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 2674 | { |
| 2675 | return nested && guest_cpuid_has_vmx(vcpu); |
| 2676 | } |
| 2677 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2678 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2679 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 2680 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 2681 | * The same values should also be used to verify that vmcs12 control fields are |
| 2682 | * valid during nested entry from L1 to L2. |
| 2683 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 2684 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 2685 | * bit in the high half is on if the corresponding bit in the control field |
| 2686 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2687 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2688 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2689 | { |
| 2690 | /* |
| 2691 | * Note that as a general rule, the high half of the MSRs (bits in |
| 2692 | * the control fields which may be 1) should be initialized by the |
| 2693 | * intersection of the underlying hardware's MSR (i.e., features which |
| 2694 | * can be supported) and the list of features we want to expose - |
| 2695 | * because they are known to be properly supported in our code. |
| 2696 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 2697 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 2698 | * reason is that if one of these bits is necessary, it will appear |
| 2699 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 2700 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 2701 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 2702 | * These rules have exceptions below. |
| 2703 | */ |
| 2704 | |
| 2705 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 2706 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2707 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2708 | vmx->nested.nested_vmx_pinbased_ctls_high); |
| 2709 | vmx->nested.nested_vmx_pinbased_ctls_low |= |
| 2710 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2711 | vmx->nested.nested_vmx_pinbased_ctls_high &= |
| 2712 | PIN_BASED_EXT_INTR_MASK | |
| 2713 | PIN_BASED_NMI_EXITING | |
| 2714 | PIN_BASED_VIRTUAL_NMIS; |
| 2715 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2716 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 2717 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2718 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 2719 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2720 | PIN_BASED_POSTED_INTR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2721 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2722 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2723 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2724 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2725 | vmx->nested.nested_vmx_exit_ctls_high); |
| 2726 | vmx->nested.nested_vmx_exit_ctls_low = |
| 2727 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2728 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2729 | vmx->nested.nested_vmx_exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2730 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2731 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2732 | #endif |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2733 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2734 | vmx->nested.nested_vmx_exit_ctls_high |= |
| 2735 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2736 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2737 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 2738 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2739 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2740 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2741 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2742 | /* We support free control of debug control saving. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2743 | vmx->nested.nested_vmx_true_exit_ctls_low = |
| 2744 | vmx->nested.nested_vmx_exit_ctls_low & |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2745 | ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 2746 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2747 | /* entry controls */ |
| 2748 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2749 | vmx->nested.nested_vmx_entry_ctls_low, |
| 2750 | vmx->nested.nested_vmx_entry_ctls_high); |
| 2751 | vmx->nested.nested_vmx_entry_ctls_low = |
| 2752 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2753 | vmx->nested.nested_vmx_entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2754 | #ifdef CONFIG_X86_64 |
| 2755 | VM_ENTRY_IA32E_MODE | |
| 2756 | #endif |
| 2757 | VM_ENTRY_LOAD_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2758 | vmx->nested.nested_vmx_entry_ctls_high |= |
| 2759 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2760 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2761 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2762 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2763 | /* We support free control of debug control loading. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2764 | vmx->nested.nested_vmx_true_entry_ctls_low = |
| 2765 | vmx->nested.nested_vmx_entry_ctls_low & |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2766 | ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 2767 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2768 | /* cpu-based controls */ |
| 2769 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2770 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2771 | vmx->nested.nested_vmx_procbased_ctls_high); |
| 2772 | vmx->nested.nested_vmx_procbased_ctls_low = |
| 2773 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2774 | vmx->nested.nested_vmx_procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 2775 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 2776 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2777 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 2778 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 2779 | CPU_BASED_CR3_STORE_EXITING | |
| 2780 | #ifdef CONFIG_X86_64 |
| 2781 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 2782 | #endif |
| 2783 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 2784 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 2785 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 2786 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 2787 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2788 | /* |
| 2789 | * We can allow some features even when not supported by the |
| 2790 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 2791 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 2792 | * without MSR bitmaps. |
| 2793 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2794 | vmx->nested.nested_vmx_procbased_ctls_high |= |
| 2795 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 2796 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2797 | |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2798 | /* We support free control of CR3 access interception. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2799 | vmx->nested.nested_vmx_true_procbased_ctls_low = |
| 2800 | vmx->nested.nested_vmx_procbased_ctls_low & |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2801 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 2802 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2803 | /* secondary cpu-based controls */ |
| 2804 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2805 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 2806 | vmx->nested.nested_vmx_secondary_ctls_high); |
| 2807 | vmx->nested.nested_vmx_secondary_ctls_low = 0; |
| 2808 | vmx->nested.nested_vmx_secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 2809 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 2810 | SECONDARY_EXEC_RDTSCP | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 2811 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 2812 | SECONDARY_EXEC_ENABLE_VPID | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 2813 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 2814 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 2815 | SECONDARY_EXEC_WBINVD_EXITING | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 2816 | SECONDARY_EXEC_XSAVES; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2817 | |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2818 | if (enable_ept) { |
| 2819 | /* nested EPT: emulate EPT also to L1 */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2820 | vmx->nested.nested_vmx_secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2821 | SECONDARY_EXEC_ENABLE_EPT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2822 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Jan Kiszka | d3134db | 2013-10-23 14:40:31 +0100 | [diff] [blame] | 2823 | VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 2824 | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 2825 | if (cpu_has_vmx_ept_execute_only()) |
| 2826 | vmx->nested.nested_vmx_ept_caps |= |
| 2827 | VMX_EPT_EXECUTE_ONLY_BIT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2828 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 2829 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 2830 | VMX_EPT_EXTENT_CONTEXT_BIT; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2831 | } else |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2832 | vmx->nested.nested_vmx_ept_caps = 0; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2833 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 2834 | /* |
| 2835 | * Old versions of KVM use the single-context version without |
| 2836 | * checking for support, so declare that it is supported even |
| 2837 | * though it is treated as global context. The alternative is |
| 2838 | * not failing the single-context invvpid, and it is worse. |
| 2839 | */ |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2840 | if (enable_vpid) |
| 2841 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 2842 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2843 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 2844 | else |
| 2845 | vmx->nested.nested_vmx_vpid_caps = 0; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 2846 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2847 | if (enable_unrestricted_guest) |
| 2848 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 2849 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2850 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2851 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2852 | rdmsr(MSR_IA32_VMX_MISC, |
| 2853 | vmx->nested.nested_vmx_misc_low, |
| 2854 | vmx->nested.nested_vmx_misc_high); |
| 2855 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 2856 | vmx->nested.nested_vmx_misc_low |= |
| 2857 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2858 | VMX_MISC_ACTIVITY_HLT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2859 | vmx->nested.nested_vmx_misc_high = 0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 2863 | { |
| 2864 | /* |
| 2865 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 2866 | */ |
| 2867 | return ((control & high) | low) == control; |
| 2868 | } |
| 2869 | |
| 2870 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 2871 | { |
| 2872 | return low | ((u64)high << 32); |
| 2873 | } |
| 2874 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2875 | /* Returns 0 on success, non-0 otherwise. */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2876 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2877 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2878 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2879 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2880 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2881 | case MSR_IA32_VMX_BASIC: |
| 2882 | /* |
| 2883 | * This MSR reports some information about VMX support. We |
| 2884 | * should return information about the VMX we emulate for the |
| 2885 | * guest, and the VMCS structure we give it - not about the |
| 2886 | * VMX support of the underlying hardware. |
| 2887 | */ |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2888 | *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2889 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2890 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 2891 | if (cpu_has_vmx_basic_inout()) |
| 2892 | *pdata |= VMX_BASIC_INOUT; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2893 | break; |
| 2894 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2895 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2896 | *pdata = vmx_control_msr( |
| 2897 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2898 | vmx->nested.nested_vmx_pinbased_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2899 | break; |
| 2900 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2901 | *pdata = vmx_control_msr( |
| 2902 | vmx->nested.nested_vmx_true_procbased_ctls_low, |
| 2903 | vmx->nested.nested_vmx_procbased_ctls_high); |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2904 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2905 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2906 | *pdata = vmx_control_msr( |
| 2907 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2908 | vmx->nested.nested_vmx_procbased_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2909 | break; |
| 2910 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2911 | *pdata = vmx_control_msr( |
| 2912 | vmx->nested.nested_vmx_true_exit_ctls_low, |
| 2913 | vmx->nested.nested_vmx_exit_ctls_high); |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2914 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2915 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2916 | *pdata = vmx_control_msr( |
| 2917 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2918 | vmx->nested.nested_vmx_exit_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2919 | break; |
| 2920 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2921 | *pdata = vmx_control_msr( |
| 2922 | vmx->nested.nested_vmx_true_entry_ctls_low, |
| 2923 | vmx->nested.nested_vmx_entry_ctls_high); |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2924 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2925 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2926 | *pdata = vmx_control_msr( |
| 2927 | vmx->nested.nested_vmx_entry_ctls_low, |
| 2928 | vmx->nested.nested_vmx_entry_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2929 | break; |
| 2930 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2931 | *pdata = vmx_control_msr( |
| 2932 | vmx->nested.nested_vmx_misc_low, |
| 2933 | vmx->nested.nested_vmx_misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2934 | break; |
| 2935 | /* |
| 2936 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 2937 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 2938 | * We picked the standard core2 setting. |
| 2939 | */ |
| 2940 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 2941 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 2942 | case MSR_IA32_VMX_CR0_FIXED0: |
| 2943 | *pdata = VMXON_CR0_ALWAYSON; |
| 2944 | break; |
| 2945 | case MSR_IA32_VMX_CR0_FIXED1: |
| 2946 | *pdata = -1ULL; |
| 2947 | break; |
| 2948 | case MSR_IA32_VMX_CR4_FIXED0: |
| 2949 | *pdata = VMXON_CR4_ALWAYSON; |
| 2950 | break; |
| 2951 | case MSR_IA32_VMX_CR4_FIXED1: |
| 2952 | *pdata = -1ULL; |
| 2953 | break; |
| 2954 | case MSR_IA32_VMX_VMCS_ENUM: |
Jan Kiszka | 5381417 | 2014-06-16 13:59:44 +0200 | [diff] [blame] | 2955 | *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2956 | break; |
| 2957 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2958 | *pdata = vmx_control_msr( |
| 2959 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 2960 | vmx->nested.nested_vmx_secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2961 | break; |
| 2962 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2963 | *pdata = vmx->nested.nested_vmx_ept_caps | |
| 2964 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2965 | break; |
| 2966 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2967 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 2968 | } |
| 2969 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2970 | return 0; |
| 2971 | } |
| 2972 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 2973 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 2974 | uint64_t val) |
| 2975 | { |
| 2976 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 2977 | |
| 2978 | return !(val & ~valid_bits); |
| 2979 | } |
| 2980 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2981 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2982 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 2983 | * Returns 0 on success, non-0 otherwise. |
| 2984 | * Assumes vcpu_load() was already called. |
| 2985 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2986 | 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] | 2987 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2988 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2989 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2990 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2991 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2992 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2993 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2994 | break; |
| 2995 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2996 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2997 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2998 | case MSR_KERNEL_GS_BASE: |
| 2999 | vmx_load_host_state(to_vmx(vcpu)); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3000 | msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3001 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3002 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3003 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3004 | return kvm_get_msr_common(vcpu, msr_info); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3005 | case MSR_IA32_TSC: |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 3006 | msr_info->data = guest_read_tsc(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3007 | break; |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3008 | case MSR_IA32_SPEC_CTRL: |
| 3009 | if (!msr_info->host_initiated && |
| 3010 | !guest_cpuid_has_ibrs(vcpu)) |
| 3011 | return 1; |
| 3012 | |
| 3013 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 3014 | break; |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3015 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3016 | if (!msr_info->host_initiated && |
| 3017 | !guest_cpuid_has_arch_capabilities(vcpu)) |
| 3018 | return 1; |
| 3019 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 3020 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3021 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3022 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3023 | break; |
| 3024 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3025 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3026 | break; |
| 3027 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3028 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3029 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3030 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | cce8d2e | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3031 | if (!kvm_mpx_supported() || |
| 3032 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3033 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3034 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3035 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3036 | case MSR_IA32_MCG_EXT_CTL: |
| 3037 | if (!msr_info->host_initiated && |
| 3038 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3039 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3040 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3041 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3042 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3043 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3044 | msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3045 | break; |
| 3046 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3047 | if (!nested_vmx_allowed(vcpu)) |
| 3048 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3049 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3050 | case MSR_IA32_XSS: |
| 3051 | if (!vmx_xsaves_supported()) |
| 3052 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3053 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3054 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3055 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3056 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3057 | return 1; |
| 3058 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3059 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3060 | msr = find_msr_entry(to_vmx(vcpu), msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3061 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3062 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3063 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3064 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3065 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3066 | } |
| 3067 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3068 | return 0; |
| 3069 | } |
| 3070 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3071 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3072 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3073 | /* |
| 3074 | * Writes msr value into into the appropriate "register". |
| 3075 | * Returns 0 on success, non-0 otherwise. |
| 3076 | * Assumes vcpu_load() was already called. |
| 3077 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3078 | 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] | 3079 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3080 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3081 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3082 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3083 | u32 msr_index = msr_info->index; |
| 3084 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3085 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3086 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3087 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3088 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3089 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3090 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3091 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3092 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3093 | vmcs_writel(GUEST_FS_BASE, data); |
| 3094 | break; |
| 3095 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3096 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3097 | vmcs_writel(GUEST_GS_BASE, data); |
| 3098 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3099 | case MSR_KERNEL_GS_BASE: |
| 3100 | vmx_load_host_state(vmx); |
| 3101 | vmx->msr_guest_kernel_gs_base = data; |
| 3102 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3103 | #endif |
| 3104 | case MSR_IA32_SYSENTER_CS: |
| 3105 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3106 | break; |
| 3107 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3108 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3109 | break; |
| 3110 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3111 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3112 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3113 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | cce8d2e | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3114 | if (!kvm_mpx_supported() || |
| 3115 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3116 | return 1; |
Jim Mattson | 07592d6 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3117 | if (is_noncanonical_address(data & PAGE_MASK) || |
| 3118 | (data & MSR_IA32_BNDCFGS_RSVD)) |
| 3119 | return 1; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3120 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3121 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3122 | case MSR_IA32_TSC: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3123 | kvm_write_tsc(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3124 | break; |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3125 | case MSR_IA32_SPEC_CTRL: |
| 3126 | if (!msr_info->host_initiated && |
| 3127 | !guest_cpuid_has_ibrs(vcpu)) |
| 3128 | return 1; |
| 3129 | |
| 3130 | /* The STIBP bit doesn't fault even if it's not advertised */ |
| 3131 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP)) |
| 3132 | return 1; |
| 3133 | |
| 3134 | vmx->spec_ctrl = data; |
| 3135 | |
| 3136 | if (!data) |
| 3137 | break; |
| 3138 | |
| 3139 | /* |
| 3140 | * For non-nested: |
| 3141 | * When it's written (to non-zero) for the first time, pass |
| 3142 | * it through. |
| 3143 | * |
| 3144 | * For nested: |
| 3145 | * The handling of the MSR bitmap for L2 guests is done in |
| 3146 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3147 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3148 | * in the merging. We update the vmcs01 here for L1 as well |
| 3149 | * since it will end up touching the MSR anyway now. |
| 3150 | */ |
| 3151 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 3152 | MSR_IA32_SPEC_CTRL, |
| 3153 | MSR_TYPE_RW); |
| 3154 | break; |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3155 | case MSR_IA32_PRED_CMD: |
| 3156 | if (!msr_info->host_initiated && |
| 3157 | !guest_cpuid_has_ibpb(vcpu)) |
| 3158 | return 1; |
| 3159 | |
| 3160 | if (data & ~PRED_CMD_IBPB) |
| 3161 | return 1; |
| 3162 | |
| 3163 | if (!data) |
| 3164 | break; |
| 3165 | |
| 3166 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 3167 | |
| 3168 | /* |
| 3169 | * For non-nested: |
| 3170 | * When it's written (to non-zero) for the first time, pass |
| 3171 | * it through. |
| 3172 | * |
| 3173 | * For nested: |
| 3174 | * The handling of the MSR bitmap for L2 guests is done in |
| 3175 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3176 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3177 | * in the merging. |
| 3178 | */ |
| 3179 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 3180 | MSR_TYPE_W); |
| 3181 | break; |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3182 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3183 | if (!msr_info->host_initiated) |
| 3184 | return 1; |
| 3185 | vmx->arch_capabilities = data; |
| 3186 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3187 | case MSR_IA32_CR_PAT: |
| 3188 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3189 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3190 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3191 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3192 | vcpu->arch.pat = data; |
| 3193 | break; |
| 3194 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3195 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3196 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3197 | case MSR_IA32_TSC_ADJUST: |
| 3198 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3199 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3200 | case MSR_IA32_MCG_EXT_CTL: |
| 3201 | if ((!msr_info->host_initiated && |
| 3202 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3203 | FEATURE_CONTROL_LMCE)) || |
| 3204 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 3205 | return 1; |
| 3206 | vcpu->arch.mcg_ext_ctl = data; |
| 3207 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3208 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3209 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3210 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3211 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 3212 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3213 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3214 | if (msr_info->host_initiated && data == 0) |
| 3215 | vmx_leave_nested(vcpu); |
| 3216 | break; |
| 3217 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3218 | return 1; /* they are read-only */ |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3219 | case MSR_IA32_XSS: |
| 3220 | if (!vmx_xsaves_supported()) |
| 3221 | return 1; |
| 3222 | /* |
| 3223 | * The only supported bit as of Skylake is bit 8, but |
| 3224 | * it is not supported on KVM. |
| 3225 | */ |
| 3226 | if (data != 0) |
| 3227 | return 1; |
| 3228 | vcpu->arch.ia32_xss = data; |
| 3229 | if (vcpu->arch.ia32_xss != host_xss) |
| 3230 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 3231 | vcpu->arch.ia32_xss, host_xss); |
| 3232 | else |
| 3233 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 3234 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3235 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3236 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3237 | return 1; |
| 3238 | /* Check reserved bit, higher 32 bits should be zero */ |
| 3239 | if ((data >> 32) != 0) |
| 3240 | return 1; |
| 3241 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3242 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3243 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3244 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3245 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3246 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3247 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 3248 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3249 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 3250 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3251 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3252 | if (ret) |
| 3253 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3254 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3255 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3256 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3257 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3258 | } |
| 3259 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3260 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3261 | } |
| 3262 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3263 | 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] | 3264 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3265 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 3266 | switch (reg) { |
| 3267 | case VCPU_REGS_RSP: |
| 3268 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 3269 | break; |
| 3270 | case VCPU_REGS_RIP: |
| 3271 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 3272 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3273 | case VCPU_EXREG_PDPTR: |
| 3274 | if (enable_ept) |
| 3275 | ept_save_pdptrs(vcpu); |
| 3276 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3277 | default: |
| 3278 | break; |
| 3279 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3280 | } |
| 3281 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3282 | static __init int cpu_has_kvm_support(void) |
| 3283 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 3284 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | static __init int vmx_disabled_by_bios(void) |
| 3288 | { |
| 3289 | u64 msr; |
| 3290 | |
| 3291 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3292 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3293 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3294 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 3295 | && tboot_enabled()) |
| 3296 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3297 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3298 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3299 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3300 | && !tboot_enabled()) { |
| 3301 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3302 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3303 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3304 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3305 | /* launched w/o TXT and VMX disabled */ |
| 3306 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 3307 | && !tboot_enabled()) |
| 3308 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3312 | } |
| 3313 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3314 | static void kvm_cpu_vmxon(u64 addr) |
| 3315 | { |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3316 | intel_pt_handle_vmx(1); |
| 3317 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3318 | asm volatile (ASM_VMX_VMXON_RAX |
| 3319 | : : "a"(&addr), "m"(addr) |
| 3320 | : "memory", "cc"); |
| 3321 | } |
| 3322 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3323 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3324 | { |
| 3325 | int cpu = raw_smp_processor_id(); |
| 3326 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3327 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3328 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 3329 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3330 | return -EBUSY; |
| 3331 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3332 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 3333 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 3334 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 3335 | |
| 3336 | /* |
| 3337 | * Now we can enable the vmclear operation in kdump |
| 3338 | * since the loaded_vmcss_on_cpu list on this cpu |
| 3339 | * has been initialized. |
| 3340 | * |
| 3341 | * Though the cpu is not in VMX operation now, there |
| 3342 | * is no problem to enable the vmclear operation |
| 3343 | * for the loaded_vmcss_on_cpu list is empty! |
| 3344 | */ |
| 3345 | crash_enable_local_vmclear(cpu); |
| 3346 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3347 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3348 | |
| 3349 | test_bits = FEATURE_CONTROL_LOCKED; |
| 3350 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 3351 | if (tboot_enabled()) |
| 3352 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 3353 | |
| 3354 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3355 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3356 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 3357 | } |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 3358 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3359 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3360 | if (vmm_exclusive) { |
| 3361 | kvm_cpu_vmxon(phys_addr); |
| 3362 | ept_sync_global(); |
| 3363 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3364 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 3365 | native_store_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 3366 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3367 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3368 | } |
| 3369 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3370 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3371 | { |
| 3372 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3373 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3374 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3375 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 3376 | loaded_vmcss_on_cpu_link) |
| 3377 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3378 | } |
| 3379 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3380 | |
| 3381 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 3382 | * tricks. |
| 3383 | */ |
| 3384 | static void kvm_cpu_vmxoff(void) |
| 3385 | { |
| 3386 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3387 | |
| 3388 | intel_pt_handle_vmx(0); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3389 | } |
| 3390 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3391 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3392 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3393 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3394 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3395 | kvm_cpu_vmxoff(); |
| 3396 | } |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 3397 | cr4_clear_bits(X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3398 | } |
| 3399 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3400 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3401 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3402 | { |
| 3403 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3404 | u32 ctl = ctl_min | ctl_opt; |
| 3405 | |
| 3406 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3407 | |
| 3408 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 3409 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 3410 | |
| 3411 | /* Ensure minimum (required) set of control bits are supported. */ |
| 3412 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3413 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3414 | |
| 3415 | *result = ctl; |
| 3416 | return 0; |
| 3417 | } |
| 3418 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3419 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 3420 | { |
| 3421 | u32 vmx_msr_low, vmx_msr_high; |
| 3422 | |
| 3423 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3424 | return vmx_msr_high & ctl; |
| 3425 | } |
| 3426 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3427 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3428 | { |
| 3429 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3430 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3431 | u32 _pin_based_exec_control = 0; |
| 3432 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3433 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3434 | u32 _vmexit_control = 0; |
| 3435 | u32 _vmentry_control = 0; |
| 3436 | |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 3437 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3438 | #ifdef CONFIG_X86_64 |
| 3439 | CPU_BASED_CR8_LOAD_EXITING | |
| 3440 | CPU_BASED_CR8_STORE_EXITING | |
| 3441 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3442 | CPU_BASED_CR3_LOAD_EXITING | |
| 3443 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3444 | CPU_BASED_USE_IO_BITMAPS | |
| 3445 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3446 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 3447 | CPU_BASED_MWAIT_EXITING | |
| 3448 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 3449 | CPU_BASED_INVLPG_EXITING | |
| 3450 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3451 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3452 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3453 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3454 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3455 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 3456 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3457 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3458 | #ifdef CONFIG_X86_64 |
| 3459 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3460 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 3461 | ~CPU_BASED_CR8_STORE_EXITING; |
| 3462 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3463 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3464 | min2 = 0; |
| 3465 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3466 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3467 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3468 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3469 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3470 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3471 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3472 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3473 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3474 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 3475 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3476 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 3477 | SECONDARY_EXEC_XSAVES | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 3478 | SECONDARY_EXEC_ENABLE_PML | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 3479 | SECONDARY_EXEC_TSC_SCALING; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3480 | if (adjust_vmx_controls(min2, opt2, |
| 3481 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3482 | &_cpu_based_2nd_exec_control) < 0) |
| 3483 | return -EIO; |
| 3484 | } |
| 3485 | #ifndef CONFIG_X86_64 |
| 3486 | if (!(_cpu_based_2nd_exec_control & |
| 3487 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 3488 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3489 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3490 | |
| 3491 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3492 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3493 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3494 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 3495 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3496 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3497 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3498 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 3499 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 3500 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3501 | CPU_BASED_CR3_STORE_EXITING | |
| 3502 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3503 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 3504 | vmx_capability.ept, vmx_capability.vpid); |
| 3505 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3506 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3507 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3508 | #ifdef CONFIG_X86_64 |
| 3509 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 3510 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 3511 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3512 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3513 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 3514 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3515 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3516 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3517 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 3518 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 3519 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3520 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 3521 | &_pin_based_exec_control) < 0) |
| 3522 | return -EIO; |
| 3523 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 3524 | if (cpu_has_broken_vmx_preemption_timer()) |
| 3525 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3526 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3527 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3528 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 3529 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 3530 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 3531 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3532 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 3533 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3534 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3535 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3536 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3537 | |
| 3538 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 3539 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3540 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3541 | |
| 3542 | #ifdef CONFIG_X86_64 |
| 3543 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 3544 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3545 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3546 | #endif |
| 3547 | |
| 3548 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 3549 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3550 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3551 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3552 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 3553 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 3554 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3555 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3556 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3557 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 3558 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3559 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3560 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 3561 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3562 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3563 | cpu_has_load_ia32_efer = |
| 3564 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3565 | VM_ENTRY_LOAD_IA32_EFER) |
| 3566 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3567 | VM_EXIT_LOAD_IA32_EFER); |
| 3568 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 3569 | cpu_has_load_perf_global_ctrl = |
| 3570 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3571 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 3572 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3573 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 3574 | |
| 3575 | /* |
| 3576 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 3577 | * 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] | 3578 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 3579 | * |
| 3580 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 3581 | * |
| 3582 | * AAK155 (model 26) |
| 3583 | * AAP115 (model 30) |
| 3584 | * AAT100 (model 37) |
| 3585 | * BC86,AAY89,BD102 (model 44) |
| 3586 | * BA97 (model 46) |
| 3587 | * |
| 3588 | */ |
| 3589 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 3590 | switch (boot_cpu_data.x86_model) { |
| 3591 | case 26: |
| 3592 | case 30: |
| 3593 | case 37: |
| 3594 | case 44: |
| 3595 | case 46: |
| 3596 | cpu_has_load_perf_global_ctrl = false; |
| 3597 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 3598 | "does not work properly. Using workaround\n"); |
| 3599 | break; |
| 3600 | default: |
| 3601 | break; |
| 3602 | } |
| 3603 | } |
| 3604 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 3605 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3606 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 3607 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3608 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3609 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3610 | |
| 3611 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 3612 | { |
| 3613 | int node = cpu_to_node(cpu); |
| 3614 | struct page *pages; |
| 3615 | struct vmcs *vmcs; |
| 3616 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 3617 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3618 | if (!pages) |
| 3619 | return NULL; |
| 3620 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3621 | memset(vmcs, 0, vmcs_config.size); |
| 3622 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3623 | return vmcs; |
| 3624 | } |
| 3625 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3626 | static void free_vmcs(struct vmcs *vmcs) |
| 3627 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3628 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3629 | } |
| 3630 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3631 | /* |
| 3632 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 3633 | */ |
| 3634 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 3635 | { |
| 3636 | if (!loaded_vmcs->vmcs) |
| 3637 | return; |
| 3638 | loaded_vmcs_clear(loaded_vmcs); |
| 3639 | free_vmcs(loaded_vmcs->vmcs); |
| 3640 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3641 | if (loaded_vmcs->msr_bitmap) |
| 3642 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 3643 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3644 | } |
| 3645 | |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 3646 | static struct vmcs *alloc_vmcs(void) |
| 3647 | { |
| 3648 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
| 3649 | } |
| 3650 | |
| 3651 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 3652 | { |
| 3653 | loaded_vmcs->vmcs = alloc_vmcs(); |
| 3654 | if (!loaded_vmcs->vmcs) |
| 3655 | return -ENOMEM; |
| 3656 | |
| 3657 | loaded_vmcs->shadow_vmcs = NULL; |
| 3658 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3659 | |
| 3660 | if (cpu_has_vmx_msr_bitmap()) { |
| 3661 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 3662 | if (!loaded_vmcs->msr_bitmap) |
| 3663 | goto out_vmcs; |
| 3664 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
| 3665 | } |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 3666 | return 0; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3667 | |
| 3668 | out_vmcs: |
| 3669 | free_loaded_vmcs(loaded_vmcs); |
| 3670 | return -ENOMEM; |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 3671 | } |
| 3672 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 3673 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3674 | { |
| 3675 | int cpu; |
| 3676 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3677 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3678 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3679 | per_cpu(vmxarea, cpu) = NULL; |
| 3680 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3681 | } |
| 3682 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3683 | static void init_vmcs_shadow_fields(void) |
| 3684 | { |
| 3685 | int i, j; |
| 3686 | |
| 3687 | /* No checks for read only fields yet */ |
| 3688 | |
| 3689 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
| 3690 | switch (shadow_read_write_fields[i]) { |
| 3691 | case GUEST_BNDCFGS: |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3692 | if (!kvm_mpx_supported()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3693 | continue; |
| 3694 | break; |
| 3695 | default: |
| 3696 | break; |
| 3697 | } |
| 3698 | |
| 3699 | if (j < i) |
| 3700 | shadow_read_write_fields[j] = |
| 3701 | shadow_read_write_fields[i]; |
| 3702 | j++; |
| 3703 | } |
| 3704 | max_shadow_read_write_fields = j; |
| 3705 | |
| 3706 | /* shadowed fields guest access without vmexit */ |
| 3707 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 3708 | clear_bit(shadow_read_write_fields[i], |
| 3709 | vmx_vmwrite_bitmap); |
| 3710 | clear_bit(shadow_read_write_fields[i], |
| 3711 | vmx_vmread_bitmap); |
| 3712 | } |
| 3713 | for (i = 0; i < max_shadow_read_only_fields; i++) |
| 3714 | clear_bit(shadow_read_only_fields[i], |
| 3715 | vmx_vmread_bitmap); |
| 3716 | } |
| 3717 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3718 | static __init int alloc_kvm_area(void) |
| 3719 | { |
| 3720 | int cpu; |
| 3721 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3722 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3723 | struct vmcs *vmcs; |
| 3724 | |
| 3725 | vmcs = alloc_vmcs_cpu(cpu); |
| 3726 | if (!vmcs) { |
| 3727 | free_kvm_area(); |
| 3728 | return -ENOMEM; |
| 3729 | } |
| 3730 | |
| 3731 | per_cpu(vmxarea, cpu) = vmcs; |
| 3732 | } |
| 3733 | return 0; |
| 3734 | } |
| 3735 | |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3736 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 3737 | { |
| 3738 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 3739 | } |
| 3740 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3741 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3742 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3743 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3744 | if (!emulate_invalid_guest_state) { |
| 3745 | /* |
| 3746 | * CS and SS RPL should be equal during guest entry according |
| 3747 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 3748 | * is in the middle of the transition from real mode to |
| 3749 | * protected mode it is safe to assume that RPL 0 is a good |
| 3750 | * default value. |
| 3751 | */ |
| 3752 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3753 | save->selector &= ~SEGMENT_RPL_MASK; |
| 3754 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3755 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3756 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3757 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 3761 | { |
| 3762 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3763 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3764 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3765 | /* |
| 3766 | * Update real mode segment cache. It may be not up-to-date if sement |
| 3767 | * register was written while vcpu was in a guest mode. |
| 3768 | */ |
| 3769 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3770 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3771 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3772 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 3773 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3774 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 3775 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3776 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3777 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3778 | vmx_segment_cache_clear(vmx); |
| 3779 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3780 | 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] | 3781 | |
| 3782 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3783 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 3784 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3785 | vmcs_writel(GUEST_RFLAGS, flags); |
| 3786 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3787 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 3788 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3789 | |
| 3790 | update_exception_bitmap(vcpu); |
| 3791 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3792 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3793 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3794 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3795 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3796 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 3797 | 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] | 3798 | } |
| 3799 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3800 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3801 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3802 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3803 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3804 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3805 | var.dpl = 0x3; |
| 3806 | if (seg == VCPU_SREG_CS) |
| 3807 | var.type = 0x3; |
| 3808 | |
| 3809 | if (!emulate_invalid_guest_state) { |
| 3810 | var.selector = var.base >> 4; |
| 3811 | var.base = var.base & 0xffff0; |
| 3812 | var.limit = 0xffff; |
| 3813 | var.g = 0; |
| 3814 | var.db = 0; |
| 3815 | var.present = 1; |
| 3816 | var.s = 1; |
| 3817 | var.l = 0; |
| 3818 | var.unusable = 0; |
| 3819 | var.type = 0x3; |
| 3820 | var.avl = 0; |
| 3821 | if (save->base & 0xf) |
| 3822 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 3823 | "paragraph aligned when entering " |
| 3824 | "protected mode (seg=%d)", seg); |
| 3825 | } |
| 3826 | |
| 3827 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 7c3bab1 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 3828 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3829 | vmcs_write32(sf->limit, var.limit); |
| 3830 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 3834 | { |
| 3835 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3836 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3837 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3838 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 3839 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3840 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3841 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3842 | 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] | 3843 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3844 | 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] | 3845 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3846 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3847 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3848 | /* |
| 3849 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3850 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3851 | */ |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3852 | if (!vcpu->kvm->arch.tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3853 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 3854 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3855 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3856 | vmx_segment_cache_clear(vmx); |
| 3857 | |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3858 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3859 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3860 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3861 | |
| 3862 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3863 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3864 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 3865 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3866 | |
| 3867 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3868 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3869 | update_exception_bitmap(vcpu); |
| 3870 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3871 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3872 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3873 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3874 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3875 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 3876 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3877 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 3878 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3879 | } |
| 3880 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3881 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 3882 | { |
| 3883 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3884 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 3885 | |
| 3886 | if (!msr) |
| 3887 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3888 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3889 | /* |
| 3890 | * Force kernel_gs_base reloading before EFER changes, as control |
| 3891 | * of this msr depends on is_long_mode(). |
| 3892 | */ |
| 3893 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3894 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3895 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3896 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3897 | msr->data = efer; |
| 3898 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3899 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3900 | |
| 3901 | msr->data = efer & ~EFER_LME; |
| 3902 | } |
| 3903 | setup_msrs(vmx); |
| 3904 | } |
| 3905 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3906 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3907 | |
| 3908 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 3909 | { |
| 3910 | u32 guest_tr_ar; |
| 3911 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3912 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 3913 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3914 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3915 | 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] | 3916 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 3917 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3918 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3919 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 3920 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3921 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3922 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 3926 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3927 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3928 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | #endif |
| 3932 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3933 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3934 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3935 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3936 | if (enable_ept) { |
| 3937 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 3938 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 3939 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3940 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3941 | } |
| 3942 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3943 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 3944 | { |
| 3945 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid); |
| 3946 | } |
| 3947 | |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 3948 | static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu) |
| 3949 | { |
| 3950 | if (enable_ept) |
| 3951 | vmx_flush_tlb(vcpu); |
| 3952 | } |
| 3953 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 3954 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 3955 | { |
| 3956 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 3957 | |
| 3958 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 3959 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 3960 | } |
| 3961 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 3962 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 3963 | { |
| 3964 | if (enable_ept && is_paging(vcpu)) |
| 3965 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 3966 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 3967 | } |
| 3968 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3969 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3970 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3971 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 3972 | |
| 3973 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 3974 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3975 | } |
| 3976 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3977 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 3978 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3979 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 3980 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3981 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 3982 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3983 | return; |
| 3984 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3985 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3986 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 3987 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 3988 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 3989 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3990 | } |
| 3991 | } |
| 3992 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3993 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 3994 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3995 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 3996 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3997 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3998 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 3999 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 4000 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 4001 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4002 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4003 | |
| 4004 | __set_bit(VCPU_EXREG_PDPTR, |
| 4005 | (unsigned long *)&vcpu->arch.regs_avail); |
| 4006 | __set_bit(VCPU_EXREG_PDPTR, |
| 4007 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4008 | } |
| 4009 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4010 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4011 | |
| 4012 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 4013 | unsigned long cr0, |
| 4014 | struct kvm_vcpu *vcpu) |
| 4015 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 4016 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 4017 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4018 | if (!(cr0 & X86_CR0_PG)) { |
| 4019 | /* From paging/starting to nonpaging */ |
| 4020 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4021 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4022 | (CPU_BASED_CR3_LOAD_EXITING | |
| 4023 | CPU_BASED_CR3_STORE_EXITING)); |
| 4024 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4025 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4026 | } else if (!is_paging(vcpu)) { |
| 4027 | /* From nonpaging to paging */ |
| 4028 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4029 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4030 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4031 | CPU_BASED_CR3_STORE_EXITING)); |
| 4032 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4033 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4034 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 4035 | |
| 4036 | if (!(cr0 & X86_CR0_WP)) |
| 4037 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4038 | } |
| 4039 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4040 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 4041 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4042 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4043 | unsigned long hw_cr0; |
| 4044 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4045 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4046 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4047 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4048 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4049 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4050 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4051 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 4052 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4053 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4054 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 4055 | enter_rmode(vcpu); |
| 4056 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4057 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4058 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4059 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4060 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4061 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4062 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4063 | exit_lmode(vcpu); |
| 4064 | } |
| 4065 | #endif |
| 4066 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4067 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4068 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 4069 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4070 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 4071 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4072 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4073 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4074 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4075 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 4076 | |
| 4077 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 4078 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4079 | } |
| 4080 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4081 | static u64 construct_eptp(unsigned long root_hpa) |
| 4082 | { |
| 4083 | u64 eptp; |
| 4084 | |
| 4085 | /* TODO write the value reading from MSR */ |
| 4086 | eptp = VMX_EPT_DEFAULT_MT | |
| 4087 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
Xudong Hao | b38f993 | 2012-05-28 19:33:36 +0800 | [diff] [blame] | 4088 | if (enable_ept_ad_bits) |
| 4089 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4090 | eptp |= (root_hpa & PAGE_MASK); |
| 4091 | |
| 4092 | return eptp; |
| 4093 | } |
| 4094 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4095 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 4096 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4097 | unsigned long guest_cr3; |
| 4098 | u64 eptp; |
| 4099 | |
| 4100 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4101 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4102 | eptp = construct_eptp(cr3); |
| 4103 | vmcs_write64(EPT_POINTER, eptp); |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 4104 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
| 4105 | guest_cr3 = kvm_read_cr3(vcpu); |
| 4106 | else |
| 4107 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 4108 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4109 | } |
| 4110 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4111 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4112 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4113 | } |
| 4114 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4115 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4116 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 4117 | /* |
| 4118 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 4119 | * is in force while we are in guest mode. Do not let guests control |
| 4120 | * this bit, even if host CR4.MCE == 0. |
| 4121 | */ |
| 4122 | unsigned long hw_cr4 = |
| 4123 | (cr4_read_shadow() & X86_CR4_MCE) | |
| 4124 | (cr4 & ~X86_CR4_MCE) | |
| 4125 | (to_vmx(vcpu)->rmode.vm86_active ? |
| 4126 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4127 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4128 | if (cr4 & X86_CR4_VMXE) { |
| 4129 | /* |
| 4130 | * To use VMXON (and later other VMX instructions), a guest |
| 4131 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 4132 | * So basically the check on whether to allow nested VMX |
| 4133 | * is here. |
| 4134 | */ |
| 4135 | if (!nested_vmx_allowed(vcpu)) |
| 4136 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4137 | } |
| 4138 | if (to_vmx(vcpu)->nested.vmxon && |
| 4139 | ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4140 | return 1; |
| 4141 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4142 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 4143 | if (enable_ept) { |
| 4144 | if (!is_paging(vcpu)) { |
| 4145 | hw_cr4 &= ~X86_CR4_PAE; |
| 4146 | hw_cr4 |= X86_CR4_PSE; |
| 4147 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 4148 | hw_cr4 &= ~X86_CR4_PAE; |
| 4149 | } |
| 4150 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4151 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4152 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
| 4153 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4154 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 4155 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 4156 | * to be manually disabled when guest switches to non-paging |
| 4157 | * mode. |
| 4158 | * |
| 4159 | * If !enable_unrestricted_guest, the CPU is always running |
| 4160 | * with CR0.PG=1 and CR4 needs to be modified. |
| 4161 | * If enable_unrestricted_guest, the CPU automatically |
| 4162 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4163 | */ |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4164 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4165 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4166 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 4167 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4168 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4169 | } |
| 4170 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4171 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 4172 | struct kvm_segment *var, int seg) |
| 4173 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4174 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4175 | u32 ar; |
| 4176 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 4177 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4178 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4179 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4180 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4181 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 4182 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4183 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4184 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4185 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4186 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4187 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 4188 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4189 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4190 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4191 | var->type = ar & 15; |
| 4192 | var->s = (ar >> 4) & 1; |
| 4193 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4194 | /* |
| 4195 | * Some userspaces do not preserve unusable property. Since usable |
| 4196 | * segment has to be present according to VMX spec we can use present |
| 4197 | * property to amend userspace bug by making unusable segment always |
| 4198 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 4199 | * segment as unusable. |
| 4200 | */ |
| 4201 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4202 | var->avl = (ar >> 12) & 1; |
| 4203 | var->l = (ar >> 13) & 1; |
| 4204 | var->db = (ar >> 14) & 1; |
| 4205 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4206 | } |
| 4207 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4208 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 4209 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4210 | struct kvm_segment s; |
| 4211 | |
| 4212 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 4213 | vmx_get_segment(vcpu, &s, seg); |
| 4214 | return s.base; |
| 4215 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4216 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4217 | } |
| 4218 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4219 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4220 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4221 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4222 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4223 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4224 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4225 | else { |
| 4226 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4227 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4228 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4229 | } |
| 4230 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4231 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4232 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4233 | u32 ar; |
| 4234 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 4235 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4236 | ar = 1 << 16; |
| 4237 | else { |
| 4238 | ar = var->type & 15; |
| 4239 | ar |= (var->s & 1) << 4; |
| 4240 | ar |= (var->dpl & 3) << 5; |
| 4241 | ar |= (var->present & 1) << 7; |
| 4242 | ar |= (var->avl & 1) << 12; |
| 4243 | ar |= (var->l & 1) << 13; |
| 4244 | ar |= (var->db & 1) << 14; |
| 4245 | ar |= (var->g & 1) << 15; |
| 4246 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4247 | |
| 4248 | return ar; |
| 4249 | } |
| 4250 | |
| 4251 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 4252 | struct kvm_segment *var, int seg) |
| 4253 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4254 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4255 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4256 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4257 | vmx_segment_cache_clear(vmx); |
| 4258 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4259 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 4260 | vmx->rmode.segs[seg] = *var; |
| 4261 | if (seg == VCPU_SREG_TR) |
| 4262 | vmcs_write16(sf->selector, var->selector); |
| 4263 | else if (var->s) |
| 4264 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4265 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4266 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4267 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4268 | vmcs_writel(sf->base, var->base); |
| 4269 | vmcs_write32(sf->limit, var->limit); |
| 4270 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4271 | |
| 4272 | /* |
| 4273 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 4274 | * qemu binaries. |
| 4275 | * IA32 arch specifies that at the time of processor reset the |
| 4276 | * "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] | 4277 | * 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] | 4278 | * state vmexit when "unrestricted guest" mode is turned on. |
| 4279 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 4280 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 4281 | * kvm hack. |
| 4282 | */ |
| 4283 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4284 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4285 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4286 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4287 | |
| 4288 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 4289 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4290 | } |
| 4291 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4292 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4293 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4294 | 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] | 4295 | |
| 4296 | *db = (ar >> 14) & 1; |
| 4297 | *l = (ar >> 13) & 1; |
| 4298 | } |
| 4299 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4300 | 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] | 4301 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4302 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4303 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4304 | } |
| 4305 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4306 | 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] | 4307 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4308 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 4309 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4310 | } |
| 4311 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4312 | 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] | 4313 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4314 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4315 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4316 | } |
| 4317 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4318 | 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] | 4319 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4320 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 4321 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4322 | } |
| 4323 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4324 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4325 | { |
| 4326 | struct kvm_segment var; |
| 4327 | u32 ar; |
| 4328 | |
| 4329 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4330 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 4331 | if (seg == VCPU_SREG_CS) |
| 4332 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4333 | ar = vmx_segment_access_rights(&var); |
| 4334 | |
| 4335 | if (var.base != (var.selector << 4)) |
| 4336 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 4337 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4338 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4339 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4340 | return false; |
| 4341 | |
| 4342 | return true; |
| 4343 | } |
| 4344 | |
| 4345 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 4346 | { |
| 4347 | struct kvm_segment cs; |
| 4348 | unsigned int cs_rpl; |
| 4349 | |
| 4350 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4351 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4352 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4353 | if (cs.unusable) |
| 4354 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4355 | 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] | 4356 | return false; |
| 4357 | if (!cs.s) |
| 4358 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4359 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4360 | if (cs.dpl > cs_rpl) |
| 4361 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4362 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4363 | if (cs.dpl != cs_rpl) |
| 4364 | return false; |
| 4365 | } |
| 4366 | if (!cs.present) |
| 4367 | return false; |
| 4368 | |
| 4369 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 4370 | return true; |
| 4371 | } |
| 4372 | |
| 4373 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 4374 | { |
| 4375 | struct kvm_segment ss; |
| 4376 | unsigned int ss_rpl; |
| 4377 | |
| 4378 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4379 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4380 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4381 | if (ss.unusable) |
| 4382 | return true; |
| 4383 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4384 | return false; |
| 4385 | if (!ss.s) |
| 4386 | return false; |
| 4387 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 4388 | return false; |
| 4389 | if (!ss.present) |
| 4390 | return false; |
| 4391 | |
| 4392 | return true; |
| 4393 | } |
| 4394 | |
| 4395 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4396 | { |
| 4397 | struct kvm_segment var; |
| 4398 | unsigned int rpl; |
| 4399 | |
| 4400 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4401 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4402 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4403 | if (var.unusable) |
| 4404 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4405 | if (!var.s) |
| 4406 | return false; |
| 4407 | if (!var.present) |
| 4408 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4409 | 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] | 4410 | if (var.dpl < rpl) /* DPL < RPL */ |
| 4411 | return false; |
| 4412 | } |
| 4413 | |
| 4414 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 4415 | * rights flags |
| 4416 | */ |
| 4417 | return true; |
| 4418 | } |
| 4419 | |
| 4420 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 4421 | { |
| 4422 | struct kvm_segment tr; |
| 4423 | |
| 4424 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 4425 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4426 | if (tr.unusable) |
| 4427 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4428 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4429 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4430 | 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] | 4431 | return false; |
| 4432 | if (!tr.present) |
| 4433 | return false; |
| 4434 | |
| 4435 | return true; |
| 4436 | } |
| 4437 | |
| 4438 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 4439 | { |
| 4440 | struct kvm_segment ldtr; |
| 4441 | |
| 4442 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 4443 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4444 | if (ldtr.unusable) |
| 4445 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4446 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4447 | return false; |
| 4448 | if (ldtr.type != 2) |
| 4449 | return false; |
| 4450 | if (!ldtr.present) |
| 4451 | return false; |
| 4452 | |
| 4453 | return true; |
| 4454 | } |
| 4455 | |
| 4456 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 4457 | { |
| 4458 | struct kvm_segment cs, ss; |
| 4459 | |
| 4460 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 4461 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 4462 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4463 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 4464 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | /* |
| 4468 | * Check if guest state is valid. Returns true if valid, false if |
| 4469 | * not. |
| 4470 | * We assume that registers are always usable |
| 4471 | */ |
| 4472 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 4473 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 4474 | if (enable_unrestricted_guest) |
| 4475 | return true; |
| 4476 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4477 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 4478 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4479 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 4480 | return false; |
| 4481 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 4482 | return false; |
| 4483 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4484 | return false; |
| 4485 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4486 | return false; |
| 4487 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4488 | return false; |
| 4489 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4490 | return false; |
| 4491 | } else { |
| 4492 | /* protected mode guest state checks */ |
| 4493 | if (!cs_ss_rpl_check(vcpu)) |
| 4494 | return false; |
| 4495 | if (!code_segment_valid(vcpu)) |
| 4496 | return false; |
| 4497 | if (!stack_segment_valid(vcpu)) |
| 4498 | return false; |
| 4499 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4500 | return false; |
| 4501 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4502 | return false; |
| 4503 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4504 | return false; |
| 4505 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4506 | return false; |
| 4507 | if (!tr_valid(vcpu)) |
| 4508 | return false; |
| 4509 | if (!ldtr_valid(vcpu)) |
| 4510 | return false; |
| 4511 | } |
| 4512 | /* TODO: |
| 4513 | * - Add checks on RIP |
| 4514 | * - Add checks on RFLAGS |
| 4515 | */ |
| 4516 | |
| 4517 | return true; |
| 4518 | } |
| 4519 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4520 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4521 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4522 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4523 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4524 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4525 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4526 | idx = srcu_read_lock(&kvm->srcu); |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4527 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4528 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4529 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4530 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4531 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 4532 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 4533 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4534 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4535 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4536 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 4537 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4538 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4539 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4540 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4541 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4542 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4543 | r = kvm_write_guest_page(kvm, fn, &data, |
| 4544 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 4545 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4546 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4547 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4548 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4549 | } |
| 4550 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4551 | static int init_rmode_identity_map(struct kvm *kvm) |
| 4552 | { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4553 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 4554 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4555 | u32 tmp; |
| 4556 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4557 | if (!enable_ept) |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4558 | return 0; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4559 | |
| 4560 | /* Protect kvm->arch.ept_identity_pagetable_done. */ |
| 4561 | mutex_lock(&kvm->slots_lock); |
| 4562 | |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4563 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4564 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4565 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4566 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4567 | |
| 4568 | r = alloc_identity_pagetable(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4569 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4570 | goto out2; |
| 4571 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4572 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4573 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 4574 | if (r < 0) |
| 4575 | goto out; |
| 4576 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 4577 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 4578 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 4579 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 4580 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 4581 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 4582 | if (r < 0) |
| 4583 | goto out; |
| 4584 | } |
| 4585 | kvm->arch.ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4586 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4587 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4588 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4589 | |
| 4590 | out2: |
| 4591 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4592 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4593 | } |
| 4594 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4595 | static void seg_setup(int seg) |
| 4596 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4597 | 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] | 4598 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4599 | |
| 4600 | vmcs_write16(sf->selector, 0); |
| 4601 | vmcs_writel(sf->base, 0); |
| 4602 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 4603 | ar = 0x93; |
| 4604 | if (seg == VCPU_SREG_CS) |
| 4605 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4606 | |
| 4607 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4608 | } |
| 4609 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4610 | static int alloc_apic_access_page(struct kvm *kvm) |
| 4611 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4612 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4613 | int r = 0; |
| 4614 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4615 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4616 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4617 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4618 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 4619 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4620 | if (r) |
| 4621 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4622 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 4623 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4624 | if (is_error_page(page)) { |
| 4625 | r = -EFAULT; |
| 4626 | goto out; |
| 4627 | } |
| 4628 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4629 | /* |
| 4630 | * Do not pin the page in memory, so that memory hot-unplug |
| 4631 | * is able to migrate it. |
| 4632 | */ |
| 4633 | put_page(page); |
| 4634 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4635 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4636 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4637 | return r; |
| 4638 | } |
| 4639 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4640 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 4641 | { |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4642 | /* Called with kvm->slots_lock held. */ |
| 4643 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4644 | int r = 0; |
| 4645 | |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4646 | BUG_ON(kvm->arch.ept_identity_pagetable_done); |
| 4647 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4648 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
| 4649 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4650 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4651 | return r; |
| 4652 | } |
| 4653 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4654 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4655 | { |
| 4656 | int vpid; |
| 4657 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 4658 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4659 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4660 | spin_lock(&vmx_vpid_lock); |
| 4661 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4662 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4663 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4664 | else |
| 4665 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4666 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4667 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4668 | } |
| 4669 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4670 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4671 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4672 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4673 | return; |
| 4674 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4675 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4676 | spin_unlock(&vmx_vpid_lock); |
| 4677 | } |
| 4678 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4679 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4680 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4681 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4682 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4683 | |
| 4684 | if (!cpu_has_vmx_msr_bitmap()) |
| 4685 | return; |
| 4686 | |
| 4687 | /* |
| 4688 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4689 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4690 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4691 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4692 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4693 | if (type & MSR_TYPE_R) |
| 4694 | /* read-low */ |
| 4695 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 4696 | |
| 4697 | if (type & MSR_TYPE_W) |
| 4698 | /* write-low */ |
| 4699 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 4700 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4701 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4702 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4703 | if (type & MSR_TYPE_R) |
| 4704 | /* read-high */ |
| 4705 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 4706 | |
| 4707 | if (type & MSR_TYPE_W) |
| 4708 | /* write-high */ |
| 4709 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 4710 | |
| 4711 | } |
| 4712 | } |
| 4713 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4714 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4715 | u32 msr, int type) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4716 | { |
| 4717 | int f = sizeof(unsigned long); |
| 4718 | |
| 4719 | if (!cpu_has_vmx_msr_bitmap()) |
| 4720 | return; |
| 4721 | |
| 4722 | /* |
| 4723 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4724 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4725 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4726 | */ |
| 4727 | if (msr <= 0x1fff) { |
| 4728 | if (type & MSR_TYPE_R) |
| 4729 | /* read-low */ |
| 4730 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 4731 | |
| 4732 | if (type & MSR_TYPE_W) |
| 4733 | /* write-low */ |
| 4734 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 4735 | |
| 4736 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4737 | msr &= 0x1fff; |
| 4738 | if (type & MSR_TYPE_R) |
| 4739 | /* read-high */ |
| 4740 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 4741 | |
| 4742 | if (type & MSR_TYPE_W) |
| 4743 | /* write-high */ |
| 4744 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 4745 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4746 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4747 | } |
| 4748 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4749 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
| 4750 | u32 msr, int type, bool value) |
| 4751 | { |
| 4752 | if (value) |
| 4753 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 4754 | else |
| 4755 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 4756 | } |
| 4757 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 4758 | /* |
| 4759 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 4760 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 4761 | */ |
| 4762 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 4763 | unsigned long *msr_bitmap_nested, |
| 4764 | u32 msr, int type) |
| 4765 | { |
| 4766 | int f = sizeof(unsigned long); |
| 4767 | |
| 4768 | if (!cpu_has_vmx_msr_bitmap()) { |
| 4769 | WARN_ON(1); |
| 4770 | return; |
| 4771 | } |
| 4772 | |
| 4773 | /* |
| 4774 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4775 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4776 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4777 | */ |
| 4778 | if (msr <= 0x1fff) { |
| 4779 | if (type & MSR_TYPE_R && |
| 4780 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 4781 | /* read-low */ |
| 4782 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 4783 | |
| 4784 | if (type & MSR_TYPE_W && |
| 4785 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 4786 | /* write-low */ |
| 4787 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 4788 | |
| 4789 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4790 | msr &= 0x1fff; |
| 4791 | if (type & MSR_TYPE_R && |
| 4792 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 4793 | /* read-high */ |
| 4794 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 4795 | |
| 4796 | if (type & MSR_TYPE_W && |
| 4797 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 4798 | /* write-high */ |
| 4799 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 4800 | |
| 4801 | } |
| 4802 | } |
| 4803 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4804 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4805 | { |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4806 | u8 mode = 0; |
| 4807 | |
| 4808 | if (cpu_has_secondary_exec_ctrls() && |
| 4809 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 4810 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 4811 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 4812 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 4813 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 4814 | } |
| 4815 | |
| 4816 | if (is_long_mode(vcpu)) |
| 4817 | mode |= MSR_BITMAP_MODE_LM; |
| 4818 | |
| 4819 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4820 | } |
| 4821 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4822 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 4823 | |
| 4824 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 4825 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4826 | { |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4827 | int msr; |
| 4828 | |
| 4829 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 4830 | unsigned word = msr / BITS_PER_LONG; |
| 4831 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 4832 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
| 4833 | } |
| 4834 | |
| 4835 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 4836 | /* |
| 4837 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 4838 | * delivery is not in use. |
| 4839 | */ |
| 4840 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 4841 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 4842 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 4843 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 4844 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 4845 | } |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4846 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4847 | } |
| 4848 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4849 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4850 | { |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4851 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4852 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 4853 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 4854 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4855 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4856 | if (!changed) |
| 4857 | return; |
| 4858 | |
| 4859 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, |
| 4860 | !(mode & MSR_BITMAP_MODE_LM)); |
| 4861 | |
| 4862 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 4863 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 4864 | |
| 4865 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4866 | } |
| 4867 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4868 | static bool vmx_get_enable_apicv(void) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4869 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4870 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4871 | } |
| 4872 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4873 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 4874 | { |
| 4875 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4876 | gfn_t gfn; |
| 4877 | |
| 4878 | /* |
| 4879 | * Don't need to mark the APIC access page dirty; it is never |
| 4880 | * written to by the CPU during APIC virtualization. |
| 4881 | */ |
| 4882 | |
| 4883 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 4884 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 4885 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 4886 | } |
| 4887 | |
| 4888 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 4889 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 4890 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 4891 | } |
| 4892 | } |
| 4893 | |
| 4894 | |
David Hildenbrand | 1edccf2 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 4895 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4896 | { |
| 4897 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4898 | int max_irr; |
| 4899 | void *vapic_page; |
| 4900 | u16 status; |
| 4901 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4902 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 4903 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4904 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4905 | vmx->nested.pi_pending = false; |
| 4906 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 4907 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4908 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4909 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 4910 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4911 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4912 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page); |
| 4913 | kunmap(vmx->nested.virtual_apic_page); |
| 4914 | |
| 4915 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 4916 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 4917 | status &= ~0xff; |
| 4918 | status |= (u8)max_irr; |
| 4919 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 4920 | } |
| 4921 | } |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4922 | |
| 4923 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4924 | } |
| 4925 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4926 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu) |
| 4927 | { |
| 4928 | #ifdef CONFIG_SMP |
| 4929 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4930 | /* |
Haozhong Zhang | 3ffbe62 | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 4931 | * The vector of interrupt to be delivered to vcpu had |
| 4932 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4933 | * |
Haozhong Zhang | 3ffbe62 | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 4934 | * Following cases will be reached in this block, and |
| 4935 | * we always send a notification event in all cases as |
| 4936 | * explained below. |
| 4937 | * |
| 4938 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 4939 | * notification event posts the interrupt to vcpu. |
| 4940 | * |
| 4941 | * Case 2: vcpu exits to root mode and is still |
| 4942 | * runnable. PIR will be synced to vIRR before the |
| 4943 | * next vcpu entry. Sending a notification event in |
| 4944 | * this case has no effect, as vcpu is not in root |
| 4945 | * mode. |
| 4946 | * |
| 4947 | * Case 3: vcpu exits to root mode and is blocked. |
| 4948 | * vcpu_block() has already synced PIR to vIRR and |
| 4949 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 4950 | * a blocked vcpu here does not wait for any requested |
| 4951 | * interrupts in PIR, and sending a notification event |
| 4952 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4953 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4954 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4955 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), |
| 4956 | POSTED_INTR_VECTOR); |
| 4957 | return true; |
| 4958 | } |
| 4959 | #endif |
| 4960 | return false; |
| 4961 | } |
| 4962 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4963 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 4964 | int vector) |
| 4965 | { |
| 4966 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4967 | |
| 4968 | if (is_guest_mode(vcpu) && |
| 4969 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4970 | /* |
| 4971 | * If a posted intr is not recognized by hardware, |
| 4972 | * we will accomplish it in the next vmentry. |
| 4973 | */ |
| 4974 | vmx->nested.pi_pending = true; |
| 4975 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | ba88289 | 2017-11-09 20:27:20 +0200 | [diff] [blame^] | 4976 | /* the PIR and ON have been set by L1. */ |
| 4977 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu)) |
| 4978 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4979 | return 0; |
| 4980 | } |
| 4981 | return -1; |
| 4982 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4983 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4984 | * Send interrupt to vcpu via posted interrupt way. |
| 4985 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 4986 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 4987 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 4988 | * interrupt from PIR in next vmentry. |
| 4989 | */ |
| 4990 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 4991 | { |
| 4992 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4993 | int r; |
| 4994 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4995 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 4996 | if (!r) |
| 4997 | return; |
| 4998 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4999 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 5000 | return; |
| 5001 | |
| 5002 | r = pi_test_and_set_on(&vmx->pi_desc); |
| 5003 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5004 | if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5005 | kvm_vcpu_kick(vcpu); |
| 5006 | } |
| 5007 | |
| 5008 | static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
| 5009 | { |
| 5010 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5011 | |
| 5012 | if (!pi_test_and_clear_on(&vmx->pi_desc)) |
| 5013 | return; |
| 5014 | |
| 5015 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); |
| 5016 | } |
| 5017 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5018 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5019 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 5020 | * will not change in the lifetime of the guest. |
| 5021 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 5022 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 5023 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5024 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5025 | { |
| 5026 | u32 low32, high32; |
| 5027 | unsigned long tmpl; |
| 5028 | struct desc_ptr dt; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5029 | unsigned long cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5030 | |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 5031 | vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5032 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 5033 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5034 | /* 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] | 5035 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5036 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
| 5037 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 5038 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5039 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5040 | #ifdef CONFIG_X86_64 |
| 5041 | /* |
| 5042 | * Load null selectors, so we can avoid reloading them in |
| 5043 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 5044 | * too (the expected case). |
| 5045 | */ |
| 5046 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 5047 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 5048 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5049 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5050 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5051 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5052 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5053 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 5054 | |
| 5055 | native_store_idt(&dt); |
| 5056 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5057 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5058 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 5059 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5060 | |
| 5061 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 5062 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 5063 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 5064 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 5065 | |
| 5066 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 5067 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 5068 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 5069 | } |
| 5070 | } |
| 5071 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5072 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 5073 | { |
| 5074 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 5075 | if (enable_ept) |
| 5076 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5077 | if (is_guest_mode(&vmx->vcpu)) |
| 5078 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 5079 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5080 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 5081 | } |
| 5082 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5083 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 5084 | { |
| 5085 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 5086 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5087 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5088 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5089 | /* Enable the preemption timer dynamically */ |
| 5090 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5091 | return pin_based_exec_ctrl; |
| 5092 | } |
| 5093 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5094 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 5095 | { |
| 5096 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5097 | |
| 5098 | 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] | 5099 | if (cpu_has_secondary_exec_ctrls()) { |
| 5100 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5101 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5102 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5103 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5104 | else |
| 5105 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5106 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5107 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5108 | } |
| 5109 | |
| 5110 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5111 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5112 | } |
| 5113 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5114 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 5115 | { |
| 5116 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 5117 | |
| 5118 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 5119 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 5120 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5121 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5122 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 5123 | #ifdef CONFIG_X86_64 |
| 5124 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 5125 | CPU_BASED_CR8_LOAD_EXITING; |
| 5126 | #endif |
| 5127 | } |
| 5128 | if (!enable_ept) |
| 5129 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 5130 | CPU_BASED_CR3_LOAD_EXITING | |
| 5131 | CPU_BASED_INVLPG_EXITING; |
| 5132 | return exec_control; |
| 5133 | } |
| 5134 | |
| 5135 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 5136 | { |
| 5137 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5138 | if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5139 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 5140 | if (vmx->vpid == 0) |
| 5141 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 5142 | if (!enable_ept) { |
| 5143 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 5144 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 5145 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 5146 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5147 | } |
| 5148 | if (!enable_unrestricted_guest) |
| 5149 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 5150 | if (!ple_gap) |
| 5151 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5152 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5153 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5154 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5155 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 5156 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 5157 | (handle_vmptrld). |
| 5158 | We can NOT enable shadow_vmcs here because we don't have yet |
| 5159 | a current VMCS12 |
| 5160 | */ |
| 5161 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5162 | |
| 5163 | if (!enable_pml) |
| 5164 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5165 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5166 | return exec_control; |
| 5167 | } |
| 5168 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5169 | static void ept_set_mmio_spte_mask(void) |
| 5170 | { |
| 5171 | /* |
| 5172 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 5173 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 5174 | * Also, magic bits (0x3ull << 62) is set to quickly identify mmio |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5175 | * spte. |
| 5176 | */ |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 5177 | kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5178 | } |
| 5179 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5180 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5181 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5182 | * Sets up the vmcs for emulated real mode. |
| 5183 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 5184 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5185 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5186 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5187 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5188 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5189 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5190 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5191 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5192 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 5193 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5194 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 5195 | if (enable_shadow_vmcs) { |
| 5196 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
| 5197 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 5198 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5199 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5200 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5201 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5202 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 5203 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5204 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5205 | 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] | 5206 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5207 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5208 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5209 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5210 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5211 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 5212 | vmx_secondary_exec_control(vmx)); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5213 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5214 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5215 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5216 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 5217 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 5218 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 5219 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 5220 | |
| 5221 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5222 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 5223 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5224 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5225 | } |
| 5226 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5227 | if (ple_gap) { |
| 5228 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 5229 | vmx->ple_window = ple_window; |
| 5230 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5231 | } |
| 5232 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 5233 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 5234 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5235 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 5236 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 5237 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 5238 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5239 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5240 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5241 | rdmsrl(MSR_FS_BASE, a); |
| 5242 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 5243 | rdmsrl(MSR_GS_BASE, a); |
| 5244 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 5245 | #else |
| 5246 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 5247 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 5248 | #endif |
| 5249 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5250 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 5251 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 5252 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5253 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 5254 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5255 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 5256 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 5257 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 5258 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 5259 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5260 | u32 index = vmx_msr_index[i]; |
| 5261 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5262 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5263 | |
| 5264 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 5265 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 5266 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 5267 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 5268 | vmx->guest_msrs[j].index = i; |
| 5269 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 5270 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5271 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5272 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5273 | |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 5274 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 5275 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5276 | |
| 5277 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5278 | |
| 5279 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5280 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 5281 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5282 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5283 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5284 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5285 | if (vmx_xsaves_supported()) |
| 5286 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 5287 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 5288 | if (enable_pml) { |
| 5289 | ASSERT(vmx->pml_pg); |
| 5290 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 5291 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 5292 | } |
| 5293 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5294 | return 0; |
| 5295 | } |
| 5296 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5297 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5298 | { |
| 5299 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 5300 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5301 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5302 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5303 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 5304 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5305 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5306 | vmx->soft_vnmi_blocked = 0; |
| 5307 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5308 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5309 | kvm_set_cr8(vcpu, 0); |
| 5310 | |
| 5311 | if (!init_event) { |
| 5312 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 5313 | MSR_IA32_APICBASE_ENABLE; |
| 5314 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 5315 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 5316 | apic_base_msr.host_initiated = true; |
| 5317 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 5318 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5319 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5320 | vmx_segment_cache_clear(vmx); |
| 5321 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 5322 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5323 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5324 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5325 | |
| 5326 | seg_setup(VCPU_SREG_DS); |
| 5327 | seg_setup(VCPU_SREG_ES); |
| 5328 | seg_setup(VCPU_SREG_FS); |
| 5329 | seg_setup(VCPU_SREG_GS); |
| 5330 | seg_setup(VCPU_SREG_SS); |
| 5331 | |
| 5332 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 5333 | vmcs_writel(GUEST_TR_BASE, 0); |
| 5334 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 5335 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 5336 | |
| 5337 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 5338 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 5339 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 5340 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 5341 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5342 | if (!init_event) { |
| 5343 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 5344 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 5345 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 5346 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 5347 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5348 | |
Wanpeng Li | 5c0b19b | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 5349 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5350 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5351 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5352 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 5353 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 5354 | |
| 5355 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 5356 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 5357 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 5358 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5359 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5360 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5361 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5362 | setup_msrs(vmx); |
| 5363 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5364 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 5365 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5366 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5367 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5368 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5369 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5370 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5371 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5372 | } |
| 5373 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 5374 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5375 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5376 | if (kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5377 | memset(&vmx->pi_desc, 0, sizeof(struct pi_desc)); |
| 5378 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5379 | if (vmx->vpid != 0) |
| 5380 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 5381 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5382 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5383 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 5384 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5385 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 5386 | vmx_set_efer(vcpu, 0); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5387 | vmx_fpu_activate(vcpu); |
| 5388 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5389 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 5390 | vpid_sync_context(vmx->vpid); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5391 | } |
| 5392 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 5393 | /* |
| 5394 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 5395 | * For most existing hypervisors, this will always return true. |
| 5396 | */ |
| 5397 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 5398 | { |
| 5399 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5400 | PIN_BASED_EXT_INTR_MASK; |
| 5401 | } |
| 5402 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 5403 | /* |
| 5404 | * In nested virtualization, check if L1 has set |
| 5405 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 5406 | */ |
| 5407 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 5408 | { |
| 5409 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 5410 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 5411 | } |
| 5412 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5413 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 5414 | { |
| 5415 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5416 | PIN_BASED_NMI_EXITING; |
| 5417 | } |
| 5418 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5419 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5420 | { |
| 5421 | u32 cpu_based_vm_exec_control; |
Jan Kiszka | 730dca4 | 2013-04-28 10:50:52 +0200 | [diff] [blame] | 5422 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5423 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5424 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 5425 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5426 | } |
| 5427 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5428 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5429 | { |
| 5430 | u32 cpu_based_vm_exec_control; |
| 5431 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5432 | if (!cpu_has_virtual_nmis() || |
| 5433 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 5434 | enable_irq_window(vcpu); |
| 5435 | return; |
| 5436 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 5437 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5438 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5439 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 5440 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5441 | } |
| 5442 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5443 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5444 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5445 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5446 | uint32_t intr; |
| 5447 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5448 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5449 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5450 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 5451 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5452 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5453 | int inc_eip = 0; |
| 5454 | if (vcpu->arch.interrupt.soft) |
| 5455 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 5456 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5457 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5458 | return; |
| 5459 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5460 | intr = irq | INTR_INFO_VALID_MASK; |
| 5461 | if (vcpu->arch.interrupt.soft) { |
| 5462 | intr |= INTR_TYPE_SOFT_INTR; |
| 5463 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 5464 | vmx->vcpu.arch.event_exit_inst_len); |
| 5465 | } else |
| 5466 | intr |= INTR_TYPE_EXT_INTR; |
| 5467 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5468 | } |
| 5469 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5470 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 5471 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5472 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5473 | |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5474 | if (!is_guest_mode(vcpu)) { |
| 5475 | if (!cpu_has_virtual_nmis()) { |
| 5476 | /* |
| 5477 | * Tracking the NMI-blocked state in software is built upon |
| 5478 | * finding the next open IRQ window. This, in turn, depends on |
| 5479 | * well-behaving guests: They have to keep IRQs disabled at |
| 5480 | * least as long as the NMI handler runs. Otherwise we may |
| 5481 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 5482 | * highly unlikely, we can live with the residual risk. |
| 5483 | */ |
| 5484 | vmx->soft_vnmi_blocked = 1; |
| 5485 | vmx->vnmi_blocked_time = 0; |
| 5486 | } |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 5487 | |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5488 | ++vcpu->stat.nmi_injections; |
| 5489 | vmx->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5490 | } |
| 5491 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5492 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5493 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5494 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5495 | return; |
| 5496 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5497 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5498 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 5499 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5500 | } |
| 5501 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5502 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 5503 | { |
| 5504 | if (!cpu_has_virtual_nmis()) |
| 5505 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5506 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 5507 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 5508 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5509 | } |
| 5510 | |
| 5511 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 5512 | { |
| 5513 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5514 | |
| 5515 | if (!cpu_has_virtual_nmis()) { |
| 5516 | if (vmx->soft_vnmi_blocked != masked) { |
| 5517 | vmx->soft_vnmi_blocked = masked; |
| 5518 | vmx->vnmi_blocked_time = 0; |
| 5519 | } |
| 5520 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5521 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5522 | if (masked) |
| 5523 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5524 | GUEST_INTR_STATE_NMI); |
| 5525 | else |
| 5526 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5527 | GUEST_INTR_STATE_NMI); |
| 5528 | } |
| 5529 | } |
| 5530 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5531 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 5532 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5533 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 5534 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5535 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5536 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
| 5537 | return 0; |
| 5538 | |
| 5539 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5540 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 5541 | | GUEST_INTR_STATE_NMI)); |
| 5542 | } |
| 5543 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5544 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5545 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5546 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 5547 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 5548 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5549 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5552 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5553 | { |
| 5554 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5555 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5556 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 5557 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5558 | if (ret) |
| 5559 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 5560 | kvm->arch.tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5561 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5562 | } |
| 5563 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5564 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5565 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5566 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5567 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5568 | /* |
| 5569 | * Update instruction length as we may reinject the exception |
| 5570 | * from user space while in guest debugging mode. |
| 5571 | */ |
| 5572 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 5573 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5574 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5575 | return false; |
| 5576 | /* fall through */ |
| 5577 | case DB_VECTOR: |
| 5578 | if (vcpu->guest_debug & |
| 5579 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5580 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5581 | /* fall through */ |
| 5582 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5583 | case OF_VECTOR: |
| 5584 | case BR_VECTOR: |
| 5585 | case UD_VECTOR: |
| 5586 | case DF_VECTOR: |
| 5587 | case SS_VECTOR: |
| 5588 | case GP_VECTOR: |
| 5589 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5590 | return true; |
| 5591 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5592 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5593 | return false; |
| 5594 | } |
| 5595 | |
| 5596 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 5597 | int vec, u32 err_code) |
| 5598 | { |
| 5599 | /* |
| 5600 | * Instruction with address size override prefix opcode 0x67 |
| 5601 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 5602 | */ |
| 5603 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 5604 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 5605 | if (vcpu->arch.halt_request) { |
| 5606 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 5607 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5608 | } |
| 5609 | return 1; |
| 5610 | } |
| 5611 | return 0; |
| 5612 | } |
| 5613 | |
| 5614 | /* |
| 5615 | * Forward all other exceptions that are valid in real mode. |
| 5616 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 5617 | * the required debugging infrastructure rework. |
| 5618 | */ |
| 5619 | kvm_queue_exception(vcpu, vec); |
| 5620 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5621 | } |
| 5622 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5623 | /* |
| 5624 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 5625 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 5626 | * We pass a fake environment to the machine check handler because we want |
| 5627 | * the guest to be always treated like user space, no matter what context |
| 5628 | * it used internally. |
| 5629 | */ |
| 5630 | static void kvm_machine_check(void) |
| 5631 | { |
| 5632 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 5633 | struct pt_regs regs = { |
| 5634 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 5635 | .flags = X86_EFLAGS_IF, |
| 5636 | }; |
| 5637 | |
| 5638 | do_machine_check(®s, 0); |
| 5639 | #endif |
| 5640 | } |
| 5641 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5642 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5643 | { |
| 5644 | /* already handled by vcpu_run */ |
| 5645 | return 1; |
| 5646 | } |
| 5647 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5648 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5649 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5650 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5651 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5652 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5653 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5654 | u32 vect_info; |
| 5655 | enum emulation_result er; |
| 5656 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5657 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 5658 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5659 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5660 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5661 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5662 | |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 5663 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 5664 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5665 | |
| 5666 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 5667 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5668 | return 1; |
| 5669 | } |
| 5670 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5671 | if (is_invalid_opcode(intr_info)) { |
Jan Kiszka | ae1f576 | 2015-03-09 20:56:43 +0100 | [diff] [blame] | 5672 | if (is_guest_mode(vcpu)) { |
| 5673 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5674 | return 1; |
| 5675 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5676 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Liran Alon | c0a4c22 | 2017-11-05 16:56:32 +0200 | [diff] [blame] | 5677 | if (er == EMULATE_USER_EXIT) |
| 5678 | return 0; |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5679 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 5680 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5681 | return 1; |
| 5682 | } |
| 5683 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5684 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 5685 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5686 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5687 | |
| 5688 | /* |
| 5689 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 5690 | * MMIO, it is better to report an internal error. |
| 5691 | * See the comments in vmx_handle_exit. |
| 5692 | */ |
| 5693 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 5694 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 5695 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5696 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5697 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5698 | vcpu->run->internal.data[0] = vect_info; |
| 5699 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5700 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5701 | return 0; |
| 5702 | } |
| 5703 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5704 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5705 | /* EPT won't cause page fault directly */ |
Julia Lawall | cf3ace7 | 2011-08-02 12:34:57 +0200 | [diff] [blame] | 5706 | BUG_ON(enable_ept); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5707 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5708 | trace_kvm_page_fault(cr2, error_code); |
| 5709 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 5710 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 5711 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 5712 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5713 | } |
| 5714 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5715 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5716 | |
| 5717 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 5718 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 5719 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5720 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 5721 | case AC_VECTOR: |
| 5722 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 5723 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5724 | case DB_VECTOR: |
| 5725 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 5726 | if (!(vcpu->guest_debug & |
| 5727 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 5728 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5729 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 5730 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
| 5731 | skip_emulated_instruction(vcpu); |
| 5732 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5733 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5734 | return 1; |
| 5735 | } |
| 5736 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 5737 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 5738 | /* fall through */ |
| 5739 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5740 | /* |
| 5741 | * Update instruction length as we may reinject #BP from |
| 5742 | * user space while in guest debugging mode. Reading it for |
| 5743 | * #DB as well causes no harm, it is not used in that case. |
| 5744 | */ |
| 5745 | vmx->vcpu.arch.event_exit_inst_len = |
| 5746 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5747 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 5748 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5749 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 5750 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5751 | break; |
| 5752 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5753 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 5754 | kvm_run->ex.exception = ex_no; |
| 5755 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5756 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5757 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5758 | return 0; |
| 5759 | } |
| 5760 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5761 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5762 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 5763 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5764 | return 1; |
| 5765 | } |
| 5766 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5767 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5768 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5769 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5770 | return 0; |
| 5771 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5772 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5773 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5774 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5775 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 5776 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5777 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5778 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5779 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5780 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5781 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5782 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5783 | ++vcpu->stat.io_exits; |
| 5784 | |
| 5785 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5786 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5787 | |
| 5788 | port = exit_qualification >> 16; |
| 5789 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 5790 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5791 | |
| 5792 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5793 | } |
| 5794 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5795 | static void |
| 5796 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5797 | { |
| 5798 | /* |
| 5799 | * Patch in the VMCALL instruction: |
| 5800 | */ |
| 5801 | hypercall[0] = 0x0f; |
| 5802 | hypercall[1] = 0x01; |
| 5803 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5804 | } |
| 5805 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5806 | static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5807 | { |
| 5808 | unsigned long always_on = VMXON_CR0_ALWAYSON; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5809 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5810 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5811 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5812 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 5813 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 5814 | always_on &= ~(X86_CR0_PE | X86_CR0_PG); |
| 5815 | return (val & always_on) == always_on; |
| 5816 | } |
| 5817 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 5818 | /* 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] | 5819 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 5820 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5821 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5822 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5823 | unsigned long orig_val = val; |
| 5824 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5825 | /* |
| 5826 | * We get here when L2 changed cr0 in a way that did not change |
| 5827 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5828 | * but did change L0 shadowed bits. So we first calculate the |
| 5829 | * effective cr0 value that L1 would like to write into the |
| 5830 | * hardware. It consists of the L2-owned bits from the new |
| 5831 | * 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] | 5832 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5833 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 5834 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 5835 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5836 | if (!nested_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5837 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5838 | |
| 5839 | if (kvm_set_cr0(vcpu, val)) |
| 5840 | return 1; |
| 5841 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5842 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5843 | } else { |
| 5844 | if (to_vmx(vcpu)->nested.vmxon && |
| 5845 | ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)) |
| 5846 | return 1; |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5847 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5848 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5849 | } |
| 5850 | |
| 5851 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 5852 | { |
| 5853 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5854 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5855 | unsigned long orig_val = val; |
| 5856 | |
| 5857 | /* analogously to handle_set_cr0 */ |
| 5858 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 5859 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 5860 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5861 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5862 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5863 | return 0; |
| 5864 | } else |
| 5865 | return kvm_set_cr4(vcpu, val); |
| 5866 | } |
| 5867 | |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 5868 | /* called to set cr0 as appropriate for clts instruction exit. */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5869 | static void handle_clts(struct kvm_vcpu *vcpu) |
| 5870 | { |
| 5871 | if (is_guest_mode(vcpu)) { |
| 5872 | /* |
| 5873 | * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS |
| 5874 | * but we did (!fpu_active). We need to keep GUEST_CR0.TS on, |
| 5875 | * just pretend it's off (also in arch.cr0 for fpu_activate). |
| 5876 | */ |
| 5877 | vmcs_writel(CR0_READ_SHADOW, |
| 5878 | vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS); |
| 5879 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 5880 | } else |
| 5881 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
| 5882 | } |
| 5883 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5884 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5885 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5886 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5887 | int cr; |
| 5888 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 5889 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5890 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5891 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5892 | cr = exit_qualification & 15; |
| 5893 | reg = (exit_qualification >> 8) & 15; |
| 5894 | switch ((exit_qualification >> 4) & 3) { |
| 5895 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5896 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5897 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5898 | switch (cr) { |
| 5899 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5900 | err = handle_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5901 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5902 | return 1; |
| 5903 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 5904 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5905 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5906 | return 1; |
| 5907 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5908 | err = handle_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5909 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5910 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5911 | case 8: { |
| 5912 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5913 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 5914 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5915 | kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5916 | if (lapic_in_kernel(vcpu)) |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5917 | return 1; |
| 5918 | if (cr8_prev <= cr8) |
| 5919 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5920 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5921 | return 0; |
| 5922 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 5923 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5924 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5925 | case 2: /* clts */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5926 | handle_clts(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5927 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5928 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 5929 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5930 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5931 | case 1: /*mov from cr*/ |
| 5932 | switch (cr) { |
| 5933 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 5934 | val = kvm_read_cr3(vcpu); |
| 5935 | kvm_register_write(vcpu, reg, val); |
| 5936 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5937 | skip_emulated_instruction(vcpu); |
| 5938 | return 1; |
| 5939 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5940 | val = kvm_get_cr8(vcpu); |
| 5941 | kvm_register_write(vcpu, reg, val); |
| 5942 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5943 | skip_emulated_instruction(vcpu); |
| 5944 | return 1; |
| 5945 | } |
| 5946 | break; |
| 5947 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 5948 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5949 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 5950 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5951 | |
| 5952 | skip_emulated_instruction(vcpu); |
| 5953 | return 1; |
| 5954 | default: |
| 5955 | break; |
| 5956 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5957 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 5958 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5959 | (int)(exit_qualification >> 4) & 3, cr); |
| 5960 | return 0; |
| 5961 | } |
| 5962 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5963 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5964 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5965 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5966 | int dr, dr7, reg; |
| 5967 | |
| 5968 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5969 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 5970 | |
| 5971 | /* First, if DR does not exist, trigger UD */ |
| 5972 | if (!kvm_require_dr(vcpu, dr)) |
| 5973 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5974 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 5975 | /* 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] | 5976 | if (!kvm_require_cpl(vcpu, 0)) |
| 5977 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5978 | dr7 = vmcs_readl(GUEST_DR7); |
| 5979 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5980 | /* |
| 5981 | * As the vm-exit takes precedence over the debug trap, we |
| 5982 | * need to emulate the latter, either for the host or the |
| 5983 | * guest debugging itself. |
| 5984 | */ |
| 5985 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5986 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5987 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 5988 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5989 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 5990 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5991 | return 0; |
| 5992 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 5993 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5994 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5995 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5996 | return 1; |
| 5997 | } |
| 5998 | } |
| 5999 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6000 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6001 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6002 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6003 | |
| 6004 | /* |
| 6005 | * No more DR vmexits; force a reload of the debug registers |
| 6006 | * and reenter on this instruction. The next vmexit will |
| 6007 | * retrieve the full state of the debug registers. |
| 6008 | */ |
| 6009 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 6010 | return 1; |
| 6011 | } |
| 6012 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6013 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 6014 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6015 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6016 | |
| 6017 | if (kvm_get_dr(vcpu, dr, &val)) |
| 6018 | return 1; |
| 6019 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6020 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 6021 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6022 | return 1; |
| 6023 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6024 | skip_emulated_instruction(vcpu); |
| 6025 | return 1; |
| 6026 | } |
| 6027 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 6028 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 6029 | { |
| 6030 | return vcpu->arch.dr6; |
| 6031 | } |
| 6032 | |
| 6033 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 6034 | { |
| 6035 | } |
| 6036 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6037 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 6038 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6039 | get_debugreg(vcpu->arch.db[0], 0); |
| 6040 | get_debugreg(vcpu->arch.db[1], 1); |
| 6041 | get_debugreg(vcpu->arch.db[2], 2); |
| 6042 | get_debugreg(vcpu->arch.db[3], 3); |
| 6043 | get_debugreg(vcpu->arch.dr6, 6); |
| 6044 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6045 | |
| 6046 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6047 | 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] | 6048 | } |
| 6049 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6050 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 6051 | { |
| 6052 | vmcs_writel(GUEST_DR7, val); |
| 6053 | } |
| 6054 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6055 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6056 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 6057 | kvm_emulate_cpuid(vcpu); |
| 6058 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6059 | } |
| 6060 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6061 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6062 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6063 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6064 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6065 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6066 | msr_info.index = ecx; |
| 6067 | msr_info.host_initiated = false; |
| 6068 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6069 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 6070 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6071 | return 1; |
| 6072 | } |
| 6073 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6074 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6075 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6076 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6077 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 6078 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6079 | skip_emulated_instruction(vcpu); |
| 6080 | return 1; |
| 6081 | } |
| 6082 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6083 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6084 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 6085 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6086 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 6087 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 6088 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6089 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 6090 | msr.data = data; |
| 6091 | msr.index = ecx; |
| 6092 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 6093 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6094 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 6095 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6096 | return 1; |
| 6097 | } |
| 6098 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6099 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6100 | skip_emulated_instruction(vcpu); |
| 6101 | return 1; |
| 6102 | } |
| 6103 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6104 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6105 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6106 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6107 | return 1; |
| 6108 | } |
| 6109 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6110 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6111 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6112 | u32 cpu_based_vm_exec_control; |
| 6113 | |
| 6114 | /* clear pending irq */ |
| 6115 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6116 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 6117 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6118 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6119 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 6120 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 6121 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6122 | return 1; |
| 6123 | } |
| 6124 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6125 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6126 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 6127 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6128 | } |
| 6129 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6130 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6131 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 6132 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6133 | } |
| 6134 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 6135 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 6136 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6137 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 6138 | } |
| 6139 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6140 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6141 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6142 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6143 | |
| 6144 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 6145 | skip_emulated_instruction(vcpu); |
| 6146 | return 1; |
| 6147 | } |
| 6148 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 6149 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 6150 | { |
| 6151 | int err; |
| 6152 | |
| 6153 | err = kvm_rdpmc(vcpu); |
| 6154 | kvm_complete_insn_gp(vcpu, err); |
| 6155 | |
| 6156 | return 1; |
| 6157 | } |
| 6158 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6159 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6160 | { |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6161 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6162 | return 1; |
| 6163 | } |
| 6164 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 6165 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 6166 | { |
| 6167 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 6168 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 6169 | |
| 6170 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 6171 | skip_emulated_instruction(vcpu); |
| 6172 | return 1; |
| 6173 | } |
| 6174 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6175 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 6176 | { |
| 6177 | skip_emulated_instruction(vcpu); |
| 6178 | WARN(1, "this should never happen\n"); |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
| 6182 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 6183 | { |
| 6184 | skip_emulated_instruction(vcpu); |
| 6185 | WARN(1, "this should never happen\n"); |
| 6186 | return 1; |
| 6187 | } |
| 6188 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6189 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6190 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 6191 | if (likely(fasteoi)) { |
| 6192 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6193 | int access_type, offset; |
| 6194 | |
| 6195 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 6196 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 6197 | /* |
| 6198 | * Sane guest uses MOV to write EOI, with written value |
| 6199 | * not cared. So make a short-circuit here by avoiding |
| 6200 | * heavy instruction emulation. |
| 6201 | */ |
| 6202 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 6203 | (offset == APIC_EOI)) { |
| 6204 | kvm_lapic_set_eoi(vcpu); |
| 6205 | skip_emulated_instruction(vcpu); |
| 6206 | return 1; |
| 6207 | } |
| 6208 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6209 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6210 | } |
| 6211 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6212 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 6213 | { |
| 6214 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6215 | int vector = exit_qualification & 0xff; |
| 6216 | |
| 6217 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 6218 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 6219 | return 1; |
| 6220 | } |
| 6221 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 6222 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 6223 | { |
| 6224 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6225 | u32 offset = exit_qualification & 0xfff; |
| 6226 | |
| 6227 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 6228 | kvm_apic_write_nodecode(vcpu, offset); |
| 6229 | return 1; |
| 6230 | } |
| 6231 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6232 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6233 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6234 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6235 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6236 | bool has_error_code = false; |
| 6237 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6238 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6239 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6240 | |
| 6241 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6242 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6243 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6244 | |
| 6245 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6246 | |
| 6247 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6248 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 6249 | switch (type) { |
| 6250 | case INTR_TYPE_NMI_INTR: |
| 6251 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6252 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6253 | break; |
| 6254 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6255 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6256 | kvm_clear_interrupt_queue(vcpu); |
| 6257 | break; |
| 6258 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6259 | if (vmx->idt_vectoring_info & |
| 6260 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 6261 | has_error_code = true; |
| 6262 | error_code = |
| 6263 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6264 | } |
| 6265 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6266 | case INTR_TYPE_SOFT_EXCEPTION: |
| 6267 | kvm_clear_exception_queue(vcpu); |
| 6268 | break; |
| 6269 | default: |
| 6270 | break; |
| 6271 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6272 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6273 | tss_selector = exit_qualification; |
| 6274 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6275 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 6276 | type != INTR_TYPE_EXT_INTR && |
| 6277 | type != INTR_TYPE_NMI_INTR)) |
| 6278 | skip_emulated_instruction(vcpu); |
| 6279 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6280 | if (kvm_task_switch(vcpu, tss_selector, |
| 6281 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 6282 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6283 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6284 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6285 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6286 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6287 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6288 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6289 | /* |
| 6290 | * TODO: What about debug traps on tss switch? |
| 6291 | * Are we supposed to inject them and update dr6? |
| 6292 | */ |
| 6293 | |
| 6294 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6295 | } |
| 6296 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6297 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6298 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6299 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6300 | gpa_t gpa; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6301 | u32 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6302 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6303 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6304 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6305 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6306 | gla_validity = (exit_qualification >> 7) & 0x3; |
Liang Li | 72e0ae5 | 2016-08-18 15:49:19 +0800 | [diff] [blame] | 6307 | if (gla_validity == 0x2) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6308 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 6309 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 6310 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6311 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6312 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 6313 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6314 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6315 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 6316 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6317 | } |
| 6318 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6319 | /* |
| 6320 | * EPT violation happened while executing iret from NMI, |
| 6321 | * "blocked by NMI" bit has to be set before next VM entry. |
| 6322 | * There are errata that may cause this bit to not be set: |
| 6323 | * AAK134, BY25. |
| 6324 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 6325 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 6326 | cpu_has_virtual_nmis() && |
| 6327 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6328 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 6329 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6330 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6331 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6332 | |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6333 | /* it is a read fault? */ |
| 6334 | error_code = (exit_qualification << 2) & PFERR_USER_MASK; |
| 6335 | /* it is a write fault? */ |
| 6336 | error_code |= exit_qualification & PFERR_WRITE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6337 | /* It is a fetch fault? */ |
Tiejun Chen | 81ed33e | 2014-11-18 17:12:56 +0800 | [diff] [blame] | 6338 | error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6339 | /* ept page table is present? */ |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6340 | error_code |= (exit_qualification & 0x38) != 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6341 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6342 | vcpu->arch.exit_qualification = exit_qualification; |
| 6343 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6344 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6345 | } |
| 6346 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6347 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6348 | { |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6349 | int ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6350 | gpa_t gpa; |
| 6351 | |
| 6352 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 6353 | if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 6354 | skip_emulated_instruction(vcpu); |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 6355 | trace_kvm_fast_mmio(gpa); |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 6356 | return 1; |
| 6357 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6358 | |
Paolo Bonzini | 450869d | 2015-11-04 13:41:21 +0100 | [diff] [blame] | 6359 | ret = handle_mmio_page_fault(vcpu, gpa, true); |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6360 | if (likely(ret == RET_MMIO_PF_EMULATE)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6361 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 6362 | EMULATE_DONE; |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 6363 | |
| 6364 | if (unlikely(ret == RET_MMIO_PF_INVALID)) |
| 6365 | return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0); |
| 6366 | |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6367 | if (unlikely(ret == RET_MMIO_PF_RETRY)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6368 | return 1; |
| 6369 | |
| 6370 | /* It is the real ept misconfig */ |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6371 | WARN_ON(1); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6372 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6373 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6374 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6375 | |
| 6376 | return 0; |
| 6377 | } |
| 6378 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6379 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6380 | { |
| 6381 | u32 cpu_based_vm_exec_control; |
| 6382 | |
| 6383 | /* clear pending NMI */ |
| 6384 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6385 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 6386 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 6387 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6388 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6389 | |
| 6390 | return 1; |
| 6391 | } |
| 6392 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6393 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6394 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 6395 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6396 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6397 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6398 | u32 cpu_exec_ctrl; |
| 6399 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 6400 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6401 | |
| 6402 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6403 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6404 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 6405 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 6406 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6407 | return handle_interrupt_window(&vmx->vcpu); |
| 6408 | |
Avi Kivity | de87dcd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 6409 | if (test_bit(KVM_REQ_EVENT, &vcpu->requests)) |
| 6410 | return 1; |
| 6411 | |
Liran Alon | 114de9b | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 6412 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6413 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 6414 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 6415 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6416 | ret = 0; |
| 6417 | goto out; |
| 6418 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 6419 | |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 6420 | if (err != EMULATE_DONE) { |
| 6421 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6422 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6423 | vcpu->run->internal.ndata = 0; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 6424 | return 0; |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 6425 | } |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6426 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6427 | if (vcpu->arch.halt_request) { |
| 6428 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6429 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6430 | goto out; |
| 6431 | } |
| 6432 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6433 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6434 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6435 | if (need_resched()) |
| 6436 | schedule(); |
| 6437 | } |
| 6438 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6439 | out: |
| 6440 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6441 | } |
| 6442 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6443 | static int __grow_ple_window(int val) |
| 6444 | { |
| 6445 | if (ple_window_grow < 1) |
| 6446 | return ple_window; |
| 6447 | |
| 6448 | val = min(val, ple_window_actual_max); |
| 6449 | |
| 6450 | if (ple_window_grow < ple_window) |
| 6451 | val *= ple_window_grow; |
| 6452 | else |
| 6453 | val += ple_window_grow; |
| 6454 | |
| 6455 | return val; |
| 6456 | } |
| 6457 | |
| 6458 | static int __shrink_ple_window(int val, int modifier, int minimum) |
| 6459 | { |
| 6460 | if (modifier < 1) |
| 6461 | return ple_window; |
| 6462 | |
| 6463 | if (modifier < ple_window) |
| 6464 | val /= modifier; |
| 6465 | else |
| 6466 | val -= modifier; |
| 6467 | |
| 6468 | return max(val, minimum); |
| 6469 | } |
| 6470 | |
| 6471 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 6472 | { |
| 6473 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6474 | int old = vmx->ple_window; |
| 6475 | |
| 6476 | vmx->ple_window = __grow_ple_window(old); |
| 6477 | |
| 6478 | if (vmx->ple_window != old) |
| 6479 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6480 | |
| 6481 | 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] | 6482 | } |
| 6483 | |
| 6484 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 6485 | { |
| 6486 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6487 | int old = vmx->ple_window; |
| 6488 | |
| 6489 | vmx->ple_window = __shrink_ple_window(old, |
| 6490 | ple_window_shrink, ple_window); |
| 6491 | |
| 6492 | if (vmx->ple_window != old) |
| 6493 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6494 | |
| 6495 | 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] | 6496 | } |
| 6497 | |
| 6498 | /* |
| 6499 | * ple_window_actual_max is computed to be one grow_ple_window() below |
| 6500 | * ple_window_max. (See __grow_ple_window for the reason.) |
| 6501 | * This prevents overflows, because ple_window_max is int. |
| 6502 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in |
| 6503 | * this process. |
| 6504 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. |
| 6505 | */ |
| 6506 | static void update_ple_window_actual_max(void) |
| 6507 | { |
| 6508 | ple_window_actual_max = |
| 6509 | __shrink_ple_window(max(ple_window_max, ple_window), |
| 6510 | ple_window_grow, INT_MIN); |
| 6511 | } |
| 6512 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6513 | /* |
| 6514 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 6515 | */ |
| 6516 | static void wakeup_handler(void) |
| 6517 | { |
| 6518 | struct kvm_vcpu *vcpu; |
| 6519 | int cpu = smp_processor_id(); |
| 6520 | |
| 6521 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6522 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 6523 | blocked_vcpu_list) { |
| 6524 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 6525 | |
| 6526 | if (pi_test_on(pi_desc) == 1) |
| 6527 | kvm_vcpu_kick(vcpu); |
| 6528 | } |
| 6529 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6530 | } |
| 6531 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6532 | static __init int hardware_setup(void) |
| 6533 | { |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6534 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6535 | |
| 6536 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 6537 | |
| 6538 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 6539 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 6540 | |
| 6541 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6542 | if (!vmx_io_bitmap_a) |
| 6543 | return r; |
| 6544 | |
| 6545 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6546 | if (!vmx_io_bitmap_b) |
| 6547 | goto out; |
| 6548 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6549 | vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6550 | if (!vmx_vmread_bitmap) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6551 | goto out1; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6552 | |
| 6553 | vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6554 | if (!vmx_vmwrite_bitmap) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6555 | goto out2; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6556 | |
| 6557 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 6558 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 6559 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6560 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6561 | |
| 6562 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
| 6563 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6564 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 6565 | r = -EIO; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6566 | goto out3; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6567 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6568 | |
| 6569 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 6570 | kvm_enable_efer_bits(EFER_NX); |
| 6571 | |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 6572 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 6573 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6574 | enable_vpid = 0; |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 6575 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6576 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6577 | enable_shadow_vmcs = 0; |
| 6578 | if (enable_shadow_vmcs) |
| 6579 | init_vmcs_shadow_fields(); |
| 6580 | |
| 6581 | if (!cpu_has_vmx_ept() || |
| 6582 | !cpu_has_vmx_ept_4levels()) { |
| 6583 | enable_ept = 0; |
| 6584 | enable_unrestricted_guest = 0; |
| 6585 | enable_ept_ad_bits = 0; |
| 6586 | } |
| 6587 | |
| 6588 | if (!cpu_has_vmx_ept_ad_bits()) |
| 6589 | enable_ept_ad_bits = 0; |
| 6590 | |
| 6591 | if (!cpu_has_vmx_unrestricted_guest()) |
| 6592 | enable_unrestricted_guest = 0; |
| 6593 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6594 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6595 | flexpriority_enabled = 0; |
| 6596 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6597 | /* |
| 6598 | * set_apic_access_page_addr() is used to reload apic access |
| 6599 | * page upon invalidation. No need to do anything if not |
| 6600 | * using the APIC_ACCESS_ADDR VMCS field. |
| 6601 | */ |
| 6602 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6603 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6604 | |
| 6605 | if (!cpu_has_vmx_tpr_shadow()) |
| 6606 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 6607 | |
| 6608 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 6609 | kvm_disable_largepages(); |
| 6610 | |
| 6611 | if (!cpu_has_vmx_ple()) |
| 6612 | ple_gap = 0; |
| 6613 | |
| 6614 | if (!cpu_has_vmx_apicv()) |
| 6615 | enable_apicv = 0; |
| 6616 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 6617 | if (cpu_has_vmx_tsc_scaling()) { |
| 6618 | kvm_has_tsc_control = true; |
| 6619 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 6620 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 6621 | } |
| 6622 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 6623 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 6624 | |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6625 | if (enable_ept) { |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6626 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6627 | (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull, |
| 6628 | (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull, |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6629 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 6630 | cpu_has_vmx_ept_execute_only() ? |
| 6631 | 0ull : VMX_EPT_READABLE_MASK); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6632 | ept_set_mmio_spte_mask(); |
| 6633 | kvm_enable_tdp(); |
| 6634 | } else |
| 6635 | kvm_disable_tdp(); |
| 6636 | |
| 6637 | update_ple_window_actual_max(); |
| 6638 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6639 | /* |
| 6640 | * Only enable PML when hardware supports PML feature, and both EPT |
| 6641 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 6642 | */ |
| 6643 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 6644 | enable_pml = 0; |
| 6645 | |
| 6646 | if (!enable_pml) { |
| 6647 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 6648 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 6649 | kvm_x86_ops->flush_log_dirty = NULL; |
| 6650 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 6651 | } |
| 6652 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6653 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 6654 | u64 vmx_msr; |
| 6655 | |
| 6656 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 6657 | cpu_preemption_timer_multi = |
| 6658 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 6659 | } else { |
| 6660 | kvm_x86_ops->set_hv_timer = NULL; |
| 6661 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 6662 | } |
| 6663 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6664 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
| 6665 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 6666 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 6667 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6668 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6669 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6670 | out3: |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6671 | free_page((unsigned long)vmx_vmwrite_bitmap); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6672 | out2: |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6673 | free_page((unsigned long)vmx_vmread_bitmap); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6674 | out1: |
| 6675 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6676 | out: |
| 6677 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6678 | |
| 6679 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6680 | } |
| 6681 | |
| 6682 | static __exit void hardware_unsetup(void) |
| 6683 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6684 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6685 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6686 | free_page((unsigned long)vmx_vmwrite_bitmap); |
| 6687 | free_page((unsigned long)vmx_vmread_bitmap); |
| 6688 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6689 | free_kvm_area(); |
| 6690 | } |
| 6691 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6692 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6693 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 6694 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 6695 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 6696 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6697 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6698 | if (ple_gap) |
| 6699 | grow_ple_window(vcpu); |
| 6700 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6701 | skip_emulated_instruction(vcpu); |
| 6702 | kvm_vcpu_on_spin(vcpu); |
| 6703 | |
| 6704 | return 1; |
| 6705 | } |
| 6706 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6707 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6708 | { |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6709 | skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6710 | return 1; |
| 6711 | } |
| 6712 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6713 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 6714 | { |
| 6715 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 6716 | return handle_nop(vcpu); |
| 6717 | } |
| 6718 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 6719 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 6720 | { |
| 6721 | return 1; |
| 6722 | } |
| 6723 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6724 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 6725 | { |
| 6726 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 6727 | return handle_nop(vcpu); |
| 6728 | } |
| 6729 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6730 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6731 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 6732 | * set the success or error code of an emulated VMX instruction, as specified |
| 6733 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 6734 | */ |
| 6735 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 6736 | { |
| 6737 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 6738 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6739 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 6740 | } |
| 6741 | |
| 6742 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 6743 | { |
| 6744 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6745 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 6746 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6747 | | X86_EFLAGS_CF); |
| 6748 | } |
| 6749 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6750 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6751 | u32 vm_instruction_error) |
| 6752 | { |
| 6753 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 6754 | /* |
| 6755 | * failValid writes the error number to the current VMCS, which |
| 6756 | * can't be done there isn't a current VMCS. |
| 6757 | */ |
| 6758 | nested_vmx_failInvalid(vcpu); |
| 6759 | return; |
| 6760 | } |
| 6761 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6762 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6763 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6764 | | X86_EFLAGS_ZF); |
| 6765 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 6766 | /* |
| 6767 | * We don't need to force a shadow sync because |
| 6768 | * VM_INSTRUCTION_ERROR is not shadowed |
| 6769 | */ |
| 6770 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6771 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6772 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 6773 | { |
| 6774 | /* TODO: not to reset guest simply here. */ |
| 6775 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 6776 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6777 | } |
| 6778 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 6779 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 6780 | { |
| 6781 | struct vcpu_vmx *vmx = |
| 6782 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 6783 | |
| 6784 | vmx->nested.preemption_timer_expired = true; |
| 6785 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 6786 | kvm_vcpu_kick(&vmx->vcpu); |
| 6787 | |
| 6788 | return HRTIMER_NORESTART; |
| 6789 | } |
| 6790 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6791 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6792 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 6793 | * exit caused by such an instruction (run by a guest hypervisor). |
| 6794 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 6795 | * #UD or #GP. |
| 6796 | */ |
| 6797 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 6798 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6799 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6800 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6801 | gva_t off; |
| 6802 | bool exn; |
| 6803 | struct kvm_segment s; |
| 6804 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6805 | /* |
| 6806 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 6807 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 6808 | * addressing components of the operand. Only the displacement part |
| 6809 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 6810 | * For how an actual address is calculated from all these components, |
| 6811 | * refer to Vol. 1, "Operand Addressing". |
| 6812 | */ |
| 6813 | int scaling = vmx_instruction_info & 3; |
| 6814 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 6815 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 6816 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 6817 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 6818 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 6819 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 6820 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 6821 | |
| 6822 | if (is_reg) { |
| 6823 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6824 | return 1; |
| 6825 | } |
| 6826 | |
| 6827 | /* Addr = segment_base + offset */ |
| 6828 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6829 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6830 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6831 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6832 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6833 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 6834 | vmx_get_segment(vcpu, &s, seg_reg); |
| 6835 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6836 | |
| 6837 | if (addr_size == 1) /* 32 bit */ |
| 6838 | *ret &= 0xffffffff; |
| 6839 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6840 | /* Checks for #GP/#SS exceptions. */ |
| 6841 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6842 | if (is_long_mode(vcpu)) { |
| 6843 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 6844 | * non-canonical form. This is the only check on the memory |
| 6845 | * destination for long mode! |
| 6846 | */ |
| 6847 | exn = is_noncanonical_address(*ret); |
| 6848 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6849 | /* Protected mode: apply checks for segment validity in the |
| 6850 | * following order: |
| 6851 | * - segment type check (#GP(0) may be thrown) |
| 6852 | * - usability check (#GP(0)/#SS(0)) |
| 6853 | * - limit check (#GP(0)/#SS(0)) |
| 6854 | */ |
| 6855 | if (wr) |
| 6856 | /* #GP(0) if the destination operand is located in a |
| 6857 | * read-only data segment or any code segment. |
| 6858 | */ |
| 6859 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 6860 | else |
| 6861 | /* #GP(0) if the source operand is located in an |
| 6862 | * execute-only code segment |
| 6863 | */ |
| 6864 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6865 | if (exn) { |
| 6866 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 6867 | return 1; |
| 6868 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6869 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 6870 | */ |
| 6871 | exn = (s.unusable != 0); |
| 6872 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 6873 | * operand is outside the segment limit. |
| 6874 | */ |
| 6875 | exn = exn || (off + sizeof(u64) > s.limit); |
| 6876 | } |
| 6877 | if (exn) { |
| 6878 | kvm_queue_exception_e(vcpu, |
| 6879 | seg_reg == VCPU_SREG_SS ? |
| 6880 | SS_VECTOR : GP_VECTOR, |
| 6881 | 0); |
| 6882 | return 1; |
| 6883 | } |
| 6884 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6885 | return 0; |
| 6886 | } |
| 6887 | |
| 6888 | /* |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6889 | * This function performs the various checks including |
| 6890 | * - if it's 4KB aligned |
| 6891 | * - No bits beyond the physical address width are set |
| 6892 | * - Returns 0 on success or else 1 |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6893 | * (Intel SDM Section 30.3) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6894 | */ |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6895 | static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason, |
| 6896 | gpa_t *vmpointer) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6897 | { |
| 6898 | gva_t gva; |
| 6899 | gpa_t vmptr; |
| 6900 | struct x86_exception e; |
| 6901 | struct page *page; |
| 6902 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6903 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 6904 | |
| 6905 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6906 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6907 | return 1; |
| 6908 | |
| 6909 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 6910 | sizeof(vmptr), &e)) { |
| 6911 | kvm_inject_page_fault(vcpu, &e); |
| 6912 | return 1; |
| 6913 | } |
| 6914 | |
| 6915 | switch (exit_reason) { |
| 6916 | case EXIT_REASON_VMON: |
| 6917 | /* |
| 6918 | * SDM 3: 24.11.5 |
| 6919 | * The first 4 bytes of VMXON region contain the supported |
| 6920 | * VMCS revision identifier |
| 6921 | * |
| 6922 | * Note - IA32_VMX_BASIC[48] will never be 1 |
| 6923 | * for the nested case; |
| 6924 | * which replaces physical address width with 32 |
| 6925 | * |
| 6926 | */ |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6927 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6928 | nested_vmx_failInvalid(vcpu); |
| 6929 | skip_emulated_instruction(vcpu); |
| 6930 | return 1; |
| 6931 | } |
| 6932 | |
| 6933 | page = nested_get_page(vcpu, vmptr); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 6934 | if (page == NULL) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6935 | nested_vmx_failInvalid(vcpu); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 6936 | skip_emulated_instruction(vcpu); |
| 6937 | return 1; |
| 6938 | } |
| 6939 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6940 | kunmap(page); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 6941 | nested_release_page_clean(page); |
| 6942 | nested_vmx_failInvalid(vcpu); |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6943 | skip_emulated_instruction(vcpu); |
| 6944 | return 1; |
| 6945 | } |
| 6946 | kunmap(page); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 6947 | nested_release_page_clean(page); |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6948 | vmx->nested.vmxon_ptr = vmptr; |
| 6949 | break; |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6950 | case EXIT_REASON_VMCLEAR: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6951 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6952 | nested_vmx_failValid(vcpu, |
| 6953 | VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 6954 | skip_emulated_instruction(vcpu); |
| 6955 | return 1; |
| 6956 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6957 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6958 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 6959 | nested_vmx_failValid(vcpu, |
| 6960 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 6961 | skip_emulated_instruction(vcpu); |
| 6962 | return 1; |
| 6963 | } |
| 6964 | break; |
| 6965 | case EXIT_REASON_VMPTRLD: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6966 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6967 | nested_vmx_failValid(vcpu, |
| 6968 | VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 6969 | skip_emulated_instruction(vcpu); |
| 6970 | return 1; |
| 6971 | } |
| 6972 | |
| 6973 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 6974 | nested_vmx_failValid(vcpu, |
| 6975 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 6976 | skip_emulated_instruction(vcpu); |
| 6977 | return 1; |
| 6978 | } |
| 6979 | break; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6980 | default: |
| 6981 | return 1; /* shouldn't happen */ |
| 6982 | } |
| 6983 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6984 | if (vmpointer) |
| 6985 | *vmpointer = vmptr; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6986 | return 0; |
| 6987 | } |
| 6988 | |
| 6989 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6990 | * Emulate the VMXON instruction. |
| 6991 | * Currently, we just remember that VMX is active, and do not save or even |
| 6992 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 6993 | * do not currently need to store anything in that guest-allocated memory |
| 6994 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 6995 | * argument is different from the VMXON pointer (which the spec says they do). |
| 6996 | */ |
| 6997 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 6998 | { |
| 6999 | struct kvm_segment cs; |
| 7000 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7001 | struct vmcs *shadow_vmcs; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7002 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 7003 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7004 | int r; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7005 | |
| 7006 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 7007 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 7008 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 7009 | * Otherwise, we should fail with #UD. We test these now: |
| 7010 | */ |
| 7011 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 7012 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 7013 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 7014 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7015 | return 1; |
| 7016 | } |
| 7017 | |
| 7018 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7019 | if (is_long_mode(vcpu) && !cs.l) { |
| 7020 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7021 | return 1; |
| 7022 | } |
| 7023 | |
| 7024 | if (vmx_get_cpl(vcpu)) { |
| 7025 | kvm_inject_gp(vcpu, 0); |
| 7026 | return 1; |
| 7027 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7028 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7029 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7030 | return 1; |
| 7031 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7032 | if (vmx->nested.vmxon) { |
| 7033 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
| 7034 | skip_emulated_instruction(vcpu); |
| 7035 | return 1; |
| 7036 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7037 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 7038 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7039 | != VMXON_NEEDED_FEATURES) { |
| 7040 | kvm_inject_gp(vcpu, 0); |
| 7041 | return 1; |
| 7042 | } |
| 7043 | |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7044 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 7045 | if (r < 0) |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7046 | goto out_vmcs02; |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7047 | |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7048 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7049 | if (!vmx->nested.cached_vmcs12) |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7050 | goto out_cached_vmcs12; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7051 | |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7052 | if (enable_shadow_vmcs) { |
| 7053 | shadow_vmcs = alloc_vmcs(); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7054 | if (!shadow_vmcs) |
| 7055 | goto out_shadow_vmcs; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7056 | /* mark vmcs as shadow */ |
| 7057 | shadow_vmcs->revision_id |= (1u << 31); |
| 7058 | /* init shadow vmcs */ |
| 7059 | vmcs_clear(shadow_vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7060 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7061 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7062 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7063 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Wanpeng Li | f15a75e | 2016-08-30 16:14:01 +0800 | [diff] [blame] | 7064 | HRTIMER_MODE_REL_PINNED); |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7065 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7066 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7067 | vmx->nested.vmxon = true; |
| 7068 | |
| 7069 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7070 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7071 | return 1; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7072 | |
| 7073 | out_shadow_vmcs: |
| 7074 | kfree(vmx->nested.cached_vmcs12); |
| 7075 | |
| 7076 | out_cached_vmcs12: |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7077 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 7078 | |
| 7079 | out_vmcs02: |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7080 | return -ENOMEM; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7081 | } |
| 7082 | |
| 7083 | /* |
| 7084 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 7085 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 7086 | * slightly different). It also specifies what exception to inject otherwise. |
| 7087 | */ |
| 7088 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 7089 | { |
| 7090 | struct kvm_segment cs; |
| 7091 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7092 | |
| 7093 | if (!vmx->nested.vmxon) { |
| 7094 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7095 | return 0; |
| 7096 | } |
| 7097 | |
| 7098 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7099 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 7100 | (is_long_mode(vcpu) && !cs.l)) { |
| 7101 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7102 | return 0; |
| 7103 | } |
| 7104 | |
| 7105 | if (vmx_get_cpl(vcpu)) { |
| 7106 | kvm_inject_gp(vcpu, 0); |
| 7107 | return 0; |
| 7108 | } |
| 7109 | |
| 7110 | return 1; |
| 7111 | } |
| 7112 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7113 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 7114 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7115 | if (vmx->nested.current_vmptr == -1ull) |
| 7116 | return; |
| 7117 | |
| 7118 | /* current_vmptr and current_vmcs12 are always set/reset together */ |
| 7119 | if (WARN_ON(vmx->nested.current_vmcs12 == NULL)) |
| 7120 | return; |
| 7121 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7122 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7123 | /* copy to memory all shadowed fields in case |
| 7124 | they were modified */ |
| 7125 | copy_shadow_to_vmcs12(vmx); |
| 7126 | vmx->nested.sync_shadow_vmcs = false; |
Xiao Guangrong | 7ec3629 | 2015-09-09 14:05:56 +0800 | [diff] [blame] | 7127 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7128 | SECONDARY_EXEC_SHADOW_VMCS); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7129 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7130 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7131 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7132 | |
| 7133 | /* Flush VMCS12 to guest memory */ |
| 7134 | memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12, |
| 7135 | VMCS12_SIZE); |
| 7136 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7137 | kunmap(vmx->nested.current_vmcs12_page); |
| 7138 | nested_release_page(vmx->nested.current_vmcs12_page); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7139 | vmx->nested.current_vmptr = -1ull; |
| 7140 | vmx->nested.current_vmcs12 = NULL; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7141 | } |
| 7142 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7143 | /* |
| 7144 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 7145 | * just stops using VMX. |
| 7146 | */ |
| 7147 | static void free_nested(struct vcpu_vmx *vmx) |
| 7148 | { |
| 7149 | if (!vmx->nested.vmxon) |
| 7150 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7151 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7152 | vmx->nested.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7153 | free_vpid(vmx->nested.vpid02); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7154 | nested_release_vmcs12(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7155 | if (enable_shadow_vmcs) { |
| 7156 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 7157 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 7158 | vmx->vmcs01.shadow_vmcs = NULL; |
| 7159 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7160 | kfree(vmx->nested.cached_vmcs12); |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7161 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7162 | if (vmx->nested.apic_access_page) { |
| 7163 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7164 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7165 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7166 | if (vmx->nested.virtual_apic_page) { |
| 7167 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7168 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7169 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7170 | if (vmx->nested.pi_desc_page) { |
| 7171 | kunmap(vmx->nested.pi_desc_page); |
| 7172 | nested_release_page(vmx->nested.pi_desc_page); |
| 7173 | vmx->nested.pi_desc_page = NULL; |
| 7174 | vmx->nested.pi_desc = NULL; |
| 7175 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7176 | |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7177 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7178 | } |
| 7179 | |
| 7180 | /* Emulate the VMXOFF instruction */ |
| 7181 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 7182 | { |
| 7183 | if (!nested_vmx_check_permission(vcpu)) |
| 7184 | return 1; |
| 7185 | free_nested(to_vmx(vcpu)); |
| 7186 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7187 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7188 | return 1; |
| 7189 | } |
| 7190 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7191 | /* Emulate the VMCLEAR instruction */ |
| 7192 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 7193 | { |
| 7194 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 29deec4 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7195 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7196 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7197 | |
| 7198 | if (!nested_vmx_check_permission(vcpu)) |
| 7199 | return 1; |
| 7200 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7201 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7202 | return 1; |
| 7203 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7204 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7205 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7206 | |
Jim Mattson | 29deec4 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7207 | kvm_vcpu_write_guest(vcpu, |
| 7208 | vmptr + offsetof(struct vmcs12, launch_state), |
| 7209 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7210 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7211 | skip_emulated_instruction(vcpu); |
| 7212 | nested_vmx_succeed(vcpu); |
| 7213 | return 1; |
| 7214 | } |
| 7215 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7216 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 7217 | |
| 7218 | /* Emulate the VMLAUNCH instruction */ |
| 7219 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 7220 | { |
| 7221 | return nested_vmx_run(vcpu, true); |
| 7222 | } |
| 7223 | |
| 7224 | /* Emulate the VMRESUME instruction */ |
| 7225 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 7226 | { |
| 7227 | |
| 7228 | return nested_vmx_run(vcpu, false); |
| 7229 | } |
| 7230 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7231 | enum vmcs_field_type { |
| 7232 | VMCS_FIELD_TYPE_U16 = 0, |
| 7233 | VMCS_FIELD_TYPE_U64 = 1, |
| 7234 | VMCS_FIELD_TYPE_U32 = 2, |
| 7235 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 7236 | }; |
| 7237 | |
| 7238 | static inline int vmcs_field_type(unsigned long field) |
| 7239 | { |
| 7240 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 7241 | return VMCS_FIELD_TYPE_U32; |
| 7242 | return (field >> 13) & 0x3 ; |
| 7243 | } |
| 7244 | |
| 7245 | static inline int vmcs_field_readonly(unsigned long field) |
| 7246 | { |
| 7247 | return (((field >> 10) & 0x3) == 1); |
| 7248 | } |
| 7249 | |
| 7250 | /* |
| 7251 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 7252 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 7253 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 7254 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 7255 | * 64-bit fields are to be returned). |
| 7256 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7257 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 7258 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7259 | { |
| 7260 | short offset = vmcs_field_to_offset(field); |
| 7261 | char *p; |
| 7262 | |
| 7263 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7264 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7265 | |
| 7266 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 7267 | |
| 7268 | switch (vmcs_field_type(field)) { |
| 7269 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7270 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7271 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7272 | case VMCS_FIELD_TYPE_U16: |
| 7273 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7274 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7275 | case VMCS_FIELD_TYPE_U32: |
| 7276 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7277 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7278 | case VMCS_FIELD_TYPE_U64: |
| 7279 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7280 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7281 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7282 | WARN_ON(1); |
| 7283 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7284 | } |
| 7285 | } |
| 7286 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7287 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7288 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 7289 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7290 | short offset = vmcs_field_to_offset(field); |
| 7291 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 7292 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7293 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7294 | |
| 7295 | switch (vmcs_field_type(field)) { |
| 7296 | case VMCS_FIELD_TYPE_U16: |
| 7297 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7298 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7299 | case VMCS_FIELD_TYPE_U32: |
| 7300 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7301 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7302 | case VMCS_FIELD_TYPE_U64: |
| 7303 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7304 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7305 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7306 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7307 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7308 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7309 | WARN_ON(1); |
| 7310 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | } |
| 7314 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7315 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 7316 | { |
| 7317 | int i; |
| 7318 | unsigned long field; |
| 7319 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7320 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7321 | const unsigned long *fields = shadow_read_write_fields; |
| 7322 | const int num_fields = max_shadow_read_write_fields; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7323 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7324 | preempt_disable(); |
| 7325 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7326 | vmcs_load(shadow_vmcs); |
| 7327 | |
| 7328 | for (i = 0; i < num_fields; i++) { |
| 7329 | field = fields[i]; |
| 7330 | switch (vmcs_field_type(field)) { |
| 7331 | case VMCS_FIELD_TYPE_U16: |
| 7332 | field_value = vmcs_read16(field); |
| 7333 | break; |
| 7334 | case VMCS_FIELD_TYPE_U32: |
| 7335 | field_value = vmcs_read32(field); |
| 7336 | break; |
| 7337 | case VMCS_FIELD_TYPE_U64: |
| 7338 | field_value = vmcs_read64(field); |
| 7339 | break; |
| 7340 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7341 | field_value = vmcs_readl(field); |
| 7342 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7343 | default: |
| 7344 | WARN_ON(1); |
| 7345 | continue; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7346 | } |
| 7347 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 7348 | } |
| 7349 | |
| 7350 | vmcs_clear(shadow_vmcs); |
| 7351 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7352 | |
| 7353 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7354 | } |
| 7355 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7356 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 7357 | { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7358 | const unsigned long *fields[] = { |
| 7359 | shadow_read_write_fields, |
| 7360 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7361 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7362 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7363 | max_shadow_read_write_fields, |
| 7364 | max_shadow_read_only_fields |
| 7365 | }; |
| 7366 | int i, q; |
| 7367 | unsigned long field; |
| 7368 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7369 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7370 | |
| 7371 | vmcs_load(shadow_vmcs); |
| 7372 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7373 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7374 | for (i = 0; i < max_fields[q]; i++) { |
| 7375 | field = fields[q][i]; |
| 7376 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
| 7377 | |
| 7378 | switch (vmcs_field_type(field)) { |
| 7379 | case VMCS_FIELD_TYPE_U16: |
| 7380 | vmcs_write16(field, (u16)field_value); |
| 7381 | break; |
| 7382 | case VMCS_FIELD_TYPE_U32: |
| 7383 | vmcs_write32(field, (u32)field_value); |
| 7384 | break; |
| 7385 | case VMCS_FIELD_TYPE_U64: |
| 7386 | vmcs_write64(field, (u64)field_value); |
| 7387 | break; |
| 7388 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7389 | vmcs_writel(field, (long)field_value); |
| 7390 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7391 | default: |
| 7392 | WARN_ON(1); |
| 7393 | break; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7394 | } |
| 7395 | } |
| 7396 | } |
| 7397 | |
| 7398 | vmcs_clear(shadow_vmcs); |
| 7399 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 7400 | } |
| 7401 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7402 | /* |
| 7403 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 7404 | * used before) all generate the same failure when it is missing. |
| 7405 | */ |
| 7406 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 7407 | { |
| 7408 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7409 | if (vmx->nested.current_vmptr == -1ull) { |
| 7410 | nested_vmx_failInvalid(vcpu); |
| 7411 | skip_emulated_instruction(vcpu); |
| 7412 | return 0; |
| 7413 | } |
| 7414 | return 1; |
| 7415 | } |
| 7416 | |
| 7417 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 7418 | { |
| 7419 | unsigned long field; |
| 7420 | u64 field_value; |
| 7421 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7422 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7423 | gva_t gva = 0; |
| 7424 | |
| 7425 | if (!nested_vmx_check_permission(vcpu) || |
| 7426 | !nested_vmx_check_vmcs12(vcpu)) |
| 7427 | return 1; |
| 7428 | |
| 7429 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7430 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7431 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7432 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7433 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 7434 | skip_emulated_instruction(vcpu); |
| 7435 | return 1; |
| 7436 | } |
| 7437 | /* |
| 7438 | * Now copy part of this value to register or memory, as requested. |
| 7439 | * Note that the number of bits actually copied is 32 or 64 depending |
| 7440 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 7441 | */ |
| 7442 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7443 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7444 | field_value); |
| 7445 | } else { |
| 7446 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7447 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7448 | return 1; |
| 7449 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
| 7450 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 7451 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 7452 | } |
| 7453 | |
| 7454 | nested_vmx_succeed(vcpu); |
| 7455 | skip_emulated_instruction(vcpu); |
| 7456 | return 1; |
| 7457 | } |
| 7458 | |
| 7459 | |
| 7460 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 7461 | { |
| 7462 | unsigned long field; |
| 7463 | gva_t gva; |
| 7464 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7465 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7466 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 7467 | * mode, and eventually we need to write that into a field of several |
| 7468 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 7469 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7470 | * bits into the vmcs12 field. |
| 7471 | */ |
| 7472 | u64 field_value = 0; |
| 7473 | struct x86_exception e; |
| 7474 | |
| 7475 | if (!nested_vmx_check_permission(vcpu) || |
| 7476 | !nested_vmx_check_vmcs12(vcpu)) |
| 7477 | return 1; |
| 7478 | |
| 7479 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7480 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7481 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 7482 | else { |
| 7483 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7484 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7485 | return 1; |
| 7486 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7487 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7488 | kvm_inject_page_fault(vcpu, &e); |
| 7489 | return 1; |
| 7490 | } |
| 7491 | } |
| 7492 | |
| 7493 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7494 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7495 | if (vmcs_field_readonly(field)) { |
| 7496 | nested_vmx_failValid(vcpu, |
| 7497 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 7498 | skip_emulated_instruction(vcpu); |
| 7499 | return 1; |
| 7500 | } |
| 7501 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7502 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7503 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 7504 | skip_emulated_instruction(vcpu); |
| 7505 | return 1; |
| 7506 | } |
| 7507 | |
| 7508 | nested_vmx_succeed(vcpu); |
| 7509 | skip_emulated_instruction(vcpu); |
| 7510 | return 1; |
| 7511 | } |
| 7512 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7513 | /* Emulate the VMPTRLD instruction */ |
| 7514 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 7515 | { |
| 7516 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7517 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7518 | |
| 7519 | if (!nested_vmx_check_permission(vcpu)) |
| 7520 | return 1; |
| 7521 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7522 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7523 | return 1; |
| 7524 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7525 | if (vmx->nested.current_vmptr != vmptr) { |
| 7526 | struct vmcs12 *new_vmcs12; |
| 7527 | struct page *page; |
| 7528 | page = nested_get_page(vcpu, vmptr); |
| 7529 | if (page == NULL) { |
| 7530 | nested_vmx_failInvalid(vcpu); |
| 7531 | skip_emulated_instruction(vcpu); |
| 7532 | return 1; |
| 7533 | } |
| 7534 | new_vmcs12 = kmap(page); |
| 7535 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 7536 | kunmap(page); |
| 7537 | nested_release_page_clean(page); |
| 7538 | nested_vmx_failValid(vcpu, |
| 7539 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 7540 | skip_emulated_instruction(vcpu); |
| 7541 | return 1; |
| 7542 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7543 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7544 | nested_release_vmcs12(vmx); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7545 | vmx->nested.current_vmptr = vmptr; |
| 7546 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 7547 | vmx->nested.current_vmcs12_page = page; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7548 | /* |
| 7549 | * Load VMCS12 from guest memory since it is not already |
| 7550 | * cached. |
| 7551 | */ |
| 7552 | memcpy(vmx->nested.cached_vmcs12, |
| 7553 | vmx->nested.current_vmcs12, VMCS12_SIZE); |
| 7554 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7555 | if (enable_shadow_vmcs) { |
Xiao Guangrong | 7ec3629 | 2015-09-09 14:05:56 +0800 | [diff] [blame] | 7556 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7557 | SECONDARY_EXEC_SHADOW_VMCS); |
Abel Gordon | 8a1b9dd | 2013-04-18 14:39:55 +0300 | [diff] [blame] | 7558 | vmcs_write64(VMCS_LINK_POINTER, |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7559 | __pa(vmx->vmcs01.shadow_vmcs)); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7560 | vmx->nested.sync_shadow_vmcs = true; |
| 7561 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7562 | } |
| 7563 | |
| 7564 | nested_vmx_succeed(vcpu); |
| 7565 | skip_emulated_instruction(vcpu); |
| 7566 | return 1; |
| 7567 | } |
| 7568 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7569 | /* Emulate the VMPTRST instruction */ |
| 7570 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 7571 | { |
| 7572 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7573 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7574 | gva_t vmcs_gva; |
| 7575 | struct x86_exception e; |
| 7576 | |
| 7577 | if (!nested_vmx_check_permission(vcpu)) |
| 7578 | return 1; |
| 7579 | |
| 7580 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7581 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7582 | return 1; |
| 7583 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
| 7584 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 7585 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 7586 | sizeof(u64), &e)) { |
| 7587 | kvm_inject_page_fault(vcpu, &e); |
| 7588 | return 1; |
| 7589 | } |
| 7590 | nested_vmx_succeed(vcpu); |
| 7591 | skip_emulated_instruction(vcpu); |
| 7592 | return 1; |
| 7593 | } |
| 7594 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7595 | /* Emulate the INVEPT instruction */ |
| 7596 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 7597 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7598 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7599 | u32 vmx_instruction_info, types; |
| 7600 | unsigned long type; |
| 7601 | gva_t gva; |
| 7602 | struct x86_exception e; |
| 7603 | struct { |
| 7604 | u64 eptp, gpa; |
| 7605 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7606 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7607 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7608 | SECONDARY_EXEC_ENABLE_EPT) || |
| 7609 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7610 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7611 | return 1; |
| 7612 | } |
| 7613 | |
| 7614 | if (!nested_vmx_check_permission(vcpu)) |
| 7615 | return 1; |
| 7616 | |
| 7617 | if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) { |
| 7618 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7619 | return 1; |
| 7620 | } |
| 7621 | |
| 7622 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7623 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7624 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7625 | types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7626 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7627 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7628 | nested_vmx_failValid(vcpu, |
| 7629 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Paolo Bonzini | 2849eb4 | 2016-03-18 16:53:29 +0100 | [diff] [blame] | 7630 | skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7631 | return 1; |
| 7632 | } |
| 7633 | |
| 7634 | /* According to the Intel VMX instruction reference, the memory |
| 7635 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7636 | */ |
| 7637 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7638 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7639 | return 1; |
| 7640 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
| 7641 | sizeof(operand), &e)) { |
| 7642 | kvm_inject_page_fault(vcpu, &e); |
| 7643 | return 1; |
| 7644 | } |
| 7645 | |
| 7646 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7647 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 7648 | /* |
| 7649 | * TODO: track mappings and invalidate |
| 7650 | * single context requests appropriately |
| 7651 | */ |
| 7652 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7653 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 7654 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7655 | nested_vmx_succeed(vcpu); |
| 7656 | break; |
| 7657 | default: |
| 7658 | BUG_ON(1); |
| 7659 | break; |
| 7660 | } |
| 7661 | |
| 7662 | skip_emulated_instruction(vcpu); |
| 7663 | return 1; |
| 7664 | } |
| 7665 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7666 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 7667 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7668 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7669 | u32 vmx_instruction_info; |
| 7670 | unsigned long type, types; |
| 7671 | gva_t gva; |
| 7672 | struct x86_exception e; |
| 7673 | int vpid; |
| 7674 | |
| 7675 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7676 | SECONDARY_EXEC_ENABLE_VPID) || |
| 7677 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 7678 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7679 | return 1; |
| 7680 | } |
| 7681 | |
| 7682 | if (!nested_vmx_check_permission(vcpu)) |
| 7683 | return 1; |
| 7684 | |
| 7685 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7686 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 7687 | |
| 7688 | types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7; |
| 7689 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7690 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7691 | nested_vmx_failValid(vcpu, |
| 7692 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Paolo Bonzini | f6870ee | 2016-03-18 16:53:42 +0100 | [diff] [blame] | 7693 | skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7694 | return 1; |
| 7695 | } |
| 7696 | |
| 7697 | /* according to the intel vmx instruction reference, the memory |
| 7698 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7699 | */ |
| 7700 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 7701 | vmx_instruction_info, false, &gva)) |
| 7702 | return 1; |
| 7703 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid, |
| 7704 | sizeof(u32), &e)) { |
| 7705 | kvm_inject_page_fault(vcpu, &e); |
| 7706 | return 1; |
| 7707 | } |
| 7708 | |
| 7709 | switch (type) { |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 7710 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 7711 | /* |
| 7712 | * Old versions of KVM use the single-context version so we |
| 7713 | * have to support it; just treat it the same as all-context. |
| 7714 | */ |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7715 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7716 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7717 | nested_vmx_succeed(vcpu); |
| 7718 | break; |
| 7719 | default: |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 7720 | /* Trap individual address invalidation invvpid calls */ |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7721 | BUG_ON(1); |
| 7722 | break; |
| 7723 | } |
| 7724 | |
| 7725 | skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7726 | return 1; |
| 7727 | } |
| 7728 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7729 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 7730 | { |
| 7731 | unsigned long exit_qualification; |
| 7732 | |
| 7733 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 7734 | |
| 7735 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7736 | |
| 7737 | /* |
| 7738 | * PML buffer FULL happened while executing iret from NMI, |
| 7739 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7740 | */ |
| 7741 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 7742 | cpu_has_virtual_nmis() && |
| 7743 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 7744 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 7745 | GUEST_INTR_STATE_NMI); |
| 7746 | |
| 7747 | /* |
| 7748 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 7749 | * here.., and there's no userspace involvement needed for PML. |
| 7750 | */ |
| 7751 | return 1; |
| 7752 | } |
| 7753 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7754 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 7755 | { |
| 7756 | kvm_lapic_expired_hv_timer(vcpu); |
| 7757 | return 1; |
| 7758 | } |
| 7759 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 7760 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7761 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 7762 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 7763 | * to be done to userspace and return 0. |
| 7764 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 7765 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7766 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 7767 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 7768 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7769 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7770 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7771 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 7772 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 7773 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 7774 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 7775 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 7776 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 7777 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7778 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7779 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7780 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7781 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7782 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7783 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7784 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7785 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7786 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7787 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7788 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7789 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 7790 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7791 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 7792 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7793 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7794 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7795 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7796 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7797 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7798 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7799 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 7800 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7801 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7802 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7803 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7804 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7805 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7806 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7807 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 7808 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7809 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7810 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7811 | }; |
| 7812 | |
| 7813 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 7814 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7815 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7816 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 7817 | struct vmcs12 *vmcs12) |
| 7818 | { |
| 7819 | unsigned long exit_qualification; |
| 7820 | gpa_t bitmap, last_bitmap; |
| 7821 | unsigned int port; |
| 7822 | int size; |
| 7823 | u8 b; |
| 7824 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7825 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 7826 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7827 | |
| 7828 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7829 | |
| 7830 | port = exit_qualification >> 16; |
| 7831 | size = (exit_qualification & 7) + 1; |
| 7832 | |
| 7833 | last_bitmap = (gpa_t)-1; |
| 7834 | b = -1; |
| 7835 | |
| 7836 | while (size > 0) { |
| 7837 | if (port < 0x8000) |
| 7838 | bitmap = vmcs12->io_bitmap_a; |
| 7839 | else if (port < 0x10000) |
| 7840 | bitmap = vmcs12->io_bitmap_b; |
| 7841 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7842 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7843 | bitmap += (port & 0x7fff) / 8; |
| 7844 | |
| 7845 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7846 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7847 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7848 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7849 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7850 | |
| 7851 | port++; |
| 7852 | size--; |
| 7853 | last_bitmap = bitmap; |
| 7854 | } |
| 7855 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7856 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7857 | } |
| 7858 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7859 | /* |
| 7860 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 7861 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 7862 | * disinterest in the current event (read or write a specific MSR) by using an |
| 7863 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 7864 | */ |
| 7865 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 7866 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 7867 | { |
| 7868 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7869 | gpa_t bitmap; |
| 7870 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 7871 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7872 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7873 | |
| 7874 | /* |
| 7875 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 7876 | * for the four combinations of read/write and low/high MSR numbers. |
| 7877 | * First we need to figure out which of the four to use: |
| 7878 | */ |
| 7879 | bitmap = vmcs12->msr_bitmap; |
| 7880 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 7881 | bitmap += 2048; |
| 7882 | if (msr_index >= 0xc0000000) { |
| 7883 | msr_index -= 0xc0000000; |
| 7884 | bitmap += 1024; |
| 7885 | } |
| 7886 | |
| 7887 | /* Then read the msr_index'th bit from this bitmap: */ |
| 7888 | if (msr_index < 1024*8) { |
| 7889 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7890 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7891 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7892 | return 1 & (b >> (msr_index & 7)); |
| 7893 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7894 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7895 | } |
| 7896 | |
| 7897 | /* |
| 7898 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 7899 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 7900 | * intercept (via guest_host_mask etc.) the current event. |
| 7901 | */ |
| 7902 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 7903 | struct vmcs12 *vmcs12) |
| 7904 | { |
| 7905 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7906 | int cr = exit_qualification & 15; |
| 7907 | int reg = (exit_qualification >> 8) & 15; |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7908 | unsigned long val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7909 | |
| 7910 | switch ((exit_qualification >> 4) & 3) { |
| 7911 | case 0: /* mov to cr */ |
| 7912 | switch (cr) { |
| 7913 | case 0: |
| 7914 | if (vmcs12->cr0_guest_host_mask & |
| 7915 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7916 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7917 | break; |
| 7918 | case 3: |
| 7919 | if ((vmcs12->cr3_target_count >= 1 && |
| 7920 | vmcs12->cr3_target_value0 == val) || |
| 7921 | (vmcs12->cr3_target_count >= 2 && |
| 7922 | vmcs12->cr3_target_value1 == val) || |
| 7923 | (vmcs12->cr3_target_count >= 3 && |
| 7924 | vmcs12->cr3_target_value2 == val) || |
| 7925 | (vmcs12->cr3_target_count >= 4 && |
| 7926 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7927 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7928 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7929 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7930 | break; |
| 7931 | case 4: |
| 7932 | if (vmcs12->cr4_guest_host_mask & |
| 7933 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7934 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7935 | break; |
| 7936 | case 8: |
| 7937 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7938 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7939 | break; |
| 7940 | } |
| 7941 | break; |
| 7942 | case 2: /* clts */ |
| 7943 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 7944 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7945 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7946 | break; |
| 7947 | case 1: /* mov from cr */ |
| 7948 | switch (cr) { |
| 7949 | case 3: |
| 7950 | if (vmcs12->cpu_based_vm_exec_control & |
| 7951 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7952 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7953 | break; |
| 7954 | case 8: |
| 7955 | if (vmcs12->cpu_based_vm_exec_control & |
| 7956 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7957 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7958 | break; |
| 7959 | } |
| 7960 | break; |
| 7961 | case 3: /* lmsw */ |
| 7962 | /* |
| 7963 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 7964 | * cr0. Other attempted changes are ignored, with no exit. |
| 7965 | */ |
| 7966 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 7967 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7968 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7969 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 7970 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 7971 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7972 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7973 | break; |
| 7974 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7975 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7976 | } |
| 7977 | |
| 7978 | /* |
| 7979 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 7980 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 7981 | * when in is_guest_mode (L2). |
| 7982 | */ |
| 7983 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) |
| 7984 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7985 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 7986 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7987 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 957c897 | 2013-02-24 14:11:34 +0100 | [diff] [blame] | 7988 | u32 exit_reason = vmx->exit_reason; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7989 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 7990 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 7991 | vmcs_readl(EXIT_QUALIFICATION), |
| 7992 | vmx->idt_vectoring_info, |
| 7993 | intr_info, |
| 7994 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 7995 | KVM_ISA_VMX); |
| 7996 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 7997 | /* |
| 7998 | * The host physical addresses of some pages of guest memory |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7999 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 8000 | * Page). The CPU may write to these pages via their host |
| 8001 | * physical address while L2 is running, bypassing any |
| 8002 | * address-translation-based dirty tracking (e.g. EPT write |
| 8003 | * protection). |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 8004 | * |
| 8005 | * Mark them dirty on every exit from L2 to prevent them from |
| 8006 | * getting out of sync with dirty tracking. |
| 8007 | */ |
| 8008 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 8009 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8010 | if (vmx->nested.nested_run_pending) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8011 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8012 | |
| 8013 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 8014 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 8015 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8016 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8017 | } |
| 8018 | |
| 8019 | switch (exit_reason) { |
| 8020 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8021 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8022 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8023 | else if (is_page_fault(intr_info)) |
| 8024 | return enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 8025 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 8026 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8027 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 8028 | else if (is_debug(intr_info) && |
| 8029 | vcpu->guest_debug & |
| 8030 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 8031 | return false; |
| 8032 | else if (is_breakpoint(intr_info) && |
| 8033 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 8034 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8035 | return vmcs12->exception_bitmap & |
| 8036 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 8037 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8038 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8039 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8040 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8041 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8042 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8043 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8044 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8045 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8046 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8047 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8048 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8049 | case EXIT_REASON_HLT: |
| 8050 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 8051 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8052 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8053 | case EXIT_REASON_INVLPG: |
| 8054 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 8055 | case EXIT_REASON_RDPMC: |
| 8056 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 8057 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8058 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 8059 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 8060 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 8061 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 8062 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 8063 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8064 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8065 | /* |
| 8066 | * VMX instructions trap unconditionally. This allows L1 to |
| 8067 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 8068 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8069 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8070 | case EXIT_REASON_CR_ACCESS: |
| 8071 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 8072 | case EXIT_REASON_DR_ACCESS: |
| 8073 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 8074 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8075 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8076 | case EXIT_REASON_MSR_READ: |
| 8077 | case EXIT_REASON_MSR_WRITE: |
| 8078 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 8079 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8080 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8081 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 8082 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8083 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 8084 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8085 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 8086 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 8087 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 8088 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 8089 | nested_cpu_has2(vmcs12, |
| 8090 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 8091 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8092 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8093 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8094 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8095 | case EXIT_REASON_APIC_ACCESS: |
| 8096 | return nested_cpu_has2(vmcs12, |
| 8097 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 8098 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 8099 | case EXIT_REASON_EOI_INDUCED: |
| 8100 | /* apic_write and eoi_induced should exit unconditionally. */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8101 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8102 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8103 | /* |
| 8104 | * L0 always deals with the EPT violation. If nested EPT is |
| 8105 | * used, and the nested mmu code discovers that the address is |
| 8106 | * missing in the guest EPT table (EPT12), the EPT violation |
| 8107 | * will be injected with nested_ept_inject_page_fault() |
| 8108 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8109 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8110 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8111 | /* |
| 8112 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 8113 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 8114 | * (EPT on EPT). So any problems with the structure of the |
| 8115 | * table is L0's fault. |
| 8116 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8117 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8118 | case EXIT_REASON_WBINVD: |
| 8119 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 8120 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8121 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 8122 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 8123 | /* |
| 8124 | * This should never happen, since it is not possible to |
| 8125 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 8126 | * If if it were, XSS would have to be checked against |
| 8127 | * the XSS exit bitmap in vmcs12. |
| 8128 | */ |
| 8129 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 8130 | case EXIT_REASON_PREEMPTION_TIMER: |
| 8131 | return false; |
Ladi Prosek | d0ee363 | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 8132 | case EXIT_REASON_PML_FULL: |
| 8133 | /* We don't expose PML support to L1. */ |
| 8134 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8135 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8136 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8137 | } |
| 8138 | } |
| 8139 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 8140 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 8141 | { |
| 8142 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 8143 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8144 | } |
| 8145 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8146 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8147 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8148 | if (vmx->pml_pg) { |
| 8149 | __free_page(vmx->pml_pg); |
| 8150 | vmx->pml_pg = NULL; |
| 8151 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8152 | } |
| 8153 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8154 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8155 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8156 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8157 | u64 *pml_buf; |
| 8158 | u16 pml_idx; |
| 8159 | |
| 8160 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 8161 | |
| 8162 | /* Do nothing if PML buffer is empty */ |
| 8163 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 8164 | return; |
| 8165 | |
| 8166 | /* PML index always points to next available PML buffer entity */ |
| 8167 | if (pml_idx >= PML_ENTITY_NUM) |
| 8168 | pml_idx = 0; |
| 8169 | else |
| 8170 | pml_idx++; |
| 8171 | |
| 8172 | pml_buf = page_address(vmx->pml_pg); |
| 8173 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 8174 | u64 gpa; |
| 8175 | |
| 8176 | gpa = pml_buf[pml_idx]; |
| 8177 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8178 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8179 | } |
| 8180 | |
| 8181 | /* reset PML index */ |
| 8182 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 8183 | } |
| 8184 | |
| 8185 | /* |
| 8186 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 8187 | * Called before reporting dirty_bitmap to userspace. |
| 8188 | */ |
| 8189 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 8190 | { |
| 8191 | int i; |
| 8192 | struct kvm_vcpu *vcpu; |
| 8193 | /* |
| 8194 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 8195 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 8196 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 8197 | * buffer. |
| 8198 | */ |
| 8199 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 8200 | kvm_vcpu_kick(vcpu); |
| 8201 | } |
| 8202 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8203 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 8204 | { |
| 8205 | pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", |
Chao Peng | 7c3bab1 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 8206 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8207 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 8208 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 8209 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 8210 | } |
| 8211 | |
| 8212 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 8213 | { |
| 8214 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 8215 | name, vmcs_read32(limit), |
| 8216 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 8217 | } |
| 8218 | |
| 8219 | static void dump_vmcs(void) |
| 8220 | { |
| 8221 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 8222 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 8223 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 8224 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 8225 | u32 secondary_exec_control = 0; |
| 8226 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 8227 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8228 | int i, n; |
| 8229 | |
| 8230 | if (cpu_has_secondary_exec_ctrls()) |
| 8231 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8232 | |
| 8233 | pr_err("*** Guest State ***\n"); |
| 8234 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8235 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 8236 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 8237 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8238 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 8239 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 8240 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 8241 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 8242 | { |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8243 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 8244 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 8245 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 8246 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8247 | } |
| 8248 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 8249 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 8250 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 8251 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 8252 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8253 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 8254 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 8255 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 8256 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 8257 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 8258 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 8259 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 8260 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 8261 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 8262 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 8263 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 8264 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 8265 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 8266 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8267 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8268 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 8269 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 8270 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8271 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
| 8272 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8273 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8274 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8275 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8276 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8277 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 8278 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 8279 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 8280 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 8281 | pr_err("InterruptStatus = %04x\n", |
| 8282 | vmcs_read16(GUEST_INTR_STATUS)); |
| 8283 | |
| 8284 | pr_err("*** Host State ***\n"); |
| 8285 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 8286 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 8287 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 8288 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 8289 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 8290 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 8291 | vmcs_read16(HOST_TR_SELECTOR)); |
| 8292 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 8293 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 8294 | vmcs_readl(HOST_TR_BASE)); |
| 8295 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 8296 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 8297 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 8298 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 8299 | vmcs_readl(HOST_CR4)); |
| 8300 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8301 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 8302 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 8303 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 8304 | if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER)) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8305 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8306 | vmcs_read64(HOST_IA32_EFER), |
| 8307 | vmcs_read64(HOST_IA32_PAT)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8308 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8309 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8310 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8311 | |
| 8312 | pr_err("*** Control State ***\n"); |
| 8313 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 8314 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 8315 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 8316 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 8317 | vmcs_read32(EXCEPTION_BITMAP), |
| 8318 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 8319 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 8320 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8321 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8322 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 8323 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 8324 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8325 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8326 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8327 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 8328 | pr_err(" reason=%08x qualification=%016lx\n", |
| 8329 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 8330 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 8331 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 8332 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8333 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 8334 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8335 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 8336 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8337 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 8338 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 8339 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 8340 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 8341 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8342 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8343 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 8344 | for (i = 0; i + 1 < n; i += 4) |
| 8345 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 8346 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 8347 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 8348 | if (i < n) |
| 8349 | pr_err("CR3 target%u=%016lx\n", |
| 8350 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 8351 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 8352 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 8353 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 8354 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 8355 | pr_err("Virtual processor ID = 0x%04x\n", |
| 8356 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 8357 | } |
| 8358 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8359 | /* |
| 8360 | * The guest has exited. See if we can fix it or if we need userspace |
| 8361 | * assistance. |
| 8362 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8363 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8364 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8365 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8366 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 8367 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8368 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 8369 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 8370 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8371 | /* |
| 8372 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 8373 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 8374 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 8375 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 8376 | * flushed already. |
| 8377 | */ |
| 8378 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8379 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8380 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8381 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 8382 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8383 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 8384 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8385 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 8386 | nested_vmx_vmexit(vcpu, exit_reason, |
| 8387 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8388 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8389 | return 1; |
| 8390 | } |
| 8391 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8392 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8393 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8394 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8395 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 8396 | = exit_reason; |
| 8397 | return 0; |
| 8398 | } |
| 8399 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8400 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8401 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8402 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8403 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 8404 | return 0; |
| 8405 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8406 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8407 | /* |
| 8408 | * Note: |
| 8409 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 8410 | * delivery event since it indicates guest is accessing MMIO. |
| 8411 | * The vm-exit can be triggered again after return to guest that |
| 8412 | * will cause infinite loop. |
| 8413 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 8414 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 8415 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 8416 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 8417 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8418 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 8419 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 8420 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
| 8421 | vcpu->run->internal.ndata = 2; |
| 8422 | vcpu->run->internal.data[0] = vectoring_info; |
| 8423 | vcpu->run->internal.data[1] = exit_reason; |
| 8424 | return 0; |
| 8425 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8426 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8427 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && |
| 8428 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 8429 | get_vmcs12(vcpu))))) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 8430 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8431 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8432 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 8433 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8434 | /* |
| 8435 | * This CPU don't support us in finding the end of an |
| 8436 | * NMI-blocked window if the guest runs with IRQs |
| 8437 | * disabled. So we pull the trigger after 1 s of |
| 8438 | * futile waiting, but inform the user about this. |
| 8439 | */ |
| 8440 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 8441 | "state on VCPU %d after 1 s timeout\n", |
| 8442 | __func__, vcpu->vcpu_id); |
| 8443 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8444 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8445 | } |
| 8446 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8447 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 8448 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8449 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8450 | else { |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 8451 | WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason); |
| 8452 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8453 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8454 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8455 | } |
| 8456 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8457 | 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] | 8458 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8459 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8460 | |
| 8461 | if (is_guest_mode(vcpu) && |
| 8462 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 8463 | return; |
| 8464 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8465 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8466 | vmcs_write32(TPR_THRESHOLD, 0); |
| 8467 | return; |
| 8468 | } |
| 8469 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8470 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8471 | } |
| 8472 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8473 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 8474 | { |
| 8475 | u32 sec_exec_control; |
| 8476 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 8477 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 8478 | if (is_guest_mode(vcpu)) { |
| 8479 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; |
| 8480 | return; |
| 8481 | } |
| 8482 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 8483 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8484 | return; |
| 8485 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 8486 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8487 | return; |
| 8488 | |
| 8489 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8490 | |
| 8491 | if (set) { |
| 8492 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 8493 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8494 | } else { |
| 8495 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8496 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8497 | vmx_flush_tlb_ept_only(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8498 | } |
| 8499 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 8500 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 8501 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8502 | } |
| 8503 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8504 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 8505 | { |
| 8506 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8507 | |
| 8508 | /* |
| 8509 | * Currently we do not handle the nested case where L2 has an |
| 8510 | * APIC access page of its own; that page is still pinned. |
| 8511 | * Hence, we skip the case where the VCPU is in guest mode _and_ |
| 8512 | * L1 prepared an APIC access page for L2. |
| 8513 | * |
| 8514 | * For the case where L1 and L2 share the same APIC access page |
| 8515 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear |
| 8516 | * in the vmcs12), this function will only update either the vmcs01 |
| 8517 | * or the vmcs02. If the former, the vmcs02 will be updated by |
| 8518 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in |
| 8519 | * the next L2->L1 exit. |
| 8520 | */ |
| 8521 | if (!is_guest_mode(vcpu) || |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8522 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8523 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8524 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8525 | vmx_flush_tlb_ept_only(vcpu); |
| 8526 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8527 | } |
| 8528 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8529 | 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] | 8530 | { |
| 8531 | u16 status; |
| 8532 | u8 old; |
| 8533 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8534 | if (max_isr == -1) |
| 8535 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8536 | |
| 8537 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8538 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8539 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8540 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8541 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8542 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8543 | } |
| 8544 | } |
| 8545 | |
| 8546 | static void vmx_set_rvi(int vector) |
| 8547 | { |
| 8548 | u16 status; |
| 8549 | u8 old; |
| 8550 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8551 | if (vector == -1) |
| 8552 | vector = 0; |
| 8553 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8554 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8555 | old = (u8)status & 0xff; |
| 8556 | if ((u8)vector != old) { |
| 8557 | status &= ~0xff; |
| 8558 | status |= (u8)vector; |
| 8559 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8560 | } |
| 8561 | } |
| 8562 | |
| 8563 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 8564 | { |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8565 | if (!is_guest_mode(vcpu)) { |
| 8566 | vmx_set_rvi(max_irr); |
| 8567 | return; |
| 8568 | } |
| 8569 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8570 | if (max_irr == -1) |
| 8571 | return; |
| 8572 | |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8573 | /* |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8574 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
| 8575 | * handles it. |
| 8576 | */ |
| 8577 | if (nested_exit_on_intr(vcpu)) |
| 8578 | return; |
| 8579 | |
| 8580 | /* |
| 8581 | * Else, fall back to pre-APICv interrupt injection since L2 |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8582 | * is run without virtual interrupt delivery. |
| 8583 | */ |
| 8584 | if (!kvm_event_needs_reinjection(vcpu) && |
| 8585 | vmx_interrupt_allowed(vcpu)) { |
| 8586 | kvm_queue_interrupt(vcpu, max_irr, false); |
| 8587 | vmx_inject_irq(vcpu); |
| 8588 | } |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8589 | } |
| 8590 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 8591 | 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] | 8592 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 8593 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 8594 | return; |
| 8595 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8596 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 8597 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 8598 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 8599 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 8600 | } |
| 8601 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8602 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8603 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8604 | u32 exit_intr_info; |
| 8605 | |
| 8606 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 8607 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 8608 | return; |
| 8609 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8610 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8611 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8612 | |
| 8613 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8614 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8615 | kvm_machine_check(); |
| 8616 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8617 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8618 | if (is_nmi(exit_intr_info)) { |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8619 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8620 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8621 | kvm_after_handle_nmi(&vmx->vcpu); |
| 8622 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8623 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8624 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8625 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 8626 | { |
| 8627 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8628 | register void *__sp asm(_ASM_SP); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8629 | |
| 8630 | /* |
| 8631 | * If external interrupt exists, IF bit is set in rflags/eflags on the |
| 8632 | * interrupt stack frame, and interrupt will be enabled on a return |
| 8633 | * from interrupt handler. |
| 8634 | */ |
| 8635 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 8636 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 8637 | unsigned int vector; |
| 8638 | unsigned long entry; |
| 8639 | gate_desc *desc; |
| 8640 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8641 | #ifdef CONFIG_X86_64 |
| 8642 | unsigned long tmp; |
| 8643 | #endif |
| 8644 | |
| 8645 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8646 | desc = (gate_desc *)vmx->host_idt_base + vector; |
| 8647 | entry = gate_offset(*desc); |
| 8648 | asm volatile( |
| 8649 | #ifdef CONFIG_X86_64 |
| 8650 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 8651 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 8652 | "push $%c[ss]\n\t" |
| 8653 | "push %[sp]\n\t" |
| 8654 | #endif |
| 8655 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8656 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | ec86a1d | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 8657 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8658 | : |
| 8659 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8660 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8661 | #endif |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8662 | "+r"(__sp) |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8663 | : |
Peter Zijlstra | ec86a1d | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 8664 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8665 | [ss]"i"(__KERNEL_DS), |
| 8666 | [cs]"i"(__KERNEL_CS) |
| 8667 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 8668 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8669 | } |
| 8670 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 8671 | static bool vmx_has_high_real_mode_segbase(void) |
| 8672 | { |
| 8673 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 8674 | } |
| 8675 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 8676 | static bool vmx_mpx_supported(void) |
| 8677 | { |
| 8678 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 8679 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 8680 | } |
| 8681 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 8682 | static bool vmx_xsaves_supported(void) |
| 8683 | { |
| 8684 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 8685 | SECONDARY_EXEC_XSAVES; |
| 8686 | } |
| 8687 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8688 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 8689 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8690 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8691 | bool unblock_nmi; |
| 8692 | u8 vector; |
| 8693 | bool idtv_info_valid; |
| 8694 | |
| 8695 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8696 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8697 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 8698 | if (vmx->nmi_known_unmasked) |
| 8699 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8700 | /* |
| 8701 | * Can't use vmx->exit_intr_info since we're not sure what |
| 8702 | * the exit reason is. |
| 8703 | */ |
| 8704 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8705 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 8706 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8707 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8708 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8709 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 8710 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8711 | * SDM 3: 23.2.2 (September 2008) |
| 8712 | * Bit 12 is undefined in any of the following cases: |
| 8713 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 8714 | * information field. |
| 8715 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8716 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8717 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 8718 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8719 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8720 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 8721 | else |
| 8722 | vmx->nmi_known_unmasked = |
| 8723 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 8724 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8725 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 8726 | vmx->vnmi_blocked_time += |
| 8727 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8728 | } |
| 8729 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8730 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8731 | u32 idt_vectoring_info, |
| 8732 | int instr_len_field, |
| 8733 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8734 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8735 | u8 vector; |
| 8736 | int type; |
| 8737 | bool idtv_info_valid; |
| 8738 | |
| 8739 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8740 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8741 | vcpu->arch.nmi_injected = false; |
| 8742 | kvm_clear_exception_queue(vcpu); |
| 8743 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8744 | |
| 8745 | if (!idtv_info_valid) |
| 8746 | return; |
| 8747 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8748 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8749 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8750 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 8751 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8752 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 8753 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8754 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8755 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8756 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8757 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8758 | * Clear bit "block by NMI" before VM entry if a NMI |
| 8759 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8760 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8761 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8762 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8763 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8764 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8765 | /* fall through */ |
| 8766 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8767 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8768 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8769 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8770 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8771 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8772 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8773 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8774 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8775 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8776 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8777 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8778 | break; |
| 8779 | default: |
| 8780 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 8781 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8782 | } |
| 8783 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8784 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 8785 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8786 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8787 | VM_EXIT_INSTRUCTION_LEN, |
| 8788 | IDT_VECTORING_ERROR_CODE); |
| 8789 | } |
| 8790 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8791 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 8792 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8793 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8794 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8795 | VM_ENTRY_INSTRUCTION_LEN, |
| 8796 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 8797 | |
| 8798 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 8799 | } |
| 8800 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8801 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 8802 | { |
| 8803 | int i, nr_msrs; |
| 8804 | struct perf_guest_switch_msr *msrs; |
| 8805 | |
| 8806 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 8807 | |
| 8808 | if (!msrs) |
| 8809 | return; |
| 8810 | |
| 8811 | for (i = 0; i < nr_msrs; i++) |
| 8812 | if (msrs[i].host == msrs[i].guest) |
| 8813 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 8814 | else |
| 8815 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 8816 | msrs[i].host); |
| 8817 | } |
| 8818 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8819 | void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
| 8820 | { |
| 8821 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8822 | u64 tscl; |
| 8823 | u32 delta_tsc; |
| 8824 | |
| 8825 | if (vmx->hv_deadline_tsc == -1) |
| 8826 | return; |
| 8827 | |
| 8828 | tscl = rdtsc(); |
| 8829 | if (vmx->hv_deadline_tsc > tscl) |
| 8830 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 8831 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 8832 | cpu_preemption_timer_multi); |
| 8833 | else |
| 8834 | delta_tsc = 0; |
| 8835 | |
| 8836 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 8837 | } |
| 8838 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 8839 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8840 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 8841 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 8842 | unsigned long debugctlmsr, cr4; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8843 | |
| 8844 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 8845 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 8846 | vmx->entry_time = ktime_get(); |
| 8847 | |
| 8848 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 8849 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 8850 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8851 | return; |
| 8852 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 8853 | if (vmx->ple_window_dirty) { |
| 8854 | vmx->ple_window_dirty = false; |
| 8855 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 8856 | } |
| 8857 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8858 | if (vmx->nested.sync_shadow_vmcs) { |
| 8859 | copy_vmcs12_to_shadow(vmx); |
| 8860 | vmx->nested.sync_shadow_vmcs = false; |
| 8861 | } |
| 8862 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8863 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 8864 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 8865 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 8866 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 8867 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 8868 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 8869 | if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) { |
| 8870 | vmcs_writel(HOST_CR4, cr4); |
| 8871 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 8872 | } |
| 8873 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8874 | /* When single-stepping over STI and MOV SS, we must clear the |
| 8875 | * corresponding interruptibility bits in the guest state. Otherwise |
| 8876 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 8877 | * exceptions being set, but that's not correct for the guest debugging |
| 8878 | * case. */ |
| 8879 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 8880 | vmx_set_interrupt_shadow(vcpu, 0); |
| 8881 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 8882 | if (vmx->guest_pkru_valid) |
| 8883 | __write_pkru(vmx->guest_pkru); |
| 8884 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8885 | atomic_switch_perf_msrs(vmx); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 8886 | debugctlmsr = get_debugctlmsr(); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8887 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8888 | vmx_arm_hv_timer(vcpu); |
| 8889 | |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 8890 | /* |
| 8891 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 8892 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 8893 | * is no need to worry about the conditional branch over the wrmsr |
| 8894 | * being speculatively taken. |
| 8895 | */ |
| 8896 | if (vmx->spec_ctrl) |
| 8897 | wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); |
| 8898 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8899 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8900 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8901 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8902 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 8903 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 8904 | "push %%" _ASM_CX " \n\t" |
| 8905 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8906 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8907 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 8908 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8909 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8910 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8911 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 8912 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 8913 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8914 | "je 2f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8915 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8916 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8917 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8918 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8919 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8920 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 8921 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 8922 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 8923 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 8924 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 8925 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8926 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8927 | "mov %c[r8](%0), %%r8 \n\t" |
| 8928 | "mov %c[r9](%0), %%r9 \n\t" |
| 8929 | "mov %c[r10](%0), %%r10 \n\t" |
| 8930 | "mov %c[r11](%0), %%r11 \n\t" |
| 8931 | "mov %c[r12](%0), %%r12 \n\t" |
| 8932 | "mov %c[r13](%0), %%r13 \n\t" |
| 8933 | "mov %c[r14](%0), %%r14 \n\t" |
| 8934 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8935 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8936 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 8937 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8938 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8939 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 8940 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8941 | "jmp 2f \n\t" |
| 8942 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 8943 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8944 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8945 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 8946 | "pop %0 \n\t" |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 8947 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8948 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 8949 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 8950 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 8951 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 8952 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 8953 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 8954 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8955 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8956 | "mov %%r8, %c[r8](%0) \n\t" |
| 8957 | "mov %%r9, %c[r9](%0) \n\t" |
| 8958 | "mov %%r10, %c[r10](%0) \n\t" |
| 8959 | "mov %%r11, %c[r11](%0) \n\t" |
| 8960 | "mov %%r12, %c[r12](%0) \n\t" |
| 8961 | "mov %%r13, %c[r13](%0) \n\t" |
| 8962 | "mov %%r14, %c[r14](%0) \n\t" |
| 8963 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 8964 | "xor %%r8d, %%r8d \n\t" |
| 8965 | "xor %%r9d, %%r9d \n\t" |
| 8966 | "xor %%r10d, %%r10d \n\t" |
| 8967 | "xor %%r11d, %%r11d \n\t" |
| 8968 | "xor %%r12d, %%r12d \n\t" |
| 8969 | "xor %%r13d, %%r13d \n\t" |
| 8970 | "xor %%r14d, %%r14d \n\t" |
| 8971 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8972 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8973 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 8974 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 8975 | |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 8976 | "xor %%eax, %%eax \n\t" |
| 8977 | "xor %%ebx, %%ebx \n\t" |
| 8978 | "xor %%esi, %%esi \n\t" |
| 8979 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8980 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8981 | ".pushsection .rodata \n\t" |
| 8982 | ".global vmx_return \n\t" |
| 8983 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 8984 | ".popsection" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8985 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8986 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8987 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8988 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 8989 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 8990 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 8991 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 8992 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 8993 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 8994 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 8995 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8996 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 8997 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 8998 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 8999 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 9000 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 9001 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 9002 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 9003 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 9004 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9005 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 9006 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 9007 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9008 | : "cc", "memory" |
| 9009 | #ifdef CONFIG_X86_64 |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9010 | , "rax", "rbx", "rdi", "rsi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9011 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9012 | #else |
| 9013 | , "eax", "ebx", "edi", "esi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9014 | #endif |
| 9015 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9016 | |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9017 | /* |
| 9018 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 9019 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 9020 | * turn it off. This is much more efficient than blindly adding |
| 9021 | * it to the atomic save/restore list. Especially as the former |
| 9022 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 9023 | * |
| 9024 | * For non-nested case: |
| 9025 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 9026 | * save it. |
| 9027 | * |
| 9028 | * For nested case: |
| 9029 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 9030 | * save it. |
| 9031 | */ |
| 9032 | if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)) |
| 9033 | rdmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); |
| 9034 | |
| 9035 | if (vmx->spec_ctrl) |
| 9036 | wrmsrl(MSR_IA32_SPEC_CTRL, 0); |
| 9037 | |
David Woodhouse | c1ddd99 | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 9038 | /* Eliminate branch target predictions from guest mode */ |
| 9039 | vmexit_fill_RSB(); |
| 9040 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 9041 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
| 9042 | if (debugctlmsr) |
| 9043 | update_debugctlmsr(debugctlmsr); |
| 9044 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 9045 | #ifndef CONFIG_X86_64 |
| 9046 | /* |
| 9047 | * The sysexit path does not restore ds/es, so we must set them to |
| 9048 | * a reasonable value ourselves. |
| 9049 | * |
| 9050 | * We can't defer this to vmx_load_host_state() since that function |
| 9051 | * may be executed in interrupt context, which saves and restore segments |
| 9052 | * around it, nullifying its effect. |
| 9053 | */ |
| 9054 | loadsegment(ds, __USER_DS); |
| 9055 | loadsegment(es, __USER_DS); |
| 9056 | #endif |
| 9057 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 9058 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 9059 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9060 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 9061 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9062 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 9063 | vcpu->arch.regs_dirty = 0; |
| 9064 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9065 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 9066 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9067 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 9068 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9069 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9070 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9071 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9072 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 9073 | * back on host, so it is safe to read guest PKRU from current |
| 9074 | * XSAVE. |
| 9075 | */ |
| 9076 | if (boot_cpu_has(X86_FEATURE_OSPKE)) { |
| 9077 | vmx->guest_pkru = __read_pkru(); |
| 9078 | if (vmx->guest_pkru != vmx->host_pkru) { |
| 9079 | vmx->guest_pkru_valid = true; |
| 9080 | __write_pkru(vmx->host_pkru); |
| 9081 | } else |
| 9082 | vmx->guest_pkru_valid = false; |
| 9083 | } |
| 9084 | |
| 9085 | /* |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9086 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 9087 | * we did not inject a still-pending event to L1 now because of |
| 9088 | * nested_run_pending, we need to re-enable this bit. |
| 9089 | */ |
| 9090 | if (vmx->nested.nested_run_pending) |
| 9091 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9092 | |
| 9093 | vmx->nested.nested_run_pending = 0; |
| 9094 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9095 | vmx_complete_atomic_exit(vmx); |
| 9096 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9097 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9098 | } |
| 9099 | |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9100 | static void vmx_load_vmcs01(struct kvm_vcpu *vcpu) |
| 9101 | { |
| 9102 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9103 | int cpu; |
| 9104 | |
| 9105 | if (vmx->loaded_vmcs == &vmx->vmcs01) |
| 9106 | return; |
| 9107 | |
| 9108 | cpu = get_cpu(); |
| 9109 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 9110 | vmx_vcpu_put(vcpu); |
| 9111 | vmx_vcpu_load(vcpu, cpu); |
| 9112 | vcpu->cpu = cpu; |
| 9113 | put_cpu(); |
| 9114 | } |
| 9115 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9116 | /* |
| 9117 | * Ensure that the current vmcs of the logical processor is the |
| 9118 | * vmcs01 of the vcpu before calling free_nested(). |
| 9119 | */ |
| 9120 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 9121 | { |
| 9122 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9123 | int r; |
| 9124 | |
| 9125 | r = vcpu_load(vcpu); |
| 9126 | BUG_ON(r); |
| 9127 | vmx_load_vmcs01(vcpu); |
| 9128 | free_nested(vmx); |
| 9129 | vcpu_put(vcpu); |
| 9130 | } |
| 9131 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9132 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 9133 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9134 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9135 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9136 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9137 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9138 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9139 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9140 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9141 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9142 | kfree(vmx->guest_msrs); |
| 9143 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9144 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9145 | } |
| 9146 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9147 | 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] | 9148 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9149 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 9150 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9151 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9152 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9153 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9154 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9155 | return ERR_PTR(-ENOMEM); |
| 9156 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9157 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 9158 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9159 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 9160 | if (err) |
| 9161 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9162 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9163 | err = -ENOMEM; |
| 9164 | |
| 9165 | /* |
| 9166 | * If PML is turned on, failure on enabling PML just results in failure |
| 9167 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 9168 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 9169 | * for the guest, etc. |
| 9170 | */ |
| 9171 | if (enable_pml) { |
| 9172 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 9173 | if (!vmx->pml_pg) |
| 9174 | goto uninit_vcpu; |
| 9175 | } |
| 9176 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9177 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 9178 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 9179 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 9180 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9181 | if (!vmx->guest_msrs) |
| 9182 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9183 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9184 | if (!vmm_exclusive) |
| 9185 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 9186 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9187 | if (!vmm_exclusive) |
| 9188 | kvm_cpu_vmxoff(); |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 9189 | if (err < 0) |
| 9190 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9191 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9192 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 9193 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 9194 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 9195 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 9196 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 9197 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 9198 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 9199 | vmx->msr_bitmap_mode = 0; |
| 9200 | |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 9201 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9202 | cpu = get_cpu(); |
| 9203 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 9204 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 9205 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9206 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9207 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9208 | if (err) |
| 9209 | goto free_vmcs; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9210 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 9211 | err = alloc_apic_access_page(kvm); |
| 9212 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 9213 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 9214 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9215 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9216 | if (enable_ept) { |
| 9217 | if (!kvm->arch.ept_identity_map_addr) |
| 9218 | kvm->arch.ept_identity_map_addr = |
| 9219 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 9220 | err = init_rmode_identity_map(kvm); |
| 9221 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 9222 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9223 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 9224 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9225 | if (nested) { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9226 | nested_vmx_setup_ctls_msrs(vmx); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9227 | vmx->nested.vpid02 = allocate_vpid(); |
| 9228 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9229 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9230 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 9231 | vmx->nested.current_vmptr = -1ull; |
| 9232 | vmx->nested.current_vmcs12 = NULL; |
| 9233 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9234 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 9235 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 9236 | /* |
| 9237 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 9238 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 9239 | */ |
| 9240 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 9241 | vmx->pi_desc.sn = 1; |
| 9242 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9243 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9244 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9245 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9246 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 9247 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9248 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9249 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9250 | free_pml: |
| 9251 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9252 | uninit_vcpu: |
| 9253 | kvm_vcpu_uninit(&vmx->vcpu); |
| 9254 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9255 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9256 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9257 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9258 | } |
| 9259 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 9260 | static void __init vmx_check_processor_compat(void *rtn) |
| 9261 | { |
| 9262 | struct vmcs_config vmcs_conf; |
| 9263 | |
| 9264 | *(int *)rtn = 0; |
| 9265 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 9266 | *(int *)rtn = -EIO; |
| 9267 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 9268 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 9269 | smp_processor_id()); |
| 9270 | *(int *)rtn = -EIO; |
| 9271 | } |
| 9272 | } |
| 9273 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 9274 | static int get_ept_level(void) |
| 9275 | { |
| 9276 | return VMX_EPT_DEFAULT_GAW + 1; |
| 9277 | } |
| 9278 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9279 | 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] | 9280 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9281 | u8 cache; |
| 9282 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9283 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9284 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9285 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9286 | * 2. EPT with VT-d: |
| 9287 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9288 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9289 | * b. VT-d with snooping control feature: snooping control feature of |
| 9290 | * VT-d engine can guarantee the cache correctness. Just set it |
| 9291 | * 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] | 9292 | * 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] | 9293 | * consistent with host MTRR |
| 9294 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9295 | if (is_mmio) { |
| 9296 | cache = MTRR_TYPE_UNCACHABLE; |
| 9297 | goto exit; |
| 9298 | } |
| 9299 | |
| 9300 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9301 | ipat = VMX_EPT_IPAT_BIT; |
| 9302 | cache = MTRR_TYPE_WRBACK; |
| 9303 | goto exit; |
| 9304 | } |
| 9305 | |
| 9306 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 9307 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 9308 | 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] | 9309 | cache = MTRR_TYPE_WRBACK; |
| 9310 | else |
| 9311 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9312 | goto exit; |
| 9313 | } |
| 9314 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 9315 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9316 | |
| 9317 | exit: |
| 9318 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 9319 | } |
| 9320 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 9321 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9322 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 9323 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 9324 | return PT_DIRECTORY_LEVEL; |
| 9325 | else |
| 9326 | /* For shadow and EPT supported 1GB page */ |
| 9327 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9328 | } |
| 9329 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9330 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 9331 | { |
| 9332 | /* |
| 9333 | * These bits in the secondary execution controls field |
| 9334 | * are dynamic, the others are mostly based on the hypervisor |
| 9335 | * architecture and the guest's CPUID. Do not touch the |
| 9336 | * dynamic bits. |
| 9337 | */ |
| 9338 | u32 mask = |
| 9339 | SECONDARY_EXEC_SHADOW_VMCS | |
| 9340 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 9341 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9342 | |
| 9343 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9344 | |
| 9345 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 9346 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 9347 | } |
| 9348 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9349 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 9350 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9351 | struct kvm_cpuid_entry2 *best; |
| 9352 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9353 | u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9354 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9355 | if (vmx_rdtscp_supported()) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9356 | bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu); |
| 9357 | if (!rdtscp_enabled) |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9358 | secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP; |
Xiao Guangrong | f36201e | 2015-09-09 14:05:53 +0800 | [diff] [blame] | 9359 | |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9360 | if (nested) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9361 | if (rdtscp_enabled) |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9362 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 9363 | SECONDARY_EXEC_RDTSCP; |
| 9364 | else |
| 9365 | vmx->nested.nested_vmx_secondary_ctls_high &= |
| 9366 | ~SECONDARY_EXEC_RDTSCP; |
| 9367 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9368 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9369 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9370 | /* Exposing INVPCID only when PCID is exposed */ |
| 9371 | best = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 9372 | if (vmx_invpcid_supported() && |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9373 | (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) || |
| 9374 | !guest_cpuid_has_pcid(vcpu))) { |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9375 | secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9376 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9377 | if (best) |
Ren, Yongjie | 4f97704 | 2012-09-07 07:36:59 +0000 | [diff] [blame] | 9378 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9379 | } |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 9380 | |
Huaitong Han | 45bdbcf | 2016-01-12 16:04:20 +0800 | [diff] [blame] | 9381 | if (cpu_has_secondary_exec_ctrls()) |
| 9382 | vmcs_set_secondary_exec_control(secondary_exec_ctl); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9383 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9384 | if (nested_vmx_allowed(vcpu)) |
| 9385 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 9386 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 9387 | else |
| 9388 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 9389 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9390 | } |
| 9391 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9392 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 9393 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 9394 | if (func == 1 && nested) |
| 9395 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9396 | } |
| 9397 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9398 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 9399 | struct x86_exception *fault) |
| 9400 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9401 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9402 | u32 exit_reason; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9403 | |
| 9404 | if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9405 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9406 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9407 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 9408 | nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9409 | vmcs12->guest_physical_address = fault->address; |
| 9410 | } |
| 9411 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9412 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 9413 | |
| 9414 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 9415 | { |
| 9416 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 9417 | return get_vmcs12(vcpu)->ept_pointer; |
| 9418 | } |
| 9419 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 9420 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9421 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 9422 | WARN_ON(mmu_is_nested(vcpu)); |
| 9423 | kvm_init_shadow_ept_mmu(vcpu, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9424 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
| 9425 | VMX_EPT_EXECUTE_ONLY_BIT); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9426 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 9427 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 9428 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 9429 | |
| 9430 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9431 | } |
| 9432 | |
| 9433 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 9434 | { |
| 9435 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 9436 | } |
| 9437 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9438 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 9439 | u16 error_code) |
| 9440 | { |
| 9441 | bool inequality, bit; |
| 9442 | |
| 9443 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 9444 | inequality = |
| 9445 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 9446 | vmcs12->page_fault_error_code_match; |
| 9447 | return inequality ^ bit; |
| 9448 | } |
| 9449 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9450 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 9451 | struct x86_exception *fault) |
| 9452 | { |
| 9453 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9454 | |
| 9455 | WARN_ON(!is_guest_mode(vcpu)); |
| 9456 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9457 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9458 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
| 9459 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9460 | vmcs_readl(EXIT_QUALIFICATION)); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9461 | else |
| 9462 | kvm_inject_page_fault(vcpu, fault); |
| 9463 | } |
| 9464 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9465 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
| 9466 | struct vmcs12 *vmcs12) |
| 9467 | { |
| 9468 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9469 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9470 | |
| 9471 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9472 | if (!PAGE_ALIGNED(vmcs12->apic_access_addr) || |
| 9473 | vmcs12->apic_access_addr >> maxphyaddr) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9474 | return false; |
| 9475 | |
| 9476 | /* |
| 9477 | * Translate L1 physical address to host physical |
| 9478 | * address for vmcs02. Keep the page pinned, so this |
| 9479 | * physical address remains valid. We keep a reference |
| 9480 | * to it so we can release it later. |
| 9481 | */ |
| 9482 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 9483 | nested_release_page(vmx->nested.apic_access_page); |
| 9484 | vmx->nested.apic_access_page = |
| 9485 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 9486 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9487 | |
| 9488 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9489 | if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) || |
| 9490 | vmcs12->virtual_apic_page_addr >> maxphyaddr) |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9491 | return false; |
| 9492 | |
| 9493 | if (vmx->nested.virtual_apic_page) /* shouldn't happen */ |
| 9494 | nested_release_page(vmx->nested.virtual_apic_page); |
| 9495 | vmx->nested.virtual_apic_page = |
| 9496 | nested_get_page(vcpu, vmcs12->virtual_apic_page_addr); |
| 9497 | |
| 9498 | /* |
| 9499 | * Failing the vm entry is _not_ what the processor does |
| 9500 | * but it's basically the only possibility we have. |
| 9501 | * We could still enter the guest if CR8 load exits are |
| 9502 | * enabled, CR8 store exits are enabled, and virtualize APIC |
| 9503 | * access is disabled; in this case the processor would never |
| 9504 | * use the TPR shadow and we could simply clear the bit from |
| 9505 | * the execution control. But such a configuration is useless, |
| 9506 | * so let's keep the code simple. |
| 9507 | */ |
| 9508 | if (!vmx->nested.virtual_apic_page) |
| 9509 | return false; |
| 9510 | } |
| 9511 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9512 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9513 | if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) || |
| 9514 | vmcs12->posted_intr_desc_addr >> maxphyaddr) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9515 | return false; |
| 9516 | |
| 9517 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 9518 | kunmap(vmx->nested.pi_desc_page); |
| 9519 | nested_release_page(vmx->nested.pi_desc_page); |
| 9520 | } |
| 9521 | vmx->nested.pi_desc_page = |
| 9522 | nested_get_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 9523 | if (!vmx->nested.pi_desc_page) |
| 9524 | return false; |
| 9525 | |
| 9526 | vmx->nested.pi_desc = |
| 9527 | (struct pi_desc *)kmap(vmx->nested.pi_desc_page); |
| 9528 | if (!vmx->nested.pi_desc) { |
| 9529 | nested_release_page_clean(vmx->nested.pi_desc_page); |
| 9530 | return false; |
| 9531 | } |
| 9532 | vmx->nested.pi_desc = |
| 9533 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 9534 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9535 | (PAGE_SIZE - 1))); |
| 9536 | } |
| 9537 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9538 | return true; |
| 9539 | } |
| 9540 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9541 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 9542 | { |
| 9543 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 9544 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9545 | |
| 9546 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 9547 | return; |
| 9548 | |
| 9549 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 9550 | * hrtimer_start does not guarantee this. */ |
| 9551 | if (preemption_timeout <= 1) { |
| 9552 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 9553 | return; |
| 9554 | } |
| 9555 | |
| 9556 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 9557 | preemption_timeout *= 1000000; |
| 9558 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 9559 | hrtimer_start(&vmx->nested.preemption_timer, |
| 9560 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 9561 | } |
| 9562 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9563 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 9564 | struct vmcs12 *vmcs12) |
| 9565 | { |
| 9566 | int maxphyaddr; |
| 9567 | u64 addr; |
| 9568 | |
| 9569 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 9570 | return 0; |
| 9571 | |
| 9572 | if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) { |
| 9573 | WARN_ON(1); |
| 9574 | return -EINVAL; |
| 9575 | } |
| 9576 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 9577 | |
| 9578 | if (!PAGE_ALIGNED(vmcs12->msr_bitmap) || |
| 9579 | ((addr + PAGE_SIZE) >> maxphyaddr)) |
| 9580 | return -EINVAL; |
| 9581 | |
| 9582 | return 0; |
| 9583 | } |
| 9584 | |
| 9585 | /* |
| 9586 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 9587 | * we do not use the hardware. |
| 9588 | */ |
| 9589 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
| 9590 | struct vmcs12 *vmcs12) |
| 9591 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9592 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9593 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9594 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9595 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9596 | /* |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9597 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9598 | * |
| 9599 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 9600 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 9601 | * from the L12 MSR bitmap that is too permissive. |
| 9602 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 9603 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 9604 | * works properly because we only update the L01 MSR bitmap lazily. |
| 9605 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 9606 | * updated to reflect this when L1 (or its L2s) actually write to |
| 9607 | * the MSR. |
| 9608 | */ |
| 9609 | bool pred_cmd = msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9610 | bool spec_ctrl = msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9611 | |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9612 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9613 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9614 | return false; |
| 9615 | |
| 9616 | page = nested_get_page(vcpu, vmcs12->msr_bitmap); |
Radim Krčmář | 215df1f | 2017-03-07 17:51:49 +0100 | [diff] [blame] | 9617 | if (!page) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9618 | return false; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9619 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9620 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9621 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
| 9622 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9623 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9624 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
| 9625 | for (msr = 0x800; msr <= 0x8ff; msr++) |
| 9626 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9627 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9628 | msr, MSR_TYPE_R); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9629 | |
| 9630 | nested_vmx_disable_intercept_for_msr( |
| 9631 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9632 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
| 9633 | MSR_TYPE_R | MSR_TYPE_W); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9634 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9635 | if (nested_cpu_has_vid(vmcs12)) { |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9636 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9637 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9638 | APIC_BASE_MSR + (APIC_EOI >> 4), |
| 9639 | MSR_TYPE_W); |
| 9640 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9641 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9642 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
| 9643 | MSR_TYPE_W); |
| 9644 | } |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9645 | } |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9646 | |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9647 | if (spec_ctrl) |
| 9648 | nested_vmx_disable_intercept_for_msr( |
| 9649 | msr_bitmap_l1, msr_bitmap_l0, |
| 9650 | MSR_IA32_SPEC_CTRL, |
| 9651 | MSR_TYPE_R | MSR_TYPE_W); |
| 9652 | |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9653 | if (pred_cmd) |
| 9654 | nested_vmx_disable_intercept_for_msr( |
| 9655 | msr_bitmap_l1, msr_bitmap_l0, |
| 9656 | MSR_IA32_PRED_CMD, |
| 9657 | MSR_TYPE_W); |
| 9658 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9659 | kunmap(page); |
| 9660 | nested_release_page_clean(page); |
| 9661 | |
| 9662 | return true; |
| 9663 | } |
| 9664 | |
| 9665 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 9666 | struct vmcs12 *vmcs12) |
| 9667 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9668 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9669 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9670 | !nested_cpu_has_vid(vmcs12) && |
| 9671 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9672 | return 0; |
| 9673 | |
| 9674 | /* |
| 9675 | * If virtualize x2apic mode is enabled, |
| 9676 | * virtualize apic access must be disabled. |
| 9677 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9678 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 9679 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9680 | return -EINVAL; |
| 9681 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9682 | /* |
| 9683 | * If virtual interrupt delivery is enabled, |
| 9684 | * we must exit on external interrupts. |
| 9685 | */ |
| 9686 | if (nested_cpu_has_vid(vmcs12) && |
| 9687 | !nested_exit_on_intr(vcpu)) |
| 9688 | return -EINVAL; |
| 9689 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9690 | /* |
| 9691 | * bits 15:8 should be zero in posted_intr_nv, |
| 9692 | * the descriptor address has been already checked |
| 9693 | * in nested_get_vmcs12_pages. |
| 9694 | */ |
| 9695 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 9696 | (!nested_cpu_has_vid(vmcs12) || |
| 9697 | !nested_exit_intr_ack_set(vcpu) || |
| 9698 | vmcs12->posted_intr_nv & 0xff00)) |
| 9699 | return -EINVAL; |
| 9700 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9701 | /* tpr shadow is needed by all apicv features. */ |
| 9702 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9703 | return -EINVAL; |
| 9704 | |
| 9705 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9706 | } |
| 9707 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9708 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 9709 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9710 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9711 | { |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9712 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9713 | u64 count, addr; |
| 9714 | |
| 9715 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 9716 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 9717 | WARN_ON(1); |
| 9718 | return -EINVAL; |
| 9719 | } |
| 9720 | if (count == 0) |
| 9721 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9722 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9723 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 9724 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9725 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9726 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 9727 | addr_field, maxphyaddr, count, addr); |
| 9728 | return -EINVAL; |
| 9729 | } |
| 9730 | return 0; |
| 9731 | } |
| 9732 | |
| 9733 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 9734 | struct vmcs12 *vmcs12) |
| 9735 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9736 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 9737 | vmcs12->vm_exit_msr_store_count == 0 && |
| 9738 | vmcs12->vm_entry_msr_load_count == 0) |
| 9739 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9740 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9741 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9742 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9743 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9744 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9745 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9746 | return -EINVAL; |
| 9747 | return 0; |
| 9748 | } |
| 9749 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9750 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 9751 | struct vmx_msr_entry *e) |
| 9752 | { |
| 9753 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 9754 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9755 | return -EINVAL; |
| 9756 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 9757 | e->index == MSR_IA32_UCODE_REV) |
| 9758 | return -EINVAL; |
| 9759 | if (e->reserved != 0) |
| 9760 | return -EINVAL; |
| 9761 | return 0; |
| 9762 | } |
| 9763 | |
| 9764 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 9765 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9766 | { |
| 9767 | if (e->index == MSR_FS_BASE || |
| 9768 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9769 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 9770 | nested_vmx_msr_check_common(vcpu, e)) |
| 9771 | return -EINVAL; |
| 9772 | return 0; |
| 9773 | } |
| 9774 | |
| 9775 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 9776 | struct vmx_msr_entry *e) |
| 9777 | { |
| 9778 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 9779 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9780 | return -EINVAL; |
| 9781 | return 0; |
| 9782 | } |
| 9783 | |
| 9784 | /* |
| 9785 | * Load guest's/host's msr at nested entry/exit. |
| 9786 | * return 0 for success, entry index for failure. |
| 9787 | */ |
| 9788 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 9789 | { |
| 9790 | u32 i; |
| 9791 | struct vmx_msr_entry e; |
| 9792 | struct msr_data msr; |
| 9793 | |
| 9794 | msr.host_initiated = false; |
| 9795 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9796 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 9797 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9798 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9799 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 9800 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9801 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9802 | } |
| 9803 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9804 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9805 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 9806 | __func__, i, e.index, e.reserved); |
| 9807 | goto fail; |
| 9808 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9809 | msr.index = e.index; |
| 9810 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9811 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9812 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9813 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 9814 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9815 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9816 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9817 | } |
| 9818 | return 0; |
| 9819 | fail: |
| 9820 | return i + 1; |
| 9821 | } |
| 9822 | |
| 9823 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 9824 | { |
| 9825 | u32 i; |
| 9826 | struct vmx_msr_entry e; |
| 9827 | |
| 9828 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9829 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9830 | if (kvm_vcpu_read_guest(vcpu, |
| 9831 | gpa + i * sizeof(e), |
| 9832 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9833 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9834 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 9835 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9836 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9837 | } |
| 9838 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9839 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9840 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 9841 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9842 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9843 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9844 | msr_info.host_initiated = false; |
| 9845 | msr_info.index = e.index; |
| 9846 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9847 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9848 | "%s cannot read MSR (%u, 0x%x)\n", |
| 9849 | __func__, i, e.index); |
| 9850 | return -EINVAL; |
| 9851 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9852 | if (kvm_vcpu_write_guest(vcpu, |
| 9853 | gpa + i * sizeof(e) + |
| 9854 | offsetof(struct vmx_msr_entry, value), |
| 9855 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9856 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9857 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9858 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9859 | return -EINVAL; |
| 9860 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9861 | } |
| 9862 | return 0; |
| 9863 | } |
| 9864 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9865 | /* |
| 9866 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 9867 | * 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] | 9868 | * 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] | 9869 | * guest in a way that will both be appropriate to L1's requests, and our |
| 9870 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 9871 | * function also has additional necessary side-effects, like setting various |
| 9872 | * vcpu->arch fields. |
| 9873 | */ |
| 9874 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 9875 | { |
| 9876 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9877 | u32 exec_control; |
| 9878 | |
| 9879 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 9880 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 9881 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 9882 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 9883 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 9884 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 9885 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 9886 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 9887 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 9888 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 9889 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 9890 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 9891 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 9892 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 9893 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 9894 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 9895 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 9896 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 9897 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 9898 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 9899 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 9900 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 9901 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 9902 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 9903 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 9904 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 9905 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 9906 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 9907 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 9908 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 9909 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 9910 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 9911 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 9912 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 9913 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 9914 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 9915 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 9916 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 9917 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 9918 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 9919 | } else { |
| 9920 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 9921 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 9922 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9923 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 9924 | vmcs12->vm_entry_intr_info_field); |
| 9925 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 9926 | vmcs12->vm_entry_exception_error_code); |
| 9927 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 9928 | vmcs12->vm_entry_instruction_len); |
| 9929 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 9930 | vmcs12->guest_interruptibility_info); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9931 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 9932 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9933 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 9934 | vmcs12->guest_pending_dbg_exceptions); |
| 9935 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 9936 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 9937 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9938 | if (nested_cpu_has_xsaves(vmcs12)) |
| 9939 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9940 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 9941 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9942 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9943 | |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 9944 | /* Preemption timer setting is only taken from vmcs01. */ |
| 9945 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 9946 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 9947 | if (vmx->hv_deadline_tsc == -1) |
| 9948 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 9949 | |
| 9950 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9951 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 9952 | /* |
| 9953 | * Note that we use L0's vector here and in |
| 9954 | * vmx_deliver_nested_posted_interrupt. |
| 9955 | */ |
| 9956 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 9957 | vmx->nested.pi_pending = false; |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 9958 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9959 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 9960 | page_to_phys(vmx->nested.pi_desc_page) + |
| 9961 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9962 | (PAGE_SIZE - 1))); |
| 9963 | } else |
| 9964 | exec_control &= ~PIN_BASED_POSTED_INTR; |
| 9965 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9966 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9967 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9968 | vmx->nested.preemption_timer_expired = false; |
| 9969 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 9970 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 9971 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9972 | /* |
| 9973 | * Whether page-faults are trapped is determined by a combination of |
| 9974 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 9975 | * If enable_ept, L0 doesn't care about page faults and we should |
| 9976 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 9977 | * care about (at least some) page faults, and because it is not easy |
| 9978 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 9979 | * to exit on each and every L2 page fault. This is done by setting |
| 9980 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 9981 | * Note that below we don't need special code to set EB.PF beyond the |
| 9982 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 9983 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 9984 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 9985 | * |
| 9986 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 9987 | * injected with more page faults than it asked for. This could have |
| 9988 | * caused problems, but in practice existing hypervisors don't care. |
| 9989 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 9990 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 9991 | */ |
| 9992 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 9993 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 9994 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 9995 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 9996 | |
| 9997 | if (cpu_has_secondary_exec_ctrls()) { |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9998 | exec_control = vmx_secondary_exec_control(vmx); |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 9999 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10000 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 10001 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 10002 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 10003 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 10004 | SECONDARY_EXEC_APIC_REGISTER_VIRT); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10005 | if (nested_cpu_has(vmcs12, |
| 10006 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 10007 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 10008 | |
| 10009 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 10010 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10011 | * If translation failed, no matter: This feature asks |
| 10012 | * to exit when accessing the given address, and if it |
| 10013 | * can never be accessed, this feature won't do |
| 10014 | * anything anyway. |
| 10015 | */ |
| 10016 | if (!vmx->nested.apic_access_page) |
| 10017 | exec_control &= |
| 10018 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 10019 | else |
| 10020 | vmcs_write64(APIC_ACCESS_ADDR, |
| 10021 | page_to_phys(vmx->nested.apic_access_page)); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10022 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 10023 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | ca3f257 | 2013-12-16 12:55:46 +0100 | [diff] [blame] | 10024 | exec_control |= |
| 10025 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10026 | kvm_vcpu_reload_apic_access_page(vcpu); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10027 | } |
| 10028 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10029 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
| 10030 | vmcs_write64(EOI_EXIT_BITMAP0, |
| 10031 | vmcs12->eoi_exit_bitmap0); |
| 10032 | vmcs_write64(EOI_EXIT_BITMAP1, |
| 10033 | vmcs12->eoi_exit_bitmap1); |
| 10034 | vmcs_write64(EOI_EXIT_BITMAP2, |
| 10035 | vmcs12->eoi_exit_bitmap2); |
| 10036 | vmcs_write64(EOI_EXIT_BITMAP3, |
| 10037 | vmcs12->eoi_exit_bitmap3); |
| 10038 | vmcs_write16(GUEST_INTR_STATUS, |
| 10039 | vmcs12->guest_intr_status); |
| 10040 | } |
| 10041 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10042 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 10043 | } |
| 10044 | |
| 10045 | |
| 10046 | /* |
| 10047 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 10048 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 10049 | * Other fields are different per CPU, and will be set later when |
| 10050 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 10051 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10052 | vmx_set_constant_host_state(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10053 | |
| 10054 | /* |
| 10055 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 10056 | * entry, but only if the current (host) sp changed from the value |
| 10057 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 10058 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 10059 | * here we just force the write to happen on entry. |
| 10060 | */ |
| 10061 | vmx->host_rsp = 0; |
| 10062 | |
| 10063 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 10064 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 10065 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 10066 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 10067 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10068 | |
| 10069 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
| 10070 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
| 10071 | page_to_phys(vmx->nested.virtual_apic_page)); |
| 10072 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 86ef97b | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 10073 | } else { |
| 10074 | #ifdef CONFIG_X86_64 |
| 10075 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 10076 | CPU_BASED_CR8_STORE_EXITING; |
| 10077 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10078 | } |
| 10079 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10080 | if (cpu_has_vmx_msr_bitmap() && |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10081 | exec_control & CPU_BASED_USE_MSR_BITMAPS && |
| 10082 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) |
| 10083 | ; /* MSR_BITMAP will be set by following vmx_set_efer. */ |
| 10084 | else |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10085 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 10086 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10087 | /* |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10088 | * Merging of IO bitmap not currently supported. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10089 | * Rather, exit every time. |
| 10090 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10091 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 10092 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 10093 | |
| 10094 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 10095 | |
| 10096 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 10097 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 10098 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 10099 | */ |
| 10100 | update_exception_bitmap(vcpu); |
| 10101 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 10102 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10103 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10104 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 10105 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 10106 | * bits are further modified by vmx_set_efer() below. |
| 10107 | */ |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10108 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10109 | |
| 10110 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 10111 | * emulated by vmx_set_efer(), below. |
| 10112 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10113 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10114 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 10115 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10116 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 10117 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10118 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10119 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10120 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 10121 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10122 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 10123 | |
| 10124 | |
| 10125 | set_cr4_guest_host_mask(vmx); |
| 10126 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10127 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) |
| 10128 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 10129 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10130 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 10131 | vmcs_write64(TSC_OFFSET, |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10132 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10133 | else |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10134 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 10135 | if (kvm_has_tsc_control) |
| 10136 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10137 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10138 | if (cpu_has_vmx_msr_bitmap()) |
| 10139 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 10140 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10141 | if (enable_vpid) { |
| 10142 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10143 | * There is no direct mapping between vpid02 and vpid12, the |
| 10144 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 10145 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 10146 | * The vpid12 is allocated by L1 for L2, so it will not |
| 10147 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 10148 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10149 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10150 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
| 10151 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 10152 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 10153 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 10154 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); |
| 10155 | } |
| 10156 | } else { |
| 10157 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 10158 | vmx_flush_tlb(vcpu); |
| 10159 | } |
| 10160 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10161 | } |
| 10162 | |
Ladi Prosek | 560a979 | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 10163 | if (enable_pml) { |
| 10164 | /* |
| 10165 | * Conceptually we want to copy the PML address and index from |
| 10166 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 10167 | * since we always flush the log on each vmexit, this happens |
| 10168 | * to be equivalent to simply resetting the fields in vmcs02. |
| 10169 | */ |
| 10170 | ASSERT(vmx->pml_pg); |
| 10171 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 10172 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 10173 | } |
| 10174 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10175 | if (nested_cpu_has_ept(vmcs12)) { |
| 10176 | kvm_mmu_unload(vcpu); |
| 10177 | nested_ept_init_mmu_context(vcpu); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 10178 | } else if (nested_cpu_has2(vmcs12, |
| 10179 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 10180 | vmx_flush_tlb_ept_only(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10181 | } |
| 10182 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10183 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 10184 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 10185 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10186 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10187 | else |
| 10188 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10189 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 10190 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10191 | |
| 10192 | /* |
| 10193 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 10194 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 10195 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 10196 | * the specifications by L1; It's not enough to take |
| 10197 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 10198 | * have more bits than L1 expected. |
| 10199 | */ |
| 10200 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 10201 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 10202 | |
| 10203 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 10204 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 10205 | |
| 10206 | /* shadow page tables on either EPT or shadow page tables */ |
| 10207 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 10208 | kvm_mmu_reset_context(vcpu); |
| 10209 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10210 | if (!enable_ept) |
| 10211 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 10212 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10213 | /* |
| 10214 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 10215 | */ |
| 10216 | if (enable_ept) { |
| 10217 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 10218 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 10219 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 10220 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 10221 | } |
| 10222 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10223 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 10224 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 10225 | } |
| 10226 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10227 | /* |
| 10228 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 10229 | * for running an L2 nested guest. |
| 10230 | */ |
| 10231 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 10232 | { |
| 10233 | struct vmcs12 *vmcs12; |
| 10234 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10235 | int cpu; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 10236 | bool ia32e; |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10237 | u32 msr_entry_idx; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10238 | |
| 10239 | if (!nested_vmx_check_permission(vcpu) || |
| 10240 | !nested_vmx_check_vmcs12(vcpu)) |
| 10241 | return 1; |
| 10242 | |
| 10243 | skip_emulated_instruction(vcpu); |
| 10244 | vmcs12 = get_vmcs12(vcpu); |
| 10245 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10246 | if (enable_shadow_vmcs) |
| 10247 | copy_shadow_to_vmcs12(vmx); |
| 10248 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10249 | /* |
| 10250 | * The nested entry process starts with enforcing various prerequisites |
| 10251 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 10252 | * they fail: As the SDM explains, some conditions should cause the |
| 10253 | * instruction to fail, while others will cause the instruction to seem |
| 10254 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 10255 | * To speed up the normal (success) code path, we should avoid checking |
| 10256 | * for misconfigurations which will anyway be caught by the processor |
| 10257 | * when using the merged vmcs02. |
| 10258 | */ |
| 10259 | if (vmcs12->launch_state == launch) { |
| 10260 | nested_vmx_failValid(vcpu, |
| 10261 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 10262 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 10263 | return 1; |
| 10264 | } |
| 10265 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10266 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 10267 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) { |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 10268 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10269 | return 1; |
| 10270 | } |
| 10271 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10272 | if (!nested_get_vmcs12_pages(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10273 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10274 | return 1; |
| 10275 | } |
| 10276 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10277 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10278 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10279 | return 1; |
| 10280 | } |
| 10281 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10282 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) { |
| 10283 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10284 | return 1; |
| 10285 | } |
| 10286 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10287 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) { |
| 10288 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10289 | return 1; |
| 10290 | } |
| 10291 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10292 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10293 | vmx->nested.nested_vmx_true_procbased_ctls_low, |
| 10294 | vmx->nested.nested_vmx_procbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10295 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10296 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 10297 | vmx->nested.nested_vmx_secondary_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10298 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10299 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 10300 | vmx->nested.nested_vmx_pinbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10301 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10302 | vmx->nested.nested_vmx_true_exit_ctls_low, |
| 10303 | vmx->nested.nested_vmx_exit_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10304 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10305 | vmx->nested.nested_vmx_true_entry_ctls_low, |
| 10306 | vmx->nested.nested_vmx_entry_ctls_high)) |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10307 | { |
| 10308 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10309 | return 1; |
| 10310 | } |
| 10311 | |
| 10312 | if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 10313 | ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 10314 | nested_vmx_failValid(vcpu, |
| 10315 | VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 10316 | return 1; |
| 10317 | } |
| 10318 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10319 | if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10320 | ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 10321 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10322 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10323 | return 1; |
| 10324 | } |
| 10325 | if (vmcs12->vmcs_link_pointer != -1ull) { |
| 10326 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10327 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR); |
| 10328 | return 1; |
| 10329 | } |
| 10330 | |
| 10331 | /* |
Jan Kiszka | cb0c8cda | 2013-04-27 12:58:00 +0200 | [diff] [blame] | 10332 | * If the load IA32_EFER VM-entry control is 1, the following checks |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 10333 | * are performed on the field for the IA32_EFER MSR: |
| 10334 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 10335 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 10336 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 10337 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 10338 | * CR0.PG) is 1. |
| 10339 | */ |
| 10340 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) { |
| 10341 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 10342 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 10343 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 10344 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 10345 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) { |
| 10346 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10347 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10348 | return 1; |
| 10349 | } |
| 10350 | } |
| 10351 | |
| 10352 | /* |
| 10353 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 10354 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 10355 | * the values of the LMA and LME bits in the field must each be that of |
| 10356 | * the host address-space size VM-exit control. |
| 10357 | */ |
| 10358 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 10359 | ia32e = (vmcs12->vm_exit_controls & |
| 10360 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 10361 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 10362 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 10363 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) { |
| 10364 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10365 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10366 | return 1; |
| 10367 | } |
| 10368 | } |
| 10369 | |
| 10370 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10371 | * We're finally done with prerequisite checking, and can start with |
| 10372 | * the nested entry. |
| 10373 | */ |
| 10374 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10375 | enter_guest_mode(vcpu); |
| 10376 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10377 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 10378 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10379 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10380 | cpu = get_cpu(); |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 10381 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10382 | vmx_vcpu_put(vcpu); |
| 10383 | vmx_vcpu_load(vcpu, cpu); |
| 10384 | vcpu->cpu = cpu; |
| 10385 | put_cpu(); |
| 10386 | |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 10387 | vmx_segment_cache_clear(vmx); |
| 10388 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10389 | prepare_vmcs02(vcpu, vmcs12); |
| 10390 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10391 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 10392 | vmcs12->vm_entry_msr_load_addr, |
| 10393 | vmcs12->vm_entry_msr_load_count); |
| 10394 | if (msr_entry_idx) { |
| 10395 | leave_guest_mode(vcpu); |
| 10396 | vmx_load_vmcs01(vcpu); |
| 10397 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10398 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); |
| 10399 | return 1; |
| 10400 | } |
| 10401 | |
| 10402 | vmcs12->launch_state = 1; |
| 10403 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10404 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 10405 | return kvm_vcpu_halt(vcpu); |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10406 | |
Jan Kiszka | 7af40ad3 | 2014-01-04 18:47:23 +0100 | [diff] [blame] | 10407 | vmx->nested.nested_run_pending = 1; |
| 10408 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10409 | /* |
| 10410 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 10411 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 10412 | * returned as far as L1 is concerned. It will only return (and set |
| 10413 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 10414 | */ |
| 10415 | return 1; |
| 10416 | } |
| 10417 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10418 | /* |
| 10419 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 10420 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 10421 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 10422 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 10423 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 10424 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 10425 | * didn't trap the bit, because if L1 did, so would L0). |
| 10426 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 10427 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 10428 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 10429 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 10430 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 10431 | * changed these bits, and therefore they need to be updated, but L0 |
| 10432 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 10433 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 10434 | */ |
| 10435 | static inline unsigned long |
| 10436 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10437 | { |
| 10438 | return |
| 10439 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 10440 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 10441 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 10442 | vcpu->arch.cr0_guest_owned_bits)); |
| 10443 | } |
| 10444 | |
| 10445 | static inline unsigned long |
| 10446 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10447 | { |
| 10448 | return |
| 10449 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 10450 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 10451 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 10452 | vcpu->arch.cr4_guest_owned_bits)); |
| 10453 | } |
| 10454 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10455 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 10456 | struct vmcs12 *vmcs12) |
| 10457 | { |
| 10458 | u32 idt_vectoring; |
| 10459 | unsigned int nr; |
| 10460 | |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10461 | if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10462 | nr = vcpu->arch.exception.nr; |
| 10463 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10464 | |
| 10465 | if (kvm_exception_is_soft(nr)) { |
| 10466 | vmcs12->vm_exit_instruction_len = |
| 10467 | vcpu->arch.event_exit_inst_len; |
| 10468 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 10469 | } else |
| 10470 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 10471 | |
| 10472 | if (vcpu->arch.exception.has_error_code) { |
| 10473 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 10474 | vmcs12->idt_vectoring_error_code = |
| 10475 | vcpu->arch.exception.error_code; |
| 10476 | } |
| 10477 | |
| 10478 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 10479 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10480 | vmcs12->idt_vectoring_info_field = |
| 10481 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 10482 | } else if (vcpu->arch.interrupt.pending) { |
| 10483 | nr = vcpu->arch.interrupt.nr; |
| 10484 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10485 | |
| 10486 | if (vcpu->arch.interrupt.soft) { |
| 10487 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 10488 | vmcs12->vm_entry_instruction_len = |
| 10489 | vcpu->arch.event_exit_inst_len; |
| 10490 | } else |
| 10491 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 10492 | |
| 10493 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 10494 | } |
| 10495 | } |
| 10496 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10497 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 10498 | { |
| 10499 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10500 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10501 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 10502 | vmx->nested.preemption_timer_expired) { |
| 10503 | if (vmx->nested.nested_run_pending) |
| 10504 | return -EBUSY; |
| 10505 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 10506 | return 0; |
| 10507 | } |
| 10508 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10509 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Jan Kiszka | 220c567 | 2014-03-07 20:03:14 +0100 | [diff] [blame] | 10510 | if (vmx->nested.nested_run_pending || |
| 10511 | vcpu->arch.interrupt.pending) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10512 | return -EBUSY; |
| 10513 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10514 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 10515 | INTR_INFO_VALID_MASK, 0); |
| 10516 | /* |
| 10517 | * The NMI-triggered VM exit counts as injection: |
| 10518 | * clear this one and block further NMIs. |
| 10519 | */ |
| 10520 | vcpu->arch.nmi_pending = 0; |
| 10521 | vmx_set_nmi_mask(vcpu, true); |
| 10522 | return 0; |
| 10523 | } |
| 10524 | |
| 10525 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 10526 | nested_exit_on_intr(vcpu)) { |
| 10527 | if (vmx->nested.nested_run_pending) |
| 10528 | return -EBUSY; |
| 10529 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10530 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10531 | } |
| 10532 | |
David Hildenbrand | 1edccf2 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 10533 | vmx_complete_nested_posted_interrupt(vcpu); |
| 10534 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10535 | } |
| 10536 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10537 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 10538 | { |
| 10539 | ktime_t remaining = |
| 10540 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 10541 | u64 value; |
| 10542 | |
| 10543 | if (ktime_to_ns(remaining) <= 0) |
| 10544 | return 0; |
| 10545 | |
| 10546 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 10547 | do_div(value, 1000000); |
| 10548 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10549 | } |
| 10550 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10551 | /* |
| 10552 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 10553 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 10554 | * and this function updates it to reflect the changes to the guest state while |
| 10555 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 10556 | * without going back to L1), and to reflect the exit reason. |
| 10557 | * Note that we do not have to copy here all VMCS fields, just those that |
| 10558 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 10559 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 10560 | * which already writes to vmcs12 directly. |
| 10561 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10562 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 10563 | u32 exit_reason, u32 exit_intr_info, |
| 10564 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10565 | { |
| 10566 | /* update guest state fields: */ |
| 10567 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 10568 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 10569 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10570 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 10571 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 10572 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 10573 | |
| 10574 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 10575 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 10576 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 10577 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 10578 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 10579 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 10580 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 10581 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 10582 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 10583 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 10584 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 10585 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 10586 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 10587 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 10588 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 10589 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 10590 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 10591 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 10592 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 10593 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 10594 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 10595 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 10596 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 10597 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 10598 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 10599 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 10600 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 10601 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 10602 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 10603 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 10604 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 10605 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 10606 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 10607 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 10608 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 10609 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 10610 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10611 | vmcs12->guest_interruptibility_info = |
| 10612 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 10613 | vmcs12->guest_pending_dbg_exceptions = |
| 10614 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 10615 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 10616 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 10617 | else |
| 10618 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10619 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10620 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 10621 | if (vmcs12->vm_exit_controls & |
| 10622 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 10623 | vmcs12->vmx_preemption_timer_value = |
| 10624 | vmx_get_preemption_timer_value(vcpu); |
| 10625 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 10626 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 10627 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10628 | /* |
| 10629 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 10630 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 10631 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 10632 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 10633 | * |
| 10634 | * Additionally, restore L2's PDPTR to vmcs12. |
| 10635 | */ |
| 10636 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 10637 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10638 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 10639 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 10640 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 10641 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 10642 | } |
| 10643 | |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 10644 | if (nested_cpu_has_ept(vmcs12)) |
| 10645 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 10646 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10647 | if (nested_cpu_has_vid(vmcs12)) |
| 10648 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 10649 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10650 | vmcs12->vm_entry_controls = |
| 10651 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10652 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10653 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10654 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 10655 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 10656 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10657 | } |
| 10658 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10659 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 10660 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 10661 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10662 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 10663 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 10664 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10665 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 10666 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 10667 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 10668 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10669 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 10670 | if (nested_cpu_has_xsaves(vmcs12)) |
| 10671 | vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10672 | |
| 10673 | /* update exit information fields: */ |
| 10674 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10675 | vmcs12->vm_exit_reason = exit_reason; |
| 10676 | vmcs12->exit_qualification = exit_qualification; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10677 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10678 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Jan Kiszka | c0d1c77 | 2013-04-14 12:12:50 +0200 | [diff] [blame] | 10679 | if ((vmcs12->vm_exit_intr_info & |
| 10680 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 10681 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) |
| 10682 | vmcs12->vm_exit_intr_error_code = |
| 10683 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10684 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10685 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 10686 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 10687 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10688 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 10689 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 10690 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10691 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10692 | |
| 10693 | /* |
| 10694 | * Transfer the event that L0 or L1 may wanted to inject into |
| 10695 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 10696 | */ |
| 10697 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 10698 | } |
| 10699 | |
| 10700 | /* |
| 10701 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 10702 | * preserved above and would only end up incorrectly in L1. |
| 10703 | */ |
| 10704 | vcpu->arch.nmi_injected = false; |
| 10705 | kvm_clear_exception_queue(vcpu); |
| 10706 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10707 | } |
| 10708 | |
| 10709 | /* |
| 10710 | * A part of what we need to when the nested L2 guest exits and we want to |
| 10711 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 10712 | * in vmcs12. |
| 10713 | * This function is to be called not only on normal nested exit, but also on |
| 10714 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 10715 | * Failures During or After Loading Guest State"). |
| 10716 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 10717 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 10718 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 10719 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10720 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10721 | struct kvm_segment seg; |
| 10722 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10723 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 10724 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 10725 | 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] | 10726 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10727 | else |
| 10728 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10729 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10730 | |
| 10731 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 10732 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 10733 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10734 | /* |
| 10735 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 10736 | * actually changed, because it depends on the current state of |
| 10737 | * fpu_active (which may have changed). |
| 10738 | * Note that vmx_set_cr0 refers to efer set above. |
| 10739 | */ |
Jan Kiszka | 9e3e4db | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 10740 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10741 | /* |
| 10742 | * If we did fpu_activate()/fpu_deactivate() during L2's run, we need |
| 10743 | * to apply the same changes to L1's vmcs. We just set cr0 correctly, |
| 10744 | * but we also need to update cr0_guest_host_mask and exception_bitmap. |
| 10745 | */ |
| 10746 | update_exception_bitmap(vcpu); |
| 10747 | vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0); |
| 10748 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10749 | |
| 10750 | /* |
| 10751 | * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01 |
| 10752 | * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask(); |
| 10753 | */ |
| 10754 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 08e1674 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 10755 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10756 | |
Jan Kiszka | 29bf08f | 2013-12-28 16:31:52 +0100 | [diff] [blame] | 10757 | nested_ept_uninit_mmu_context(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10758 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10759 | kvm_set_cr3(vcpu, vmcs12->host_cr3); |
| 10760 | kvm_mmu_reset_context(vcpu); |
| 10761 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10762 | if (!enable_ept) |
| 10763 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 10764 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10765 | if (enable_vpid) { |
| 10766 | /* |
| 10767 | * Trivially support vpid by letting L2s share their parent |
| 10768 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 10769 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 10770 | */ |
| 10771 | vmx_flush_tlb(vcpu); |
| 10772 | } |
| 10773 | |
| 10774 | |
| 10775 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 10776 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 10777 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 10778 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 10779 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 1be0c0e | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 10780 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 10781 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10782 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10783 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 10784 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 10785 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 10786 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10787 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10788 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10789 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 10790 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10791 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 10792 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 10793 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 10794 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10795 | /* Set L1 segment info according to Intel SDM |
| 10796 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 10797 | seg = (struct kvm_segment) { |
| 10798 | .base = 0, |
| 10799 | .limit = 0xFFFFFFFF, |
| 10800 | .selector = vmcs12->host_cs_selector, |
| 10801 | .type = 11, |
| 10802 | .present = 1, |
| 10803 | .s = 1, |
| 10804 | .g = 1 |
| 10805 | }; |
| 10806 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 10807 | seg.l = 1; |
| 10808 | else |
| 10809 | seg.db = 1; |
| 10810 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 10811 | seg = (struct kvm_segment) { |
| 10812 | .base = 0, |
| 10813 | .limit = 0xFFFFFFFF, |
| 10814 | .type = 3, |
| 10815 | .present = 1, |
| 10816 | .s = 1, |
| 10817 | .db = 1, |
| 10818 | .g = 1 |
| 10819 | }; |
| 10820 | seg.selector = vmcs12->host_ds_selector; |
| 10821 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 10822 | seg.selector = vmcs12->host_es_selector; |
| 10823 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 10824 | seg.selector = vmcs12->host_ss_selector; |
| 10825 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 10826 | seg.selector = vmcs12->host_fs_selector; |
| 10827 | seg.base = vmcs12->host_fs_base; |
| 10828 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 10829 | seg.selector = vmcs12->host_gs_selector; |
| 10830 | seg.base = vmcs12->host_gs_base; |
| 10831 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 10832 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 10833 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10834 | .limit = 0x67, |
| 10835 | .selector = vmcs12->host_tr_selector, |
| 10836 | .type = 11, |
| 10837 | .present = 1 |
| 10838 | }; |
| 10839 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 10840 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 10841 | kvm_set_dr(vcpu, 7, 0x400); |
| 10842 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10843 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10844 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10845 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10846 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10847 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 10848 | vmcs12->vm_exit_msr_load_count)) |
| 10849 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10850 | } |
| 10851 | |
| 10852 | /* |
| 10853 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 10854 | * and modify vmcs12 to make it see what it would expect to see there if |
| 10855 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 10856 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10857 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 10858 | u32 exit_intr_info, |
| 10859 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10860 | { |
| 10861 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10862 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10863 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10864 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 10865 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 10866 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10867 | leave_guest_mode(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10868 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 10869 | exit_qualification); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10870 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10871 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 10872 | vmcs12->vm_exit_msr_store_count)) |
| 10873 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
| 10874 | |
Wanpeng Li | f3380ca | 2014-08-05 12:42:23 +0800 | [diff] [blame] | 10875 | vmx_load_vmcs01(vcpu); |
| 10876 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 10877 | if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) |
| 10878 | && nested_exit_intr_ack_set(vcpu)) { |
| 10879 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 10880 | WARN_ON(irq < 0); |
| 10881 | vmcs12->vm_exit_intr_info = irq | |
| 10882 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 10883 | } |
| 10884 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 10885 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 10886 | vmcs12->exit_qualification, |
| 10887 | vmcs12->idt_vectoring_info_field, |
| 10888 | vmcs12->vm_exit_intr_info, |
| 10889 | vmcs12->vm_exit_intr_error_code, |
| 10890 | KVM_ISA_VMX); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10891 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 10892 | vm_entry_controls_reset_shadow(vmx); |
| 10893 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 10894 | vmx_segment_cache_clear(vmx); |
| 10895 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10896 | load_vmcs12_host_state(vcpu, vmcs12); |
| 10897 | |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 10898 | /* Update any VMCS fields that might have changed while L2 ran */ |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10899 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 10900 | if (vmx->hv_deadline_tsc == -1) |
| 10901 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 10902 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 10903 | else |
| 10904 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 10905 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 10906 | if (kvm_has_tsc_control) |
| 10907 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10908 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 10909 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
| 10910 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; |
| 10911 | vmx_set_virtual_x2apic_mode(vcpu, |
| 10912 | vcpu->arch.apic_base & X2APIC_ENABLE); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 10913 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 10914 | nested_cpu_has2(vmcs12, |
| 10915 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 10916 | vmx_flush_tlb_ept_only(vcpu); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 10917 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10918 | |
| 10919 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 10920 | vmx->host_rsp = 0; |
| 10921 | |
| 10922 | /* Unpin physical memory we referred to in vmcs02 */ |
| 10923 | if (vmx->nested.apic_access_page) { |
| 10924 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 10925 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10926 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10927 | if (vmx->nested.virtual_apic_page) { |
| 10928 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 10929 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10930 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10931 | if (vmx->nested.pi_desc_page) { |
| 10932 | kunmap(vmx->nested.pi_desc_page); |
| 10933 | nested_release_page(vmx->nested.pi_desc_page); |
| 10934 | vmx->nested.pi_desc_page = NULL; |
| 10935 | vmx->nested.pi_desc = NULL; |
| 10936 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10937 | |
| 10938 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10939 | * We are now running in L2, mmu_notifier will force to reload the |
| 10940 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 10941 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 10942 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10943 | |
| 10944 | /* |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10945 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 10946 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 10947 | * success or failure flag accordingly. |
| 10948 | */ |
| 10949 | if (unlikely(vmx->fail)) { |
| 10950 | vmx->fail = 0; |
| 10951 | nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 10952 | } else |
| 10953 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10954 | if (enable_shadow_vmcs) |
| 10955 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10956 | |
| 10957 | /* in case we halted in L2 */ |
| 10958 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10959 | } |
| 10960 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10961 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 10962 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 10963 | */ |
| 10964 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 10965 | { |
Wanpeng Li | c886f28 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 10966 | if (is_guest_mode(vcpu)) { |
| 10967 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10968 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | c886f28 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 10969 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 10970 | free_nested(to_vmx(vcpu)); |
| 10971 | } |
| 10972 | |
| 10973 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10974 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 10975 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 10976 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 10977 | * It should only be called before L2 actually succeeded to run, and when |
| 10978 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 10979 | */ |
| 10980 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 10981 | struct vmcs12 *vmcs12, |
| 10982 | u32 reason, unsigned long qualification) |
| 10983 | { |
| 10984 | load_vmcs12_host_state(vcpu, vmcs12); |
| 10985 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 10986 | vmcs12->exit_qualification = qualification; |
| 10987 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10988 | if (enable_shadow_vmcs) |
| 10989 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10990 | } |
| 10991 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 10992 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 10993 | struct x86_instruction_info *info, |
| 10994 | enum x86_intercept_stage stage) |
| 10995 | { |
| 10996 | return X86EMUL_CONTINUE; |
| 10997 | } |
| 10998 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10999 | #ifdef CONFIG_X86_64 |
| 11000 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 11001 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 11002 | u64 divisor, u64 *result) |
| 11003 | { |
| 11004 | u64 low = a << shift, high = a >> (64 - shift); |
| 11005 | |
| 11006 | /* To avoid the overflow on divq */ |
| 11007 | if (high >= divisor) |
| 11008 | return 1; |
| 11009 | |
| 11010 | /* Low hold the result, high hold rem which is discarded */ |
| 11011 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 11012 | "rm" (divisor), "0" (low), "1" (high)); |
| 11013 | *result = low; |
| 11014 | |
| 11015 | return 0; |
| 11016 | } |
| 11017 | |
| 11018 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 11019 | { |
| 11020 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 9175d2e | 2016-06-27 15:08:01 +0200 | [diff] [blame] | 11021 | u64 tscl = rdtsc(); |
| 11022 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 11023 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11024 | |
| 11025 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 11026 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 11027 | u64_shl_div_u64(delta_tsc, |
| 11028 | kvm_tsc_scaling_ratio_frac_bits, |
| 11029 | vcpu->arch.tsc_scaling_ratio, |
| 11030 | &delta_tsc)) |
| 11031 | return -ERANGE; |
| 11032 | |
| 11033 | /* |
| 11034 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 11035 | * we can't use the preemption timer. |
| 11036 | * It's possible that it fits on later vmentries, but checking |
| 11037 | * on every vmentry is costly so we just use an hrtimer. |
| 11038 | */ |
| 11039 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 11040 | return -ERANGE; |
| 11041 | |
| 11042 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 11043 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11044 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11045 | return 0; |
| 11046 | } |
| 11047 | |
| 11048 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 11049 | { |
| 11050 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11051 | vmx->hv_deadline_tsc = -1; |
| 11052 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11053 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11054 | } |
| 11055 | #endif |
| 11056 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 11057 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11058 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 11059 | if (ple_gap) |
| 11060 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11061 | } |
| 11062 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11063 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 11064 | struct kvm_memory_slot *slot) |
| 11065 | { |
| 11066 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 11067 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 11068 | } |
| 11069 | |
| 11070 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 11071 | struct kvm_memory_slot *slot) |
| 11072 | { |
| 11073 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 11074 | } |
| 11075 | |
| 11076 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 11077 | { |
| 11078 | kvm_flush_pml_buffers(kvm); |
| 11079 | } |
| 11080 | |
| 11081 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 11082 | struct kvm_memory_slot *memslot, |
| 11083 | gfn_t offset, unsigned long mask) |
| 11084 | { |
| 11085 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 11086 | } |
| 11087 | |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11088 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 11089 | { |
| 11090 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11091 | struct pi_desc old, new; |
| 11092 | unsigned int dest; |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11093 | |
| 11094 | do { |
| 11095 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11096 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 11097 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11098 | |
| 11099 | dest = cpu_physical_id(vcpu->cpu); |
| 11100 | |
| 11101 | if (x2apic_enabled()) |
| 11102 | new.ndst = dest; |
| 11103 | else |
| 11104 | new.ndst = (dest << 8) & 0xFF00; |
| 11105 | |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11106 | /* set 'NV' to 'notification vector' */ |
| 11107 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 11108 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 11109 | new.control) != old.control); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11110 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11111 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 11112 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11113 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11114 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11115 | vcpu->pre_pcpu = -1; |
| 11116 | } |
| 11117 | } |
| 11118 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11119 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11120 | * This routine does the following things for vCPU which is going |
| 11121 | * to be blocked if VT-d PI is enabled. |
| 11122 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 11123 | * we can find the right vCPU to wake up. |
| 11124 | * - Change the Posted-interrupt descriptor as below: |
| 11125 | * 'NDST' <-- vcpu->pre_pcpu |
| 11126 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 11127 | * - If 'ON' is set during this process, which means at least one |
| 11128 | * interrupt is posted for this vCPU, we cannot block it, in |
| 11129 | * this case, return 1, otherwise, return 0. |
| 11130 | * |
| 11131 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11132 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11133 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11134 | unsigned int dest; |
| 11135 | struct pi_desc old, new; |
| 11136 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11137 | |
| 11138 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11139 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11140 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11141 | return 0; |
| 11142 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11143 | WARN_ON(irqs_disabled()); |
| 11144 | local_irq_disable(); |
| 11145 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 11146 | vcpu->pre_pcpu = vcpu->cpu; |
| 11147 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 11148 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 11149 | &per_cpu(blocked_vcpu_on_cpu, |
| 11150 | vcpu->pre_pcpu)); |
| 11151 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 11152 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11153 | |
| 11154 | do { |
| 11155 | old.control = new.control = pi_desc->control; |
| 11156 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11157 | WARN((pi_desc->sn == 1), |
| 11158 | "Warning: SN field of posted-interrupts " |
| 11159 | "is set before blocking\n"); |
| 11160 | |
| 11161 | /* |
| 11162 | * Since vCPU can be preempted during this process, |
| 11163 | * vcpu->cpu could be different with pre_pcpu, we |
| 11164 | * need to set pre_pcpu as the destination of wakeup |
| 11165 | * notification event, then we can find the right vCPU |
| 11166 | * to wakeup in wakeup handler if interrupts happen |
| 11167 | * when the vCPU is in blocked state. |
| 11168 | */ |
| 11169 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 11170 | |
| 11171 | if (x2apic_enabled()) |
| 11172 | new.ndst = dest; |
| 11173 | else |
| 11174 | new.ndst = (dest << 8) & 0xFF00; |
| 11175 | |
| 11176 | /* set 'NV' to 'wakeup vector' */ |
| 11177 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 11178 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 11179 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11180 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11181 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 11182 | if (pi_test_on(pi_desc) == 1) |
| 11183 | __pi_post_block(vcpu); |
| 11184 | |
| 11185 | local_irq_enable(); |
| 11186 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11187 | } |
| 11188 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11189 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 11190 | { |
| 11191 | if (pi_pre_block(vcpu)) |
| 11192 | return 1; |
| 11193 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11194 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 11195 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 11196 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11197 | return 0; |
| 11198 | } |
| 11199 | |
| 11200 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11201 | { |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11202 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11203 | return; |
| 11204 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11205 | WARN_ON(irqs_disabled()); |
| 11206 | local_irq_disable(); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11207 | __pi_post_block(vcpu); |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11208 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11209 | } |
| 11210 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11211 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 11212 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11213 | if (kvm_x86_ops->set_hv_timer) |
| 11214 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 11215 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11216 | pi_post_block(vcpu); |
| 11217 | } |
| 11218 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11219 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11220 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 11221 | * |
| 11222 | * @kvm: kvm |
| 11223 | * @host_irq: host irq of the interrupt |
| 11224 | * @guest_irq: gsi of the interrupt |
| 11225 | * @set: set or unset PI |
| 11226 | * returns 0 on success, < 0 on failure |
| 11227 | */ |
| 11228 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 11229 | uint32_t guest_irq, bool set) |
| 11230 | { |
| 11231 | struct kvm_kernel_irq_routing_entry *e; |
| 11232 | struct kvm_irq_routing_table *irq_rt; |
| 11233 | struct kvm_lapic_irq irq; |
| 11234 | struct kvm_vcpu *vcpu; |
| 11235 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3d4213f | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 11236 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11237 | |
| 11238 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11239 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11240 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11241 | return 0; |
| 11242 | |
| 11243 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 11244 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3d4213f | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 11245 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 11246 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 11247 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 11248 | guest_irq, irq_rt->nr_rt_entries); |
| 11249 | goto out; |
| 11250 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11251 | |
| 11252 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 11253 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 11254 | continue; |
| 11255 | /* |
| 11256 | * VT-d PI cannot support posting multicast/broadcast |
| 11257 | * interrupts to a vCPU, we still use interrupt remapping |
| 11258 | * for these kind of interrupts. |
| 11259 | * |
| 11260 | * For lowest-priority interrupts, we only support |
| 11261 | * those with single CPU as the destination, e.g. user |
| 11262 | * configures the interrupts via /proc/irq or uses |
| 11263 | * irqbalance to make the interrupts single-CPU. |
| 11264 | * |
| 11265 | * We will support full lowest-priority interrupt later. |
| 11266 | */ |
| 11267 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 11268 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11269 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 11270 | /* |
| 11271 | * Make sure the IRTE is in remapped mode if |
| 11272 | * we don't handle it in posted mode. |
| 11273 | */ |
| 11274 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 11275 | if (ret < 0) { |
| 11276 | printk(KERN_INFO |
| 11277 | "failed to back to remapped mode, irq: %u\n", |
| 11278 | host_irq); |
| 11279 | goto out; |
| 11280 | } |
| 11281 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11282 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11283 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11284 | |
| 11285 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 11286 | vcpu_info.vector = irq.vector; |
| 11287 | |
Feng Wu | b6ce978 | 2016-01-25 16:53:35 +0800 | [diff] [blame] | 11288 | trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11289 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 11290 | |
| 11291 | if (set) |
| 11292 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | 0c4e39c | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 11293 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11294 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11295 | |
| 11296 | if (ret < 0) { |
| 11297 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 11298 | __func__); |
| 11299 | goto out; |
| 11300 | } |
| 11301 | } |
| 11302 | |
| 11303 | ret = 0; |
| 11304 | out: |
| 11305 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 11306 | return ret; |
| 11307 | } |
| 11308 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11309 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 11310 | { |
| 11311 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 11312 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 11313 | FEATURE_CONTROL_LMCE; |
| 11314 | else |
| 11315 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 11316 | ~FEATURE_CONTROL_LMCE; |
| 11317 | } |
| 11318 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 11319 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11320 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 11321 | .disabled_by_bios = vmx_disabled_by_bios, |
| 11322 | .hardware_setup = hardware_setup, |
| 11323 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 11324 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11325 | .hardware_enable = hardware_enable, |
| 11326 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 11327 | .cpu_has_accelerated_tpr = report_flexpriority, |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 11328 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11329 | |
| 11330 | .vcpu_create = vmx_create_vcpu, |
| 11331 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11332 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11333 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11334 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11335 | .vcpu_load = vmx_vcpu_load, |
| 11336 | .vcpu_put = vmx_vcpu_put, |
| 11337 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 11338 | .update_bp_intercept = update_exception_bitmap, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11339 | .get_msr = vmx_get_msr, |
| 11340 | .set_msr = vmx_set_msr, |
| 11341 | .get_segment_base = vmx_get_segment_base, |
| 11342 | .get_segment = vmx_get_segment, |
| 11343 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 11344 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11345 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 11346 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 11347 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 11348 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11349 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11350 | .set_cr3 = vmx_set_cr3, |
| 11351 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11352 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11353 | .get_idt = vmx_get_idt, |
| 11354 | .set_idt = vmx_set_idt, |
| 11355 | .get_gdt = vmx_get_gdt, |
| 11356 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 11357 | .get_dr6 = vmx_get_dr6, |
| 11358 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 11359 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 11360 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 11361 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11362 | .get_rflags = vmx_get_rflags, |
| 11363 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 11364 | |
| 11365 | .get_pkru = vmx_get_pkru, |
| 11366 | |
Paolo Bonzini | 0fdd74f | 2015-05-20 11:33:43 +0200 | [diff] [blame] | 11367 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 11368 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11369 | |
| 11370 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11371 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11372 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 11373 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11374 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 11375 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 11376 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 11377 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 11378 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11379 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 11380 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 11381 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 11382 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11383 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 11384 | .get_nmi_mask = vmx_get_nmi_mask, |
| 11385 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11386 | .enable_nmi_window = enable_nmi_window, |
| 11387 | .enable_irq_window = enable_irq_window, |
| 11388 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 11389 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11390 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 11391 | .get_enable_apicv = vmx_get_enable_apicv, |
| 11392 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 11393 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
| 11394 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 11395 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 11396 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 11397 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11398 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 11399 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 11400 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 11401 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 11402 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11403 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11404 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 11405 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 11406 | |
| 11407 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 11408 | |
| 11409 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 11410 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 11411 | |
| 11412 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 11413 | |
| 11414 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 11415 | |
| 11416 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 11417 | |
| 11418 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 11419 | |
| 11420 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 11421 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 11422 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 11423 | .xsaves_supported = vmx_xsaves_supported, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11424 | |
| 11425 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11426 | |
| 11427 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11428 | |
| 11429 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 11430 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 11431 | .flush_log_dirty = vmx_flush_log_dirty, |
| 11432 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11433 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11434 | .pre_block = vmx_pre_block, |
| 11435 | .post_block = vmx_post_block, |
| 11436 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11437 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11438 | |
| 11439 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11440 | |
| 11441 | #ifdef CONFIG_X86_64 |
| 11442 | .set_hv_timer = vmx_set_hv_timer, |
| 11443 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 11444 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11445 | |
| 11446 | .setup_mce = vmx_setup_mce, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11447 | }; |
| 11448 | |
| 11449 | static int __init vmx_init(void) |
| 11450 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11451 | int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 11452 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11453 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11454 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 11455 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11456 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11457 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 11458 | crash_vmclear_local_loaded_vmcss); |
| 11459 | #endif |
| 11460 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11461 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11462 | } |
| 11463 | |
| 11464 | static void __exit vmx_exit(void) |
| 11465 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11466 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 11467 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11468 | synchronize_rcu(); |
| 11469 | #endif |
| 11470 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 11471 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11472 | } |
| 11473 | |
| 11474 | module_init(vmx_init) |
| 11475 | module_exit(vmx_exit) |