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> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 36 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 37 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 38 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 39 | #include <asm/cpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 40 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 41 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 42 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 43 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 44 | #include <asm/mce.h> |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 45 | #include <asm/fpu/internal.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 46 | #include <asm/perf_event.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 47 | #include <asm/debugreg.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 48 | #include <asm/kexec.h> |
Radim Krčmář | dab2087 | 2015-02-09 22:44:07 +0100 | [diff] [blame] | 49 | #include <asm/apic.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 50 | #include <asm/irq_remapping.h> |
David Woodhouse | c1ddd99 | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 51 | #include <asm/nospec-branch.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 52 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 53 | #include "trace.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 54 | #include "pmu.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 55 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 56 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 57 | #define __ex_clear(x, reg) \ |
| 58 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 59 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 60 | MODULE_AUTHOR("Qumranet"); |
| 61 | MODULE_LICENSE("GPL"); |
| 62 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 63 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 64 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 65 | {} |
| 66 | }; |
| 67 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 68 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 69 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 70 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 71 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 72 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 73 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 74 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 75 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 76 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 77 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 78 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 79 | module_param_named(unrestricted_guest, |
| 80 | enable_unrestricted_guest, bool, S_IRUGO); |
| 81 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 82 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 83 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 84 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 85 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 86 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 87 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 88 | static bool __read_mostly vmm_exclusive = 1; |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 89 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 90 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 91 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 92 | module_param(fasteoi, bool, S_IRUGO); |
| 93 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 94 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 95 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 96 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 97 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 98 | 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] | 99 | /* |
| 100 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 101 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 102 | * use VMX instructions. |
| 103 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 104 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 105 | module_param(nested, bool, S_IRUGO); |
| 106 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 107 | static u64 __read_mostly host_xss; |
| 108 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 109 | static bool __read_mostly enable_pml = 1; |
| 110 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 111 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 112 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 113 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 114 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 115 | static int __read_mostly cpu_preemption_timer_multi; |
| 116 | static bool __read_mostly enable_preemption_timer = 1; |
| 117 | #ifdef CONFIG_X86_64 |
| 118 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 119 | #endif |
| 120 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 121 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
| 122 | #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] | 123 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 124 | (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] | 125 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 126 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Andy Lutomirski | 52ce3c2 | 2014-10-07 17:16:21 -0700 | [diff] [blame] | 127 | | X86_CR4_OSXMMEXCPT | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 128 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 129 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 130 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 131 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 132 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 133 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 134 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 135 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 136 | /* |
| 137 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 138 | * ple_gap: upper bound on the amount of time between two successive |
| 139 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 140 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 141 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 142 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 143 | * less than 2^12 cycles |
| 144 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 145 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 146 | */ |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 147 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
| 148 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 149 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 |
| 150 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 |
| 151 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ |
| 152 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW |
| 153 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 154 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 155 | module_param(ple_gap, int, S_IRUGO); |
| 156 | |
| 157 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 158 | module_param(ple_window, int, S_IRUGO); |
| 159 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 160 | /* Default doubles per-vcpu window every exit. */ |
| 161 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; |
| 162 | module_param(ple_window_grow, int, S_IRUGO); |
| 163 | |
| 164 | /* Default resets per-vcpu window every exit to ple_window. */ |
| 165 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; |
| 166 | module_param(ple_window_shrink, int, S_IRUGO); |
| 167 | |
| 168 | /* Default is to compute the maximum so we can never overflow. */ |
| 169 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 170 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 171 | module_param(ple_window_max, int, S_IRUGO); |
| 172 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 173 | extern const ulong vmx_return; |
| 174 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 175 | #define NR_AUTOLOAD_MSRS 8 |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 176 | #define VMCS02_POOL_SIZE 1 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 177 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 178 | struct vmcs { |
| 179 | u32 revision_id; |
| 180 | u32 abort; |
| 181 | char data[0]; |
| 182 | }; |
| 183 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 184 | /* |
| 185 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 186 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 187 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 188 | */ |
| 189 | struct loaded_vmcs { |
| 190 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 191 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 192 | int cpu; |
| 193 | int launched; |
| 194 | struct list_head loaded_vmcss_on_cpu_link; |
| 195 | }; |
| 196 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 197 | struct shared_msr_entry { |
| 198 | unsigned index; |
| 199 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 200 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 201 | }; |
| 202 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 203 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 204 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 205 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 206 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 207 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 208 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 209 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
| 210 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the |
| 211 | * underlying hardware which will be used to run L2. |
| 212 | * This structure is packed to ensure that its layout is identical across |
| 213 | * machines (necessary for live migration). |
| 214 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 215 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 216 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 217 | struct __packed vmcs12 { |
| 218 | /* According to the Intel spec, a VMCS region must start with the |
| 219 | * following two fields. Then follow implementation-specific data. |
| 220 | */ |
| 221 | u32 revision_id; |
| 222 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 223 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 224 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 225 | u32 padding[7]; /* room for future expansion */ |
| 226 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 227 | u64 io_bitmap_a; |
| 228 | u64 io_bitmap_b; |
| 229 | u64 msr_bitmap; |
| 230 | u64 vm_exit_msr_store_addr; |
| 231 | u64 vm_exit_msr_load_addr; |
| 232 | u64 vm_entry_msr_load_addr; |
| 233 | u64 tsc_offset; |
| 234 | u64 virtual_apic_page_addr; |
| 235 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 236 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 237 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 238 | u64 eoi_exit_bitmap0; |
| 239 | u64 eoi_exit_bitmap1; |
| 240 | u64 eoi_exit_bitmap2; |
| 241 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 242 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 243 | u64 guest_physical_address; |
| 244 | u64 vmcs_link_pointer; |
| 245 | u64 guest_ia32_debugctl; |
| 246 | u64 guest_ia32_pat; |
| 247 | u64 guest_ia32_efer; |
| 248 | u64 guest_ia32_perf_global_ctrl; |
| 249 | u64 guest_pdptr0; |
| 250 | u64 guest_pdptr1; |
| 251 | u64 guest_pdptr2; |
| 252 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 253 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 254 | u64 host_ia32_pat; |
| 255 | u64 host_ia32_efer; |
| 256 | u64 host_ia32_perf_global_ctrl; |
| 257 | u64 padding64[8]; /* room for future expansion */ |
| 258 | /* |
| 259 | * To allow migration of L1 (complete with its L2 guests) between |
| 260 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 261 | * unsigned long fields with no explict size. We use u64 (aliased |
| 262 | * natural_width) instead. Luckily, x86 is little-endian. |
| 263 | */ |
| 264 | natural_width cr0_guest_host_mask; |
| 265 | natural_width cr4_guest_host_mask; |
| 266 | natural_width cr0_read_shadow; |
| 267 | natural_width cr4_read_shadow; |
| 268 | natural_width cr3_target_value0; |
| 269 | natural_width cr3_target_value1; |
| 270 | natural_width cr3_target_value2; |
| 271 | natural_width cr3_target_value3; |
| 272 | natural_width exit_qualification; |
| 273 | natural_width guest_linear_address; |
| 274 | natural_width guest_cr0; |
| 275 | natural_width guest_cr3; |
| 276 | natural_width guest_cr4; |
| 277 | natural_width guest_es_base; |
| 278 | natural_width guest_cs_base; |
| 279 | natural_width guest_ss_base; |
| 280 | natural_width guest_ds_base; |
| 281 | natural_width guest_fs_base; |
| 282 | natural_width guest_gs_base; |
| 283 | natural_width guest_ldtr_base; |
| 284 | natural_width guest_tr_base; |
| 285 | natural_width guest_gdtr_base; |
| 286 | natural_width guest_idtr_base; |
| 287 | natural_width guest_dr7; |
| 288 | natural_width guest_rsp; |
| 289 | natural_width guest_rip; |
| 290 | natural_width guest_rflags; |
| 291 | natural_width guest_pending_dbg_exceptions; |
| 292 | natural_width guest_sysenter_esp; |
| 293 | natural_width guest_sysenter_eip; |
| 294 | natural_width host_cr0; |
| 295 | natural_width host_cr3; |
| 296 | natural_width host_cr4; |
| 297 | natural_width host_fs_base; |
| 298 | natural_width host_gs_base; |
| 299 | natural_width host_tr_base; |
| 300 | natural_width host_gdtr_base; |
| 301 | natural_width host_idtr_base; |
| 302 | natural_width host_ia32_sysenter_esp; |
| 303 | natural_width host_ia32_sysenter_eip; |
| 304 | natural_width host_rsp; |
| 305 | natural_width host_rip; |
| 306 | natural_width paddingl[8]; /* room for future expansion */ |
| 307 | u32 pin_based_vm_exec_control; |
| 308 | u32 cpu_based_vm_exec_control; |
| 309 | u32 exception_bitmap; |
| 310 | u32 page_fault_error_code_mask; |
| 311 | u32 page_fault_error_code_match; |
| 312 | u32 cr3_target_count; |
| 313 | u32 vm_exit_controls; |
| 314 | u32 vm_exit_msr_store_count; |
| 315 | u32 vm_exit_msr_load_count; |
| 316 | u32 vm_entry_controls; |
| 317 | u32 vm_entry_msr_load_count; |
| 318 | u32 vm_entry_intr_info_field; |
| 319 | u32 vm_entry_exception_error_code; |
| 320 | u32 vm_entry_instruction_len; |
| 321 | u32 tpr_threshold; |
| 322 | u32 secondary_vm_exec_control; |
| 323 | u32 vm_instruction_error; |
| 324 | u32 vm_exit_reason; |
| 325 | u32 vm_exit_intr_info; |
| 326 | u32 vm_exit_intr_error_code; |
| 327 | u32 idt_vectoring_info_field; |
| 328 | u32 idt_vectoring_error_code; |
| 329 | u32 vm_exit_instruction_len; |
| 330 | u32 vmx_instruction_info; |
| 331 | u32 guest_es_limit; |
| 332 | u32 guest_cs_limit; |
| 333 | u32 guest_ss_limit; |
| 334 | u32 guest_ds_limit; |
| 335 | u32 guest_fs_limit; |
| 336 | u32 guest_gs_limit; |
| 337 | u32 guest_ldtr_limit; |
| 338 | u32 guest_tr_limit; |
| 339 | u32 guest_gdtr_limit; |
| 340 | u32 guest_idtr_limit; |
| 341 | u32 guest_es_ar_bytes; |
| 342 | u32 guest_cs_ar_bytes; |
| 343 | u32 guest_ss_ar_bytes; |
| 344 | u32 guest_ds_ar_bytes; |
| 345 | u32 guest_fs_ar_bytes; |
| 346 | u32 guest_gs_ar_bytes; |
| 347 | u32 guest_ldtr_ar_bytes; |
| 348 | u32 guest_tr_ar_bytes; |
| 349 | u32 guest_interruptibility_info; |
| 350 | u32 guest_activity_state; |
| 351 | u32 guest_sysenter_cs; |
| 352 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 353 | u32 vmx_preemption_timer_value; |
| 354 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 355 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 356 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 357 | u16 guest_es_selector; |
| 358 | u16 guest_cs_selector; |
| 359 | u16 guest_ss_selector; |
| 360 | u16 guest_ds_selector; |
| 361 | u16 guest_fs_selector; |
| 362 | u16 guest_gs_selector; |
| 363 | u16 guest_ldtr_selector; |
| 364 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 365 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 366 | u16 host_es_selector; |
| 367 | u16 host_cs_selector; |
| 368 | u16 host_ss_selector; |
| 369 | u16 host_ds_selector; |
| 370 | u16 host_fs_selector; |
| 371 | u16 host_gs_selector; |
| 372 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | /* |
| 376 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 377 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 378 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 379 | */ |
| 380 | #define VMCS12_REVISION 0x11e57ed0 |
| 381 | |
| 382 | /* |
| 383 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 384 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 385 | * current implementation, 4K are reserved to avoid future complications. |
| 386 | */ |
| 387 | #define VMCS12_SIZE 0x1000 |
| 388 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 389 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
| 390 | struct vmcs02_list { |
| 391 | struct list_head list; |
| 392 | gpa_t vmptr; |
| 393 | struct loaded_vmcs vmcs02; |
| 394 | }; |
| 395 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 396 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 397 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 398 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 399 | */ |
| 400 | struct nested_vmx { |
| 401 | /* Has the level1 guest done vmxon? */ |
| 402 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 403 | gpa_t vmxon_ptr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 404 | |
| 405 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 406 | gpa_t current_vmptr; |
| 407 | /* The host-usable pointer to the above */ |
| 408 | struct page *current_vmcs12_page; |
| 409 | struct vmcs12 *current_vmcs12; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 410 | /* |
| 411 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 412 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
| 413 | * memory during VMXOFF, VMCLEAR, VMPTRLD. |
| 414 | */ |
| 415 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 416 | /* |
| 417 | * Indicates if the shadow vmcs must be updated with the |
| 418 | * data hold by vmcs12 |
| 419 | */ |
| 420 | bool sync_shadow_vmcs; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 421 | |
| 422 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ |
| 423 | struct list_head vmcs02_pool; |
| 424 | int vmcs02_num; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 425 | bool change_vmcs01_virtual_x2apic_mode; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 426 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 427 | bool nested_run_pending; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 428 | /* |
| 429 | * Guest pages referred to in vmcs02 with host-physical pointers, so |
| 430 | * we must keep them pinned while L2 runs. |
| 431 | */ |
| 432 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 433 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 434 | struct page *pi_desc_page; |
| 435 | struct pi_desc *pi_desc; |
| 436 | bool pi_pending; |
| 437 | u16 posted_intr_nv; |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 438 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 439 | unsigned long *msr_bitmap; |
| 440 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 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; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 541 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 542 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 543 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 544 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 545 | int nmsrs; |
| 546 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 547 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 548 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 549 | u64 msr_host_kernel_gs_base; |
| 550 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 551 | #endif |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 552 | u32 vm_entry_controls_shadow; |
| 553 | u32 vm_exit_controls_shadow; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 554 | /* |
| 555 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 556 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 557 | * guest (L2), it points to a different VMCS. |
| 558 | */ |
| 559 | struct loaded_vmcs vmcs01; |
| 560 | struct loaded_vmcs *loaded_vmcs; |
| 561 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 562 | struct msr_autoload { |
| 563 | unsigned nr; |
| 564 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 565 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 566 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 567 | struct { |
| 568 | int loaded; |
| 569 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 570 | #ifdef CONFIG_X86_64 |
| 571 | u16 ds_sel, es_sel; |
| 572 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 573 | int gs_ldt_reload_needed; |
| 574 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 575 | u64 msr_host_bndcfgs; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 576 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 577 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 578 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 579 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 580 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 581 | struct kvm_segment segs[8]; |
| 582 | } rmode; |
| 583 | struct { |
| 584 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 585 | struct kvm_save_segment { |
| 586 | u16 selector; |
| 587 | unsigned long base; |
| 588 | u32 limit; |
| 589 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 590 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 591 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 592 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 593 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 594 | |
| 595 | /* Support for vnmi-less CPUs */ |
| 596 | int soft_vnmi_blocked; |
| 597 | ktime_t entry_time; |
| 598 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 599 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 600 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 601 | /* Posted interrupt descriptor */ |
| 602 | struct pi_desc pi_desc; |
| 603 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 604 | /* Support for a guest hypervisor (nested VMX) */ |
| 605 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 606 | |
| 607 | /* Dynamic PLE window. */ |
| 608 | int ple_window; |
| 609 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 610 | |
| 611 | /* Support for PML */ |
| 612 | #define PML_ENTITY_NUM 512 |
| 613 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 614 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 615 | /* apic deadline value in host tsc */ |
| 616 | u64 hv_deadline_tsc; |
| 617 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 618 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 619 | |
| 620 | bool guest_pkru_valid; |
| 621 | u32 guest_pkru; |
| 622 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 623 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 624 | /* |
| 625 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 626 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 627 | * in msr_ia32_feature_control_valid_bits. |
| 628 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 629 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 630 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 631 | }; |
| 632 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 633 | enum segment_cache_field { |
| 634 | SEG_FIELD_SEL = 0, |
| 635 | SEG_FIELD_BASE = 1, |
| 636 | SEG_FIELD_LIMIT = 2, |
| 637 | SEG_FIELD_AR = 3, |
| 638 | |
| 639 | SEG_FIELD_NR = 4 |
| 640 | }; |
| 641 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 642 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 643 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 644 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 645 | } |
| 646 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 647 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 648 | { |
| 649 | return &(to_vmx(vcpu)->pi_desc); |
| 650 | } |
| 651 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 652 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 653 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 654 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 655 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 656 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 657 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 658 | static unsigned long shadow_read_only_fields[] = { |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 659 | /* |
| 660 | * We do NOT shadow fields that are modified when L0 |
| 661 | * traps and emulates any vmx instruction (e.g. VMPTRLD, |
| 662 | * VMXON...) executed by L1. |
| 663 | * For example, VM_INSTRUCTION_ERROR is read |
| 664 | * by L1 if a vmx instruction fails (part of the error path). |
| 665 | * Note the code assumes this logic. If for some reason |
| 666 | * we start shadowing these fields then we need to |
| 667 | * force a shadow sync when L0 emulates vmx instructions |
| 668 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified |
| 669 | * by nested_vmx_failValid) |
| 670 | */ |
| 671 | VM_EXIT_REASON, |
| 672 | VM_EXIT_INTR_INFO, |
| 673 | VM_EXIT_INSTRUCTION_LEN, |
| 674 | IDT_VECTORING_INFO_FIELD, |
| 675 | IDT_VECTORING_ERROR_CODE, |
| 676 | VM_EXIT_INTR_ERROR_CODE, |
| 677 | EXIT_QUALIFICATION, |
| 678 | GUEST_LINEAR_ADDRESS, |
| 679 | GUEST_PHYSICAL_ADDRESS |
| 680 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 681 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 682 | ARRAY_SIZE(shadow_read_only_fields); |
| 683 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 684 | static unsigned long shadow_read_write_fields[] = { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 685 | TPR_THRESHOLD, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 686 | GUEST_RIP, |
| 687 | GUEST_RSP, |
| 688 | GUEST_CR0, |
| 689 | GUEST_CR3, |
| 690 | GUEST_CR4, |
| 691 | GUEST_INTERRUPTIBILITY_INFO, |
| 692 | GUEST_RFLAGS, |
| 693 | GUEST_CS_SELECTOR, |
| 694 | GUEST_CS_AR_BYTES, |
| 695 | GUEST_CS_LIMIT, |
| 696 | GUEST_CS_BASE, |
| 697 | GUEST_ES_BASE, |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 698 | GUEST_BNDCFGS, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 699 | CR0_GUEST_HOST_MASK, |
| 700 | CR0_READ_SHADOW, |
| 701 | CR4_READ_SHADOW, |
| 702 | TSC_OFFSET, |
| 703 | EXCEPTION_BITMAP, |
| 704 | CPU_BASED_VM_EXEC_CONTROL, |
| 705 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 706 | VM_ENTRY_INTR_INFO_FIELD, |
| 707 | VM_ENTRY_INSTRUCTION_LEN, |
| 708 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 709 | HOST_FS_BASE, |
| 710 | HOST_GS_BASE, |
| 711 | HOST_FS_SELECTOR, |
| 712 | HOST_GS_SELECTOR |
| 713 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 714 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 715 | ARRAY_SIZE(shadow_read_write_fields); |
| 716 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 717 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 718 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 719 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 720 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 721 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 722 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 723 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 724 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 725 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 726 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 727 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 728 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 729 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 730 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 731 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 732 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 733 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 734 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 735 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 736 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 737 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 738 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 739 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 740 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 741 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 742 | FIELD64(TSC_OFFSET, tsc_offset), |
| 743 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 744 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 745 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 746 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 747 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 748 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 749 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 750 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 751 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 752 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 753 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 754 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 755 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 756 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 757 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 758 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 759 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 760 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 761 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 762 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 763 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 764 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 765 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 766 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 767 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 768 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 769 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 770 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 771 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 772 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 773 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 774 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 775 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 776 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 777 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 778 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 779 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 780 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 781 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 782 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 783 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 784 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 785 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 786 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 787 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 788 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 789 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 790 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 791 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 792 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 793 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 794 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 795 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 796 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 797 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 798 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 799 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 800 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 801 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 802 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 803 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 804 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 805 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 806 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 807 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 808 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 809 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 810 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 811 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 812 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 813 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 814 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 815 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 816 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 817 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 818 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 819 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 820 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 821 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 822 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 823 | FIELD(GUEST_CR0, guest_cr0), |
| 824 | FIELD(GUEST_CR3, guest_cr3), |
| 825 | FIELD(GUEST_CR4, guest_cr4), |
| 826 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 827 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 828 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 829 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 830 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 831 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 832 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 833 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 834 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 835 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 836 | FIELD(GUEST_DR7, guest_dr7), |
| 837 | FIELD(GUEST_RSP, guest_rsp), |
| 838 | FIELD(GUEST_RIP, guest_rip), |
| 839 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 840 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 841 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 842 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 843 | FIELD(HOST_CR0, host_cr0), |
| 844 | FIELD(HOST_CR3, host_cr3), |
| 845 | FIELD(HOST_CR4, host_cr4), |
| 846 | FIELD(HOST_FS_BASE, host_fs_base), |
| 847 | FIELD(HOST_GS_BASE, host_gs_base), |
| 848 | FIELD(HOST_TR_BASE, host_tr_base), |
| 849 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 850 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 851 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 852 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 853 | FIELD(HOST_RSP, host_rsp), |
| 854 | FIELD(HOST_RIP, host_rip), |
| 855 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 856 | |
| 857 | static inline short vmcs_field_to_offset(unsigned long field) |
| 858 | { |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 859 | BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX); |
| 860 | |
Andrew Honig | 012df71 | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 861 | if (field >= ARRAY_SIZE(vmcs_field_to_offset_table)) |
| 862 | return -ENOENT; |
| 863 | |
| 864 | /* |
| 865 | * FIXME: Mitigation for CVE-2017-5753. To be replaced with a |
| 866 | * generic mechanism. |
| 867 | */ |
| 868 | asm("lfence"); |
| 869 | |
| 870 | if (vmcs_field_to_offset_table[field] == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 871 | return -ENOENT; |
| 872 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 873 | return vmcs_field_to_offset_table[field]; |
| 874 | } |
| 875 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 876 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 877 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 878 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 882 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 883 | struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT); |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 884 | if (is_error_page(page)) |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 885 | return NULL; |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 886 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 887 | return page; |
| 888 | } |
| 889 | |
| 890 | static void nested_release_page(struct page *page) |
| 891 | { |
| 892 | kvm_release_page_dirty(page); |
| 893 | } |
| 894 | |
| 895 | static void nested_release_page_clean(struct page *page) |
| 896 | { |
| 897 | kvm_release_page_clean(page); |
| 898 | } |
| 899 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 900 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 901 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 902 | static void kvm_cpu_vmxon(u64 addr); |
| 903 | static void kvm_cpu_vmxoff(void); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 904 | static bool vmx_xsaves_supported(void); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 905 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 906 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 907 | struct kvm_segment *var, int seg); |
| 908 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 909 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 910 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 911 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 912 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 913 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 914 | static int alloc_identity_pagetable(struct kvm *kvm); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 915 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 916 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 917 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 918 | /* |
| 919 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 920 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 921 | */ |
| 922 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 923 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 924 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 925 | /* |
| 926 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 927 | * can find which vCPU should be waken up. |
| 928 | */ |
| 929 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 930 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 931 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 932 | static unsigned long *vmx_io_bitmap_a; |
| 933 | static unsigned long *vmx_io_bitmap_b; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 934 | static unsigned long *vmx_msr_bitmap_legacy; |
| 935 | static unsigned long *vmx_msr_bitmap_longmode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 936 | static unsigned long *vmx_msr_bitmap_legacy_x2apic; |
| 937 | static unsigned long *vmx_msr_bitmap_longmode_x2apic; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 938 | static unsigned long *vmx_msr_bitmap_legacy_x2apic_apicv_inactive; |
| 939 | static unsigned long *vmx_msr_bitmap_longmode_x2apic_apicv_inactive; |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 940 | static unsigned long *vmx_vmread_bitmap; |
| 941 | static unsigned long *vmx_vmwrite_bitmap; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 942 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 943 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 944 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 945 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 946 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 947 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 948 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 949 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 950 | int size; |
| 951 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 952 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 953 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 954 | u32 pin_based_exec_ctrl; |
| 955 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 956 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 957 | u32 vmexit_ctrl; |
| 958 | u32 vmentry_ctrl; |
| 959 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 960 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 961 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 962 | u32 ept; |
| 963 | u32 vpid; |
| 964 | } vmx_capability; |
| 965 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 966 | #define VMX_SEGMENT_FIELD(seg) \ |
| 967 | [VCPU_SREG_##seg] = { \ |
| 968 | .selector = GUEST_##seg##_SELECTOR, \ |
| 969 | .base = GUEST_##seg##_BASE, \ |
| 970 | .limit = GUEST_##seg##_LIMIT, \ |
| 971 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 972 | } |
| 973 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 974 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 975 | unsigned selector; |
| 976 | unsigned base; |
| 977 | unsigned limit; |
| 978 | unsigned ar_bytes; |
| 979 | } kvm_vmx_segment_fields[] = { |
| 980 | VMX_SEGMENT_FIELD(CS), |
| 981 | VMX_SEGMENT_FIELD(DS), |
| 982 | VMX_SEGMENT_FIELD(ES), |
| 983 | VMX_SEGMENT_FIELD(FS), |
| 984 | VMX_SEGMENT_FIELD(GS), |
| 985 | VMX_SEGMENT_FIELD(SS), |
| 986 | VMX_SEGMENT_FIELD(TR), |
| 987 | VMX_SEGMENT_FIELD(LDTR), |
| 988 | }; |
| 989 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 990 | static u64 host_efer; |
| 991 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 992 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 993 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 994 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 995 | * 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] | 996 | * away by decrementing the array size. |
| 997 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 998 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 999 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1000 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1001 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1002 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1003 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1004 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1005 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1006 | { |
| 1007 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1008 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1009 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1010 | } |
| 1011 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1012 | static inline bool is_debug(u32 intr_info) |
| 1013 | { |
| 1014 | return is_exception_n(intr_info, DB_VECTOR); |
| 1015 | } |
| 1016 | |
| 1017 | static inline bool is_breakpoint(u32 intr_info) |
| 1018 | { |
| 1019 | return is_exception_n(intr_info, BP_VECTOR); |
| 1020 | } |
| 1021 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1022 | static inline bool is_page_fault(u32 intr_info) |
| 1023 | { |
| 1024 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1027 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1028 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1029 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1030 | } |
| 1031 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1032 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1033 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1034 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1035 | } |
| 1036 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1037 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1038 | { |
| 1039 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1040 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1041 | } |
| 1042 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1043 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1044 | { |
| 1045 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1046 | INTR_INFO_VALID_MASK)) == |
| 1047 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1048 | } |
| 1049 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1050 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1051 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1052 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1055 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1056 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1057 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1058 | } |
| 1059 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1060 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1061 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1062 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1063 | } |
| 1064 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1065 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1066 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1067 | return vmcs_config.cpu_based_exec_ctrl & |
| 1068 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1071 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1072 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1073 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1074 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1075 | } |
| 1076 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1077 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1078 | { |
| 1079 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1080 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1081 | } |
| 1082 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1083 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1084 | { |
| 1085 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1086 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1087 | } |
| 1088 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1089 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1090 | { |
| 1091 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1092 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1093 | } |
| 1094 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1095 | /* |
| 1096 | * Comment's format: document - errata name - stepping - processor name. |
| 1097 | * Refer from |
| 1098 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1099 | */ |
| 1100 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1101 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1102 | 0x000206E6, |
| 1103 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1104 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1105 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1106 | 0x00020652, |
| 1107 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1108 | 0x00020655, |
| 1109 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1110 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1111 | /* |
| 1112 | * 320767.pdf - AAP86 - B1 - |
| 1113 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1114 | */ |
| 1115 | 0x000106E5, |
| 1116 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1117 | 0x000106A0, |
| 1118 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1119 | 0x000106A1, |
| 1120 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1121 | 0x000106A4, |
| 1122 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1123 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1124 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1125 | 0x000106A5, |
| 1126 | }; |
| 1127 | |
| 1128 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1129 | { |
| 1130 | u32 eax = cpuid_eax(0x00000001), i; |
| 1131 | |
| 1132 | /* Clear the reserved bits */ |
| 1133 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1134 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1135 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1136 | return true; |
| 1137 | |
| 1138 | return false; |
| 1139 | } |
| 1140 | |
| 1141 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1142 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1143 | return vmcs_config.pin_based_exec_ctrl & |
| 1144 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1145 | } |
| 1146 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1147 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1148 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1149 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1150 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static inline bool cpu_has_vmx_apicv(void) |
| 1154 | { |
| 1155 | return cpu_has_vmx_apic_register_virt() && |
| 1156 | cpu_has_vmx_virtual_intr_delivery() && |
| 1157 | cpu_has_vmx_posted_intr(); |
| 1158 | } |
| 1159 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1160 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1161 | { |
| 1162 | return cpu_has_vmx_tpr_shadow() && |
| 1163 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1166 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1167 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1168 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1169 | } |
| 1170 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1171 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1172 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1173 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1174 | } |
| 1175 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1176 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1177 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1178 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1181 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1182 | { |
| 1183 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1184 | } |
| 1185 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1186 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1187 | { |
| 1188 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1189 | } |
| 1190 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1191 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1192 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1193 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1196 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1197 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1198 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1201 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1202 | { |
| 1203 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1204 | } |
| 1205 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1206 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1207 | { |
| 1208 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1209 | } |
| 1210 | |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1211 | static inline bool cpu_has_vmx_invvpid(void) |
| 1212 | { |
| 1213 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1214 | } |
| 1215 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1216 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1217 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1218 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1219 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1220 | } |
| 1221 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1222 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1223 | { |
| 1224 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1225 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1226 | } |
| 1227 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1228 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1229 | { |
| 1230 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1231 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1232 | } |
| 1233 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1234 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1235 | { |
| 1236 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1237 | } |
| 1238 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1239 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1240 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1241 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1244 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1245 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1246 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1247 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1250 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1251 | { |
| 1252 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1253 | SECONDARY_EXEC_RDTSCP; |
| 1254 | } |
| 1255 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1256 | static inline bool cpu_has_vmx_invpcid(void) |
| 1257 | { |
| 1258 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1259 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1260 | } |
| 1261 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1262 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 1263 | { |
| 1264 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1265 | } |
| 1266 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1267 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1268 | { |
| 1269 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1270 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1271 | } |
| 1272 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1273 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1274 | { |
| 1275 | u64 vmx_msr; |
| 1276 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1277 | /* check if the cpu supports writing r/o exit information fields */ |
| 1278 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1279 | return false; |
| 1280 | |
| 1281 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1282 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1283 | } |
| 1284 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1285 | static inline bool cpu_has_vmx_pml(void) |
| 1286 | { |
| 1287 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1288 | } |
| 1289 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1290 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1291 | { |
| 1292 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1293 | SECONDARY_EXEC_TSC_SCALING; |
| 1294 | } |
| 1295 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1296 | static inline bool report_flexpriority(void) |
| 1297 | { |
| 1298 | return flexpriority_enabled; |
| 1299 | } |
| 1300 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1301 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1302 | { |
| 1303 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1304 | } |
| 1305 | |
| 1306 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1307 | { |
| 1308 | return (vmcs12->cpu_based_vm_exec_control & |
| 1309 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1310 | (vmcs12->secondary_vm_exec_control & bit); |
| 1311 | } |
| 1312 | |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 1313 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1314 | { |
| 1315 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1316 | } |
| 1317 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1318 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1319 | { |
| 1320 | return vmcs12->pin_based_vm_exec_control & |
| 1321 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1322 | } |
| 1323 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1324 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1325 | { |
| 1326 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1327 | } |
| 1328 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1329 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1330 | { |
| 1331 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) && |
| 1332 | vmx_xsaves_supported(); |
| 1333 | } |
| 1334 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1335 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1336 | { |
| 1337 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1338 | } |
| 1339 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1340 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1341 | { |
| 1342 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1343 | } |
| 1344 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1345 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1346 | { |
| 1347 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1348 | } |
| 1349 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1350 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1351 | { |
| 1352 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1353 | } |
| 1354 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1355 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1356 | { |
| 1357 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1358 | } |
| 1359 | |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1360 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1361 | { |
| 1362 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1363 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1364 | } |
| 1365 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1366 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1367 | u32 exit_intr_info, |
| 1368 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1369 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1370 | struct vmcs12 *vmcs12, |
| 1371 | u32 reason, unsigned long qualification); |
| 1372 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1373 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1374 | { |
| 1375 | int i; |
| 1376 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1377 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1378 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1379 | return i; |
| 1380 | return -1; |
| 1381 | } |
| 1382 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1383 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1384 | { |
| 1385 | struct { |
| 1386 | u64 vpid : 16; |
| 1387 | u64 rsvd : 48; |
| 1388 | u64 gva; |
| 1389 | } operand = { vpid, 0, gva }; |
| 1390 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1391 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1392 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1393 | "; ja 1f ; ud2 ; 1:" |
| 1394 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1395 | } |
| 1396 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1397 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1398 | { |
| 1399 | struct { |
| 1400 | u64 eptp, gpa; |
| 1401 | } operand = {eptp, gpa}; |
| 1402 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1403 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1404 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1405 | "; ja 1f ; ud2 ; 1:\n" |
| 1406 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1407 | } |
| 1408 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1409 | 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] | 1410 | { |
| 1411 | int i; |
| 1412 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1413 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1414 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1415 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1416 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1419 | static void vmcs_clear(struct vmcs *vmcs) |
| 1420 | { |
| 1421 | u64 phys_addr = __pa(vmcs); |
| 1422 | u8 error; |
| 1423 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1424 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1425 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1426 | : "cc", "memory"); |
| 1427 | if (error) |
| 1428 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1429 | vmcs, phys_addr); |
| 1430 | } |
| 1431 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1432 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1433 | { |
| 1434 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1435 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1436 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1437 | loaded_vmcs->cpu = -1; |
| 1438 | loaded_vmcs->launched = 0; |
| 1439 | } |
| 1440 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1441 | static void vmcs_load(struct vmcs *vmcs) |
| 1442 | { |
| 1443 | u64 phys_addr = __pa(vmcs); |
| 1444 | u8 error; |
| 1445 | |
| 1446 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1447 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1448 | : "cc", "memory"); |
| 1449 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1450 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1451 | vmcs, phys_addr); |
| 1452 | } |
| 1453 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1454 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1455 | /* |
| 1456 | * This bitmap is used to indicate whether the vmclear |
| 1457 | * operation is enabled on all cpus. All disabled by |
| 1458 | * default. |
| 1459 | */ |
| 1460 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1461 | |
| 1462 | static inline void crash_enable_local_vmclear(int cpu) |
| 1463 | { |
| 1464 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1465 | } |
| 1466 | |
| 1467 | static inline void crash_disable_local_vmclear(int cpu) |
| 1468 | { |
| 1469 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1470 | } |
| 1471 | |
| 1472 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1473 | { |
| 1474 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1475 | } |
| 1476 | |
| 1477 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1478 | { |
| 1479 | int cpu = raw_smp_processor_id(); |
| 1480 | struct loaded_vmcs *v; |
| 1481 | |
| 1482 | if (!crash_local_vmclear_enabled(cpu)) |
| 1483 | return; |
| 1484 | |
| 1485 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1486 | loaded_vmcss_on_cpu_link) |
| 1487 | vmcs_clear(v->vmcs); |
| 1488 | } |
| 1489 | #else |
| 1490 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1491 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1492 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1493 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1494 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1495 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1496 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1497 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1498 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1499 | if (loaded_vmcs->cpu != cpu) |
| 1500 | return; /* vcpu migration can race with cpu offline */ |
| 1501 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1502 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1503 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1504 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1505 | |
| 1506 | /* |
| 1507 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1508 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1509 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1510 | * then adds the vmcs into percpu list before it is deleted. |
| 1511 | */ |
| 1512 | smp_wmb(); |
| 1513 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1514 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1515 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1518 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1519 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1520 | int cpu = loaded_vmcs->cpu; |
| 1521 | |
| 1522 | if (cpu != -1) |
| 1523 | smp_call_function_single(cpu, |
| 1524 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1527 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1528 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1529 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1530 | return; |
| 1531 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1532 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1533 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1536 | static inline void vpid_sync_vcpu_global(void) |
| 1537 | { |
| 1538 | if (cpu_has_vmx_invvpid_global()) |
| 1539 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1540 | } |
| 1541 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1542 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1543 | { |
| 1544 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1545 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1546 | else |
| 1547 | vpid_sync_vcpu_global(); |
| 1548 | } |
| 1549 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1550 | static inline void ept_sync_global(void) |
| 1551 | { |
| 1552 | if (cpu_has_vmx_invept_global()) |
| 1553 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1554 | } |
| 1555 | |
| 1556 | static inline void ept_sync_context(u64 eptp) |
| 1557 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1558 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1559 | if (cpu_has_vmx_invept_context()) |
| 1560 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1561 | else |
| 1562 | ept_sync_global(); |
| 1563 | } |
| 1564 | } |
| 1565 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1566 | static __always_inline void vmcs_check16(unsigned long field) |
| 1567 | { |
| 1568 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1569 | "16-bit accessor invalid for 64-bit field"); |
| 1570 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1571 | "16-bit accessor invalid for 64-bit high field"); |
| 1572 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1573 | "16-bit accessor invalid for 32-bit high field"); |
| 1574 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1575 | "16-bit accessor invalid for natural width field"); |
| 1576 | } |
| 1577 | |
| 1578 | static __always_inline void vmcs_check32(unsigned long field) |
| 1579 | { |
| 1580 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1581 | "32-bit accessor invalid for 16-bit field"); |
| 1582 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1583 | "32-bit accessor invalid for natural width field"); |
| 1584 | } |
| 1585 | |
| 1586 | static __always_inline void vmcs_check64(unsigned long field) |
| 1587 | { |
| 1588 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1589 | "64-bit accessor invalid for 16-bit field"); |
| 1590 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1591 | "64-bit accessor invalid for 64-bit high field"); |
| 1592 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1593 | "64-bit accessor invalid for 32-bit field"); |
| 1594 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1595 | "64-bit accessor invalid for natural width field"); |
| 1596 | } |
| 1597 | |
| 1598 | static __always_inline void vmcs_checkl(unsigned long field) |
| 1599 | { |
| 1600 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1601 | "Natural width accessor invalid for 16-bit field"); |
| 1602 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1603 | "Natural width accessor invalid for 64-bit field"); |
| 1604 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1605 | "Natural width accessor invalid for 64-bit high field"); |
| 1606 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1607 | "Natural width accessor invalid for 32-bit field"); |
| 1608 | } |
| 1609 | |
| 1610 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1611 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1612 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1613 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1614 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1615 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1616 | return value; |
| 1617 | } |
| 1618 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1619 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1620 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1621 | vmcs_check16(field); |
| 1622 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1625 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1626 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1627 | vmcs_check32(field); |
| 1628 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1631 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1632 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1633 | vmcs_check64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1634 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1635 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1636 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1637 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1638 | #endif |
| 1639 | } |
| 1640 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1641 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 1642 | { |
| 1643 | vmcs_checkl(field); |
| 1644 | return __vmcs_readl(field); |
| 1645 | } |
| 1646 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1647 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1648 | { |
| 1649 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1650 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1651 | dump_stack(); |
| 1652 | } |
| 1653 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1654 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1655 | { |
| 1656 | u8 error; |
| 1657 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1658 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1659 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1660 | if (unlikely(error)) |
| 1661 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1662 | } |
| 1663 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1664 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1665 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1666 | vmcs_check16(field); |
| 1667 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1668 | } |
| 1669 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1670 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1671 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1672 | vmcs_check32(field); |
| 1673 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1676 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1677 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1678 | vmcs_check64(field); |
| 1679 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1680 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1681 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1682 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1683 | #endif |
| 1684 | } |
| 1685 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1686 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1687 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1688 | vmcs_checkl(field); |
| 1689 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1690 | } |
| 1691 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1692 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1693 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1694 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1695 | "vmcs_clear_bits does not support 64-bit fields"); |
| 1696 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 1697 | } |
| 1698 | |
| 1699 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 1700 | { |
| 1701 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1702 | "vmcs_set_bits does not support 64-bit fields"); |
| 1703 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1704 | } |
| 1705 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1706 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1707 | { |
| 1708 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 1709 | } |
| 1710 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1711 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1712 | { |
| 1713 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 1714 | vmx->vm_entry_controls_shadow = val; |
| 1715 | } |
| 1716 | |
| 1717 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1718 | { |
| 1719 | if (vmx->vm_entry_controls_shadow != val) |
| 1720 | vm_entry_controls_init(vmx, val); |
| 1721 | } |
| 1722 | |
| 1723 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 1724 | { |
| 1725 | return vmx->vm_entry_controls_shadow; |
| 1726 | } |
| 1727 | |
| 1728 | |
| 1729 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1730 | { |
| 1731 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 1732 | } |
| 1733 | |
| 1734 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1735 | { |
| 1736 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 1737 | } |
| 1738 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1739 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1740 | { |
| 1741 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 1742 | } |
| 1743 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1744 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1745 | { |
| 1746 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 1747 | vmx->vm_exit_controls_shadow = val; |
| 1748 | } |
| 1749 | |
| 1750 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1751 | { |
| 1752 | if (vmx->vm_exit_controls_shadow != val) |
| 1753 | vm_exit_controls_init(vmx, val); |
| 1754 | } |
| 1755 | |
| 1756 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 1757 | { |
| 1758 | return vmx->vm_exit_controls_shadow; |
| 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1763 | { |
| 1764 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 1765 | } |
| 1766 | |
| 1767 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1768 | { |
| 1769 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 1770 | } |
| 1771 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1772 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1773 | { |
| 1774 | vmx->segment_cache.bitmask = 0; |
| 1775 | } |
| 1776 | |
| 1777 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1778 | unsigned field) |
| 1779 | { |
| 1780 | bool ret; |
| 1781 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1782 | |
| 1783 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1784 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1785 | vmx->segment_cache.bitmask = 0; |
| 1786 | } |
| 1787 | ret = vmx->segment_cache.bitmask & mask; |
| 1788 | vmx->segment_cache.bitmask |= mask; |
| 1789 | return ret; |
| 1790 | } |
| 1791 | |
| 1792 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1793 | { |
| 1794 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1795 | |
| 1796 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1797 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1798 | return *p; |
| 1799 | } |
| 1800 | |
| 1801 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1802 | { |
| 1803 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1804 | |
| 1805 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1806 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1807 | return *p; |
| 1808 | } |
| 1809 | |
| 1810 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1811 | { |
| 1812 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1813 | |
| 1814 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1815 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1816 | return *p; |
| 1817 | } |
| 1818 | |
| 1819 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1820 | { |
| 1821 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1822 | |
| 1823 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1824 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1825 | return *p; |
| 1826 | } |
| 1827 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1828 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1829 | { |
| 1830 | u32 eb; |
| 1831 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1832 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1833 | (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1834 | if ((vcpu->guest_debug & |
| 1835 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1836 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1837 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1838 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1839 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1840 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1841 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1842 | if (vcpu->fpu_active) |
| 1843 | eb &= ~(1u << NM_VECTOR); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1844 | |
| 1845 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1846 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1847 | * them to L1. When running L2, we will only handle the exceptions |
| 1848 | * specified above if L1 did not want them. |
| 1849 | */ |
| 1850 | if (is_guest_mode(vcpu)) |
| 1851 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1852 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1853 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1854 | } |
| 1855 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1856 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1857 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1858 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1859 | vm_entry_controls_clearbit(vmx, entry); |
| 1860 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1861 | } |
| 1862 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1863 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1864 | { |
| 1865 | unsigned i; |
| 1866 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1867 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1868 | switch (msr) { |
| 1869 | case MSR_EFER: |
| 1870 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1871 | clear_atomic_switch_msr_special(vmx, |
| 1872 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1873 | VM_EXIT_LOAD_IA32_EFER); |
| 1874 | return; |
| 1875 | } |
| 1876 | break; |
| 1877 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1878 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1879 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1880 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1881 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 1882 | return; |
| 1883 | } |
| 1884 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1885 | } |
| 1886 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1887 | for (i = 0; i < m->nr; ++i) |
| 1888 | if (m->guest[i].index == msr) |
| 1889 | break; |
| 1890 | |
| 1891 | if (i == m->nr) |
| 1892 | return; |
| 1893 | --m->nr; |
| 1894 | m->guest[i] = m->guest[m->nr]; |
| 1895 | m->host[i] = m->host[m->nr]; |
| 1896 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1897 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1898 | } |
| 1899 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1900 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1901 | unsigned long entry, unsigned long exit, |
| 1902 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 1903 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1904 | { |
| 1905 | vmcs_write64(guest_val_vmcs, guest_val); |
| 1906 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1907 | vm_entry_controls_setbit(vmx, entry); |
| 1908 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1909 | } |
| 1910 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1911 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1912 | u64 guest_val, u64 host_val) |
| 1913 | { |
| 1914 | unsigned i; |
| 1915 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1916 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1917 | switch (msr) { |
| 1918 | case MSR_EFER: |
| 1919 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1920 | add_atomic_switch_msr_special(vmx, |
| 1921 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1922 | VM_EXIT_LOAD_IA32_EFER, |
| 1923 | GUEST_IA32_EFER, |
| 1924 | HOST_IA32_EFER, |
| 1925 | guest_val, host_val); |
| 1926 | return; |
| 1927 | } |
| 1928 | break; |
| 1929 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1930 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1931 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1932 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1933 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1934 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 1935 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 1936 | guest_val, host_val); |
| 1937 | return; |
| 1938 | } |
| 1939 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 1940 | case MSR_IA32_PEBS_ENABLE: |
| 1941 | /* PEBS needs a quiescent period after being disabled (to write |
| 1942 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 1943 | * provide that period, so a CPU could write host's record into |
| 1944 | * guest's memory. |
| 1945 | */ |
| 1946 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1949 | for (i = 0; i < m->nr; ++i) |
| 1950 | if (m->guest[i].index == msr) |
| 1951 | break; |
| 1952 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1953 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 1954 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1955 | "Can't add msr %x\n", msr); |
| 1956 | return; |
| 1957 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1958 | ++m->nr; |
| 1959 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1960 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1961 | } |
| 1962 | |
| 1963 | m->guest[i].index = msr; |
| 1964 | m->guest[i].value = guest_val; |
| 1965 | m->host[i].index = msr; |
| 1966 | m->host[i].value = host_val; |
| 1967 | } |
| 1968 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1969 | static void reload_tss(void) |
| 1970 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1971 | /* |
| 1972 | * VT restores TR but not its size. Useless. |
| 1973 | */ |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1974 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 1975 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1976 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1977 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1978 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 1979 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1980 | } |
| 1981 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1982 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1983 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 1984 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 1985 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1986 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 1987 | if (!enable_ept) { |
| 1988 | /* |
| 1989 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 1990 | * host CPUID is more efficient than testing guest CPUID |
| 1991 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 1992 | */ |
| 1993 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 1994 | guest_efer |= EFER_NX; |
| 1995 | else if (!(guest_efer & EFER_NX)) |
| 1996 | ignore_bits |= EFER_NX; |
| 1997 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1998 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1999 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2000 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2001 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2002 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2003 | #ifdef CONFIG_X86_64 |
| 2004 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2005 | /* SCE is meaningful only in long mode on Intel */ |
| 2006 | if (guest_efer & EFER_LMA) |
| 2007 | ignore_bits &= ~(u64)EFER_SCE; |
| 2008 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2009 | |
| 2010 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2011 | |
| 2012 | /* |
| 2013 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2014 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2015 | * atomically, since it's faster than switching it manually. |
| 2016 | */ |
| 2017 | if (cpu_has_load_ia32_efer || |
| 2018 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2019 | if (!(guest_efer & EFER_LMA)) |
| 2020 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2021 | if (guest_efer != host_efer) |
| 2022 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2023 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2024 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2025 | } else { |
| 2026 | guest_efer &= ~ignore_bits; |
| 2027 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2028 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2029 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2030 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2031 | |
| 2032 | return true; |
| 2033 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2034 | } |
| 2035 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2036 | static unsigned long segment_base(u16 selector) |
| 2037 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2038 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2039 | struct desc_struct *d; |
| 2040 | unsigned long table_base; |
| 2041 | unsigned long v; |
| 2042 | |
| 2043 | if (!(selector & ~3)) |
| 2044 | return 0; |
| 2045 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2046 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2047 | |
| 2048 | if (selector & 4) { /* from ldt */ |
| 2049 | u16 ldt_selector = kvm_read_ldt(); |
| 2050 | |
| 2051 | if (!(ldt_selector & ~3)) |
| 2052 | return 0; |
| 2053 | |
| 2054 | table_base = segment_base(ldt_selector); |
| 2055 | } |
| 2056 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 2057 | v = get_desc_base(d); |
| 2058 | #ifdef CONFIG_X86_64 |
| 2059 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 2060 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 2061 | #endif |
| 2062 | return v; |
| 2063 | } |
| 2064 | |
| 2065 | static inline unsigned long kvm_read_tr_base(void) |
| 2066 | { |
| 2067 | u16 tr; |
| 2068 | asm("str %0" : "=g"(tr)); |
| 2069 | return segment_base(tr); |
| 2070 | } |
| 2071 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2072 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2073 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2074 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2075 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2076 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2077 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2078 | return; |
| 2079 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2080 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2081 | /* |
| 2082 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2083 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2084 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2085 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2086 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2087 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2088 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2089 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2090 | vmx->host_state.fs_reload_needed = 0; |
| 2091 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2092 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2093 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2094 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2095 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2096 | if (!(vmx->host_state.gs_sel & 7)) |
| 2097 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2098 | else { |
| 2099 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2100 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2104 | savesegment(ds, vmx->host_state.ds_sel); |
| 2105 | savesegment(es, vmx->host_state.es_sel); |
| 2106 | #endif |
| 2107 | |
| 2108 | #ifdef CONFIG_X86_64 |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2109 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 2110 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 2111 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2112 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2113 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2114 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2115 | |
| 2116 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2117 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 2118 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2119 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2120 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2121 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 2122 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2123 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2124 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2125 | vmx->guest_msrs[i].data, |
| 2126 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2127 | } |
| 2128 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2129 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2130 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2131 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2132 | return; |
| 2133 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2134 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2135 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2136 | #ifdef CONFIG_X86_64 |
| 2137 | if (is_long_mode(&vmx->vcpu)) |
| 2138 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2139 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2140 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2141 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2142 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2143 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2144 | #else |
| 2145 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2146 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2147 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2148 | if (vmx->host_state.fs_reload_needed) |
| 2149 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2150 | #ifdef CONFIG_X86_64 |
| 2151 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2152 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2153 | loadsegment(es, vmx->host_state.es_sel); |
| 2154 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2155 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2156 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2157 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2158 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2159 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2160 | if (vmx->host_state.msr_host_bndcfgs) |
| 2161 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 2162 | /* |
| 2163 | * If the FPU is not active (through the host task or |
| 2164 | * the guest vcpu), then restore the cr0.TS bit. |
| 2165 | */ |
Ingo Molnar | 3c6dffa | 2015-04-28 12:28:08 +0200 | [diff] [blame] | 2166 | if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded) |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 2167 | stts(); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2168 | load_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2169 | } |
| 2170 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2171 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2172 | { |
| 2173 | preempt_disable(); |
| 2174 | __vmx_load_host_state(vmx); |
| 2175 | preempt_enable(); |
| 2176 | } |
| 2177 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2178 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2179 | { |
| 2180 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2181 | struct pi_desc old, new; |
| 2182 | unsigned int dest; |
| 2183 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2184 | /* |
| 2185 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2186 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2187 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2188 | * code easier, and CPU migration is not a fast path. |
| 2189 | */ |
| 2190 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2191 | return; |
| 2192 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2193 | /* |
| 2194 | * First handle the simple case where no cmpxchg is necessary; just |
| 2195 | * allow posting non-urgent interrupts. |
| 2196 | * |
| 2197 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2198 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2199 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2200 | * PI.NDST. |
| 2201 | */ |
| 2202 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2203 | vcpu->cpu == cpu) { |
| 2204 | pi_clear_sn(pi_desc); |
| 2205 | return; |
| 2206 | } |
| 2207 | |
| 2208 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2209 | do { |
| 2210 | old.control = new.control = pi_desc->control; |
| 2211 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2212 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2213 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2214 | if (x2apic_enabled()) |
| 2215 | new.ndst = dest; |
| 2216 | else |
| 2217 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2218 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2219 | new.sn = 0; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2220 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2221 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2222 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2223 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2224 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2225 | { |
| 2226 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2227 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2228 | } |
| 2229 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2230 | /* |
| 2231 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2232 | * vcpu mutex is already taken. |
| 2233 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2234 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2235 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2236 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2237 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2238 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2239 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2240 | if (!vmm_exclusive) |
| 2241 | kvm_cpu_vmxon(phys_addr); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2242 | else if (!already_loaded) |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2243 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2244 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2245 | if (!already_loaded) { |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2246 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2247 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2248 | |
| 2249 | /* |
| 2250 | * Read loaded_vmcs->cpu should be before fetching |
| 2251 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2252 | * See the comments in __loaded_vmcs_clear(). |
| 2253 | */ |
| 2254 | smp_rmb(); |
| 2255 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2256 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2257 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2258 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2259 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2263 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2264 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 2265 | } |
| 2266 | |
| 2267 | if (!already_loaded) { |
| 2268 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
| 2269 | unsigned long sysenter_esp; |
| 2270 | |
| 2271 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2272 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2273 | /* |
| 2274 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 2275 | * processors. |
| 2276 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2277 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2278 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2279 | |
| 2280 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2281 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2282 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2283 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2284 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2285 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2286 | /* Setup TSC multiplier */ |
| 2287 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2288 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2289 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2290 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2291 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2292 | vmx->host_pkru = read_pkru(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2296 | { |
| 2297 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2298 | |
| 2299 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2300 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2301 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2302 | return; |
| 2303 | |
| 2304 | /* Set SN when the vCPU is preempted */ |
| 2305 | if (vcpu->preempted) |
| 2306 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2310 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2311 | vmx_vcpu_pi_put(vcpu); |
| 2312 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2313 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2314 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2315 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 2316 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2317 | kvm_cpu_vmxoff(); |
| 2318 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2319 | } |
| 2320 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2321 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 2322 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2323 | ulong cr0; |
| 2324 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2325 | if (vcpu->fpu_active) |
| 2326 | return; |
| 2327 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2328 | cr0 = vmcs_readl(GUEST_CR0); |
| 2329 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 2330 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 2331 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2332 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2333 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2334 | if (is_guest_mode(vcpu)) |
| 2335 | vcpu->arch.cr0_guest_owned_bits &= |
| 2336 | ~get_vmcs12(vcpu)->cr0_guest_host_mask; |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2337 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2338 | } |
| 2339 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2340 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2341 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2342 | /* |
| 2343 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2344 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2345 | * its hypervisor (cr0_read_shadow). |
| 2346 | */ |
| 2347 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2348 | { |
| 2349 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2350 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2351 | } |
| 2352 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2353 | { |
| 2354 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2355 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2356 | } |
| 2357 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2358 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 2359 | { |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2360 | /* Note that there is no vcpu->fpu_active = 0 here. The caller must |
| 2361 | * set this *before* calling this function. |
| 2362 | */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2363 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2364 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2365 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2366 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 2367 | 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] | 2368 | if (is_guest_mode(vcpu)) { |
| 2369 | /* |
| 2370 | * L1's specified read shadow might not contain the TS bit, |
| 2371 | * so now that we turned on shadowing of this bit, we need to |
| 2372 | * set this bit of the shadow. Like in nested_vmx_run we need |
| 2373 | * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet |
| 2374 | * up-to-date here because we just decached cr0.TS (and we'll |
| 2375 | * only update vmcs12->guest_cr0 on nested exit). |
| 2376 | */ |
| 2377 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2378 | vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) | |
| 2379 | (vcpu->arch.cr0 & X86_CR0_TS); |
| 2380 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2381 | } else |
| 2382 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2383 | } |
| 2384 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2385 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2386 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2387 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2388 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2389 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2390 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2391 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2392 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2393 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2394 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2395 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2396 | } |
| 2397 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2398 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2399 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2403 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2404 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2405 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2406 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2407 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2408 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2409 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2410 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 2411 | } |
| 2412 | |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 2413 | static u32 vmx_get_pkru(struct kvm_vcpu *vcpu) |
| 2414 | { |
| 2415 | return to_vmx(vcpu)->guest_pkru; |
| 2416 | } |
| 2417 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2418 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2419 | { |
| 2420 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2421 | int ret = 0; |
| 2422 | |
| 2423 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2424 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2425 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2426 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2427 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2428 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2432 | { |
| 2433 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2434 | u32 interruptibility = interruptibility_old; |
| 2435 | |
| 2436 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2437 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2438 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2439 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2440 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2441 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2442 | |
| 2443 | if ((interruptibility != interruptibility_old)) |
| 2444 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2445 | } |
| 2446 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2447 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2448 | { |
| 2449 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2450 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2451 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2452 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2453 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2454 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2455 | /* skipping an emulated instruction also counts */ |
| 2456 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2457 | } |
| 2458 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2459 | /* |
| 2460 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2461 | * 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] | 2462 | */ |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2463 | 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] | 2464 | { |
| 2465 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2466 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2467 | if (!(vmcs12->exception_bitmap & (1u << nr))) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2468 | return 0; |
| 2469 | |
Wanpeng Li | a29fd27 | 2017-06-05 05:19:09 -0700 | [diff] [blame] | 2470 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 2471 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 2472 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2473 | return 1; |
| 2474 | } |
| 2475 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2476 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 2477 | bool has_error_code, u32 error_code, |
| 2478 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2479 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2480 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2481 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2482 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2483 | if (!reinject && is_guest_mode(vcpu) && |
| 2484 | nested_vmx_check_exception(vcpu, nr)) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2485 | return; |
| 2486 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2487 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2488 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2489 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2490 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2491 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2492 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 2493 | int inc_eip = 0; |
| 2494 | if (kvm_exception_is_soft(nr)) |
| 2495 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 2496 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2497 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2498 | return; |
| 2499 | } |
| 2500 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2501 | if (kvm_exception_is_soft(nr)) { |
| 2502 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2503 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2504 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2505 | } else |
| 2506 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2507 | |
| 2508 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2509 | } |
| 2510 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2511 | static bool vmx_rdtscp_supported(void) |
| 2512 | { |
| 2513 | return cpu_has_vmx_rdtscp(); |
| 2514 | } |
| 2515 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2516 | static bool vmx_invpcid_supported(void) |
| 2517 | { |
| 2518 | return cpu_has_vmx_invpcid() && enable_ept; |
| 2519 | } |
| 2520 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2521 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2522 | * Swap MSR entry in host/guest MSR entry array. |
| 2523 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2524 | 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] | 2525 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2526 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2527 | |
| 2528 | tmp = vmx->guest_msrs[to]; |
| 2529 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 2530 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2531 | } |
| 2532 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2533 | static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) |
| 2534 | { |
| 2535 | unsigned long *msr_bitmap; |
| 2536 | |
Wincy Van | 670125b | 2015-03-04 14:31:56 +0800 | [diff] [blame] | 2537 | if (is_guest_mode(vcpu)) |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 2538 | msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap; |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 2539 | else if (cpu_has_secondary_exec_ctrls() && |
| 2540 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 2541 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 2542 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) { |
| 2543 | if (is_long_mode(vcpu)) |
| 2544 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic; |
| 2545 | else |
| 2546 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic; |
| 2547 | } else { |
| 2548 | if (is_long_mode(vcpu)) |
| 2549 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv_inactive; |
| 2550 | else |
| 2551 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv_inactive; |
| 2552 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2553 | } else { |
| 2554 | if (is_long_mode(vcpu)) |
| 2555 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 2556 | else |
| 2557 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 2558 | } |
| 2559 | |
| 2560 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 2561 | } |
| 2562 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2563 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2564 | * Set up the vmcs to automatically save and restore system |
| 2565 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 2566 | * mode, as fiddling with msrs is very expensive. |
| 2567 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2568 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2569 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2570 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2571 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2572 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2573 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2574 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2575 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2576 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2577 | move_msr_up(vmx, index, save_nmsrs++); |
| 2578 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2579 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2580 | move_msr_up(vmx, index, save_nmsrs++); |
| 2581 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2582 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2583 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2584 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 2585 | if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2586 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2587 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2588 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2589 | * if efer.sce is enabled. |
| 2590 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2591 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2592 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2593 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2594 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2595 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2596 | index = __find_msr_index(vmx, MSR_EFER); |
| 2597 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2598 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2599 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2600 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2601 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2602 | if (cpu_has_vmx_msr_bitmap()) |
| 2603 | vmx_set_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2607 | * reads and returns guest's timestamp counter "register" |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2608 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
| 2609 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2610 | */ |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2611 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2612 | { |
| 2613 | u64 host_tsc, tsc_offset; |
| 2614 | |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 2615 | host_tsc = rdtsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2616 | tsc_offset = vmcs_read64(TSC_OFFSET); |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2617 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2621 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2622 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2623 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2624 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2625 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2626 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2627 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 2628 | * to the spec, this should set L1's TSC; The offset that L1 |
| 2629 | * set for L2 remains unchanged, and still needs to be added |
| 2630 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2631 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2632 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2633 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 2634 | vmcs12 = get_vmcs12(vcpu); |
| 2635 | vmcs_write64(TSC_OFFSET, offset + |
| 2636 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 2637 | vmcs12->tsc_offset : 0)); |
| 2638 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2639 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 2640 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2641 | vmcs_write64(TSC_OFFSET, offset); |
| 2642 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2643 | } |
| 2644 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 2645 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 2646 | { |
| 2647 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 2648 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 2649 | } |
| 2650 | |
| 2651 | /* |
| 2652 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 2653 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 2654 | * all guests if the "nested" module option is off, and can also be disabled |
| 2655 | * for a single guest by disabling its VMX cpuid bit. |
| 2656 | */ |
| 2657 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 2658 | { |
| 2659 | return nested && guest_cpuid_has_vmx(vcpu); |
| 2660 | } |
| 2661 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2662 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2663 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 2664 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 2665 | * The same values should also be used to verify that vmcs12 control fields are |
| 2666 | * valid during nested entry from L1 to L2. |
| 2667 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 2668 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 2669 | * bit in the high half is on if the corresponding bit in the control field |
| 2670 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2671 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2672 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2673 | { |
| 2674 | /* |
| 2675 | * Note that as a general rule, the high half of the MSRs (bits in |
| 2676 | * the control fields which may be 1) should be initialized by the |
| 2677 | * intersection of the underlying hardware's MSR (i.e., features which |
| 2678 | * can be supported) and the list of features we want to expose - |
| 2679 | * because they are known to be properly supported in our code. |
| 2680 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 2681 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 2682 | * reason is that if one of these bits is necessary, it will appear |
| 2683 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 2684 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 2685 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 2686 | * These rules have exceptions below. |
| 2687 | */ |
| 2688 | |
| 2689 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 2690 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2691 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2692 | vmx->nested.nested_vmx_pinbased_ctls_high); |
| 2693 | vmx->nested.nested_vmx_pinbased_ctls_low |= |
| 2694 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2695 | vmx->nested.nested_vmx_pinbased_ctls_high &= |
| 2696 | PIN_BASED_EXT_INTR_MASK | |
| 2697 | PIN_BASED_NMI_EXITING | |
| 2698 | PIN_BASED_VIRTUAL_NMIS; |
| 2699 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2700 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 2701 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2702 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 2703 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2704 | PIN_BASED_POSTED_INTR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2705 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2706 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2707 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2708 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2709 | vmx->nested.nested_vmx_exit_ctls_high); |
| 2710 | vmx->nested.nested_vmx_exit_ctls_low = |
| 2711 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2712 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2713 | vmx->nested.nested_vmx_exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2714 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2715 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2716 | #endif |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2717 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2718 | vmx->nested.nested_vmx_exit_ctls_high |= |
| 2719 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2720 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2721 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 2722 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2723 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2724 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2725 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2726 | /* We support free control of debug control saving. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2727 | vmx->nested.nested_vmx_true_exit_ctls_low = |
| 2728 | vmx->nested.nested_vmx_exit_ctls_low & |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2729 | ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 2730 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2731 | /* entry controls */ |
| 2732 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2733 | vmx->nested.nested_vmx_entry_ctls_low, |
| 2734 | vmx->nested.nested_vmx_entry_ctls_high); |
| 2735 | vmx->nested.nested_vmx_entry_ctls_low = |
| 2736 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2737 | vmx->nested.nested_vmx_entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2738 | #ifdef CONFIG_X86_64 |
| 2739 | VM_ENTRY_IA32E_MODE | |
| 2740 | #endif |
| 2741 | VM_ENTRY_LOAD_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2742 | vmx->nested.nested_vmx_entry_ctls_high |= |
| 2743 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2744 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2745 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2746 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2747 | /* We support free control of debug control loading. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2748 | vmx->nested.nested_vmx_true_entry_ctls_low = |
| 2749 | vmx->nested.nested_vmx_entry_ctls_low & |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2750 | ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 2751 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2752 | /* cpu-based controls */ |
| 2753 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2754 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2755 | vmx->nested.nested_vmx_procbased_ctls_high); |
| 2756 | vmx->nested.nested_vmx_procbased_ctls_low = |
| 2757 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2758 | vmx->nested.nested_vmx_procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 2759 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 2760 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2761 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 2762 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 2763 | CPU_BASED_CR3_STORE_EXITING | |
| 2764 | #ifdef CONFIG_X86_64 |
| 2765 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 2766 | #endif |
| 2767 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 2768 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 2769 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 2770 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 2771 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2772 | /* |
| 2773 | * We can allow some features even when not supported by the |
| 2774 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 2775 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 2776 | * without MSR bitmaps. |
| 2777 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2778 | vmx->nested.nested_vmx_procbased_ctls_high |= |
| 2779 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 2780 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2781 | |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2782 | /* We support free control of CR3 access interception. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2783 | vmx->nested.nested_vmx_true_procbased_ctls_low = |
| 2784 | vmx->nested.nested_vmx_procbased_ctls_low & |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2785 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 2786 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2787 | /* secondary cpu-based controls */ |
| 2788 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2789 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 2790 | vmx->nested.nested_vmx_secondary_ctls_high); |
| 2791 | vmx->nested.nested_vmx_secondary_ctls_low = 0; |
| 2792 | vmx->nested.nested_vmx_secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 2793 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 2794 | SECONDARY_EXEC_RDTSCP | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 2795 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 2796 | SECONDARY_EXEC_ENABLE_VPID | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 2797 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 2798 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 2799 | SECONDARY_EXEC_WBINVD_EXITING | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 2800 | SECONDARY_EXEC_XSAVES; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2801 | |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2802 | if (enable_ept) { |
| 2803 | /* nested EPT: emulate EPT also to L1 */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2804 | vmx->nested.nested_vmx_secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2805 | SECONDARY_EXEC_ENABLE_EPT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2806 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Jan Kiszka | d3134db | 2013-10-23 14:40:31 +0100 | [diff] [blame] | 2807 | VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 2808 | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 2809 | if (cpu_has_vmx_ept_execute_only()) |
| 2810 | vmx->nested.nested_vmx_ept_caps |= |
| 2811 | VMX_EPT_EXECUTE_ONLY_BIT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2812 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 2813 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 2814 | VMX_EPT_EXTENT_CONTEXT_BIT; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2815 | } else |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2816 | vmx->nested.nested_vmx_ept_caps = 0; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2817 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 2818 | /* |
| 2819 | * Old versions of KVM use the single-context version without |
| 2820 | * checking for support, so declare that it is supported even |
| 2821 | * though it is treated as global context. The alternative is |
| 2822 | * not failing the single-context invvpid, and it is worse. |
| 2823 | */ |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2824 | if (enable_vpid) |
| 2825 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 2826 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2827 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 2828 | else |
| 2829 | vmx->nested.nested_vmx_vpid_caps = 0; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 2830 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2831 | if (enable_unrestricted_guest) |
| 2832 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 2833 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2834 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2835 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2836 | rdmsr(MSR_IA32_VMX_MISC, |
| 2837 | vmx->nested.nested_vmx_misc_low, |
| 2838 | vmx->nested.nested_vmx_misc_high); |
| 2839 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 2840 | vmx->nested.nested_vmx_misc_low |= |
| 2841 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2842 | VMX_MISC_ACTIVITY_HLT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2843 | vmx->nested.nested_vmx_misc_high = 0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 2847 | { |
| 2848 | /* |
| 2849 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 2850 | */ |
| 2851 | return ((control & high) | low) == control; |
| 2852 | } |
| 2853 | |
| 2854 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 2855 | { |
| 2856 | return low | ((u64)high << 32); |
| 2857 | } |
| 2858 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2859 | /* Returns 0 on success, non-0 otherwise. */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2860 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2861 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2862 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2863 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2864 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2865 | case MSR_IA32_VMX_BASIC: |
| 2866 | /* |
| 2867 | * This MSR reports some information about VMX support. We |
| 2868 | * should return information about the VMX we emulate for the |
| 2869 | * guest, and the VMCS structure we give it - not about the |
| 2870 | * VMX support of the underlying hardware. |
| 2871 | */ |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2872 | *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2873 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2874 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 2875 | if (cpu_has_vmx_basic_inout()) |
| 2876 | *pdata |= VMX_BASIC_INOUT; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2877 | break; |
| 2878 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2879 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2880 | *pdata = vmx_control_msr( |
| 2881 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2882 | vmx->nested.nested_vmx_pinbased_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2883 | break; |
| 2884 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2885 | *pdata = vmx_control_msr( |
| 2886 | vmx->nested.nested_vmx_true_procbased_ctls_low, |
| 2887 | vmx->nested.nested_vmx_procbased_ctls_high); |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2888 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2889 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2890 | *pdata = vmx_control_msr( |
| 2891 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2892 | vmx->nested.nested_vmx_procbased_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2893 | break; |
| 2894 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2895 | *pdata = vmx_control_msr( |
| 2896 | vmx->nested.nested_vmx_true_exit_ctls_low, |
| 2897 | vmx->nested.nested_vmx_exit_ctls_high); |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2898 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2899 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2900 | *pdata = vmx_control_msr( |
| 2901 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2902 | vmx->nested.nested_vmx_exit_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2903 | break; |
| 2904 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2905 | *pdata = vmx_control_msr( |
| 2906 | vmx->nested.nested_vmx_true_entry_ctls_low, |
| 2907 | vmx->nested.nested_vmx_entry_ctls_high); |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2908 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2909 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2910 | *pdata = vmx_control_msr( |
| 2911 | vmx->nested.nested_vmx_entry_ctls_low, |
| 2912 | vmx->nested.nested_vmx_entry_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2913 | break; |
| 2914 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2915 | *pdata = vmx_control_msr( |
| 2916 | vmx->nested.nested_vmx_misc_low, |
| 2917 | vmx->nested.nested_vmx_misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2918 | break; |
| 2919 | /* |
| 2920 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 2921 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 2922 | * We picked the standard core2 setting. |
| 2923 | */ |
| 2924 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 2925 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 2926 | case MSR_IA32_VMX_CR0_FIXED0: |
| 2927 | *pdata = VMXON_CR0_ALWAYSON; |
| 2928 | break; |
| 2929 | case MSR_IA32_VMX_CR0_FIXED1: |
| 2930 | *pdata = -1ULL; |
| 2931 | break; |
| 2932 | case MSR_IA32_VMX_CR4_FIXED0: |
| 2933 | *pdata = VMXON_CR4_ALWAYSON; |
| 2934 | break; |
| 2935 | case MSR_IA32_VMX_CR4_FIXED1: |
| 2936 | *pdata = -1ULL; |
| 2937 | break; |
| 2938 | case MSR_IA32_VMX_VMCS_ENUM: |
Jan Kiszka | 5381417 | 2014-06-16 13:59:44 +0200 | [diff] [blame] | 2939 | *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2940 | break; |
| 2941 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2942 | *pdata = vmx_control_msr( |
| 2943 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 2944 | vmx->nested.nested_vmx_secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2945 | break; |
| 2946 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2947 | *pdata = vmx->nested.nested_vmx_ept_caps | |
| 2948 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2949 | break; |
| 2950 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2951 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 2952 | } |
| 2953 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2954 | return 0; |
| 2955 | } |
| 2956 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 2957 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 2958 | uint64_t val) |
| 2959 | { |
| 2960 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 2961 | |
| 2962 | return !(val & ~valid_bits); |
| 2963 | } |
| 2964 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2965 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2966 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 2967 | * Returns 0 on success, non-0 otherwise. |
| 2968 | * Assumes vcpu_load() was already called. |
| 2969 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2970 | 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] | 2971 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2972 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2973 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2974 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2975 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2976 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2977 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2978 | break; |
| 2979 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2980 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2981 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2982 | case MSR_KERNEL_GS_BASE: |
| 2983 | vmx_load_host_state(to_vmx(vcpu)); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2984 | msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2985 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2986 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2987 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2988 | return kvm_get_msr_common(vcpu, msr_info); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2989 | case MSR_IA32_TSC: |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2990 | msr_info->data = guest_read_tsc(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2991 | break; |
| 2992 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2993 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2994 | break; |
| 2995 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2996 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2997 | break; |
| 2998 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2999 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3000 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3001 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | cce8d2e | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3002 | if (!kvm_mpx_supported() || |
| 3003 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3004 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3005 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3006 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3007 | case MSR_IA32_MCG_EXT_CTL: |
| 3008 | if (!msr_info->host_initiated && |
| 3009 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3010 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3011 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3012 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3013 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3014 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3015 | msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3016 | break; |
| 3017 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3018 | if (!nested_vmx_allowed(vcpu)) |
| 3019 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3020 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3021 | case MSR_IA32_XSS: |
| 3022 | if (!vmx_xsaves_supported()) |
| 3023 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3024 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3025 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3026 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3027 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3028 | return 1; |
| 3029 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3030 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3031 | msr = find_msr_entry(to_vmx(vcpu), msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3032 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3033 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3034 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3035 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3036 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3037 | } |
| 3038 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3039 | return 0; |
| 3040 | } |
| 3041 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3042 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3043 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3044 | /* |
| 3045 | * Writes msr value into into the appropriate "register". |
| 3046 | * Returns 0 on success, non-0 otherwise. |
| 3047 | * Assumes vcpu_load() was already called. |
| 3048 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3049 | 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] | 3050 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3051 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3052 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3053 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3054 | u32 msr_index = msr_info->index; |
| 3055 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3056 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3057 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3058 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3059 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3060 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3061 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3062 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3063 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3064 | vmcs_writel(GUEST_FS_BASE, data); |
| 3065 | break; |
| 3066 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3067 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3068 | vmcs_writel(GUEST_GS_BASE, data); |
| 3069 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3070 | case MSR_KERNEL_GS_BASE: |
| 3071 | vmx_load_host_state(vmx); |
| 3072 | vmx->msr_guest_kernel_gs_base = data; |
| 3073 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3074 | #endif |
| 3075 | case MSR_IA32_SYSENTER_CS: |
| 3076 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3077 | break; |
| 3078 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3079 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3080 | break; |
| 3081 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3082 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3083 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3084 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | cce8d2e | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3085 | if (!kvm_mpx_supported() || |
| 3086 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3087 | return 1; |
Jim Mattson | 07592d6 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3088 | if (is_noncanonical_address(data & PAGE_MASK) || |
| 3089 | (data & MSR_IA32_BNDCFGS_RSVD)) |
| 3090 | return 1; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3091 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3092 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3093 | case MSR_IA32_TSC: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3094 | kvm_write_tsc(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3095 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3096 | case MSR_IA32_CR_PAT: |
| 3097 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3098 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3099 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3100 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3101 | vcpu->arch.pat = data; |
| 3102 | break; |
| 3103 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3104 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3105 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3106 | case MSR_IA32_TSC_ADJUST: |
| 3107 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3108 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3109 | case MSR_IA32_MCG_EXT_CTL: |
| 3110 | if ((!msr_info->host_initiated && |
| 3111 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3112 | FEATURE_CONTROL_LMCE)) || |
| 3113 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 3114 | return 1; |
| 3115 | vcpu->arch.mcg_ext_ctl = data; |
| 3116 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3117 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3118 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3119 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3120 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 3121 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3122 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3123 | if (msr_info->host_initiated && data == 0) |
| 3124 | vmx_leave_nested(vcpu); |
| 3125 | break; |
| 3126 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3127 | return 1; /* they are read-only */ |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3128 | case MSR_IA32_XSS: |
| 3129 | if (!vmx_xsaves_supported()) |
| 3130 | return 1; |
| 3131 | /* |
| 3132 | * The only supported bit as of Skylake is bit 8, but |
| 3133 | * it is not supported on KVM. |
| 3134 | */ |
| 3135 | if (data != 0) |
| 3136 | return 1; |
| 3137 | vcpu->arch.ia32_xss = data; |
| 3138 | if (vcpu->arch.ia32_xss != host_xss) |
| 3139 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 3140 | vcpu->arch.ia32_xss, host_xss); |
| 3141 | else |
| 3142 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 3143 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3144 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3145 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3146 | return 1; |
| 3147 | /* Check reserved bit, higher 32 bits should be zero */ |
| 3148 | if ((data >> 32) != 0) |
| 3149 | return 1; |
| 3150 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3151 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3152 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3153 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3154 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3155 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3156 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 3157 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3158 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 3159 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3160 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3161 | if (ret) |
| 3162 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3163 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3164 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3165 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3166 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3167 | } |
| 3168 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3169 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3170 | } |
| 3171 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3172 | 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] | 3173 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3174 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 3175 | switch (reg) { |
| 3176 | case VCPU_REGS_RSP: |
| 3177 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 3178 | break; |
| 3179 | case VCPU_REGS_RIP: |
| 3180 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 3181 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3182 | case VCPU_EXREG_PDPTR: |
| 3183 | if (enable_ept) |
| 3184 | ept_save_pdptrs(vcpu); |
| 3185 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3186 | default: |
| 3187 | break; |
| 3188 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3189 | } |
| 3190 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3191 | static __init int cpu_has_kvm_support(void) |
| 3192 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 3193 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | static __init int vmx_disabled_by_bios(void) |
| 3197 | { |
| 3198 | u64 msr; |
| 3199 | |
| 3200 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3201 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3202 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3203 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 3204 | && tboot_enabled()) |
| 3205 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3206 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3207 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3208 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3209 | && !tboot_enabled()) { |
| 3210 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3211 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3212 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3213 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3214 | /* launched w/o TXT and VMX disabled */ |
| 3215 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 3216 | && !tboot_enabled()) |
| 3217 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3221 | } |
| 3222 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3223 | static void kvm_cpu_vmxon(u64 addr) |
| 3224 | { |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3225 | intel_pt_handle_vmx(1); |
| 3226 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3227 | asm volatile (ASM_VMX_VMXON_RAX |
| 3228 | : : "a"(&addr), "m"(addr) |
| 3229 | : "memory", "cc"); |
| 3230 | } |
| 3231 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3232 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3233 | { |
| 3234 | int cpu = raw_smp_processor_id(); |
| 3235 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3236 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3237 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 3238 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3239 | return -EBUSY; |
| 3240 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3241 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 3242 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 3243 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 3244 | |
| 3245 | /* |
| 3246 | * Now we can enable the vmclear operation in kdump |
| 3247 | * since the loaded_vmcss_on_cpu list on this cpu |
| 3248 | * has been initialized. |
| 3249 | * |
| 3250 | * Though the cpu is not in VMX operation now, there |
| 3251 | * is no problem to enable the vmclear operation |
| 3252 | * for the loaded_vmcss_on_cpu list is empty! |
| 3253 | */ |
| 3254 | crash_enable_local_vmclear(cpu); |
| 3255 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3256 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3257 | |
| 3258 | test_bits = FEATURE_CONTROL_LOCKED; |
| 3259 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 3260 | if (tboot_enabled()) |
| 3261 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 3262 | |
| 3263 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3264 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3265 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 3266 | } |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 3267 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3268 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3269 | if (vmm_exclusive) { |
| 3270 | kvm_cpu_vmxon(phys_addr); |
| 3271 | ept_sync_global(); |
| 3272 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3273 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 3274 | native_store_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 3275 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3276 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3277 | } |
| 3278 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3279 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3280 | { |
| 3281 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3282 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3283 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3284 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 3285 | loaded_vmcss_on_cpu_link) |
| 3286 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3287 | } |
| 3288 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3289 | |
| 3290 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 3291 | * tricks. |
| 3292 | */ |
| 3293 | static void kvm_cpu_vmxoff(void) |
| 3294 | { |
| 3295 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3296 | |
| 3297 | intel_pt_handle_vmx(0); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3298 | } |
| 3299 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3300 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3301 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3302 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3303 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3304 | kvm_cpu_vmxoff(); |
| 3305 | } |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 3306 | cr4_clear_bits(X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3307 | } |
| 3308 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3309 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3310 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3311 | { |
| 3312 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3313 | u32 ctl = ctl_min | ctl_opt; |
| 3314 | |
| 3315 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3316 | |
| 3317 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 3318 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 3319 | |
| 3320 | /* Ensure minimum (required) set of control bits are supported. */ |
| 3321 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3322 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3323 | |
| 3324 | *result = ctl; |
| 3325 | return 0; |
| 3326 | } |
| 3327 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3328 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 3329 | { |
| 3330 | u32 vmx_msr_low, vmx_msr_high; |
| 3331 | |
| 3332 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3333 | return vmx_msr_high & ctl; |
| 3334 | } |
| 3335 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3336 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3337 | { |
| 3338 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3339 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3340 | u32 _pin_based_exec_control = 0; |
| 3341 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3342 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3343 | u32 _vmexit_control = 0; |
| 3344 | u32 _vmentry_control = 0; |
| 3345 | |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 3346 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3347 | #ifdef CONFIG_X86_64 |
| 3348 | CPU_BASED_CR8_LOAD_EXITING | |
| 3349 | CPU_BASED_CR8_STORE_EXITING | |
| 3350 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3351 | CPU_BASED_CR3_LOAD_EXITING | |
| 3352 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3353 | CPU_BASED_USE_IO_BITMAPS | |
| 3354 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3355 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 3356 | CPU_BASED_MWAIT_EXITING | |
| 3357 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 3358 | CPU_BASED_INVLPG_EXITING | |
| 3359 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3360 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3361 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3362 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3363 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3364 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 3365 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3366 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3367 | #ifdef CONFIG_X86_64 |
| 3368 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3369 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 3370 | ~CPU_BASED_CR8_STORE_EXITING; |
| 3371 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3372 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3373 | min2 = 0; |
| 3374 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3375 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3376 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3377 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3378 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3379 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3380 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3381 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3382 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3383 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 3384 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3385 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 3386 | SECONDARY_EXEC_XSAVES | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 3387 | SECONDARY_EXEC_ENABLE_PML | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 3388 | SECONDARY_EXEC_TSC_SCALING; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3389 | if (adjust_vmx_controls(min2, opt2, |
| 3390 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3391 | &_cpu_based_2nd_exec_control) < 0) |
| 3392 | return -EIO; |
| 3393 | } |
| 3394 | #ifndef CONFIG_X86_64 |
| 3395 | if (!(_cpu_based_2nd_exec_control & |
| 3396 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 3397 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3398 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3399 | |
| 3400 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3401 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3402 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3403 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 3404 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3405 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3406 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3407 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 3408 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 3409 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3410 | CPU_BASED_CR3_STORE_EXITING | |
| 3411 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3412 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 3413 | vmx_capability.ept, vmx_capability.vpid); |
| 3414 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3415 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3416 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3417 | #ifdef CONFIG_X86_64 |
| 3418 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 3419 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 3420 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3421 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3422 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 3423 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3424 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3425 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3426 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 3427 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 3428 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3429 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 3430 | &_pin_based_exec_control) < 0) |
| 3431 | return -EIO; |
| 3432 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 3433 | if (cpu_has_broken_vmx_preemption_timer()) |
| 3434 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3435 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3436 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3437 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 3438 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 3439 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 3440 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3441 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 3442 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3443 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3444 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3445 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3446 | |
| 3447 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 3448 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3449 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3450 | |
| 3451 | #ifdef CONFIG_X86_64 |
| 3452 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 3453 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3454 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3455 | #endif |
| 3456 | |
| 3457 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 3458 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3459 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3460 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3461 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 3462 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 3463 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3464 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3465 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3466 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 3467 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3468 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3469 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 3470 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3471 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3472 | cpu_has_load_ia32_efer = |
| 3473 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3474 | VM_ENTRY_LOAD_IA32_EFER) |
| 3475 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3476 | VM_EXIT_LOAD_IA32_EFER); |
| 3477 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 3478 | cpu_has_load_perf_global_ctrl = |
| 3479 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3480 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 3481 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3482 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 3483 | |
| 3484 | /* |
| 3485 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 3486 | * 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] | 3487 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 3488 | * |
| 3489 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 3490 | * |
| 3491 | * AAK155 (model 26) |
| 3492 | * AAP115 (model 30) |
| 3493 | * AAT100 (model 37) |
| 3494 | * BC86,AAY89,BD102 (model 44) |
| 3495 | * BA97 (model 46) |
| 3496 | * |
| 3497 | */ |
| 3498 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 3499 | switch (boot_cpu_data.x86_model) { |
| 3500 | case 26: |
| 3501 | case 30: |
| 3502 | case 37: |
| 3503 | case 44: |
| 3504 | case 46: |
| 3505 | cpu_has_load_perf_global_ctrl = false; |
| 3506 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 3507 | "does not work properly. Using workaround\n"); |
| 3508 | break; |
| 3509 | default: |
| 3510 | break; |
| 3511 | } |
| 3512 | } |
| 3513 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 3514 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3515 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 3516 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3517 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3518 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3519 | |
| 3520 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 3521 | { |
| 3522 | int node = cpu_to_node(cpu); |
| 3523 | struct page *pages; |
| 3524 | struct vmcs *vmcs; |
| 3525 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 3526 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3527 | if (!pages) |
| 3528 | return NULL; |
| 3529 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3530 | memset(vmcs, 0, vmcs_config.size); |
| 3531 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3532 | return vmcs; |
| 3533 | } |
| 3534 | |
| 3535 | static struct vmcs *alloc_vmcs(void) |
| 3536 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3537 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | static void free_vmcs(struct vmcs *vmcs) |
| 3541 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3542 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3543 | } |
| 3544 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3545 | /* |
| 3546 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 3547 | */ |
| 3548 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 3549 | { |
| 3550 | if (!loaded_vmcs->vmcs) |
| 3551 | return; |
| 3552 | loaded_vmcs_clear(loaded_vmcs); |
| 3553 | free_vmcs(loaded_vmcs->vmcs); |
| 3554 | loaded_vmcs->vmcs = NULL; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 3555 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3556 | } |
| 3557 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 3558 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3559 | { |
| 3560 | int cpu; |
| 3561 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3562 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3563 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3564 | per_cpu(vmxarea, cpu) = NULL; |
| 3565 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3566 | } |
| 3567 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3568 | static void init_vmcs_shadow_fields(void) |
| 3569 | { |
| 3570 | int i, j; |
| 3571 | |
| 3572 | /* No checks for read only fields yet */ |
| 3573 | |
| 3574 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
| 3575 | switch (shadow_read_write_fields[i]) { |
| 3576 | case GUEST_BNDCFGS: |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3577 | if (!kvm_mpx_supported()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3578 | continue; |
| 3579 | break; |
| 3580 | default: |
| 3581 | break; |
| 3582 | } |
| 3583 | |
| 3584 | if (j < i) |
| 3585 | shadow_read_write_fields[j] = |
| 3586 | shadow_read_write_fields[i]; |
| 3587 | j++; |
| 3588 | } |
| 3589 | max_shadow_read_write_fields = j; |
| 3590 | |
| 3591 | /* shadowed fields guest access without vmexit */ |
| 3592 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 3593 | clear_bit(shadow_read_write_fields[i], |
| 3594 | vmx_vmwrite_bitmap); |
| 3595 | clear_bit(shadow_read_write_fields[i], |
| 3596 | vmx_vmread_bitmap); |
| 3597 | } |
| 3598 | for (i = 0; i < max_shadow_read_only_fields; i++) |
| 3599 | clear_bit(shadow_read_only_fields[i], |
| 3600 | vmx_vmread_bitmap); |
| 3601 | } |
| 3602 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3603 | static __init int alloc_kvm_area(void) |
| 3604 | { |
| 3605 | int cpu; |
| 3606 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3607 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3608 | struct vmcs *vmcs; |
| 3609 | |
| 3610 | vmcs = alloc_vmcs_cpu(cpu); |
| 3611 | if (!vmcs) { |
| 3612 | free_kvm_area(); |
| 3613 | return -ENOMEM; |
| 3614 | } |
| 3615 | |
| 3616 | per_cpu(vmxarea, cpu) = vmcs; |
| 3617 | } |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3621 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 3622 | { |
| 3623 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 3624 | } |
| 3625 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3626 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3627 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3628 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3629 | if (!emulate_invalid_guest_state) { |
| 3630 | /* |
| 3631 | * CS and SS RPL should be equal during guest entry according |
| 3632 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 3633 | * is in the middle of the transition from real mode to |
| 3634 | * protected mode it is safe to assume that RPL 0 is a good |
| 3635 | * default value. |
| 3636 | */ |
| 3637 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3638 | save->selector &= ~SEGMENT_RPL_MASK; |
| 3639 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3640 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3641 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3642 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 3646 | { |
| 3647 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3648 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3649 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3650 | /* |
| 3651 | * Update real mode segment cache. It may be not up-to-date if sement |
| 3652 | * register was written while vcpu was in a guest mode. |
| 3653 | */ |
| 3654 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3655 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3656 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3657 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 3658 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3659 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 3660 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3661 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3662 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3663 | vmx_segment_cache_clear(vmx); |
| 3664 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3665 | 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] | 3666 | |
| 3667 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3668 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 3669 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3670 | vmcs_writel(GUEST_RFLAGS, flags); |
| 3671 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3672 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 3673 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3674 | |
| 3675 | update_exception_bitmap(vcpu); |
| 3676 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3677 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3678 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3679 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3680 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3681 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 3682 | 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] | 3683 | } |
| 3684 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3685 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3686 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3687 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3688 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3689 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3690 | var.dpl = 0x3; |
| 3691 | if (seg == VCPU_SREG_CS) |
| 3692 | var.type = 0x3; |
| 3693 | |
| 3694 | if (!emulate_invalid_guest_state) { |
| 3695 | var.selector = var.base >> 4; |
| 3696 | var.base = var.base & 0xffff0; |
| 3697 | var.limit = 0xffff; |
| 3698 | var.g = 0; |
| 3699 | var.db = 0; |
| 3700 | var.present = 1; |
| 3701 | var.s = 1; |
| 3702 | var.l = 0; |
| 3703 | var.unusable = 0; |
| 3704 | var.type = 0x3; |
| 3705 | var.avl = 0; |
| 3706 | if (save->base & 0xf) |
| 3707 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 3708 | "paragraph aligned when entering " |
| 3709 | "protected mode (seg=%d)", seg); |
| 3710 | } |
| 3711 | |
| 3712 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 7c3bab1 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 3713 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3714 | vmcs_write32(sf->limit, var.limit); |
| 3715 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 3719 | { |
| 3720 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3721 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3722 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3723 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 3724 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3725 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3726 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3727 | 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] | 3728 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3729 | 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] | 3730 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3731 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3732 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3733 | /* |
| 3734 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3735 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3736 | */ |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3737 | if (!vcpu->kvm->arch.tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3738 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 3739 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3740 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3741 | vmx_segment_cache_clear(vmx); |
| 3742 | |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3743 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3744 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3745 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3746 | |
| 3747 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3748 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3749 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 3750 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3751 | |
| 3752 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3753 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3754 | update_exception_bitmap(vcpu); |
| 3755 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3756 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3757 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3758 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3759 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3760 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 3761 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3762 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 3763 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3764 | } |
| 3765 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3766 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 3767 | { |
| 3768 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3769 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 3770 | |
| 3771 | if (!msr) |
| 3772 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3773 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3774 | /* |
| 3775 | * Force kernel_gs_base reloading before EFER changes, as control |
| 3776 | * of this msr depends on is_long_mode(). |
| 3777 | */ |
| 3778 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3779 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3780 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3781 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3782 | msr->data = efer; |
| 3783 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3784 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3785 | |
| 3786 | msr->data = efer & ~EFER_LME; |
| 3787 | } |
| 3788 | setup_msrs(vmx); |
| 3789 | } |
| 3790 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3791 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3792 | |
| 3793 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 3794 | { |
| 3795 | u32 guest_tr_ar; |
| 3796 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3797 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 3798 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3799 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3800 | 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] | 3801 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 3802 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3803 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3804 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 3805 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3806 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3807 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 3811 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3812 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3813 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | #endif |
| 3817 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3818 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3819 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3820 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3821 | if (enable_ept) { |
| 3822 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 3823 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 3824 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3825 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3826 | } |
| 3827 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3828 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 3829 | { |
| 3830 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid); |
| 3831 | } |
| 3832 | |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 3833 | static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu) |
| 3834 | { |
| 3835 | if (enable_ept) |
| 3836 | vmx_flush_tlb(vcpu); |
| 3837 | } |
| 3838 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 3839 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 3840 | { |
| 3841 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 3842 | |
| 3843 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 3844 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 3845 | } |
| 3846 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 3847 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 3848 | { |
| 3849 | if (enable_ept && is_paging(vcpu)) |
| 3850 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 3851 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 3852 | } |
| 3853 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3854 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3855 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3856 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 3857 | |
| 3858 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 3859 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3860 | } |
| 3861 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3862 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 3863 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3864 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 3865 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3866 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 3867 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3868 | return; |
| 3869 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3870 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3871 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 3872 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 3873 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 3874 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3875 | } |
| 3876 | } |
| 3877 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3878 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 3879 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3880 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 3881 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3882 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3883 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 3884 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 3885 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 3886 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3887 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3888 | |
| 3889 | __set_bit(VCPU_EXREG_PDPTR, |
| 3890 | (unsigned long *)&vcpu->arch.regs_avail); |
| 3891 | __set_bit(VCPU_EXREG_PDPTR, |
| 3892 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3893 | } |
| 3894 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3895 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3896 | |
| 3897 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 3898 | unsigned long cr0, |
| 3899 | struct kvm_vcpu *vcpu) |
| 3900 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 3901 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 3902 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3903 | if (!(cr0 & X86_CR0_PG)) { |
| 3904 | /* From paging/starting to nonpaging */ |
| 3905 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 3906 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3907 | (CPU_BASED_CR3_LOAD_EXITING | |
| 3908 | CPU_BASED_CR3_STORE_EXITING)); |
| 3909 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3910 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3911 | } else if (!is_paging(vcpu)) { |
| 3912 | /* From nonpaging to paging */ |
| 3913 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 3914 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3915 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3916 | CPU_BASED_CR3_STORE_EXITING)); |
| 3917 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3918 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3919 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 3920 | |
| 3921 | if (!(cr0 & X86_CR0_WP)) |
| 3922 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3923 | } |
| 3924 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3925 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 3926 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3927 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3928 | unsigned long hw_cr0; |
| 3929 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3930 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3931 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3932 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3933 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3934 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3935 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3936 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 3937 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3938 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3939 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 3940 | enter_rmode(vcpu); |
| 3941 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3942 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3943 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3944 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3945 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3946 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3947 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3948 | exit_lmode(vcpu); |
| 3949 | } |
| 3950 | #endif |
| 3951 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3952 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3953 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 3954 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3955 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 3956 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3957 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3958 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3959 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3960 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3961 | |
| 3962 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 3963 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3964 | } |
| 3965 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3966 | static u64 construct_eptp(unsigned long root_hpa) |
| 3967 | { |
| 3968 | u64 eptp; |
| 3969 | |
| 3970 | /* TODO write the value reading from MSR */ |
| 3971 | eptp = VMX_EPT_DEFAULT_MT | |
| 3972 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
Xudong Hao | b38f993 | 2012-05-28 19:33:36 +0800 | [diff] [blame] | 3973 | if (enable_ept_ad_bits) |
| 3974 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3975 | eptp |= (root_hpa & PAGE_MASK); |
| 3976 | |
| 3977 | return eptp; |
| 3978 | } |
| 3979 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3980 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 3981 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3982 | unsigned long guest_cr3; |
| 3983 | u64 eptp; |
| 3984 | |
| 3985 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3986 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3987 | eptp = construct_eptp(cr3); |
| 3988 | vmcs_write64(EPT_POINTER, eptp); |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 3989 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
| 3990 | guest_cr3 = kvm_read_cr3(vcpu); |
| 3991 | else |
| 3992 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 3993 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3994 | } |
| 3995 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3996 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3997 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3998 | } |
| 3999 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4000 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4001 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 4002 | /* |
| 4003 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 4004 | * is in force while we are in guest mode. Do not let guests control |
| 4005 | * this bit, even if host CR4.MCE == 0. |
| 4006 | */ |
| 4007 | unsigned long hw_cr4 = |
| 4008 | (cr4_read_shadow() & X86_CR4_MCE) | |
| 4009 | (cr4 & ~X86_CR4_MCE) | |
| 4010 | (to_vmx(vcpu)->rmode.vm86_active ? |
| 4011 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4012 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4013 | if (cr4 & X86_CR4_VMXE) { |
| 4014 | /* |
| 4015 | * To use VMXON (and later other VMX instructions), a guest |
| 4016 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 4017 | * So basically the check on whether to allow nested VMX |
| 4018 | * is here. |
| 4019 | */ |
| 4020 | if (!nested_vmx_allowed(vcpu)) |
| 4021 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4022 | } |
| 4023 | if (to_vmx(vcpu)->nested.vmxon && |
| 4024 | ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4025 | return 1; |
| 4026 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4027 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 4028 | if (enable_ept) { |
| 4029 | if (!is_paging(vcpu)) { |
| 4030 | hw_cr4 &= ~X86_CR4_PAE; |
| 4031 | hw_cr4 |= X86_CR4_PSE; |
| 4032 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 4033 | hw_cr4 &= ~X86_CR4_PAE; |
| 4034 | } |
| 4035 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4036 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4037 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
| 4038 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4039 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 4040 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 4041 | * to be manually disabled when guest switches to non-paging |
| 4042 | * mode. |
| 4043 | * |
| 4044 | * If !enable_unrestricted_guest, the CPU is always running |
| 4045 | * with CR0.PG=1 and CR4 needs to be modified. |
| 4046 | * If enable_unrestricted_guest, the CPU automatically |
| 4047 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4048 | */ |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4049 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4050 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4051 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 4052 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4053 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4054 | } |
| 4055 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4056 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 4057 | struct kvm_segment *var, int seg) |
| 4058 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4059 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4060 | u32 ar; |
| 4061 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 4062 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4063 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4064 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4065 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4066 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 4067 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4068 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4069 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4070 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4071 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4072 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 4073 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4074 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4075 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4076 | var->type = ar & 15; |
| 4077 | var->s = (ar >> 4) & 1; |
| 4078 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4079 | /* |
| 4080 | * Some userspaces do not preserve unusable property. Since usable |
| 4081 | * segment has to be present according to VMX spec we can use present |
| 4082 | * property to amend userspace bug by making unusable segment always |
| 4083 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 4084 | * segment as unusable. |
| 4085 | */ |
| 4086 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4087 | var->avl = (ar >> 12) & 1; |
| 4088 | var->l = (ar >> 13) & 1; |
| 4089 | var->db = (ar >> 14) & 1; |
| 4090 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4091 | } |
| 4092 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4093 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 4094 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4095 | struct kvm_segment s; |
| 4096 | |
| 4097 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 4098 | vmx_get_segment(vcpu, &s, seg); |
| 4099 | return s.base; |
| 4100 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4101 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4102 | } |
| 4103 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4104 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4105 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4106 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4107 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4108 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4109 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4110 | else { |
| 4111 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4112 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4113 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4114 | } |
| 4115 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4116 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4117 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4118 | u32 ar; |
| 4119 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 4120 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4121 | ar = 1 << 16; |
| 4122 | else { |
| 4123 | ar = var->type & 15; |
| 4124 | ar |= (var->s & 1) << 4; |
| 4125 | ar |= (var->dpl & 3) << 5; |
| 4126 | ar |= (var->present & 1) << 7; |
| 4127 | ar |= (var->avl & 1) << 12; |
| 4128 | ar |= (var->l & 1) << 13; |
| 4129 | ar |= (var->db & 1) << 14; |
| 4130 | ar |= (var->g & 1) << 15; |
| 4131 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4132 | |
| 4133 | return ar; |
| 4134 | } |
| 4135 | |
| 4136 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 4137 | struct kvm_segment *var, int seg) |
| 4138 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4139 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4140 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4141 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4142 | vmx_segment_cache_clear(vmx); |
| 4143 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4144 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 4145 | vmx->rmode.segs[seg] = *var; |
| 4146 | if (seg == VCPU_SREG_TR) |
| 4147 | vmcs_write16(sf->selector, var->selector); |
| 4148 | else if (var->s) |
| 4149 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4150 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4151 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4152 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4153 | vmcs_writel(sf->base, var->base); |
| 4154 | vmcs_write32(sf->limit, var->limit); |
| 4155 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4156 | |
| 4157 | /* |
| 4158 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 4159 | * qemu binaries. |
| 4160 | * IA32 arch specifies that at the time of processor reset the |
| 4161 | * "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] | 4162 | * 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] | 4163 | * state vmexit when "unrestricted guest" mode is turned on. |
| 4164 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 4165 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 4166 | * kvm hack. |
| 4167 | */ |
| 4168 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4169 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4170 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4171 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4172 | |
| 4173 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 4174 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4175 | } |
| 4176 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4177 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4178 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4179 | 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] | 4180 | |
| 4181 | *db = (ar >> 14) & 1; |
| 4182 | *l = (ar >> 13) & 1; |
| 4183 | } |
| 4184 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4185 | 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] | 4186 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4187 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4188 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4189 | } |
| 4190 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4191 | 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] | 4192 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4193 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 4194 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4195 | } |
| 4196 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4197 | 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] | 4198 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4199 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4200 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4201 | } |
| 4202 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4203 | 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] | 4204 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4205 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 4206 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4207 | } |
| 4208 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4209 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4210 | { |
| 4211 | struct kvm_segment var; |
| 4212 | u32 ar; |
| 4213 | |
| 4214 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4215 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 4216 | if (seg == VCPU_SREG_CS) |
| 4217 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4218 | ar = vmx_segment_access_rights(&var); |
| 4219 | |
| 4220 | if (var.base != (var.selector << 4)) |
| 4221 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 4222 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4223 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4224 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4225 | return false; |
| 4226 | |
| 4227 | return true; |
| 4228 | } |
| 4229 | |
| 4230 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 4231 | { |
| 4232 | struct kvm_segment cs; |
| 4233 | unsigned int cs_rpl; |
| 4234 | |
| 4235 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4236 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4237 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4238 | if (cs.unusable) |
| 4239 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4240 | 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] | 4241 | return false; |
| 4242 | if (!cs.s) |
| 4243 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4244 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4245 | if (cs.dpl > cs_rpl) |
| 4246 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4247 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4248 | if (cs.dpl != cs_rpl) |
| 4249 | return false; |
| 4250 | } |
| 4251 | if (!cs.present) |
| 4252 | return false; |
| 4253 | |
| 4254 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 4255 | return true; |
| 4256 | } |
| 4257 | |
| 4258 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 4259 | { |
| 4260 | struct kvm_segment ss; |
| 4261 | unsigned int ss_rpl; |
| 4262 | |
| 4263 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4264 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4265 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4266 | if (ss.unusable) |
| 4267 | return true; |
| 4268 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4269 | return false; |
| 4270 | if (!ss.s) |
| 4271 | return false; |
| 4272 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 4273 | return false; |
| 4274 | if (!ss.present) |
| 4275 | return false; |
| 4276 | |
| 4277 | return true; |
| 4278 | } |
| 4279 | |
| 4280 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4281 | { |
| 4282 | struct kvm_segment var; |
| 4283 | unsigned int rpl; |
| 4284 | |
| 4285 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4286 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4287 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4288 | if (var.unusable) |
| 4289 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4290 | if (!var.s) |
| 4291 | return false; |
| 4292 | if (!var.present) |
| 4293 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4294 | 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] | 4295 | if (var.dpl < rpl) /* DPL < RPL */ |
| 4296 | return false; |
| 4297 | } |
| 4298 | |
| 4299 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 4300 | * rights flags |
| 4301 | */ |
| 4302 | return true; |
| 4303 | } |
| 4304 | |
| 4305 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 4306 | { |
| 4307 | struct kvm_segment tr; |
| 4308 | |
| 4309 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 4310 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4311 | if (tr.unusable) |
| 4312 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4313 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4314 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4315 | 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] | 4316 | return false; |
| 4317 | if (!tr.present) |
| 4318 | return false; |
| 4319 | |
| 4320 | return true; |
| 4321 | } |
| 4322 | |
| 4323 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 4324 | { |
| 4325 | struct kvm_segment ldtr; |
| 4326 | |
| 4327 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 4328 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4329 | if (ldtr.unusable) |
| 4330 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4331 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4332 | return false; |
| 4333 | if (ldtr.type != 2) |
| 4334 | return false; |
| 4335 | if (!ldtr.present) |
| 4336 | return false; |
| 4337 | |
| 4338 | return true; |
| 4339 | } |
| 4340 | |
| 4341 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 4342 | { |
| 4343 | struct kvm_segment cs, ss; |
| 4344 | |
| 4345 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 4346 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 4347 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4348 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 4349 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | /* |
| 4353 | * Check if guest state is valid. Returns true if valid, false if |
| 4354 | * not. |
| 4355 | * We assume that registers are always usable |
| 4356 | */ |
| 4357 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 4358 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 4359 | if (enable_unrestricted_guest) |
| 4360 | return true; |
| 4361 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4362 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 4363 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4364 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 4365 | return false; |
| 4366 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 4367 | return false; |
| 4368 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4369 | return false; |
| 4370 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4371 | return false; |
| 4372 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4373 | return false; |
| 4374 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4375 | return false; |
| 4376 | } else { |
| 4377 | /* protected mode guest state checks */ |
| 4378 | if (!cs_ss_rpl_check(vcpu)) |
| 4379 | return false; |
| 4380 | if (!code_segment_valid(vcpu)) |
| 4381 | return false; |
| 4382 | if (!stack_segment_valid(vcpu)) |
| 4383 | return false; |
| 4384 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4385 | return false; |
| 4386 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4387 | return false; |
| 4388 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4389 | return false; |
| 4390 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4391 | return false; |
| 4392 | if (!tr_valid(vcpu)) |
| 4393 | return false; |
| 4394 | if (!ldtr_valid(vcpu)) |
| 4395 | return false; |
| 4396 | } |
| 4397 | /* TODO: |
| 4398 | * - Add checks on RIP |
| 4399 | * - Add checks on RFLAGS |
| 4400 | */ |
| 4401 | |
| 4402 | return true; |
| 4403 | } |
| 4404 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4405 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4406 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4407 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4408 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4409 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4410 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4411 | idx = srcu_read_lock(&kvm->srcu); |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4412 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4413 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4414 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4415 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4416 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 4417 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 4418 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4419 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4420 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4421 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 4422 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4423 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4424 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4425 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4426 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4427 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4428 | r = kvm_write_guest_page(kvm, fn, &data, |
| 4429 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 4430 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4431 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4432 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4433 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4434 | } |
| 4435 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4436 | static int init_rmode_identity_map(struct kvm *kvm) |
| 4437 | { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4438 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 4439 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4440 | u32 tmp; |
| 4441 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4442 | if (!enable_ept) |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4443 | return 0; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4444 | |
| 4445 | /* Protect kvm->arch.ept_identity_pagetable_done. */ |
| 4446 | mutex_lock(&kvm->slots_lock); |
| 4447 | |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4448 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4449 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4450 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4451 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4452 | |
| 4453 | r = alloc_identity_pagetable(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4454 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4455 | goto out2; |
| 4456 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4457 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4458 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 4459 | if (r < 0) |
| 4460 | goto out; |
| 4461 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 4462 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 4463 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 4464 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 4465 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 4466 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 4467 | if (r < 0) |
| 4468 | goto out; |
| 4469 | } |
| 4470 | kvm->arch.ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4471 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4472 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4473 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4474 | |
| 4475 | out2: |
| 4476 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4477 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4478 | } |
| 4479 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4480 | static void seg_setup(int seg) |
| 4481 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4482 | 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] | 4483 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4484 | |
| 4485 | vmcs_write16(sf->selector, 0); |
| 4486 | vmcs_writel(sf->base, 0); |
| 4487 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 4488 | ar = 0x93; |
| 4489 | if (seg == VCPU_SREG_CS) |
| 4490 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4491 | |
| 4492 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4493 | } |
| 4494 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4495 | static int alloc_apic_access_page(struct kvm *kvm) |
| 4496 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4497 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4498 | int r = 0; |
| 4499 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4500 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4501 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4502 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4503 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 4504 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4505 | if (r) |
| 4506 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4507 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 4508 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4509 | if (is_error_page(page)) { |
| 4510 | r = -EFAULT; |
| 4511 | goto out; |
| 4512 | } |
| 4513 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4514 | /* |
| 4515 | * Do not pin the page in memory, so that memory hot-unplug |
| 4516 | * is able to migrate it. |
| 4517 | */ |
| 4518 | put_page(page); |
| 4519 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4520 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4521 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4522 | return r; |
| 4523 | } |
| 4524 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4525 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 4526 | { |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4527 | /* Called with kvm->slots_lock held. */ |
| 4528 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4529 | int r = 0; |
| 4530 | |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4531 | BUG_ON(kvm->arch.ept_identity_pagetable_done); |
| 4532 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4533 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
| 4534 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4535 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4536 | return r; |
| 4537 | } |
| 4538 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4539 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4540 | { |
| 4541 | int vpid; |
| 4542 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 4543 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4544 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4545 | spin_lock(&vmx_vpid_lock); |
| 4546 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4547 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4548 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4549 | else |
| 4550 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4551 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4552 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4553 | } |
| 4554 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4555 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4556 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4557 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4558 | return; |
| 4559 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4560 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4561 | spin_unlock(&vmx_vpid_lock); |
| 4562 | } |
| 4563 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4564 | #define MSR_TYPE_R 1 |
| 4565 | #define MSR_TYPE_W 2 |
| 4566 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4567 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4568 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4569 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4570 | |
| 4571 | if (!cpu_has_vmx_msr_bitmap()) |
| 4572 | return; |
| 4573 | |
| 4574 | /* |
| 4575 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4576 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4577 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4578 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4579 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4580 | if (type & MSR_TYPE_R) |
| 4581 | /* read-low */ |
| 4582 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 4583 | |
| 4584 | if (type & MSR_TYPE_W) |
| 4585 | /* write-low */ |
| 4586 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 4587 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4588 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4589 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4590 | if (type & MSR_TYPE_R) |
| 4591 | /* read-high */ |
| 4592 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 4593 | |
| 4594 | if (type & MSR_TYPE_W) |
| 4595 | /* write-high */ |
| 4596 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 4597 | |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4602 | u32 msr, int type) |
| 4603 | { |
| 4604 | int f = sizeof(unsigned long); |
| 4605 | |
| 4606 | if (!cpu_has_vmx_msr_bitmap()) |
| 4607 | return; |
| 4608 | |
| 4609 | /* |
| 4610 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4611 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4612 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4613 | */ |
| 4614 | if (msr <= 0x1fff) { |
| 4615 | if (type & MSR_TYPE_R) |
| 4616 | /* read-low */ |
| 4617 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 4618 | |
| 4619 | if (type & MSR_TYPE_W) |
| 4620 | /* write-low */ |
| 4621 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 4622 | |
| 4623 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4624 | msr &= 0x1fff; |
| 4625 | if (type & MSR_TYPE_R) |
| 4626 | /* read-high */ |
| 4627 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 4628 | |
| 4629 | if (type & MSR_TYPE_W) |
| 4630 | /* write-high */ |
| 4631 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 4632 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4633 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4634 | } |
| 4635 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 4636 | /* |
| 4637 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 4638 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 4639 | */ |
| 4640 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 4641 | unsigned long *msr_bitmap_nested, |
| 4642 | u32 msr, int type) |
| 4643 | { |
| 4644 | int f = sizeof(unsigned long); |
| 4645 | |
| 4646 | if (!cpu_has_vmx_msr_bitmap()) { |
| 4647 | WARN_ON(1); |
| 4648 | return; |
| 4649 | } |
| 4650 | |
| 4651 | /* |
| 4652 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4653 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4654 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4655 | */ |
| 4656 | if (msr <= 0x1fff) { |
| 4657 | if (type & MSR_TYPE_R && |
| 4658 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 4659 | /* read-low */ |
| 4660 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 4661 | |
| 4662 | if (type & MSR_TYPE_W && |
| 4663 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 4664 | /* write-low */ |
| 4665 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 4666 | |
| 4667 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4668 | msr &= 0x1fff; |
| 4669 | if (type & MSR_TYPE_R && |
| 4670 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 4671 | /* read-high */ |
| 4672 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 4673 | |
| 4674 | if (type & MSR_TYPE_W && |
| 4675 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 4676 | /* write-high */ |
| 4677 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 4678 | |
| 4679 | } |
| 4680 | } |
| 4681 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4682 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 4683 | { |
| 4684 | if (!longmode_only) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4685 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, |
| 4686 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 4687 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, |
| 4688 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 4689 | } |
| 4690 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4691 | static void vmx_enable_intercept_msr_read_x2apic(u32 msr, bool apicv_active) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4692 | { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4693 | if (apicv_active) { |
| 4694 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4695 | msr, MSR_TYPE_R); |
| 4696 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4697 | msr, MSR_TYPE_R); |
| 4698 | } else { |
| 4699 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive, |
| 4700 | msr, MSR_TYPE_R); |
| 4701 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive, |
| 4702 | msr, MSR_TYPE_R); |
| 4703 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4704 | } |
| 4705 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4706 | static void vmx_disable_intercept_msr_read_x2apic(u32 msr, bool apicv_active) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4707 | { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4708 | if (apicv_active) { |
| 4709 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4710 | msr, MSR_TYPE_R); |
| 4711 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4712 | msr, MSR_TYPE_R); |
| 4713 | } else { |
| 4714 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive, |
| 4715 | msr, MSR_TYPE_R); |
| 4716 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive, |
| 4717 | msr, MSR_TYPE_R); |
| 4718 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4719 | } |
| 4720 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4721 | static void vmx_disable_intercept_msr_write_x2apic(u32 msr, bool apicv_active) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4722 | { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4723 | if (apicv_active) { |
| 4724 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4725 | msr, MSR_TYPE_W); |
| 4726 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4727 | msr, MSR_TYPE_W); |
| 4728 | } else { |
| 4729 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive, |
| 4730 | msr, MSR_TYPE_W); |
| 4731 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive, |
| 4732 | msr, MSR_TYPE_W); |
| 4733 | } |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4734 | } |
| 4735 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4736 | static bool vmx_get_enable_apicv(void) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4737 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4738 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4739 | } |
| 4740 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4741 | static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
| 4742 | { |
| 4743 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4744 | int max_irr; |
| 4745 | void *vapic_page; |
| 4746 | u16 status; |
| 4747 | |
| 4748 | if (vmx->nested.pi_desc && |
| 4749 | vmx->nested.pi_pending) { |
| 4750 | vmx->nested.pi_pending = false; |
| 4751 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 4752 | return 0; |
| 4753 | |
| 4754 | max_irr = find_last_bit( |
| 4755 | (unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 4756 | |
| 4757 | if (max_irr == 256) |
| 4758 | return 0; |
| 4759 | |
| 4760 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
| 4761 | if (!vapic_page) { |
| 4762 | WARN_ON(1); |
| 4763 | return -ENOMEM; |
| 4764 | } |
| 4765 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page); |
| 4766 | kunmap(vmx->nested.virtual_apic_page); |
| 4767 | |
| 4768 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 4769 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 4770 | status &= ~0xff; |
| 4771 | status |= (u8)max_irr; |
| 4772 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 4773 | } |
| 4774 | } |
| 4775 | return 0; |
| 4776 | } |
| 4777 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4778 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu) |
| 4779 | { |
| 4780 | #ifdef CONFIG_SMP |
| 4781 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4782 | /* |
Haozhong Zhang | 3ffbe62 | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 4783 | * The vector of interrupt to be delivered to vcpu had |
| 4784 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4785 | * |
Haozhong Zhang | 3ffbe62 | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 4786 | * Following cases will be reached in this block, and |
| 4787 | * we always send a notification event in all cases as |
| 4788 | * explained below. |
| 4789 | * |
| 4790 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 4791 | * notification event posts the interrupt to vcpu. |
| 4792 | * |
| 4793 | * Case 2: vcpu exits to root mode and is still |
| 4794 | * runnable. PIR will be synced to vIRR before the |
| 4795 | * next vcpu entry. Sending a notification event in |
| 4796 | * this case has no effect, as vcpu is not in root |
| 4797 | * mode. |
| 4798 | * |
| 4799 | * Case 3: vcpu exits to root mode and is blocked. |
| 4800 | * vcpu_block() has already synced PIR to vIRR and |
| 4801 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 4802 | * a blocked vcpu here does not wait for any requested |
| 4803 | * interrupts in PIR, and sending a notification event |
| 4804 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4805 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4806 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4807 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), |
| 4808 | POSTED_INTR_VECTOR); |
| 4809 | return true; |
| 4810 | } |
| 4811 | #endif |
| 4812 | return false; |
| 4813 | } |
| 4814 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4815 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 4816 | int vector) |
| 4817 | { |
| 4818 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4819 | |
| 4820 | if (is_guest_mode(vcpu) && |
| 4821 | vector == vmx->nested.posted_intr_nv) { |
| 4822 | /* the PIR and ON have been set by L1. */ |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4823 | kvm_vcpu_trigger_posted_interrupt(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4824 | /* |
| 4825 | * If a posted intr is not recognized by hardware, |
| 4826 | * we will accomplish it in the next vmentry. |
| 4827 | */ |
| 4828 | vmx->nested.pi_pending = true; |
| 4829 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4830 | return 0; |
| 4831 | } |
| 4832 | return -1; |
| 4833 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4834 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4835 | * Send interrupt to vcpu via posted interrupt way. |
| 4836 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 4837 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 4838 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 4839 | * interrupt from PIR in next vmentry. |
| 4840 | */ |
| 4841 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 4842 | { |
| 4843 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4844 | int r; |
| 4845 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4846 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 4847 | if (!r) |
| 4848 | return; |
| 4849 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4850 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 4851 | return; |
| 4852 | |
| 4853 | r = pi_test_and_set_on(&vmx->pi_desc); |
| 4854 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4855 | if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4856 | kvm_vcpu_kick(vcpu); |
| 4857 | } |
| 4858 | |
| 4859 | static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
| 4860 | { |
| 4861 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4862 | |
| 4863 | if (!pi_test_and_clear_on(&vmx->pi_desc)) |
| 4864 | return; |
| 4865 | |
| 4866 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); |
| 4867 | } |
| 4868 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4869 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4870 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 4871 | * will not change in the lifetime of the guest. |
| 4872 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 4873 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 4874 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4875 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4876 | { |
| 4877 | u32 low32, high32; |
| 4878 | unsigned long tmpl; |
| 4879 | struct desc_ptr dt; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 4880 | unsigned long cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4881 | |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 4882 | 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] | 4883 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 4884 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 4885 | /* 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] | 4886 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 4887 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
| 4888 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 4889 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4890 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 4891 | #ifdef CONFIG_X86_64 |
| 4892 | /* |
| 4893 | * Load null selectors, so we can avoid reloading them in |
| 4894 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 4895 | * too (the expected case). |
| 4896 | */ |
| 4897 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 4898 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 4899 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4900 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 4901 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 4902 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4903 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 4904 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 4905 | |
| 4906 | native_store_idt(&dt); |
| 4907 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4908 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4909 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 4910 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4911 | |
| 4912 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 4913 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 4914 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 4915 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 4916 | |
| 4917 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 4918 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 4919 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 4920 | } |
| 4921 | } |
| 4922 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4923 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 4924 | { |
| 4925 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 4926 | if (enable_ept) |
| 4927 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 4928 | if (is_guest_mode(&vmx->vcpu)) |
| 4929 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 4930 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4931 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 4932 | } |
| 4933 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4934 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 4935 | { |
| 4936 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 4937 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4938 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4939 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 4940 | /* Enable the preemption timer dynamically */ |
| 4941 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4942 | return pin_based_exec_ctrl; |
| 4943 | } |
| 4944 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4945 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 4946 | { |
| 4947 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4948 | |
| 4949 | 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] | 4950 | if (cpu_has_secondary_exec_ctrls()) { |
| 4951 | if (kvm_vcpu_apicv_active(vcpu)) |
| 4952 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 4953 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 4954 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 4955 | else |
| 4956 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 4957 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 4958 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 4959 | } |
| 4960 | |
| 4961 | if (cpu_has_vmx_msr_bitmap()) |
| 4962 | vmx_set_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4963 | } |
| 4964 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4965 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 4966 | { |
| 4967 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 4968 | |
| 4969 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 4970 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 4971 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4972 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4973 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4974 | #ifdef CONFIG_X86_64 |
| 4975 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 4976 | CPU_BASED_CR8_LOAD_EXITING; |
| 4977 | #endif |
| 4978 | } |
| 4979 | if (!enable_ept) |
| 4980 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 4981 | CPU_BASED_CR3_LOAD_EXITING | |
| 4982 | CPU_BASED_INVLPG_EXITING; |
| 4983 | return exec_control; |
| 4984 | } |
| 4985 | |
| 4986 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 4987 | { |
| 4988 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4989 | if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4990 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 4991 | if (vmx->vpid == 0) |
| 4992 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 4993 | if (!enable_ept) { |
| 4994 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 4995 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4996 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 4997 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4998 | } |
| 4999 | if (!enable_unrestricted_guest) |
| 5000 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 5001 | if (!ple_gap) |
| 5002 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5003 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5004 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5005 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5006 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 5007 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 5008 | (handle_vmptrld). |
| 5009 | We can NOT enable shadow_vmcs here because we don't have yet |
| 5010 | a current VMCS12 |
| 5011 | */ |
| 5012 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5013 | |
| 5014 | if (!enable_pml) |
| 5015 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5016 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5017 | return exec_control; |
| 5018 | } |
| 5019 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5020 | static void ept_set_mmio_spte_mask(void) |
| 5021 | { |
| 5022 | /* |
| 5023 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 5024 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 5025 | * Also, magic bits (0x3ull << 62) is set to quickly identify mmio |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5026 | * spte. |
| 5027 | */ |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 5028 | kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5029 | } |
| 5030 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5031 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5032 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5033 | * Sets up the vmcs for emulated real mode. |
| 5034 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 5035 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5036 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5037 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5038 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5039 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5040 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5041 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5042 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5043 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 5044 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5045 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 5046 | if (enable_shadow_vmcs) { |
| 5047 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
| 5048 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 5049 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5050 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5051 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5052 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5053 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 5054 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5055 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5056 | 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] | 5057 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5058 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5059 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5060 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5061 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5062 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 5063 | vmx_secondary_exec_control(vmx)); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5064 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5065 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5066 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5067 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 5068 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 5069 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 5070 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 5071 | |
| 5072 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5073 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 5074 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5075 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5076 | } |
| 5077 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5078 | if (ple_gap) { |
| 5079 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 5080 | vmx->ple_window = ple_window; |
| 5081 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5082 | } |
| 5083 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 5084 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 5085 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5086 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 5087 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 5088 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 5089 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5090 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5091 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5092 | rdmsrl(MSR_FS_BASE, a); |
| 5093 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 5094 | rdmsrl(MSR_GS_BASE, a); |
| 5095 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 5096 | #else |
| 5097 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 5098 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 5099 | #endif |
| 5100 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5101 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 5102 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 5103 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5104 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 5105 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5106 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 5107 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 5108 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 5109 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 5110 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5111 | u32 index = vmx_msr_index[i]; |
| 5112 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5113 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5114 | |
| 5115 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 5116 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 5117 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 5118 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 5119 | vmx->guest_msrs[j].index = i; |
| 5120 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 5121 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5122 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5123 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5124 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5125 | |
| 5126 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5127 | |
| 5128 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5129 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 5130 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5131 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5132 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5133 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5134 | if (vmx_xsaves_supported()) |
| 5135 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 5136 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 5137 | if (enable_pml) { |
| 5138 | ASSERT(vmx->pml_pg); |
| 5139 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 5140 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 5141 | } |
| 5142 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5143 | return 0; |
| 5144 | } |
| 5145 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5146 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5147 | { |
| 5148 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 5149 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5150 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5151 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5152 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5153 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5154 | vmx->soft_vnmi_blocked = 0; |
| 5155 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5156 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5157 | kvm_set_cr8(vcpu, 0); |
| 5158 | |
| 5159 | if (!init_event) { |
| 5160 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 5161 | MSR_IA32_APICBASE_ENABLE; |
| 5162 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 5163 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 5164 | apic_base_msr.host_initiated = true; |
| 5165 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 5166 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5167 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5168 | vmx_segment_cache_clear(vmx); |
| 5169 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 5170 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5171 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5172 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5173 | |
| 5174 | seg_setup(VCPU_SREG_DS); |
| 5175 | seg_setup(VCPU_SREG_ES); |
| 5176 | seg_setup(VCPU_SREG_FS); |
| 5177 | seg_setup(VCPU_SREG_GS); |
| 5178 | seg_setup(VCPU_SREG_SS); |
| 5179 | |
| 5180 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 5181 | vmcs_writel(GUEST_TR_BASE, 0); |
| 5182 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 5183 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 5184 | |
| 5185 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 5186 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 5187 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 5188 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 5189 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5190 | if (!init_event) { |
| 5191 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 5192 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 5193 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 5194 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 5195 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5196 | |
Wanpeng Li | 5c0b19b | 2017-11-20 14:52:21 -0800 | [diff] [blame^] | 5197 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5198 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5199 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5200 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 5201 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 5202 | |
| 5203 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 5204 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 5205 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 5206 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5207 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5208 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5209 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5210 | setup_msrs(vmx); |
| 5211 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5212 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 5213 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5214 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5215 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5216 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5217 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5218 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5219 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5220 | } |
| 5221 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 5222 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5223 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5224 | if (kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5225 | memset(&vmx->pi_desc, 0, sizeof(struct pi_desc)); |
| 5226 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5227 | if (vmx->vpid != 0) |
| 5228 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 5229 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5230 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5231 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 5232 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5233 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 5234 | vmx_set_efer(vcpu, 0); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5235 | vmx_fpu_activate(vcpu); |
| 5236 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5237 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 5238 | vpid_sync_context(vmx->vpid); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5239 | } |
| 5240 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 5241 | /* |
| 5242 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 5243 | * For most existing hypervisors, this will always return true. |
| 5244 | */ |
| 5245 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 5246 | { |
| 5247 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5248 | PIN_BASED_EXT_INTR_MASK; |
| 5249 | } |
| 5250 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 5251 | /* |
| 5252 | * In nested virtualization, check if L1 has set |
| 5253 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 5254 | */ |
| 5255 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 5256 | { |
| 5257 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 5258 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 5259 | } |
| 5260 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5261 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 5262 | { |
| 5263 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5264 | PIN_BASED_NMI_EXITING; |
| 5265 | } |
| 5266 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5267 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5268 | { |
| 5269 | u32 cpu_based_vm_exec_control; |
Jan Kiszka | 730dca4 | 2013-04-28 10:50:52 +0200 | [diff] [blame] | 5270 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5271 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5272 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 5273 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5274 | } |
| 5275 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5276 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5277 | { |
| 5278 | u32 cpu_based_vm_exec_control; |
| 5279 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5280 | if (!cpu_has_virtual_nmis() || |
| 5281 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 5282 | enable_irq_window(vcpu); |
| 5283 | return; |
| 5284 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 5285 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5286 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5287 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 5288 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5289 | } |
| 5290 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5291 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5292 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5293 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5294 | uint32_t intr; |
| 5295 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5296 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5297 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5298 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 5299 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5300 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5301 | int inc_eip = 0; |
| 5302 | if (vcpu->arch.interrupt.soft) |
| 5303 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 5304 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5305 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5306 | return; |
| 5307 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5308 | intr = irq | INTR_INFO_VALID_MASK; |
| 5309 | if (vcpu->arch.interrupt.soft) { |
| 5310 | intr |= INTR_TYPE_SOFT_INTR; |
| 5311 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 5312 | vmx->vcpu.arch.event_exit_inst_len); |
| 5313 | } else |
| 5314 | intr |= INTR_TYPE_EXT_INTR; |
| 5315 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5316 | } |
| 5317 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5318 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 5319 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5320 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5321 | |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5322 | if (!is_guest_mode(vcpu)) { |
| 5323 | if (!cpu_has_virtual_nmis()) { |
| 5324 | /* |
| 5325 | * Tracking the NMI-blocked state in software is built upon |
| 5326 | * finding the next open IRQ window. This, in turn, depends on |
| 5327 | * well-behaving guests: They have to keep IRQs disabled at |
| 5328 | * least as long as the NMI handler runs. Otherwise we may |
| 5329 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 5330 | * highly unlikely, we can live with the residual risk. |
| 5331 | */ |
| 5332 | vmx->soft_vnmi_blocked = 1; |
| 5333 | vmx->vnmi_blocked_time = 0; |
| 5334 | } |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 5335 | |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5336 | ++vcpu->stat.nmi_injections; |
| 5337 | vmx->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5338 | } |
| 5339 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5340 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5341 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5342 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5343 | return; |
| 5344 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5345 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5346 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 5347 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5348 | } |
| 5349 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5350 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 5351 | { |
| 5352 | if (!cpu_has_virtual_nmis()) |
| 5353 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5354 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 5355 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 5356 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 5360 | { |
| 5361 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5362 | |
| 5363 | if (!cpu_has_virtual_nmis()) { |
| 5364 | if (vmx->soft_vnmi_blocked != masked) { |
| 5365 | vmx->soft_vnmi_blocked = masked; |
| 5366 | vmx->vnmi_blocked_time = 0; |
| 5367 | } |
| 5368 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5369 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5370 | if (masked) |
| 5371 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5372 | GUEST_INTR_STATE_NMI); |
| 5373 | else |
| 5374 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5375 | GUEST_INTR_STATE_NMI); |
| 5376 | } |
| 5377 | } |
| 5378 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5379 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 5380 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5381 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 5382 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5383 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5384 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
| 5385 | return 0; |
| 5386 | |
| 5387 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5388 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 5389 | | GUEST_INTR_STATE_NMI)); |
| 5390 | } |
| 5391 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5392 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5393 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5394 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 5395 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 5396 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5397 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5398 | } |
| 5399 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5400 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5401 | { |
| 5402 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5403 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5404 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 5405 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5406 | if (ret) |
| 5407 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 5408 | kvm->arch.tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5409 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5410 | } |
| 5411 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5412 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5413 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5414 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5415 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5416 | /* |
| 5417 | * Update instruction length as we may reinject the exception |
| 5418 | * from user space while in guest debugging mode. |
| 5419 | */ |
| 5420 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 5421 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5422 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5423 | return false; |
| 5424 | /* fall through */ |
| 5425 | case DB_VECTOR: |
| 5426 | if (vcpu->guest_debug & |
| 5427 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5428 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5429 | /* fall through */ |
| 5430 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5431 | case OF_VECTOR: |
| 5432 | case BR_VECTOR: |
| 5433 | case UD_VECTOR: |
| 5434 | case DF_VECTOR: |
| 5435 | case SS_VECTOR: |
| 5436 | case GP_VECTOR: |
| 5437 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5438 | return true; |
| 5439 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5440 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5441 | return false; |
| 5442 | } |
| 5443 | |
| 5444 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 5445 | int vec, u32 err_code) |
| 5446 | { |
| 5447 | /* |
| 5448 | * Instruction with address size override prefix opcode 0x67 |
| 5449 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 5450 | */ |
| 5451 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 5452 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 5453 | if (vcpu->arch.halt_request) { |
| 5454 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 5455 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5456 | } |
| 5457 | return 1; |
| 5458 | } |
| 5459 | return 0; |
| 5460 | } |
| 5461 | |
| 5462 | /* |
| 5463 | * Forward all other exceptions that are valid in real mode. |
| 5464 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 5465 | * the required debugging infrastructure rework. |
| 5466 | */ |
| 5467 | kvm_queue_exception(vcpu, vec); |
| 5468 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5469 | } |
| 5470 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5471 | /* |
| 5472 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 5473 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 5474 | * We pass a fake environment to the machine check handler because we want |
| 5475 | * the guest to be always treated like user space, no matter what context |
| 5476 | * it used internally. |
| 5477 | */ |
| 5478 | static void kvm_machine_check(void) |
| 5479 | { |
| 5480 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 5481 | struct pt_regs regs = { |
| 5482 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 5483 | .flags = X86_EFLAGS_IF, |
| 5484 | }; |
| 5485 | |
| 5486 | do_machine_check(®s, 0); |
| 5487 | #endif |
| 5488 | } |
| 5489 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5490 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5491 | { |
| 5492 | /* already handled by vcpu_run */ |
| 5493 | return 1; |
| 5494 | } |
| 5495 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5496 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5497 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5498 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5499 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5500 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5501 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5502 | u32 vect_info; |
| 5503 | enum emulation_result er; |
| 5504 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5505 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 5506 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5507 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5508 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5509 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5510 | |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 5511 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 5512 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5513 | |
| 5514 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 5515 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5516 | return 1; |
| 5517 | } |
| 5518 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5519 | if (is_invalid_opcode(intr_info)) { |
Jan Kiszka | ae1f576 | 2015-03-09 20:56:43 +0100 | [diff] [blame] | 5520 | if (is_guest_mode(vcpu)) { |
| 5521 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5522 | return 1; |
| 5523 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5524 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Liran Alon | c0a4c22 | 2017-11-05 16:56:32 +0200 | [diff] [blame] | 5525 | if (er == EMULATE_USER_EXIT) |
| 5526 | return 0; |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5527 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 5528 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5529 | return 1; |
| 5530 | } |
| 5531 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5532 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 5533 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5534 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5535 | |
| 5536 | /* |
| 5537 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 5538 | * MMIO, it is better to report an internal error. |
| 5539 | * See the comments in vmx_handle_exit. |
| 5540 | */ |
| 5541 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 5542 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 5543 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5544 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5545 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5546 | vcpu->run->internal.data[0] = vect_info; |
| 5547 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5548 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5549 | return 0; |
| 5550 | } |
| 5551 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5552 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5553 | /* EPT won't cause page fault directly */ |
Julia Lawall | cf3ace7 | 2011-08-02 12:34:57 +0200 | [diff] [blame] | 5554 | BUG_ON(enable_ept); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5555 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5556 | trace_kvm_page_fault(cr2, error_code); |
| 5557 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 5558 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 5559 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 5560 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5561 | } |
| 5562 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5563 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5564 | |
| 5565 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 5566 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 5567 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5568 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 5569 | case AC_VECTOR: |
| 5570 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 5571 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5572 | case DB_VECTOR: |
| 5573 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 5574 | if (!(vcpu->guest_debug & |
| 5575 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 5576 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5577 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 5578 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
| 5579 | skip_emulated_instruction(vcpu); |
| 5580 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5581 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5582 | return 1; |
| 5583 | } |
| 5584 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 5585 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 5586 | /* fall through */ |
| 5587 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5588 | /* |
| 5589 | * Update instruction length as we may reinject #BP from |
| 5590 | * user space while in guest debugging mode. Reading it for |
| 5591 | * #DB as well causes no harm, it is not used in that case. |
| 5592 | */ |
| 5593 | vmx->vcpu.arch.event_exit_inst_len = |
| 5594 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5595 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 5596 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5597 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 5598 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5599 | break; |
| 5600 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5601 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 5602 | kvm_run->ex.exception = ex_no; |
| 5603 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5604 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5605 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5606 | return 0; |
| 5607 | } |
| 5608 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5609 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5610 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 5611 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5612 | return 1; |
| 5613 | } |
| 5614 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5615 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5616 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5617 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5618 | return 0; |
| 5619 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5620 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5621 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5622 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5623 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 5624 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5625 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5626 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5627 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5628 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5629 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5630 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5631 | ++vcpu->stat.io_exits; |
| 5632 | |
| 5633 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5634 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5635 | |
| 5636 | port = exit_qualification >> 16; |
| 5637 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 5638 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5639 | |
| 5640 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5641 | } |
| 5642 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5643 | static void |
| 5644 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5645 | { |
| 5646 | /* |
| 5647 | * Patch in the VMCALL instruction: |
| 5648 | */ |
| 5649 | hypercall[0] = 0x0f; |
| 5650 | hypercall[1] = 0x01; |
| 5651 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5652 | } |
| 5653 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5654 | static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5655 | { |
| 5656 | unsigned long always_on = VMXON_CR0_ALWAYSON; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5657 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5658 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5659 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5660 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 5661 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 5662 | always_on &= ~(X86_CR0_PE | X86_CR0_PG); |
| 5663 | return (val & always_on) == always_on; |
| 5664 | } |
| 5665 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 5666 | /* 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] | 5667 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 5668 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5669 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5670 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5671 | unsigned long orig_val = val; |
| 5672 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5673 | /* |
| 5674 | * We get here when L2 changed cr0 in a way that did not change |
| 5675 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5676 | * but did change L0 shadowed bits. So we first calculate the |
| 5677 | * effective cr0 value that L1 would like to write into the |
| 5678 | * hardware. It consists of the L2-owned bits from the new |
| 5679 | * 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] | 5680 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5681 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 5682 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 5683 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5684 | if (!nested_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5685 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5686 | |
| 5687 | if (kvm_set_cr0(vcpu, val)) |
| 5688 | return 1; |
| 5689 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5690 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5691 | } else { |
| 5692 | if (to_vmx(vcpu)->nested.vmxon && |
| 5693 | ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)) |
| 5694 | return 1; |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5695 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5696 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 5700 | { |
| 5701 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5702 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5703 | unsigned long orig_val = val; |
| 5704 | |
| 5705 | /* analogously to handle_set_cr0 */ |
| 5706 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 5707 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 5708 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5709 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5710 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5711 | return 0; |
| 5712 | } else |
| 5713 | return kvm_set_cr4(vcpu, val); |
| 5714 | } |
| 5715 | |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 5716 | /* called to set cr0 as appropriate for clts instruction exit. */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5717 | static void handle_clts(struct kvm_vcpu *vcpu) |
| 5718 | { |
| 5719 | if (is_guest_mode(vcpu)) { |
| 5720 | /* |
| 5721 | * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS |
| 5722 | * but we did (!fpu_active). We need to keep GUEST_CR0.TS on, |
| 5723 | * just pretend it's off (also in arch.cr0 for fpu_activate). |
| 5724 | */ |
| 5725 | vmcs_writel(CR0_READ_SHADOW, |
| 5726 | vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS); |
| 5727 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 5728 | } else |
| 5729 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
| 5730 | } |
| 5731 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5732 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5733 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5734 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5735 | int cr; |
| 5736 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 5737 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5738 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5739 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5740 | cr = exit_qualification & 15; |
| 5741 | reg = (exit_qualification >> 8) & 15; |
| 5742 | switch ((exit_qualification >> 4) & 3) { |
| 5743 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5744 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5745 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5746 | switch (cr) { |
| 5747 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5748 | err = handle_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5749 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5750 | return 1; |
| 5751 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 5752 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5753 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5754 | return 1; |
| 5755 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5756 | err = handle_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5757 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5758 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5759 | case 8: { |
| 5760 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5761 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 5762 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5763 | kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5764 | if (lapic_in_kernel(vcpu)) |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5765 | return 1; |
| 5766 | if (cr8_prev <= cr8) |
| 5767 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5768 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5769 | return 0; |
| 5770 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 5771 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5772 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5773 | case 2: /* clts */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5774 | handle_clts(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5775 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5776 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 5777 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5778 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5779 | case 1: /*mov from cr*/ |
| 5780 | switch (cr) { |
| 5781 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 5782 | val = kvm_read_cr3(vcpu); |
| 5783 | kvm_register_write(vcpu, reg, val); |
| 5784 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5785 | skip_emulated_instruction(vcpu); |
| 5786 | return 1; |
| 5787 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5788 | val = kvm_get_cr8(vcpu); |
| 5789 | kvm_register_write(vcpu, reg, val); |
| 5790 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5791 | skip_emulated_instruction(vcpu); |
| 5792 | return 1; |
| 5793 | } |
| 5794 | break; |
| 5795 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 5796 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5797 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 5798 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5799 | |
| 5800 | skip_emulated_instruction(vcpu); |
| 5801 | return 1; |
| 5802 | default: |
| 5803 | break; |
| 5804 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5805 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 5806 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5807 | (int)(exit_qualification >> 4) & 3, cr); |
| 5808 | return 0; |
| 5809 | } |
| 5810 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5811 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5812 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5813 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5814 | int dr, dr7, reg; |
| 5815 | |
| 5816 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5817 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 5818 | |
| 5819 | /* First, if DR does not exist, trigger UD */ |
| 5820 | if (!kvm_require_dr(vcpu, dr)) |
| 5821 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5822 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 5823 | /* 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] | 5824 | if (!kvm_require_cpl(vcpu, 0)) |
| 5825 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5826 | dr7 = vmcs_readl(GUEST_DR7); |
| 5827 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5828 | /* |
| 5829 | * As the vm-exit takes precedence over the debug trap, we |
| 5830 | * need to emulate the latter, either for the host or the |
| 5831 | * guest debugging itself. |
| 5832 | */ |
| 5833 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5834 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5835 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 5836 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5837 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 5838 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5839 | return 0; |
| 5840 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 5841 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5842 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5843 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5844 | return 1; |
| 5845 | } |
| 5846 | } |
| 5847 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5848 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 5849 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 5850 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5851 | |
| 5852 | /* |
| 5853 | * No more DR vmexits; force a reload of the debug registers |
| 5854 | * and reenter on this instruction. The next vmexit will |
| 5855 | * retrieve the full state of the debug registers. |
| 5856 | */ |
| 5857 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 5858 | return 1; |
| 5859 | } |
| 5860 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5861 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 5862 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5863 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 5864 | |
| 5865 | if (kvm_get_dr(vcpu, dr, &val)) |
| 5866 | return 1; |
| 5867 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5868 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 5869 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 5870 | return 1; |
| 5871 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5872 | skip_emulated_instruction(vcpu); |
| 5873 | return 1; |
| 5874 | } |
| 5875 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 5876 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 5877 | { |
| 5878 | return vcpu->arch.dr6; |
| 5879 | } |
| 5880 | |
| 5881 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 5882 | { |
| 5883 | } |
| 5884 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5885 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 5886 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5887 | get_debugreg(vcpu->arch.db[0], 0); |
| 5888 | get_debugreg(vcpu->arch.db[1], 1); |
| 5889 | get_debugreg(vcpu->arch.db[2], 2); |
| 5890 | get_debugreg(vcpu->arch.db[3], 3); |
| 5891 | get_debugreg(vcpu->arch.dr6, 6); |
| 5892 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 5893 | |
| 5894 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 5895 | 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] | 5896 | } |
| 5897 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5898 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 5899 | { |
| 5900 | vmcs_writel(GUEST_DR7, val); |
| 5901 | } |
| 5902 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5903 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5904 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 5905 | kvm_emulate_cpuid(vcpu); |
| 5906 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5907 | } |
| 5908 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5909 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5910 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5911 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 5912 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5913 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 5914 | msr_info.index = ecx; |
| 5915 | msr_info.host_initiated = false; |
| 5916 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5917 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 5918 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5919 | return 1; |
| 5920 | } |
| 5921 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 5922 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5923 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5924 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 5925 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 5926 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5927 | skip_emulated_instruction(vcpu); |
| 5928 | return 1; |
| 5929 | } |
| 5930 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5931 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5932 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 5933 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5934 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 5935 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 5936 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5937 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 5938 | msr.data = data; |
| 5939 | msr.index = ecx; |
| 5940 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 5941 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5942 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 5943 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5944 | return 1; |
| 5945 | } |
| 5946 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5947 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5948 | skip_emulated_instruction(vcpu); |
| 5949 | return 1; |
| 5950 | } |
| 5951 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5952 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5953 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5954 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5955 | return 1; |
| 5956 | } |
| 5957 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5958 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5959 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5960 | u32 cpu_based_vm_exec_control; |
| 5961 | |
| 5962 | /* clear pending irq */ |
| 5963 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5964 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 5965 | 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] | 5966 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5967 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5968 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 5969 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5970 | return 1; |
| 5971 | } |
| 5972 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5973 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5974 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 5975 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5976 | } |
| 5977 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5978 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5979 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 5980 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5981 | } |
| 5982 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5983 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 5984 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5985 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5986 | } |
| 5987 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5988 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5989 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5990 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5991 | |
| 5992 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 5993 | skip_emulated_instruction(vcpu); |
| 5994 | return 1; |
| 5995 | } |
| 5996 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 5997 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 5998 | { |
| 5999 | int err; |
| 6000 | |
| 6001 | err = kvm_rdpmc(vcpu); |
| 6002 | kvm_complete_insn_gp(vcpu, err); |
| 6003 | |
| 6004 | return 1; |
| 6005 | } |
| 6006 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6007 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6008 | { |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6009 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6010 | return 1; |
| 6011 | } |
| 6012 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 6013 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 6014 | { |
| 6015 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 6016 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 6017 | |
| 6018 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 6019 | skip_emulated_instruction(vcpu); |
| 6020 | return 1; |
| 6021 | } |
| 6022 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6023 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 6024 | { |
| 6025 | skip_emulated_instruction(vcpu); |
| 6026 | WARN(1, "this should never happen\n"); |
| 6027 | return 1; |
| 6028 | } |
| 6029 | |
| 6030 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 6031 | { |
| 6032 | skip_emulated_instruction(vcpu); |
| 6033 | WARN(1, "this should never happen\n"); |
| 6034 | return 1; |
| 6035 | } |
| 6036 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6037 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6038 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 6039 | if (likely(fasteoi)) { |
| 6040 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6041 | int access_type, offset; |
| 6042 | |
| 6043 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 6044 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 6045 | /* |
| 6046 | * Sane guest uses MOV to write EOI, with written value |
| 6047 | * not cared. So make a short-circuit here by avoiding |
| 6048 | * heavy instruction emulation. |
| 6049 | */ |
| 6050 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 6051 | (offset == APIC_EOI)) { |
| 6052 | kvm_lapic_set_eoi(vcpu); |
| 6053 | skip_emulated_instruction(vcpu); |
| 6054 | return 1; |
| 6055 | } |
| 6056 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6057 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6058 | } |
| 6059 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6060 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 6061 | { |
| 6062 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6063 | int vector = exit_qualification & 0xff; |
| 6064 | |
| 6065 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 6066 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 6067 | return 1; |
| 6068 | } |
| 6069 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 6070 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 6071 | { |
| 6072 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6073 | u32 offset = exit_qualification & 0xfff; |
| 6074 | |
| 6075 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 6076 | kvm_apic_write_nodecode(vcpu, offset); |
| 6077 | return 1; |
| 6078 | } |
| 6079 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6080 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6081 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6082 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6083 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6084 | bool has_error_code = false; |
| 6085 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6086 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6087 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6088 | |
| 6089 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6090 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6091 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6092 | |
| 6093 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6094 | |
| 6095 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6096 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 6097 | switch (type) { |
| 6098 | case INTR_TYPE_NMI_INTR: |
| 6099 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6100 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6101 | break; |
| 6102 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6103 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6104 | kvm_clear_interrupt_queue(vcpu); |
| 6105 | break; |
| 6106 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6107 | if (vmx->idt_vectoring_info & |
| 6108 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 6109 | has_error_code = true; |
| 6110 | error_code = |
| 6111 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6112 | } |
| 6113 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6114 | case INTR_TYPE_SOFT_EXCEPTION: |
| 6115 | kvm_clear_exception_queue(vcpu); |
| 6116 | break; |
| 6117 | default: |
| 6118 | break; |
| 6119 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6120 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6121 | tss_selector = exit_qualification; |
| 6122 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6123 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 6124 | type != INTR_TYPE_EXT_INTR && |
| 6125 | type != INTR_TYPE_NMI_INTR)) |
| 6126 | skip_emulated_instruction(vcpu); |
| 6127 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6128 | if (kvm_task_switch(vcpu, tss_selector, |
| 6129 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 6130 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6131 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6132 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6133 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6134 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6135 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6136 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6137 | /* |
| 6138 | * TODO: What about debug traps on tss switch? |
| 6139 | * Are we supposed to inject them and update dr6? |
| 6140 | */ |
| 6141 | |
| 6142 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6143 | } |
| 6144 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6145 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6146 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6147 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6148 | gpa_t gpa; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6149 | u32 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6150 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6151 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6152 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6153 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6154 | gla_validity = (exit_qualification >> 7) & 0x3; |
Liang Li | 72e0ae5 | 2016-08-18 15:49:19 +0800 | [diff] [blame] | 6155 | if (gla_validity == 0x2) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6156 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 6157 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 6158 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6159 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6160 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 6161 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6162 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6163 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 6164 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6165 | } |
| 6166 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6167 | /* |
| 6168 | * EPT violation happened while executing iret from NMI, |
| 6169 | * "blocked by NMI" bit has to be set before next VM entry. |
| 6170 | * There are errata that may cause this bit to not be set: |
| 6171 | * AAK134, BY25. |
| 6172 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 6173 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 6174 | cpu_has_virtual_nmis() && |
| 6175 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6176 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 6177 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6178 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6179 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6180 | |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6181 | /* it is a read fault? */ |
| 6182 | error_code = (exit_qualification << 2) & PFERR_USER_MASK; |
| 6183 | /* it is a write fault? */ |
| 6184 | error_code |= exit_qualification & PFERR_WRITE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6185 | /* It is a fetch fault? */ |
Tiejun Chen | 81ed33e | 2014-11-18 17:12:56 +0800 | [diff] [blame] | 6186 | error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6187 | /* ept page table is present? */ |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6188 | error_code |= (exit_qualification & 0x38) != 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6189 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6190 | vcpu->arch.exit_qualification = exit_qualification; |
| 6191 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6192 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6193 | } |
| 6194 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6195 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6196 | { |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6197 | int ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6198 | gpa_t gpa; |
| 6199 | |
| 6200 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 6201 | 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] | 6202 | skip_emulated_instruction(vcpu); |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 6203 | trace_kvm_fast_mmio(gpa); |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 6204 | return 1; |
| 6205 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6206 | |
Paolo Bonzini | 450869d | 2015-11-04 13:41:21 +0100 | [diff] [blame] | 6207 | ret = handle_mmio_page_fault(vcpu, gpa, true); |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6208 | if (likely(ret == RET_MMIO_PF_EMULATE)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6209 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 6210 | EMULATE_DONE; |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 6211 | |
| 6212 | if (unlikely(ret == RET_MMIO_PF_INVALID)) |
| 6213 | return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0); |
| 6214 | |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6215 | if (unlikely(ret == RET_MMIO_PF_RETRY)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6216 | return 1; |
| 6217 | |
| 6218 | /* It is the real ept misconfig */ |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6219 | WARN_ON(1); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6220 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6221 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6222 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6223 | |
| 6224 | return 0; |
| 6225 | } |
| 6226 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6227 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6228 | { |
| 6229 | u32 cpu_based_vm_exec_control; |
| 6230 | |
| 6231 | /* clear pending NMI */ |
| 6232 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6233 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 6234 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 6235 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6236 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6237 | |
| 6238 | return 1; |
| 6239 | } |
| 6240 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6241 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6242 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 6243 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6244 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6245 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6246 | u32 cpu_exec_ctrl; |
| 6247 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 6248 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6249 | |
| 6250 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6251 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6252 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 6253 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 6254 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6255 | return handle_interrupt_window(&vmx->vcpu); |
| 6256 | |
Avi Kivity | de87dcd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 6257 | if (test_bit(KVM_REQ_EVENT, &vcpu->requests)) |
| 6258 | return 1; |
| 6259 | |
Liran Alon | 114de9b | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 6260 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6261 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 6262 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 6263 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6264 | ret = 0; |
| 6265 | goto out; |
| 6266 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 6267 | |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 6268 | if (err != EMULATE_DONE) { |
| 6269 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6270 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6271 | vcpu->run->internal.ndata = 0; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 6272 | return 0; |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 6273 | } |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6274 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6275 | if (vcpu->arch.halt_request) { |
| 6276 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6277 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6278 | goto out; |
| 6279 | } |
| 6280 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6281 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6282 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6283 | if (need_resched()) |
| 6284 | schedule(); |
| 6285 | } |
| 6286 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6287 | out: |
| 6288 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6289 | } |
| 6290 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6291 | static int __grow_ple_window(int val) |
| 6292 | { |
| 6293 | if (ple_window_grow < 1) |
| 6294 | return ple_window; |
| 6295 | |
| 6296 | val = min(val, ple_window_actual_max); |
| 6297 | |
| 6298 | if (ple_window_grow < ple_window) |
| 6299 | val *= ple_window_grow; |
| 6300 | else |
| 6301 | val += ple_window_grow; |
| 6302 | |
| 6303 | return val; |
| 6304 | } |
| 6305 | |
| 6306 | static int __shrink_ple_window(int val, int modifier, int minimum) |
| 6307 | { |
| 6308 | if (modifier < 1) |
| 6309 | return ple_window; |
| 6310 | |
| 6311 | if (modifier < ple_window) |
| 6312 | val /= modifier; |
| 6313 | else |
| 6314 | val -= modifier; |
| 6315 | |
| 6316 | return max(val, minimum); |
| 6317 | } |
| 6318 | |
| 6319 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 6320 | { |
| 6321 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6322 | int old = vmx->ple_window; |
| 6323 | |
| 6324 | vmx->ple_window = __grow_ple_window(old); |
| 6325 | |
| 6326 | if (vmx->ple_window != old) |
| 6327 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6328 | |
| 6329 | 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] | 6330 | } |
| 6331 | |
| 6332 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 6333 | { |
| 6334 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6335 | int old = vmx->ple_window; |
| 6336 | |
| 6337 | vmx->ple_window = __shrink_ple_window(old, |
| 6338 | ple_window_shrink, ple_window); |
| 6339 | |
| 6340 | if (vmx->ple_window != old) |
| 6341 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6342 | |
| 6343 | 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] | 6344 | } |
| 6345 | |
| 6346 | /* |
| 6347 | * ple_window_actual_max is computed to be one grow_ple_window() below |
| 6348 | * ple_window_max. (See __grow_ple_window for the reason.) |
| 6349 | * This prevents overflows, because ple_window_max is int. |
| 6350 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in |
| 6351 | * this process. |
| 6352 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. |
| 6353 | */ |
| 6354 | static void update_ple_window_actual_max(void) |
| 6355 | { |
| 6356 | ple_window_actual_max = |
| 6357 | __shrink_ple_window(max(ple_window_max, ple_window), |
| 6358 | ple_window_grow, INT_MIN); |
| 6359 | } |
| 6360 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6361 | /* |
| 6362 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 6363 | */ |
| 6364 | static void wakeup_handler(void) |
| 6365 | { |
| 6366 | struct kvm_vcpu *vcpu; |
| 6367 | int cpu = smp_processor_id(); |
| 6368 | |
| 6369 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6370 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 6371 | blocked_vcpu_list) { |
| 6372 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 6373 | |
| 6374 | if (pi_test_on(pi_desc) == 1) |
| 6375 | kvm_vcpu_kick(vcpu); |
| 6376 | } |
| 6377 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6378 | } |
| 6379 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6380 | static __init int hardware_setup(void) |
| 6381 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6382 | int r = -ENOMEM, i, msr; |
| 6383 | |
| 6384 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 6385 | |
| 6386 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 6387 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 6388 | |
| 6389 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6390 | if (!vmx_io_bitmap_a) |
| 6391 | return r; |
| 6392 | |
| 6393 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6394 | if (!vmx_io_bitmap_b) |
| 6395 | goto out; |
| 6396 | |
| 6397 | vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6398 | if (!vmx_msr_bitmap_legacy) |
| 6399 | goto out1; |
| 6400 | |
| 6401 | vmx_msr_bitmap_legacy_x2apic = |
| 6402 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6403 | if (!vmx_msr_bitmap_legacy_x2apic) |
| 6404 | goto out2; |
| 6405 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6406 | vmx_msr_bitmap_legacy_x2apic_apicv_inactive = |
| 6407 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6408 | if (!vmx_msr_bitmap_legacy_x2apic_apicv_inactive) |
| 6409 | goto out3; |
| 6410 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6411 | vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6412 | if (!vmx_msr_bitmap_longmode) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6413 | goto out4; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6414 | |
| 6415 | vmx_msr_bitmap_longmode_x2apic = |
| 6416 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6417 | if (!vmx_msr_bitmap_longmode_x2apic) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6418 | goto out5; |
| 6419 | |
| 6420 | vmx_msr_bitmap_longmode_x2apic_apicv_inactive = |
| 6421 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6422 | if (!vmx_msr_bitmap_longmode_x2apic_apicv_inactive) |
| 6423 | goto out6; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 6424 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6425 | vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6426 | if (!vmx_vmread_bitmap) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6427 | goto out7; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6428 | |
| 6429 | vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6430 | if (!vmx_vmwrite_bitmap) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6431 | goto out8; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6432 | |
| 6433 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 6434 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 6435 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6436 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6437 | |
| 6438 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
| 6439 | |
| 6440 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 6441 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
| 6442 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6443 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 6444 | r = -EIO; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6445 | goto out9; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6446 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6447 | |
| 6448 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 6449 | kvm_enable_efer_bits(EFER_NX); |
| 6450 | |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 6451 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 6452 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6453 | enable_vpid = 0; |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 6454 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6455 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6456 | enable_shadow_vmcs = 0; |
| 6457 | if (enable_shadow_vmcs) |
| 6458 | init_vmcs_shadow_fields(); |
| 6459 | |
| 6460 | if (!cpu_has_vmx_ept() || |
| 6461 | !cpu_has_vmx_ept_4levels()) { |
| 6462 | enable_ept = 0; |
| 6463 | enable_unrestricted_guest = 0; |
| 6464 | enable_ept_ad_bits = 0; |
| 6465 | } |
| 6466 | |
| 6467 | if (!cpu_has_vmx_ept_ad_bits()) |
| 6468 | enable_ept_ad_bits = 0; |
| 6469 | |
| 6470 | if (!cpu_has_vmx_unrestricted_guest()) |
| 6471 | enable_unrestricted_guest = 0; |
| 6472 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6473 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6474 | flexpriority_enabled = 0; |
| 6475 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6476 | /* |
| 6477 | * set_apic_access_page_addr() is used to reload apic access |
| 6478 | * page upon invalidation. No need to do anything if not |
| 6479 | * using the APIC_ACCESS_ADDR VMCS field. |
| 6480 | */ |
| 6481 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6482 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6483 | |
| 6484 | if (!cpu_has_vmx_tpr_shadow()) |
| 6485 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 6486 | |
| 6487 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 6488 | kvm_disable_largepages(); |
| 6489 | |
| 6490 | if (!cpu_has_vmx_ple()) |
| 6491 | ple_gap = 0; |
| 6492 | |
| 6493 | if (!cpu_has_vmx_apicv()) |
| 6494 | enable_apicv = 0; |
| 6495 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 6496 | if (cpu_has_vmx_tsc_scaling()) { |
| 6497 | kvm_has_tsc_control = true; |
| 6498 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 6499 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 6500 | } |
| 6501 | |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6502 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 6503 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 6504 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 6505 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 6506 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 6507 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6508 | |
| 6509 | memcpy(vmx_msr_bitmap_legacy_x2apic, |
| 6510 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
| 6511 | memcpy(vmx_msr_bitmap_longmode_x2apic, |
| 6512 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6513 | memcpy(vmx_msr_bitmap_legacy_x2apic_apicv_inactive, |
| 6514 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
| 6515 | memcpy(vmx_msr_bitmap_longmode_x2apic_apicv_inactive, |
| 6516 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6517 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 6518 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 6519 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6520 | /* |
| 6521 | * enable_apicv && kvm_vcpu_apicv_active() |
| 6522 | */ |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6523 | for (msr = 0x800; msr <= 0x8ff; msr++) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6524 | vmx_disable_intercept_msr_read_x2apic(msr, true); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6525 | |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6526 | /* TMCCT */ |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6527 | vmx_enable_intercept_msr_read_x2apic(0x839, true); |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6528 | /* TPR */ |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6529 | vmx_disable_intercept_msr_write_x2apic(0x808, true); |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6530 | /* EOI */ |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6531 | vmx_disable_intercept_msr_write_x2apic(0x80b, true); |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6532 | /* SELF-IPI */ |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6533 | vmx_disable_intercept_msr_write_x2apic(0x83f, true); |
| 6534 | |
| 6535 | /* |
| 6536 | * (enable_apicv && !kvm_vcpu_apicv_active()) || |
| 6537 | * !enable_apicv |
| 6538 | */ |
| 6539 | /* TPR */ |
| 6540 | vmx_disable_intercept_msr_read_x2apic(0x808, false); |
| 6541 | vmx_disable_intercept_msr_write_x2apic(0x808, false); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6542 | |
| 6543 | if (enable_ept) { |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6544 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6545 | (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull, |
| 6546 | (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull, |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6547 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 6548 | cpu_has_vmx_ept_execute_only() ? |
| 6549 | 0ull : VMX_EPT_READABLE_MASK); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6550 | ept_set_mmio_spte_mask(); |
| 6551 | kvm_enable_tdp(); |
| 6552 | } else |
| 6553 | kvm_disable_tdp(); |
| 6554 | |
| 6555 | update_ple_window_actual_max(); |
| 6556 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6557 | /* |
| 6558 | * Only enable PML when hardware supports PML feature, and both EPT |
| 6559 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 6560 | */ |
| 6561 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 6562 | enable_pml = 0; |
| 6563 | |
| 6564 | if (!enable_pml) { |
| 6565 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 6566 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 6567 | kvm_x86_ops->flush_log_dirty = NULL; |
| 6568 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 6569 | } |
| 6570 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6571 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 6572 | u64 vmx_msr; |
| 6573 | |
| 6574 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 6575 | cpu_preemption_timer_multi = |
| 6576 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 6577 | } else { |
| 6578 | kvm_x86_ops->set_hv_timer = NULL; |
| 6579 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 6580 | } |
| 6581 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6582 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
| 6583 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 6584 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 6585 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6586 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6587 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6588 | out9: |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6589 | free_page((unsigned long)vmx_vmwrite_bitmap); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6590 | out8: |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6591 | free_page((unsigned long)vmx_vmread_bitmap); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6592 | out7: |
| 6593 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv_inactive); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 6594 | out6: |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6595 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6596 | out5: |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6597 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6598 | out4: |
| 6599 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv_inactive); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6600 | out3: |
| 6601 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic); |
| 6602 | out2: |
| 6603 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 6604 | out1: |
| 6605 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6606 | out: |
| 6607 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6608 | |
| 6609 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6610 | } |
| 6611 | |
| 6612 | static __exit void hardware_unsetup(void) |
| 6613 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6614 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6615 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv_inactive); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6616 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6617 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv_inactive); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6618 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 6619 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
| 6620 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6621 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6622 | free_page((unsigned long)vmx_vmwrite_bitmap); |
| 6623 | free_page((unsigned long)vmx_vmread_bitmap); |
| 6624 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6625 | free_kvm_area(); |
| 6626 | } |
| 6627 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6628 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6629 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 6630 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 6631 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 6632 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6633 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6634 | if (ple_gap) |
| 6635 | grow_ple_window(vcpu); |
| 6636 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6637 | skip_emulated_instruction(vcpu); |
| 6638 | kvm_vcpu_on_spin(vcpu); |
| 6639 | |
| 6640 | return 1; |
| 6641 | } |
| 6642 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6643 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6644 | { |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6645 | skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6646 | return 1; |
| 6647 | } |
| 6648 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6649 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 6650 | { |
| 6651 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 6652 | return handle_nop(vcpu); |
| 6653 | } |
| 6654 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 6655 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 6656 | { |
| 6657 | return 1; |
| 6658 | } |
| 6659 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6660 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 6661 | { |
| 6662 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 6663 | return handle_nop(vcpu); |
| 6664 | } |
| 6665 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6666 | /* |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6667 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. |
| 6668 | * We could reuse a single VMCS for all the L2 guests, but we also want the |
| 6669 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this |
| 6670 | * allows keeping them loaded on the processor, and in the future will allow |
| 6671 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on |
| 6672 | * every entry if they never change. |
| 6673 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE |
| 6674 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. |
| 6675 | * |
| 6676 | * The following functions allocate and free a vmcs02 in this pool. |
| 6677 | */ |
| 6678 | |
| 6679 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ |
| 6680 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) |
| 6681 | { |
| 6682 | struct vmcs02_list *item; |
| 6683 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 6684 | if (item->vmptr == vmx->nested.current_vmptr) { |
| 6685 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 6686 | return &item->vmcs02; |
| 6687 | } |
| 6688 | |
| 6689 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { |
| 6690 | /* Recycle the least recently used VMCS. */ |
Geliang Tang | d74c0e6 | 2016-01-01 19:47:14 +0800 | [diff] [blame] | 6691 | item = list_last_entry(&vmx->nested.vmcs02_pool, |
| 6692 | struct vmcs02_list, list); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6693 | item->vmptr = vmx->nested.current_vmptr; |
| 6694 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 6695 | return &item->vmcs02; |
| 6696 | } |
| 6697 | |
| 6698 | /* Create a new VMCS */ |
Ioan Orghici | 0fa24ce | 2013-03-10 15:46:00 +0200 | [diff] [blame] | 6699 | item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6700 | if (!item) |
| 6701 | return NULL; |
| 6702 | item->vmcs02.vmcs = alloc_vmcs(); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 6703 | item->vmcs02.shadow_vmcs = NULL; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6704 | if (!item->vmcs02.vmcs) { |
| 6705 | kfree(item); |
| 6706 | return NULL; |
| 6707 | } |
| 6708 | loaded_vmcs_init(&item->vmcs02); |
| 6709 | item->vmptr = vmx->nested.current_vmptr; |
| 6710 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); |
| 6711 | vmx->nested.vmcs02_num++; |
| 6712 | return &item->vmcs02; |
| 6713 | } |
| 6714 | |
| 6715 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ |
| 6716 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 6717 | { |
| 6718 | struct vmcs02_list *item; |
| 6719 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 6720 | if (item->vmptr == vmptr) { |
| 6721 | free_loaded_vmcs(&item->vmcs02); |
| 6722 | list_del(&item->list); |
| 6723 | kfree(item); |
| 6724 | vmx->nested.vmcs02_num--; |
| 6725 | return; |
| 6726 | } |
| 6727 | } |
| 6728 | |
| 6729 | /* |
| 6730 | * Free all VMCSs saved for this vcpu, except the one pointed by |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6731 | * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs |
| 6732 | * must be &vmx->vmcs01. |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6733 | */ |
| 6734 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) |
| 6735 | { |
| 6736 | struct vmcs02_list *item, *n; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6737 | |
| 6738 | WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6739 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6740 | /* |
| 6741 | * Something will leak if the above WARN triggers. Better than |
| 6742 | * a use-after-free. |
| 6743 | */ |
| 6744 | if (vmx->loaded_vmcs == &item->vmcs02) |
| 6745 | continue; |
| 6746 | |
| 6747 | free_loaded_vmcs(&item->vmcs02); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6748 | list_del(&item->list); |
| 6749 | kfree(item); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6750 | vmx->nested.vmcs02_num--; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6751 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6752 | } |
| 6753 | |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6754 | /* |
| 6755 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 6756 | * set the success or error code of an emulated VMX instruction, as specified |
| 6757 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 6758 | */ |
| 6759 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 6760 | { |
| 6761 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 6762 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6763 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 6764 | } |
| 6765 | |
| 6766 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 6767 | { |
| 6768 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6769 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 6770 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6771 | | X86_EFLAGS_CF); |
| 6772 | } |
| 6773 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6774 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6775 | u32 vm_instruction_error) |
| 6776 | { |
| 6777 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 6778 | /* |
| 6779 | * failValid writes the error number to the current VMCS, which |
| 6780 | * can't be done there isn't a current VMCS. |
| 6781 | */ |
| 6782 | nested_vmx_failInvalid(vcpu); |
| 6783 | return; |
| 6784 | } |
| 6785 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6786 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6787 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6788 | | X86_EFLAGS_ZF); |
| 6789 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 6790 | /* |
| 6791 | * We don't need to force a shadow sync because |
| 6792 | * VM_INSTRUCTION_ERROR is not shadowed |
| 6793 | */ |
| 6794 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6795 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6796 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 6797 | { |
| 6798 | /* TODO: not to reset guest simply here. */ |
| 6799 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 6800 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6801 | } |
| 6802 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 6803 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 6804 | { |
| 6805 | struct vcpu_vmx *vmx = |
| 6806 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 6807 | |
| 6808 | vmx->nested.preemption_timer_expired = true; |
| 6809 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 6810 | kvm_vcpu_kick(&vmx->vcpu); |
| 6811 | |
| 6812 | return HRTIMER_NORESTART; |
| 6813 | } |
| 6814 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6815 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6816 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 6817 | * exit caused by such an instruction (run by a guest hypervisor). |
| 6818 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 6819 | * #UD or #GP. |
| 6820 | */ |
| 6821 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 6822 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6823 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6824 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6825 | gva_t off; |
| 6826 | bool exn; |
| 6827 | struct kvm_segment s; |
| 6828 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6829 | /* |
| 6830 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 6831 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 6832 | * addressing components of the operand. Only the displacement part |
| 6833 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 6834 | * For how an actual address is calculated from all these components, |
| 6835 | * refer to Vol. 1, "Operand Addressing". |
| 6836 | */ |
| 6837 | int scaling = vmx_instruction_info & 3; |
| 6838 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 6839 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 6840 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 6841 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 6842 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 6843 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 6844 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 6845 | |
| 6846 | if (is_reg) { |
| 6847 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6848 | return 1; |
| 6849 | } |
| 6850 | |
| 6851 | /* Addr = segment_base + offset */ |
| 6852 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6853 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6854 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6855 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6856 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6857 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 6858 | vmx_get_segment(vcpu, &s, seg_reg); |
| 6859 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6860 | |
| 6861 | if (addr_size == 1) /* 32 bit */ |
| 6862 | *ret &= 0xffffffff; |
| 6863 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6864 | /* Checks for #GP/#SS exceptions. */ |
| 6865 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6866 | if (is_long_mode(vcpu)) { |
| 6867 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 6868 | * non-canonical form. This is the only check on the memory |
| 6869 | * destination for long mode! |
| 6870 | */ |
| 6871 | exn = is_noncanonical_address(*ret); |
| 6872 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6873 | /* Protected mode: apply checks for segment validity in the |
| 6874 | * following order: |
| 6875 | * - segment type check (#GP(0) may be thrown) |
| 6876 | * - usability check (#GP(0)/#SS(0)) |
| 6877 | * - limit check (#GP(0)/#SS(0)) |
| 6878 | */ |
| 6879 | if (wr) |
| 6880 | /* #GP(0) if the destination operand is located in a |
| 6881 | * read-only data segment or any code segment. |
| 6882 | */ |
| 6883 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 6884 | else |
| 6885 | /* #GP(0) if the source operand is located in an |
| 6886 | * execute-only code segment |
| 6887 | */ |
| 6888 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6889 | if (exn) { |
| 6890 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 6891 | return 1; |
| 6892 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6893 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 6894 | */ |
| 6895 | exn = (s.unusable != 0); |
| 6896 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 6897 | * operand is outside the segment limit. |
| 6898 | */ |
| 6899 | exn = exn || (off + sizeof(u64) > s.limit); |
| 6900 | } |
| 6901 | if (exn) { |
| 6902 | kvm_queue_exception_e(vcpu, |
| 6903 | seg_reg == VCPU_SREG_SS ? |
| 6904 | SS_VECTOR : GP_VECTOR, |
| 6905 | 0); |
| 6906 | return 1; |
| 6907 | } |
| 6908 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6909 | return 0; |
| 6910 | } |
| 6911 | |
| 6912 | /* |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6913 | * This function performs the various checks including |
| 6914 | * - if it's 4KB aligned |
| 6915 | * - No bits beyond the physical address width are set |
| 6916 | * - Returns 0 on success or else 1 |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6917 | * (Intel SDM Section 30.3) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6918 | */ |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6919 | static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason, |
| 6920 | gpa_t *vmpointer) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6921 | { |
| 6922 | gva_t gva; |
| 6923 | gpa_t vmptr; |
| 6924 | struct x86_exception e; |
| 6925 | struct page *page; |
| 6926 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6927 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 6928 | |
| 6929 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6930 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6931 | return 1; |
| 6932 | |
| 6933 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 6934 | sizeof(vmptr), &e)) { |
| 6935 | kvm_inject_page_fault(vcpu, &e); |
| 6936 | return 1; |
| 6937 | } |
| 6938 | |
| 6939 | switch (exit_reason) { |
| 6940 | case EXIT_REASON_VMON: |
| 6941 | /* |
| 6942 | * SDM 3: 24.11.5 |
| 6943 | * The first 4 bytes of VMXON region contain the supported |
| 6944 | * VMCS revision identifier |
| 6945 | * |
| 6946 | * Note - IA32_VMX_BASIC[48] will never be 1 |
| 6947 | * for the nested case; |
| 6948 | * which replaces physical address width with 32 |
| 6949 | * |
| 6950 | */ |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6951 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6952 | nested_vmx_failInvalid(vcpu); |
| 6953 | skip_emulated_instruction(vcpu); |
| 6954 | return 1; |
| 6955 | } |
| 6956 | |
| 6957 | page = nested_get_page(vcpu, vmptr); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 6958 | if (page == NULL) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6959 | nested_vmx_failInvalid(vcpu); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 6960 | skip_emulated_instruction(vcpu); |
| 6961 | return 1; |
| 6962 | } |
| 6963 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6964 | kunmap(page); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 6965 | nested_release_page_clean(page); |
| 6966 | nested_vmx_failInvalid(vcpu); |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6967 | skip_emulated_instruction(vcpu); |
| 6968 | return 1; |
| 6969 | } |
| 6970 | kunmap(page); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 6971 | nested_release_page_clean(page); |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6972 | vmx->nested.vmxon_ptr = vmptr; |
| 6973 | break; |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6974 | case EXIT_REASON_VMCLEAR: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6975 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6976 | nested_vmx_failValid(vcpu, |
| 6977 | VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 6978 | skip_emulated_instruction(vcpu); |
| 6979 | return 1; |
| 6980 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6981 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6982 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 6983 | nested_vmx_failValid(vcpu, |
| 6984 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 6985 | skip_emulated_instruction(vcpu); |
| 6986 | return 1; |
| 6987 | } |
| 6988 | break; |
| 6989 | case EXIT_REASON_VMPTRLD: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6990 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6991 | nested_vmx_failValid(vcpu, |
| 6992 | VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 6993 | skip_emulated_instruction(vcpu); |
| 6994 | return 1; |
| 6995 | } |
| 6996 | |
| 6997 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 6998 | nested_vmx_failValid(vcpu, |
| 6999 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 7000 | skip_emulated_instruction(vcpu); |
| 7001 | return 1; |
| 7002 | } |
| 7003 | break; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7004 | default: |
| 7005 | return 1; /* shouldn't happen */ |
| 7006 | } |
| 7007 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7008 | if (vmpointer) |
| 7009 | *vmpointer = vmptr; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7010 | return 0; |
| 7011 | } |
| 7012 | |
| 7013 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7014 | * Emulate the VMXON instruction. |
| 7015 | * Currently, we just remember that VMX is active, and do not save or even |
| 7016 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 7017 | * do not currently need to store anything in that guest-allocated memory |
| 7018 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 7019 | * argument is different from the VMXON pointer (which the spec says they do). |
| 7020 | */ |
| 7021 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 7022 | { |
| 7023 | struct kvm_segment cs; |
| 7024 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7025 | struct vmcs *shadow_vmcs; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7026 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 7027 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7028 | |
| 7029 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 7030 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 7031 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 7032 | * Otherwise, we should fail with #UD. We test these now: |
| 7033 | */ |
| 7034 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 7035 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 7036 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 7037 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7038 | return 1; |
| 7039 | } |
| 7040 | |
| 7041 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7042 | if (is_long_mode(vcpu) && !cs.l) { |
| 7043 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7044 | return 1; |
| 7045 | } |
| 7046 | |
| 7047 | if (vmx_get_cpl(vcpu)) { |
| 7048 | kvm_inject_gp(vcpu, 0); |
| 7049 | return 1; |
| 7050 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7051 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7052 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7053 | return 1; |
| 7054 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7055 | if (vmx->nested.vmxon) { |
| 7056 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
| 7057 | skip_emulated_instruction(vcpu); |
| 7058 | return 1; |
| 7059 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7060 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 7061 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7062 | != VMXON_NEEDED_FEATURES) { |
| 7063 | kvm_inject_gp(vcpu, 0); |
| 7064 | return 1; |
| 7065 | } |
| 7066 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7067 | if (cpu_has_vmx_msr_bitmap()) { |
| 7068 | vmx->nested.msr_bitmap = |
| 7069 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7070 | if (!vmx->nested.msr_bitmap) |
| 7071 | goto out_msr_bitmap; |
| 7072 | } |
| 7073 | |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7074 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7075 | if (!vmx->nested.cached_vmcs12) |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7076 | goto out_cached_vmcs12; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7077 | |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7078 | if (enable_shadow_vmcs) { |
| 7079 | shadow_vmcs = alloc_vmcs(); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7080 | if (!shadow_vmcs) |
| 7081 | goto out_shadow_vmcs; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7082 | /* mark vmcs as shadow */ |
| 7083 | shadow_vmcs->revision_id |= (1u << 31); |
| 7084 | /* init shadow vmcs */ |
| 7085 | vmcs_clear(shadow_vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7086 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7087 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7088 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7089 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); |
| 7090 | vmx->nested.vmcs02_num = 0; |
| 7091 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7092 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Wanpeng Li | f15a75e | 2016-08-30 16:14:01 +0800 | [diff] [blame] | 7093 | HRTIMER_MODE_REL_PINNED); |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7094 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7095 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7096 | vmx->nested.vmxon = true; |
| 7097 | |
| 7098 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7099 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7100 | return 1; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7101 | |
| 7102 | out_shadow_vmcs: |
| 7103 | kfree(vmx->nested.cached_vmcs12); |
| 7104 | |
| 7105 | out_cached_vmcs12: |
| 7106 | free_page((unsigned long)vmx->nested.msr_bitmap); |
| 7107 | |
| 7108 | out_msr_bitmap: |
| 7109 | return -ENOMEM; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7110 | } |
| 7111 | |
| 7112 | /* |
| 7113 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 7114 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 7115 | * slightly different). It also specifies what exception to inject otherwise. |
| 7116 | */ |
| 7117 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 7118 | { |
| 7119 | struct kvm_segment cs; |
| 7120 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7121 | |
| 7122 | if (!vmx->nested.vmxon) { |
| 7123 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7124 | return 0; |
| 7125 | } |
| 7126 | |
| 7127 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7128 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 7129 | (is_long_mode(vcpu) && !cs.l)) { |
| 7130 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7131 | return 0; |
| 7132 | } |
| 7133 | |
| 7134 | if (vmx_get_cpl(vcpu)) { |
| 7135 | kvm_inject_gp(vcpu, 0); |
| 7136 | return 0; |
| 7137 | } |
| 7138 | |
| 7139 | return 1; |
| 7140 | } |
| 7141 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7142 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 7143 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7144 | if (vmx->nested.current_vmptr == -1ull) |
| 7145 | return; |
| 7146 | |
| 7147 | /* current_vmptr and current_vmcs12 are always set/reset together */ |
| 7148 | if (WARN_ON(vmx->nested.current_vmcs12 == NULL)) |
| 7149 | return; |
| 7150 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7151 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7152 | /* copy to memory all shadowed fields in case |
| 7153 | they were modified */ |
| 7154 | copy_shadow_to_vmcs12(vmx); |
| 7155 | vmx->nested.sync_shadow_vmcs = false; |
Xiao Guangrong | 7ec3629 | 2015-09-09 14:05:56 +0800 | [diff] [blame] | 7156 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7157 | SECONDARY_EXEC_SHADOW_VMCS); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7158 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7159 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7160 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7161 | |
| 7162 | /* Flush VMCS12 to guest memory */ |
| 7163 | memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12, |
| 7164 | VMCS12_SIZE); |
| 7165 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7166 | kunmap(vmx->nested.current_vmcs12_page); |
| 7167 | nested_release_page(vmx->nested.current_vmcs12_page); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7168 | vmx->nested.current_vmptr = -1ull; |
| 7169 | vmx->nested.current_vmcs12 = NULL; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7170 | } |
| 7171 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7172 | /* |
| 7173 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 7174 | * just stops using VMX. |
| 7175 | */ |
| 7176 | static void free_nested(struct vcpu_vmx *vmx) |
| 7177 | { |
| 7178 | if (!vmx->nested.vmxon) |
| 7179 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7180 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7181 | vmx->nested.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7182 | free_vpid(vmx->nested.vpid02); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7183 | nested_release_vmcs12(vmx); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7184 | if (vmx->nested.msr_bitmap) { |
| 7185 | free_page((unsigned long)vmx->nested.msr_bitmap); |
| 7186 | vmx->nested.msr_bitmap = NULL; |
| 7187 | } |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7188 | if (enable_shadow_vmcs) { |
| 7189 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 7190 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 7191 | vmx->vmcs01.shadow_vmcs = NULL; |
| 7192 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7193 | kfree(vmx->nested.cached_vmcs12); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7194 | /* Unpin physical memory we referred to in current vmcs02 */ |
| 7195 | if (vmx->nested.apic_access_page) { |
| 7196 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7197 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7198 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7199 | if (vmx->nested.virtual_apic_page) { |
| 7200 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7201 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7202 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7203 | if (vmx->nested.pi_desc_page) { |
| 7204 | kunmap(vmx->nested.pi_desc_page); |
| 7205 | nested_release_page(vmx->nested.pi_desc_page); |
| 7206 | vmx->nested.pi_desc_page = NULL; |
| 7207 | vmx->nested.pi_desc = NULL; |
| 7208 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7209 | |
| 7210 | nested_free_all_saved_vmcss(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7211 | } |
| 7212 | |
| 7213 | /* Emulate the VMXOFF instruction */ |
| 7214 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 7215 | { |
| 7216 | if (!nested_vmx_check_permission(vcpu)) |
| 7217 | return 1; |
| 7218 | free_nested(to_vmx(vcpu)); |
| 7219 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7220 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7221 | return 1; |
| 7222 | } |
| 7223 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7224 | /* Emulate the VMCLEAR instruction */ |
| 7225 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 7226 | { |
| 7227 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 29deec4 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7228 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7229 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7230 | |
| 7231 | if (!nested_vmx_check_permission(vcpu)) |
| 7232 | return 1; |
| 7233 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7234 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7235 | return 1; |
| 7236 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7237 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7238 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7239 | |
Jim Mattson | 29deec4 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7240 | kvm_vcpu_write_guest(vcpu, |
| 7241 | vmptr + offsetof(struct vmcs12, launch_state), |
| 7242 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7243 | |
| 7244 | nested_free_vmcs02(vmx, vmptr); |
| 7245 | |
| 7246 | skip_emulated_instruction(vcpu); |
| 7247 | nested_vmx_succeed(vcpu); |
| 7248 | return 1; |
| 7249 | } |
| 7250 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7251 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 7252 | |
| 7253 | /* Emulate the VMLAUNCH instruction */ |
| 7254 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 7255 | { |
| 7256 | return nested_vmx_run(vcpu, true); |
| 7257 | } |
| 7258 | |
| 7259 | /* Emulate the VMRESUME instruction */ |
| 7260 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 7261 | { |
| 7262 | |
| 7263 | return nested_vmx_run(vcpu, false); |
| 7264 | } |
| 7265 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7266 | enum vmcs_field_type { |
| 7267 | VMCS_FIELD_TYPE_U16 = 0, |
| 7268 | VMCS_FIELD_TYPE_U64 = 1, |
| 7269 | VMCS_FIELD_TYPE_U32 = 2, |
| 7270 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 7271 | }; |
| 7272 | |
| 7273 | static inline int vmcs_field_type(unsigned long field) |
| 7274 | { |
| 7275 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 7276 | return VMCS_FIELD_TYPE_U32; |
| 7277 | return (field >> 13) & 0x3 ; |
| 7278 | } |
| 7279 | |
| 7280 | static inline int vmcs_field_readonly(unsigned long field) |
| 7281 | { |
| 7282 | return (((field >> 10) & 0x3) == 1); |
| 7283 | } |
| 7284 | |
| 7285 | /* |
| 7286 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 7287 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 7288 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 7289 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 7290 | * 64-bit fields are to be returned). |
| 7291 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7292 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 7293 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7294 | { |
| 7295 | short offset = vmcs_field_to_offset(field); |
| 7296 | char *p; |
| 7297 | |
| 7298 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7299 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7300 | |
| 7301 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 7302 | |
| 7303 | switch (vmcs_field_type(field)) { |
| 7304 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7305 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7306 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7307 | case VMCS_FIELD_TYPE_U16: |
| 7308 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7309 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7310 | case VMCS_FIELD_TYPE_U32: |
| 7311 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7312 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7313 | case VMCS_FIELD_TYPE_U64: |
| 7314 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7315 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7316 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7317 | WARN_ON(1); |
| 7318 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7319 | } |
| 7320 | } |
| 7321 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7322 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7323 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 7324 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7325 | short offset = vmcs_field_to_offset(field); |
| 7326 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 7327 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7328 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7329 | |
| 7330 | switch (vmcs_field_type(field)) { |
| 7331 | case VMCS_FIELD_TYPE_U16: |
| 7332 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7333 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7334 | case VMCS_FIELD_TYPE_U32: |
| 7335 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7336 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7337 | case VMCS_FIELD_TYPE_U64: |
| 7338 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7339 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7340 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7341 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7342 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7343 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7344 | WARN_ON(1); |
| 7345 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7346 | } |
| 7347 | |
| 7348 | } |
| 7349 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7350 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 7351 | { |
| 7352 | int i; |
| 7353 | unsigned long field; |
| 7354 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7355 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7356 | const unsigned long *fields = shadow_read_write_fields; |
| 7357 | const int num_fields = max_shadow_read_write_fields; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7358 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7359 | preempt_disable(); |
| 7360 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7361 | vmcs_load(shadow_vmcs); |
| 7362 | |
| 7363 | for (i = 0; i < num_fields; i++) { |
| 7364 | field = fields[i]; |
| 7365 | switch (vmcs_field_type(field)) { |
| 7366 | case VMCS_FIELD_TYPE_U16: |
| 7367 | field_value = vmcs_read16(field); |
| 7368 | break; |
| 7369 | case VMCS_FIELD_TYPE_U32: |
| 7370 | field_value = vmcs_read32(field); |
| 7371 | break; |
| 7372 | case VMCS_FIELD_TYPE_U64: |
| 7373 | field_value = vmcs_read64(field); |
| 7374 | break; |
| 7375 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7376 | field_value = vmcs_readl(field); |
| 7377 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7378 | default: |
| 7379 | WARN_ON(1); |
| 7380 | continue; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7381 | } |
| 7382 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 7383 | } |
| 7384 | |
| 7385 | vmcs_clear(shadow_vmcs); |
| 7386 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7387 | |
| 7388 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7389 | } |
| 7390 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7391 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 7392 | { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7393 | const unsigned long *fields[] = { |
| 7394 | shadow_read_write_fields, |
| 7395 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7396 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7397 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7398 | max_shadow_read_write_fields, |
| 7399 | max_shadow_read_only_fields |
| 7400 | }; |
| 7401 | int i, q; |
| 7402 | unsigned long field; |
| 7403 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7404 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7405 | |
| 7406 | vmcs_load(shadow_vmcs); |
| 7407 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7408 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7409 | for (i = 0; i < max_fields[q]; i++) { |
| 7410 | field = fields[q][i]; |
| 7411 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
| 7412 | |
| 7413 | switch (vmcs_field_type(field)) { |
| 7414 | case VMCS_FIELD_TYPE_U16: |
| 7415 | vmcs_write16(field, (u16)field_value); |
| 7416 | break; |
| 7417 | case VMCS_FIELD_TYPE_U32: |
| 7418 | vmcs_write32(field, (u32)field_value); |
| 7419 | break; |
| 7420 | case VMCS_FIELD_TYPE_U64: |
| 7421 | vmcs_write64(field, (u64)field_value); |
| 7422 | break; |
| 7423 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7424 | vmcs_writel(field, (long)field_value); |
| 7425 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7426 | default: |
| 7427 | WARN_ON(1); |
| 7428 | break; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7429 | } |
| 7430 | } |
| 7431 | } |
| 7432 | |
| 7433 | vmcs_clear(shadow_vmcs); |
| 7434 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 7435 | } |
| 7436 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7437 | /* |
| 7438 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 7439 | * used before) all generate the same failure when it is missing. |
| 7440 | */ |
| 7441 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 7442 | { |
| 7443 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7444 | if (vmx->nested.current_vmptr == -1ull) { |
| 7445 | nested_vmx_failInvalid(vcpu); |
| 7446 | skip_emulated_instruction(vcpu); |
| 7447 | return 0; |
| 7448 | } |
| 7449 | return 1; |
| 7450 | } |
| 7451 | |
| 7452 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 7453 | { |
| 7454 | unsigned long field; |
| 7455 | u64 field_value; |
| 7456 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7457 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7458 | gva_t gva = 0; |
| 7459 | |
| 7460 | if (!nested_vmx_check_permission(vcpu) || |
| 7461 | !nested_vmx_check_vmcs12(vcpu)) |
| 7462 | return 1; |
| 7463 | |
| 7464 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7465 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7466 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7467 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7468 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 7469 | skip_emulated_instruction(vcpu); |
| 7470 | return 1; |
| 7471 | } |
| 7472 | /* |
| 7473 | * Now copy part of this value to register or memory, as requested. |
| 7474 | * Note that the number of bits actually copied is 32 or 64 depending |
| 7475 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 7476 | */ |
| 7477 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7478 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7479 | field_value); |
| 7480 | } else { |
| 7481 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7482 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7483 | return 1; |
| 7484 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
| 7485 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 7486 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 7487 | } |
| 7488 | |
| 7489 | nested_vmx_succeed(vcpu); |
| 7490 | skip_emulated_instruction(vcpu); |
| 7491 | return 1; |
| 7492 | } |
| 7493 | |
| 7494 | |
| 7495 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 7496 | { |
| 7497 | unsigned long field; |
| 7498 | gva_t gva; |
| 7499 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7500 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7501 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 7502 | * mode, and eventually we need to write that into a field of several |
| 7503 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 7504 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7505 | * bits into the vmcs12 field. |
| 7506 | */ |
| 7507 | u64 field_value = 0; |
| 7508 | struct x86_exception e; |
| 7509 | |
| 7510 | if (!nested_vmx_check_permission(vcpu) || |
| 7511 | !nested_vmx_check_vmcs12(vcpu)) |
| 7512 | return 1; |
| 7513 | |
| 7514 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7515 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7516 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 7517 | else { |
| 7518 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7519 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7520 | return 1; |
| 7521 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7522 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7523 | kvm_inject_page_fault(vcpu, &e); |
| 7524 | return 1; |
| 7525 | } |
| 7526 | } |
| 7527 | |
| 7528 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7529 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7530 | if (vmcs_field_readonly(field)) { |
| 7531 | nested_vmx_failValid(vcpu, |
| 7532 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 7533 | skip_emulated_instruction(vcpu); |
| 7534 | return 1; |
| 7535 | } |
| 7536 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7537 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7538 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 7539 | skip_emulated_instruction(vcpu); |
| 7540 | return 1; |
| 7541 | } |
| 7542 | |
| 7543 | nested_vmx_succeed(vcpu); |
| 7544 | skip_emulated_instruction(vcpu); |
| 7545 | return 1; |
| 7546 | } |
| 7547 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7548 | /* Emulate the VMPTRLD instruction */ |
| 7549 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 7550 | { |
| 7551 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7552 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7553 | |
| 7554 | if (!nested_vmx_check_permission(vcpu)) |
| 7555 | return 1; |
| 7556 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7557 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7558 | return 1; |
| 7559 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7560 | if (vmx->nested.current_vmptr != vmptr) { |
| 7561 | struct vmcs12 *new_vmcs12; |
| 7562 | struct page *page; |
| 7563 | page = nested_get_page(vcpu, vmptr); |
| 7564 | if (page == NULL) { |
| 7565 | nested_vmx_failInvalid(vcpu); |
| 7566 | skip_emulated_instruction(vcpu); |
| 7567 | return 1; |
| 7568 | } |
| 7569 | new_vmcs12 = kmap(page); |
| 7570 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 7571 | kunmap(page); |
| 7572 | nested_release_page_clean(page); |
| 7573 | nested_vmx_failValid(vcpu, |
| 7574 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 7575 | skip_emulated_instruction(vcpu); |
| 7576 | return 1; |
| 7577 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7578 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7579 | nested_release_vmcs12(vmx); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7580 | vmx->nested.current_vmptr = vmptr; |
| 7581 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 7582 | vmx->nested.current_vmcs12_page = page; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7583 | /* |
| 7584 | * Load VMCS12 from guest memory since it is not already |
| 7585 | * cached. |
| 7586 | */ |
| 7587 | memcpy(vmx->nested.cached_vmcs12, |
| 7588 | vmx->nested.current_vmcs12, VMCS12_SIZE); |
| 7589 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7590 | if (enable_shadow_vmcs) { |
Xiao Guangrong | 7ec3629 | 2015-09-09 14:05:56 +0800 | [diff] [blame] | 7591 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7592 | SECONDARY_EXEC_SHADOW_VMCS); |
Abel Gordon | 8a1b9dd | 2013-04-18 14:39:55 +0300 | [diff] [blame] | 7593 | vmcs_write64(VMCS_LINK_POINTER, |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7594 | __pa(vmx->vmcs01.shadow_vmcs)); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7595 | vmx->nested.sync_shadow_vmcs = true; |
| 7596 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7597 | } |
| 7598 | |
| 7599 | nested_vmx_succeed(vcpu); |
| 7600 | skip_emulated_instruction(vcpu); |
| 7601 | return 1; |
| 7602 | } |
| 7603 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7604 | /* Emulate the VMPTRST instruction */ |
| 7605 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 7606 | { |
| 7607 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7608 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7609 | gva_t vmcs_gva; |
| 7610 | struct x86_exception e; |
| 7611 | |
| 7612 | if (!nested_vmx_check_permission(vcpu)) |
| 7613 | return 1; |
| 7614 | |
| 7615 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7616 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7617 | return 1; |
| 7618 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
| 7619 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 7620 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 7621 | sizeof(u64), &e)) { |
| 7622 | kvm_inject_page_fault(vcpu, &e); |
| 7623 | return 1; |
| 7624 | } |
| 7625 | nested_vmx_succeed(vcpu); |
| 7626 | skip_emulated_instruction(vcpu); |
| 7627 | return 1; |
| 7628 | } |
| 7629 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7630 | /* Emulate the INVEPT instruction */ |
| 7631 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 7632 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7633 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7634 | u32 vmx_instruction_info, types; |
| 7635 | unsigned long type; |
| 7636 | gva_t gva; |
| 7637 | struct x86_exception e; |
| 7638 | struct { |
| 7639 | u64 eptp, gpa; |
| 7640 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7641 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7642 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7643 | SECONDARY_EXEC_ENABLE_EPT) || |
| 7644 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7645 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7646 | return 1; |
| 7647 | } |
| 7648 | |
| 7649 | if (!nested_vmx_check_permission(vcpu)) |
| 7650 | return 1; |
| 7651 | |
| 7652 | if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) { |
| 7653 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7654 | return 1; |
| 7655 | } |
| 7656 | |
| 7657 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7658 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7659 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7660 | 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] | 7661 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7662 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7663 | nested_vmx_failValid(vcpu, |
| 7664 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Paolo Bonzini | 2849eb4 | 2016-03-18 16:53:29 +0100 | [diff] [blame] | 7665 | skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7666 | return 1; |
| 7667 | } |
| 7668 | |
| 7669 | /* According to the Intel VMX instruction reference, the memory |
| 7670 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7671 | */ |
| 7672 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7673 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7674 | return 1; |
| 7675 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
| 7676 | sizeof(operand), &e)) { |
| 7677 | kvm_inject_page_fault(vcpu, &e); |
| 7678 | return 1; |
| 7679 | } |
| 7680 | |
| 7681 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7682 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 7683 | /* |
| 7684 | * TODO: track mappings and invalidate |
| 7685 | * single context requests appropriately |
| 7686 | */ |
| 7687 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7688 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 7689 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7690 | nested_vmx_succeed(vcpu); |
| 7691 | break; |
| 7692 | default: |
| 7693 | BUG_ON(1); |
| 7694 | break; |
| 7695 | } |
| 7696 | |
| 7697 | skip_emulated_instruction(vcpu); |
| 7698 | return 1; |
| 7699 | } |
| 7700 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7701 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 7702 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7703 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7704 | u32 vmx_instruction_info; |
| 7705 | unsigned long type, types; |
| 7706 | gva_t gva; |
| 7707 | struct x86_exception e; |
| 7708 | int vpid; |
| 7709 | |
| 7710 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7711 | SECONDARY_EXEC_ENABLE_VPID) || |
| 7712 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 7713 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7714 | return 1; |
| 7715 | } |
| 7716 | |
| 7717 | if (!nested_vmx_check_permission(vcpu)) |
| 7718 | return 1; |
| 7719 | |
| 7720 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7721 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 7722 | |
| 7723 | types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7; |
| 7724 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7725 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7726 | nested_vmx_failValid(vcpu, |
| 7727 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Paolo Bonzini | f6870ee | 2016-03-18 16:53:42 +0100 | [diff] [blame] | 7728 | skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7729 | return 1; |
| 7730 | } |
| 7731 | |
| 7732 | /* according to the intel vmx instruction reference, the memory |
| 7733 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7734 | */ |
| 7735 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 7736 | vmx_instruction_info, false, &gva)) |
| 7737 | return 1; |
| 7738 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid, |
| 7739 | sizeof(u32), &e)) { |
| 7740 | kvm_inject_page_fault(vcpu, &e); |
| 7741 | return 1; |
| 7742 | } |
| 7743 | |
| 7744 | switch (type) { |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 7745 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 7746 | /* |
| 7747 | * Old versions of KVM use the single-context version so we |
| 7748 | * have to support it; just treat it the same as all-context. |
| 7749 | */ |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7750 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7751 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7752 | nested_vmx_succeed(vcpu); |
| 7753 | break; |
| 7754 | default: |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 7755 | /* Trap individual address invalidation invvpid calls */ |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7756 | BUG_ON(1); |
| 7757 | break; |
| 7758 | } |
| 7759 | |
| 7760 | skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7761 | return 1; |
| 7762 | } |
| 7763 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7764 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 7765 | { |
| 7766 | unsigned long exit_qualification; |
| 7767 | |
| 7768 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 7769 | |
| 7770 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7771 | |
| 7772 | /* |
| 7773 | * PML buffer FULL happened while executing iret from NMI, |
| 7774 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7775 | */ |
| 7776 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 7777 | cpu_has_virtual_nmis() && |
| 7778 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 7779 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 7780 | GUEST_INTR_STATE_NMI); |
| 7781 | |
| 7782 | /* |
| 7783 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 7784 | * here.., and there's no userspace involvement needed for PML. |
| 7785 | */ |
| 7786 | return 1; |
| 7787 | } |
| 7788 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7789 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 7790 | { |
| 7791 | kvm_lapic_expired_hv_timer(vcpu); |
| 7792 | return 1; |
| 7793 | } |
| 7794 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 7795 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7796 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 7797 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 7798 | * to be done to userspace and return 0. |
| 7799 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 7800 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7801 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 7802 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 7803 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7804 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7805 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7806 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 7807 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 7808 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 7809 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 7810 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 7811 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 7812 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7813 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7814 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7815 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7816 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7817 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7818 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7819 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7820 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7821 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7822 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7823 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7824 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 7825 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7826 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 7827 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7828 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7829 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7830 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7831 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7832 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7833 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7834 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 7835 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7836 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7837 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7838 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7839 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7840 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7841 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7842 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 7843 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7844 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7845 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7846 | }; |
| 7847 | |
| 7848 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 7849 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7850 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7851 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 7852 | struct vmcs12 *vmcs12) |
| 7853 | { |
| 7854 | unsigned long exit_qualification; |
| 7855 | gpa_t bitmap, last_bitmap; |
| 7856 | unsigned int port; |
| 7857 | int size; |
| 7858 | u8 b; |
| 7859 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7860 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 7861 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7862 | |
| 7863 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7864 | |
| 7865 | port = exit_qualification >> 16; |
| 7866 | size = (exit_qualification & 7) + 1; |
| 7867 | |
| 7868 | last_bitmap = (gpa_t)-1; |
| 7869 | b = -1; |
| 7870 | |
| 7871 | while (size > 0) { |
| 7872 | if (port < 0x8000) |
| 7873 | bitmap = vmcs12->io_bitmap_a; |
| 7874 | else if (port < 0x10000) |
| 7875 | bitmap = vmcs12->io_bitmap_b; |
| 7876 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7877 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7878 | bitmap += (port & 0x7fff) / 8; |
| 7879 | |
| 7880 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7881 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7882 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7883 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7884 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7885 | |
| 7886 | port++; |
| 7887 | size--; |
| 7888 | last_bitmap = bitmap; |
| 7889 | } |
| 7890 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7891 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7892 | } |
| 7893 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7894 | /* |
| 7895 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 7896 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 7897 | * disinterest in the current event (read or write a specific MSR) by using an |
| 7898 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 7899 | */ |
| 7900 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 7901 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 7902 | { |
| 7903 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7904 | gpa_t bitmap; |
| 7905 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 7906 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7907 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7908 | |
| 7909 | /* |
| 7910 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 7911 | * for the four combinations of read/write and low/high MSR numbers. |
| 7912 | * First we need to figure out which of the four to use: |
| 7913 | */ |
| 7914 | bitmap = vmcs12->msr_bitmap; |
| 7915 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 7916 | bitmap += 2048; |
| 7917 | if (msr_index >= 0xc0000000) { |
| 7918 | msr_index -= 0xc0000000; |
| 7919 | bitmap += 1024; |
| 7920 | } |
| 7921 | |
| 7922 | /* Then read the msr_index'th bit from this bitmap: */ |
| 7923 | if (msr_index < 1024*8) { |
| 7924 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7925 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7926 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7927 | return 1 & (b >> (msr_index & 7)); |
| 7928 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7929 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7930 | } |
| 7931 | |
| 7932 | /* |
| 7933 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 7934 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 7935 | * intercept (via guest_host_mask etc.) the current event. |
| 7936 | */ |
| 7937 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 7938 | struct vmcs12 *vmcs12) |
| 7939 | { |
| 7940 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7941 | int cr = exit_qualification & 15; |
| 7942 | int reg = (exit_qualification >> 8) & 15; |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7943 | unsigned long val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7944 | |
| 7945 | switch ((exit_qualification >> 4) & 3) { |
| 7946 | case 0: /* mov to cr */ |
| 7947 | switch (cr) { |
| 7948 | case 0: |
| 7949 | if (vmcs12->cr0_guest_host_mask & |
| 7950 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7951 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7952 | break; |
| 7953 | case 3: |
| 7954 | if ((vmcs12->cr3_target_count >= 1 && |
| 7955 | vmcs12->cr3_target_value0 == val) || |
| 7956 | (vmcs12->cr3_target_count >= 2 && |
| 7957 | vmcs12->cr3_target_value1 == val) || |
| 7958 | (vmcs12->cr3_target_count >= 3 && |
| 7959 | vmcs12->cr3_target_value2 == val) || |
| 7960 | (vmcs12->cr3_target_count >= 4 && |
| 7961 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7962 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7963 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7964 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7965 | break; |
| 7966 | case 4: |
| 7967 | if (vmcs12->cr4_guest_host_mask & |
| 7968 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7969 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7970 | break; |
| 7971 | case 8: |
| 7972 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7973 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7974 | break; |
| 7975 | } |
| 7976 | break; |
| 7977 | case 2: /* clts */ |
| 7978 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 7979 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7980 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7981 | break; |
| 7982 | case 1: /* mov from cr */ |
| 7983 | switch (cr) { |
| 7984 | case 3: |
| 7985 | if (vmcs12->cpu_based_vm_exec_control & |
| 7986 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7987 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7988 | break; |
| 7989 | case 8: |
| 7990 | if (vmcs12->cpu_based_vm_exec_control & |
| 7991 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7992 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7993 | break; |
| 7994 | } |
| 7995 | break; |
| 7996 | case 3: /* lmsw */ |
| 7997 | /* |
| 7998 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 7999 | * cr0. Other attempted changes are ignored, with no exit. |
| 8000 | */ |
| 8001 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 8002 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8003 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8004 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 8005 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 8006 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8007 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8008 | break; |
| 8009 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8010 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8011 | } |
| 8012 | |
| 8013 | /* |
| 8014 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 8015 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 8016 | * when in is_guest_mode (L2). |
| 8017 | */ |
| 8018 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) |
| 8019 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8020 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8021 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8022 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 957c897 | 2013-02-24 14:11:34 +0100 | [diff] [blame] | 8023 | u32 exit_reason = vmx->exit_reason; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8024 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 8025 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 8026 | vmcs_readl(EXIT_QUALIFICATION), |
| 8027 | vmx->idt_vectoring_info, |
| 8028 | intr_info, |
| 8029 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8030 | KVM_ISA_VMX); |
| 8031 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8032 | if (vmx->nested.nested_run_pending) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8033 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8034 | |
| 8035 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 8036 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 8037 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8038 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8039 | } |
| 8040 | |
| 8041 | switch (exit_reason) { |
| 8042 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8043 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8044 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8045 | else if (is_page_fault(intr_info)) |
| 8046 | return enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 8047 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 8048 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8049 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 8050 | else if (is_debug(intr_info) && |
| 8051 | vcpu->guest_debug & |
| 8052 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 8053 | return false; |
| 8054 | else if (is_breakpoint(intr_info) && |
| 8055 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 8056 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8057 | return vmcs12->exception_bitmap & |
| 8058 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 8059 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8060 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8061 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8062 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8063 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8064 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8065 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8066 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8067 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8068 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8069 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8070 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8071 | case EXIT_REASON_HLT: |
| 8072 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 8073 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8074 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8075 | case EXIT_REASON_INVLPG: |
| 8076 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 8077 | case EXIT_REASON_RDPMC: |
| 8078 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 8079 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8080 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 8081 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 8082 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 8083 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 8084 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 8085 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8086 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8087 | /* |
| 8088 | * VMX instructions trap unconditionally. This allows L1 to |
| 8089 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 8090 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8091 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8092 | case EXIT_REASON_CR_ACCESS: |
| 8093 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 8094 | case EXIT_REASON_DR_ACCESS: |
| 8095 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 8096 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8097 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8098 | case EXIT_REASON_MSR_READ: |
| 8099 | case EXIT_REASON_MSR_WRITE: |
| 8100 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 8101 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8102 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8103 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 8104 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8105 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 8106 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8107 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 8108 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 8109 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 8110 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 8111 | nested_cpu_has2(vmcs12, |
| 8112 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 8113 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8114 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8115 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8116 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8117 | case EXIT_REASON_APIC_ACCESS: |
| 8118 | return nested_cpu_has2(vmcs12, |
| 8119 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 8120 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 8121 | case EXIT_REASON_EOI_INDUCED: |
| 8122 | /* apic_write and eoi_induced should exit unconditionally. */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8123 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8124 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8125 | /* |
| 8126 | * L0 always deals with the EPT violation. If nested EPT is |
| 8127 | * used, and the nested mmu code discovers that the address is |
| 8128 | * missing in the guest EPT table (EPT12), the EPT violation |
| 8129 | * will be injected with nested_ept_inject_page_fault() |
| 8130 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8131 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8132 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8133 | /* |
| 8134 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 8135 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 8136 | * (EPT on EPT). So any problems with the structure of the |
| 8137 | * table is L0's fault. |
| 8138 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8139 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8140 | case EXIT_REASON_WBINVD: |
| 8141 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 8142 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8143 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 8144 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 8145 | /* |
| 8146 | * This should never happen, since it is not possible to |
| 8147 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 8148 | * If if it were, XSS would have to be checked against |
| 8149 | * the XSS exit bitmap in vmcs12. |
| 8150 | */ |
| 8151 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 8152 | case EXIT_REASON_PREEMPTION_TIMER: |
| 8153 | return false; |
Ladi Prosek | d0ee363 | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 8154 | case EXIT_REASON_PML_FULL: |
| 8155 | /* We don't expose PML support to L1. */ |
| 8156 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8157 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8158 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8159 | } |
| 8160 | } |
| 8161 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 8162 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 8163 | { |
| 8164 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 8165 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8166 | } |
| 8167 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8168 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8169 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8170 | if (vmx->pml_pg) { |
| 8171 | __free_page(vmx->pml_pg); |
| 8172 | vmx->pml_pg = NULL; |
| 8173 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8174 | } |
| 8175 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8176 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8177 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8178 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8179 | u64 *pml_buf; |
| 8180 | u16 pml_idx; |
| 8181 | |
| 8182 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 8183 | |
| 8184 | /* Do nothing if PML buffer is empty */ |
| 8185 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 8186 | return; |
| 8187 | |
| 8188 | /* PML index always points to next available PML buffer entity */ |
| 8189 | if (pml_idx >= PML_ENTITY_NUM) |
| 8190 | pml_idx = 0; |
| 8191 | else |
| 8192 | pml_idx++; |
| 8193 | |
| 8194 | pml_buf = page_address(vmx->pml_pg); |
| 8195 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 8196 | u64 gpa; |
| 8197 | |
| 8198 | gpa = pml_buf[pml_idx]; |
| 8199 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8200 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8201 | } |
| 8202 | |
| 8203 | /* reset PML index */ |
| 8204 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 8205 | } |
| 8206 | |
| 8207 | /* |
| 8208 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 8209 | * Called before reporting dirty_bitmap to userspace. |
| 8210 | */ |
| 8211 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 8212 | { |
| 8213 | int i; |
| 8214 | struct kvm_vcpu *vcpu; |
| 8215 | /* |
| 8216 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 8217 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 8218 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 8219 | * buffer. |
| 8220 | */ |
| 8221 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 8222 | kvm_vcpu_kick(vcpu); |
| 8223 | } |
| 8224 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8225 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 8226 | { |
| 8227 | 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] | 8228 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8229 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 8230 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 8231 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 8232 | } |
| 8233 | |
| 8234 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 8235 | { |
| 8236 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 8237 | name, vmcs_read32(limit), |
| 8238 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 8239 | } |
| 8240 | |
| 8241 | static void dump_vmcs(void) |
| 8242 | { |
| 8243 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 8244 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 8245 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 8246 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 8247 | u32 secondary_exec_control = 0; |
| 8248 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 8249 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8250 | int i, n; |
| 8251 | |
| 8252 | if (cpu_has_secondary_exec_ctrls()) |
| 8253 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8254 | |
| 8255 | pr_err("*** Guest State ***\n"); |
| 8256 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8257 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 8258 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 8259 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8260 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 8261 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 8262 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 8263 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 8264 | { |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8265 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 8266 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 8267 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 8268 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8269 | } |
| 8270 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 8271 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 8272 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 8273 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 8274 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8275 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 8276 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 8277 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 8278 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 8279 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 8280 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 8281 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 8282 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 8283 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 8284 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 8285 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 8286 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 8287 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 8288 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8289 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8290 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 8291 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 8292 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8293 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
| 8294 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8295 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8296 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8297 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8298 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8299 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 8300 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 8301 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 8302 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 8303 | pr_err("InterruptStatus = %04x\n", |
| 8304 | vmcs_read16(GUEST_INTR_STATUS)); |
| 8305 | |
| 8306 | pr_err("*** Host State ***\n"); |
| 8307 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 8308 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 8309 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 8310 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 8311 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 8312 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 8313 | vmcs_read16(HOST_TR_SELECTOR)); |
| 8314 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 8315 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 8316 | vmcs_readl(HOST_TR_BASE)); |
| 8317 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 8318 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 8319 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 8320 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 8321 | vmcs_readl(HOST_CR4)); |
| 8322 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8323 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 8324 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 8325 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 8326 | 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] | 8327 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8328 | vmcs_read64(HOST_IA32_EFER), |
| 8329 | vmcs_read64(HOST_IA32_PAT)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8330 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8331 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8332 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8333 | |
| 8334 | pr_err("*** Control State ***\n"); |
| 8335 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 8336 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 8337 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 8338 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 8339 | vmcs_read32(EXCEPTION_BITMAP), |
| 8340 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 8341 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 8342 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8343 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8344 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 8345 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 8346 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8347 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8348 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8349 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 8350 | pr_err(" reason=%08x qualification=%016lx\n", |
| 8351 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 8352 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 8353 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 8354 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8355 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 8356 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8357 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 8358 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8359 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 8360 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 8361 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 8362 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 8363 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8364 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8365 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 8366 | for (i = 0; i + 1 < n; i += 4) |
| 8367 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 8368 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 8369 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 8370 | if (i < n) |
| 8371 | pr_err("CR3 target%u=%016lx\n", |
| 8372 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 8373 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 8374 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 8375 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 8376 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 8377 | pr_err("Virtual processor ID = 0x%04x\n", |
| 8378 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 8379 | } |
| 8380 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8381 | /* |
| 8382 | * The guest has exited. See if we can fix it or if we need userspace |
| 8383 | * assistance. |
| 8384 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8385 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8386 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8387 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8388 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 8389 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8390 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 8391 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 8392 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8393 | /* |
| 8394 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 8395 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 8396 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 8397 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 8398 | * flushed already. |
| 8399 | */ |
| 8400 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8401 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8402 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8403 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 8404 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8405 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 8406 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8407 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 8408 | nested_vmx_vmexit(vcpu, exit_reason, |
| 8409 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8410 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8411 | return 1; |
| 8412 | } |
| 8413 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8414 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8415 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8416 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8417 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 8418 | = exit_reason; |
| 8419 | return 0; |
| 8420 | } |
| 8421 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8422 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8423 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8424 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8425 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 8426 | return 0; |
| 8427 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8428 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8429 | /* |
| 8430 | * Note: |
| 8431 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 8432 | * delivery event since it indicates guest is accessing MMIO. |
| 8433 | * The vm-exit can be triggered again after return to guest that |
| 8434 | * will cause infinite loop. |
| 8435 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 8436 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 8437 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 8438 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 8439 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8440 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 8441 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 8442 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
| 8443 | vcpu->run->internal.ndata = 2; |
| 8444 | vcpu->run->internal.data[0] = vectoring_info; |
| 8445 | vcpu->run->internal.data[1] = exit_reason; |
| 8446 | return 0; |
| 8447 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8448 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8449 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && |
| 8450 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 8451 | get_vmcs12(vcpu))))) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 8452 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8453 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8454 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 8455 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8456 | /* |
| 8457 | * This CPU don't support us in finding the end of an |
| 8458 | * NMI-blocked window if the guest runs with IRQs |
| 8459 | * disabled. So we pull the trigger after 1 s of |
| 8460 | * futile waiting, but inform the user about this. |
| 8461 | */ |
| 8462 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 8463 | "state on VCPU %d after 1 s timeout\n", |
| 8464 | __func__, vcpu->vcpu_id); |
| 8465 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8466 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8467 | } |
| 8468 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8469 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 8470 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8471 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8472 | else { |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 8473 | WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason); |
| 8474 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8475 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8476 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8477 | } |
| 8478 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8479 | 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] | 8480 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8481 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8482 | |
| 8483 | if (is_guest_mode(vcpu) && |
| 8484 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 8485 | return; |
| 8486 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8487 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8488 | vmcs_write32(TPR_THRESHOLD, 0); |
| 8489 | return; |
| 8490 | } |
| 8491 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8492 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8493 | } |
| 8494 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8495 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 8496 | { |
| 8497 | u32 sec_exec_control; |
| 8498 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 8499 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 8500 | if (is_guest_mode(vcpu)) { |
| 8501 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; |
| 8502 | return; |
| 8503 | } |
| 8504 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 8505 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8506 | return; |
| 8507 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 8508 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8509 | return; |
| 8510 | |
| 8511 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8512 | |
| 8513 | if (set) { |
| 8514 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 8515 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8516 | } else { |
| 8517 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8518 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8519 | vmx_flush_tlb_ept_only(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8520 | } |
| 8521 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 8522 | |
| 8523 | vmx_set_msr_bitmap(vcpu); |
| 8524 | } |
| 8525 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8526 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 8527 | { |
| 8528 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8529 | |
| 8530 | /* |
| 8531 | * Currently we do not handle the nested case where L2 has an |
| 8532 | * APIC access page of its own; that page is still pinned. |
| 8533 | * Hence, we skip the case where the VCPU is in guest mode _and_ |
| 8534 | * L1 prepared an APIC access page for L2. |
| 8535 | * |
| 8536 | * For the case where L1 and L2 share the same APIC access page |
| 8537 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear |
| 8538 | * in the vmcs12), this function will only update either the vmcs01 |
| 8539 | * or the vmcs02. If the former, the vmcs02 will be updated by |
| 8540 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in |
| 8541 | * the next L2->L1 exit. |
| 8542 | */ |
| 8543 | if (!is_guest_mode(vcpu) || |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8544 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8545 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8546 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8547 | vmx_flush_tlb_ept_only(vcpu); |
| 8548 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8549 | } |
| 8550 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8551 | 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] | 8552 | { |
| 8553 | u16 status; |
| 8554 | u8 old; |
| 8555 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8556 | if (max_isr == -1) |
| 8557 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8558 | |
| 8559 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8560 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8561 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8562 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8563 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8564 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8565 | } |
| 8566 | } |
| 8567 | |
| 8568 | static void vmx_set_rvi(int vector) |
| 8569 | { |
| 8570 | u16 status; |
| 8571 | u8 old; |
| 8572 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8573 | if (vector == -1) |
| 8574 | vector = 0; |
| 8575 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8576 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8577 | old = (u8)status & 0xff; |
| 8578 | if ((u8)vector != old) { |
| 8579 | status &= ~0xff; |
| 8580 | status |= (u8)vector; |
| 8581 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8582 | } |
| 8583 | } |
| 8584 | |
| 8585 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 8586 | { |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8587 | if (!is_guest_mode(vcpu)) { |
| 8588 | vmx_set_rvi(max_irr); |
| 8589 | return; |
| 8590 | } |
| 8591 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8592 | if (max_irr == -1) |
| 8593 | return; |
| 8594 | |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8595 | /* |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8596 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
| 8597 | * handles it. |
| 8598 | */ |
| 8599 | if (nested_exit_on_intr(vcpu)) |
| 8600 | return; |
| 8601 | |
| 8602 | /* |
| 8603 | * Else, fall back to pre-APICv interrupt injection since L2 |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8604 | * is run without virtual interrupt delivery. |
| 8605 | */ |
| 8606 | if (!kvm_event_needs_reinjection(vcpu) && |
| 8607 | vmx_interrupt_allowed(vcpu)) { |
| 8608 | kvm_queue_interrupt(vcpu, max_irr, false); |
| 8609 | vmx_inject_irq(vcpu); |
| 8610 | } |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8611 | } |
| 8612 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 8613 | 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] | 8614 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 8615 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 8616 | return; |
| 8617 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8618 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 8619 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 8620 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 8621 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 8622 | } |
| 8623 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8624 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8625 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8626 | u32 exit_intr_info; |
| 8627 | |
| 8628 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 8629 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 8630 | return; |
| 8631 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8632 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8633 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8634 | |
| 8635 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8636 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8637 | kvm_machine_check(); |
| 8638 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8639 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8640 | if (is_nmi(exit_intr_info)) { |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8641 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8642 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8643 | kvm_after_handle_nmi(&vmx->vcpu); |
| 8644 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8645 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8646 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8647 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 8648 | { |
| 8649 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8650 | register void *__sp asm(_ASM_SP); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8651 | |
| 8652 | /* |
| 8653 | * If external interrupt exists, IF bit is set in rflags/eflags on the |
| 8654 | * interrupt stack frame, and interrupt will be enabled on a return |
| 8655 | * from interrupt handler. |
| 8656 | */ |
| 8657 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 8658 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 8659 | unsigned int vector; |
| 8660 | unsigned long entry; |
| 8661 | gate_desc *desc; |
| 8662 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8663 | #ifdef CONFIG_X86_64 |
| 8664 | unsigned long tmp; |
| 8665 | #endif |
| 8666 | |
| 8667 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8668 | desc = (gate_desc *)vmx->host_idt_base + vector; |
| 8669 | entry = gate_offset(*desc); |
| 8670 | asm volatile( |
| 8671 | #ifdef CONFIG_X86_64 |
| 8672 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 8673 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 8674 | "push $%c[ss]\n\t" |
| 8675 | "push %[sp]\n\t" |
| 8676 | #endif |
| 8677 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8678 | __ASM_SIZE(push) " $%c[cs]\n\t" |
| 8679 | "call *%[entry]\n\t" |
| 8680 | : |
| 8681 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8682 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8683 | #endif |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8684 | "+r"(__sp) |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8685 | : |
| 8686 | [entry]"r"(entry), |
| 8687 | [ss]"i"(__KERNEL_DS), |
| 8688 | [cs]"i"(__KERNEL_CS) |
| 8689 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 8690 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8691 | } |
| 8692 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 8693 | static bool vmx_has_high_real_mode_segbase(void) |
| 8694 | { |
| 8695 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 8696 | } |
| 8697 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 8698 | static bool vmx_mpx_supported(void) |
| 8699 | { |
| 8700 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 8701 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 8702 | } |
| 8703 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 8704 | static bool vmx_xsaves_supported(void) |
| 8705 | { |
| 8706 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 8707 | SECONDARY_EXEC_XSAVES; |
| 8708 | } |
| 8709 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8710 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 8711 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8712 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8713 | bool unblock_nmi; |
| 8714 | u8 vector; |
| 8715 | bool idtv_info_valid; |
| 8716 | |
| 8717 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8718 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8719 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 8720 | if (vmx->nmi_known_unmasked) |
| 8721 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8722 | /* |
| 8723 | * Can't use vmx->exit_intr_info since we're not sure what |
| 8724 | * the exit reason is. |
| 8725 | */ |
| 8726 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8727 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 8728 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8729 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8730 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8731 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 8732 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8733 | * SDM 3: 23.2.2 (September 2008) |
| 8734 | * Bit 12 is undefined in any of the following cases: |
| 8735 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 8736 | * information field. |
| 8737 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8738 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8739 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 8740 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8741 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8742 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 8743 | else |
| 8744 | vmx->nmi_known_unmasked = |
| 8745 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 8746 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8747 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 8748 | vmx->vnmi_blocked_time += |
| 8749 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8750 | } |
| 8751 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8752 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8753 | u32 idt_vectoring_info, |
| 8754 | int instr_len_field, |
| 8755 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8756 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8757 | u8 vector; |
| 8758 | int type; |
| 8759 | bool idtv_info_valid; |
| 8760 | |
| 8761 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8762 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8763 | vcpu->arch.nmi_injected = false; |
| 8764 | kvm_clear_exception_queue(vcpu); |
| 8765 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8766 | |
| 8767 | if (!idtv_info_valid) |
| 8768 | return; |
| 8769 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8770 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8771 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8772 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 8773 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8774 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 8775 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8776 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8777 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8778 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8779 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8780 | * Clear bit "block by NMI" before VM entry if a NMI |
| 8781 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8782 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8783 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8784 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8785 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8786 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8787 | /* fall through */ |
| 8788 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8789 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8790 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8791 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8792 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8793 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8794 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8795 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8796 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8797 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8798 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8799 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8800 | break; |
| 8801 | default: |
| 8802 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 8803 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8804 | } |
| 8805 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8806 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 8807 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8808 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8809 | VM_EXIT_INSTRUCTION_LEN, |
| 8810 | IDT_VECTORING_ERROR_CODE); |
| 8811 | } |
| 8812 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8813 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 8814 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8815 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8816 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8817 | VM_ENTRY_INSTRUCTION_LEN, |
| 8818 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 8819 | |
| 8820 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 8821 | } |
| 8822 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8823 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 8824 | { |
| 8825 | int i, nr_msrs; |
| 8826 | struct perf_guest_switch_msr *msrs; |
| 8827 | |
| 8828 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 8829 | |
| 8830 | if (!msrs) |
| 8831 | return; |
| 8832 | |
| 8833 | for (i = 0; i < nr_msrs; i++) |
| 8834 | if (msrs[i].host == msrs[i].guest) |
| 8835 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 8836 | else |
| 8837 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 8838 | msrs[i].host); |
| 8839 | } |
| 8840 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8841 | void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
| 8842 | { |
| 8843 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8844 | u64 tscl; |
| 8845 | u32 delta_tsc; |
| 8846 | |
| 8847 | if (vmx->hv_deadline_tsc == -1) |
| 8848 | return; |
| 8849 | |
| 8850 | tscl = rdtsc(); |
| 8851 | if (vmx->hv_deadline_tsc > tscl) |
| 8852 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 8853 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 8854 | cpu_preemption_timer_multi); |
| 8855 | else |
| 8856 | delta_tsc = 0; |
| 8857 | |
| 8858 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 8859 | } |
| 8860 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 8861 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8862 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 8863 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 8864 | unsigned long debugctlmsr, cr4; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8865 | |
| 8866 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 8867 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 8868 | vmx->entry_time = ktime_get(); |
| 8869 | |
| 8870 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 8871 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 8872 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8873 | return; |
| 8874 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 8875 | if (vmx->ple_window_dirty) { |
| 8876 | vmx->ple_window_dirty = false; |
| 8877 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 8878 | } |
| 8879 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8880 | if (vmx->nested.sync_shadow_vmcs) { |
| 8881 | copy_vmcs12_to_shadow(vmx); |
| 8882 | vmx->nested.sync_shadow_vmcs = false; |
| 8883 | } |
| 8884 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8885 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 8886 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 8887 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 8888 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 8889 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 8890 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 8891 | if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) { |
| 8892 | vmcs_writel(HOST_CR4, cr4); |
| 8893 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 8894 | } |
| 8895 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8896 | /* When single-stepping over STI and MOV SS, we must clear the |
| 8897 | * corresponding interruptibility bits in the guest state. Otherwise |
| 8898 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 8899 | * exceptions being set, but that's not correct for the guest debugging |
| 8900 | * case. */ |
| 8901 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 8902 | vmx_set_interrupt_shadow(vcpu, 0); |
| 8903 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 8904 | if (vmx->guest_pkru_valid) |
| 8905 | __write_pkru(vmx->guest_pkru); |
| 8906 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8907 | atomic_switch_perf_msrs(vmx); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 8908 | debugctlmsr = get_debugctlmsr(); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8909 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8910 | vmx_arm_hv_timer(vcpu); |
| 8911 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8912 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8913 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8914 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8915 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 8916 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 8917 | "push %%" _ASM_CX " \n\t" |
| 8918 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8919 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8920 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 8921 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8922 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8923 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8924 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 8925 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 8926 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8927 | "je 2f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8928 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8929 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8930 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8931 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8932 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8933 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 8934 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 8935 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 8936 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 8937 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 8938 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8939 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8940 | "mov %c[r8](%0), %%r8 \n\t" |
| 8941 | "mov %c[r9](%0), %%r9 \n\t" |
| 8942 | "mov %c[r10](%0), %%r10 \n\t" |
| 8943 | "mov %c[r11](%0), %%r11 \n\t" |
| 8944 | "mov %c[r12](%0), %%r12 \n\t" |
| 8945 | "mov %c[r13](%0), %%r13 \n\t" |
| 8946 | "mov %c[r14](%0), %%r14 \n\t" |
| 8947 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8948 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8949 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 8950 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8951 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8952 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 8953 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8954 | "jmp 2f \n\t" |
| 8955 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 8956 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8957 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8958 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 8959 | "pop %0 \n\t" |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 8960 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8961 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 8962 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 8963 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 8964 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 8965 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 8966 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 8967 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8968 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8969 | "mov %%r8, %c[r8](%0) \n\t" |
| 8970 | "mov %%r9, %c[r9](%0) \n\t" |
| 8971 | "mov %%r10, %c[r10](%0) \n\t" |
| 8972 | "mov %%r11, %c[r11](%0) \n\t" |
| 8973 | "mov %%r12, %c[r12](%0) \n\t" |
| 8974 | "mov %%r13, %c[r13](%0) \n\t" |
| 8975 | "mov %%r14, %c[r14](%0) \n\t" |
| 8976 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 8977 | "xor %%r8d, %%r8d \n\t" |
| 8978 | "xor %%r9d, %%r9d \n\t" |
| 8979 | "xor %%r10d, %%r10d \n\t" |
| 8980 | "xor %%r11d, %%r11d \n\t" |
| 8981 | "xor %%r12d, %%r12d \n\t" |
| 8982 | "xor %%r13d, %%r13d \n\t" |
| 8983 | "xor %%r14d, %%r14d \n\t" |
| 8984 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8985 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8986 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 8987 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 8988 | |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 8989 | "xor %%eax, %%eax \n\t" |
| 8990 | "xor %%ebx, %%ebx \n\t" |
| 8991 | "xor %%esi, %%esi \n\t" |
| 8992 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8993 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8994 | ".pushsection .rodata \n\t" |
| 8995 | ".global vmx_return \n\t" |
| 8996 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 8997 | ".popsection" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8998 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8999 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9000 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9001 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9002 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 9003 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 9004 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 9005 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 9006 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 9007 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 9008 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9009 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9010 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 9011 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 9012 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 9013 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 9014 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 9015 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 9016 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 9017 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9018 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 9019 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 9020 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9021 | : "cc", "memory" |
| 9022 | #ifdef CONFIG_X86_64 |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9023 | , "rax", "rbx", "rdi", "rsi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9024 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9025 | #else |
| 9026 | , "eax", "ebx", "edi", "esi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9027 | #endif |
| 9028 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9029 | |
David Woodhouse | c1ddd99 | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 9030 | /* Eliminate branch target predictions from guest mode */ |
| 9031 | vmexit_fill_RSB(); |
| 9032 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 9033 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
| 9034 | if (debugctlmsr) |
| 9035 | update_debugctlmsr(debugctlmsr); |
| 9036 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 9037 | #ifndef CONFIG_X86_64 |
| 9038 | /* |
| 9039 | * The sysexit path does not restore ds/es, so we must set them to |
| 9040 | * a reasonable value ourselves. |
| 9041 | * |
| 9042 | * We can't defer this to vmx_load_host_state() since that function |
| 9043 | * may be executed in interrupt context, which saves and restore segments |
| 9044 | * around it, nullifying its effect. |
| 9045 | */ |
| 9046 | loadsegment(ds, __USER_DS); |
| 9047 | loadsegment(es, __USER_DS); |
| 9048 | #endif |
| 9049 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 9050 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 9051 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9052 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 9053 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9054 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 9055 | vcpu->arch.regs_dirty = 0; |
| 9056 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9057 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 9058 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9059 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 9060 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9061 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9062 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9063 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9064 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 9065 | * back on host, so it is safe to read guest PKRU from current |
| 9066 | * XSAVE. |
| 9067 | */ |
| 9068 | if (boot_cpu_has(X86_FEATURE_OSPKE)) { |
| 9069 | vmx->guest_pkru = __read_pkru(); |
| 9070 | if (vmx->guest_pkru != vmx->host_pkru) { |
| 9071 | vmx->guest_pkru_valid = true; |
| 9072 | __write_pkru(vmx->host_pkru); |
| 9073 | } else |
| 9074 | vmx->guest_pkru_valid = false; |
| 9075 | } |
| 9076 | |
| 9077 | /* |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9078 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 9079 | * we did not inject a still-pending event to L1 now because of |
| 9080 | * nested_run_pending, we need to re-enable this bit. |
| 9081 | */ |
| 9082 | if (vmx->nested.nested_run_pending) |
| 9083 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9084 | |
| 9085 | vmx->nested.nested_run_pending = 0; |
| 9086 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9087 | vmx_complete_atomic_exit(vmx); |
| 9088 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9089 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9090 | } |
| 9091 | |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9092 | static void vmx_load_vmcs01(struct kvm_vcpu *vcpu) |
| 9093 | { |
| 9094 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9095 | int cpu; |
| 9096 | |
| 9097 | if (vmx->loaded_vmcs == &vmx->vmcs01) |
| 9098 | return; |
| 9099 | |
| 9100 | cpu = get_cpu(); |
| 9101 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 9102 | vmx_vcpu_put(vcpu); |
| 9103 | vmx_vcpu_load(vcpu, cpu); |
| 9104 | vcpu->cpu = cpu; |
| 9105 | put_cpu(); |
| 9106 | } |
| 9107 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9108 | /* |
| 9109 | * Ensure that the current vmcs of the logical processor is the |
| 9110 | * vmcs01 of the vcpu before calling free_nested(). |
| 9111 | */ |
| 9112 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 9113 | { |
| 9114 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9115 | int r; |
| 9116 | |
| 9117 | r = vcpu_load(vcpu); |
| 9118 | BUG_ON(r); |
| 9119 | vmx_load_vmcs01(vcpu); |
| 9120 | free_nested(vmx); |
| 9121 | vcpu_put(vcpu); |
| 9122 | } |
| 9123 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9124 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 9125 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9126 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9127 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9128 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9129 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9130 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9131 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9132 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9133 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9134 | kfree(vmx->guest_msrs); |
| 9135 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9136 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9137 | } |
| 9138 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9139 | 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] | 9140 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9141 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 9142 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9143 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9144 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9145 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9146 | return ERR_PTR(-ENOMEM); |
| 9147 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9148 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 9149 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9150 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 9151 | if (err) |
| 9152 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9153 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9154 | err = -ENOMEM; |
| 9155 | |
| 9156 | /* |
| 9157 | * If PML is turned on, failure on enabling PML just results in failure |
| 9158 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 9159 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 9160 | * for the guest, etc. |
| 9161 | */ |
| 9162 | if (enable_pml) { |
| 9163 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 9164 | if (!vmx->pml_pg) |
| 9165 | goto uninit_vcpu; |
| 9166 | } |
| 9167 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9168 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 9169 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 9170 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 9171 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9172 | if (!vmx->guest_msrs) |
| 9173 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9174 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9175 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 9176 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 9177 | vmx->loaded_vmcs->shadow_vmcs = NULL; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9178 | if (!vmx->loaded_vmcs->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9179 | goto free_msrs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9180 | if (!vmm_exclusive) |
| 9181 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
| 9182 | loaded_vmcs_init(vmx->loaded_vmcs); |
| 9183 | if (!vmm_exclusive) |
| 9184 | kvm_cpu_vmxoff(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9185 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9186 | cpu = get_cpu(); |
| 9187 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 9188 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 9189 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9190 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9191 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9192 | if (err) |
| 9193 | goto free_vmcs; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9194 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 9195 | err = alloc_apic_access_page(kvm); |
| 9196 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 9197 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 9198 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9199 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9200 | if (enable_ept) { |
| 9201 | if (!kvm->arch.ept_identity_map_addr) |
| 9202 | kvm->arch.ept_identity_map_addr = |
| 9203 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 9204 | err = init_rmode_identity_map(kvm); |
| 9205 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 9206 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9207 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 9208 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9209 | if (nested) { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9210 | nested_vmx_setup_ctls_msrs(vmx); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9211 | vmx->nested.vpid02 = allocate_vpid(); |
| 9212 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9213 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9214 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 9215 | vmx->nested.current_vmptr = -1ull; |
| 9216 | vmx->nested.current_vmcs12 = NULL; |
| 9217 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9218 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 9219 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 9220 | /* |
| 9221 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 9222 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 9223 | */ |
| 9224 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 9225 | vmx->pi_desc.sn = 1; |
| 9226 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9227 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9228 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9229 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9230 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 9231 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9232 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9233 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9234 | free_pml: |
| 9235 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9236 | uninit_vcpu: |
| 9237 | kvm_vcpu_uninit(&vmx->vcpu); |
| 9238 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9239 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9240 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9241 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9242 | } |
| 9243 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 9244 | static void __init vmx_check_processor_compat(void *rtn) |
| 9245 | { |
| 9246 | struct vmcs_config vmcs_conf; |
| 9247 | |
| 9248 | *(int *)rtn = 0; |
| 9249 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 9250 | *(int *)rtn = -EIO; |
| 9251 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 9252 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 9253 | smp_processor_id()); |
| 9254 | *(int *)rtn = -EIO; |
| 9255 | } |
| 9256 | } |
| 9257 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 9258 | static int get_ept_level(void) |
| 9259 | { |
| 9260 | return VMX_EPT_DEFAULT_GAW + 1; |
| 9261 | } |
| 9262 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9263 | 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] | 9264 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9265 | u8 cache; |
| 9266 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9267 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9268 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9269 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9270 | * 2. EPT with VT-d: |
| 9271 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9272 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9273 | * b. VT-d with snooping control feature: snooping control feature of |
| 9274 | * VT-d engine can guarantee the cache correctness. Just set it |
| 9275 | * 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] | 9276 | * 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] | 9277 | * consistent with host MTRR |
| 9278 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9279 | if (is_mmio) { |
| 9280 | cache = MTRR_TYPE_UNCACHABLE; |
| 9281 | goto exit; |
| 9282 | } |
| 9283 | |
| 9284 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9285 | ipat = VMX_EPT_IPAT_BIT; |
| 9286 | cache = MTRR_TYPE_WRBACK; |
| 9287 | goto exit; |
| 9288 | } |
| 9289 | |
| 9290 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 9291 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 9292 | 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] | 9293 | cache = MTRR_TYPE_WRBACK; |
| 9294 | else |
| 9295 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9296 | goto exit; |
| 9297 | } |
| 9298 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 9299 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9300 | |
| 9301 | exit: |
| 9302 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 9303 | } |
| 9304 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 9305 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9306 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 9307 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 9308 | return PT_DIRECTORY_LEVEL; |
| 9309 | else |
| 9310 | /* For shadow and EPT supported 1GB page */ |
| 9311 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9312 | } |
| 9313 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9314 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 9315 | { |
| 9316 | /* |
| 9317 | * These bits in the secondary execution controls field |
| 9318 | * are dynamic, the others are mostly based on the hypervisor |
| 9319 | * architecture and the guest's CPUID. Do not touch the |
| 9320 | * dynamic bits. |
| 9321 | */ |
| 9322 | u32 mask = |
| 9323 | SECONDARY_EXEC_SHADOW_VMCS | |
| 9324 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 9325 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9326 | |
| 9327 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9328 | |
| 9329 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 9330 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 9331 | } |
| 9332 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9333 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 9334 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9335 | struct kvm_cpuid_entry2 *best; |
| 9336 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9337 | u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9338 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9339 | if (vmx_rdtscp_supported()) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9340 | bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu); |
| 9341 | if (!rdtscp_enabled) |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9342 | secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP; |
Xiao Guangrong | f36201e | 2015-09-09 14:05:53 +0800 | [diff] [blame] | 9343 | |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9344 | if (nested) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9345 | if (rdtscp_enabled) |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9346 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 9347 | SECONDARY_EXEC_RDTSCP; |
| 9348 | else |
| 9349 | vmx->nested.nested_vmx_secondary_ctls_high &= |
| 9350 | ~SECONDARY_EXEC_RDTSCP; |
| 9351 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9352 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9353 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9354 | /* Exposing INVPCID only when PCID is exposed */ |
| 9355 | best = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 9356 | if (vmx_invpcid_supported() && |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9357 | (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) || |
| 9358 | !guest_cpuid_has_pcid(vcpu))) { |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9359 | secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9360 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9361 | if (best) |
Ren, Yongjie | 4f97704 | 2012-09-07 07:36:59 +0000 | [diff] [blame] | 9362 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9363 | } |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 9364 | |
Huaitong Han | 45bdbcf | 2016-01-12 16:04:20 +0800 | [diff] [blame] | 9365 | if (cpu_has_secondary_exec_ctrls()) |
| 9366 | vmcs_set_secondary_exec_control(secondary_exec_ctl); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9367 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9368 | if (nested_vmx_allowed(vcpu)) |
| 9369 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 9370 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 9371 | else |
| 9372 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 9373 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9374 | } |
| 9375 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9376 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 9377 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 9378 | if (func == 1 && nested) |
| 9379 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9380 | } |
| 9381 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9382 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 9383 | struct x86_exception *fault) |
| 9384 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9385 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9386 | u32 exit_reason; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9387 | |
| 9388 | if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9389 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9390 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9391 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 9392 | nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9393 | vmcs12->guest_physical_address = fault->address; |
| 9394 | } |
| 9395 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9396 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 9397 | |
| 9398 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 9399 | { |
| 9400 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 9401 | return get_vmcs12(vcpu)->ept_pointer; |
| 9402 | } |
| 9403 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 9404 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9405 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 9406 | WARN_ON(mmu_is_nested(vcpu)); |
| 9407 | kvm_init_shadow_ept_mmu(vcpu, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9408 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
| 9409 | VMX_EPT_EXECUTE_ONLY_BIT); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9410 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 9411 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 9412 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 9413 | |
| 9414 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9415 | } |
| 9416 | |
| 9417 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 9418 | { |
| 9419 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 9420 | } |
| 9421 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9422 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 9423 | u16 error_code) |
| 9424 | { |
| 9425 | bool inequality, bit; |
| 9426 | |
| 9427 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 9428 | inequality = |
| 9429 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 9430 | vmcs12->page_fault_error_code_match; |
| 9431 | return inequality ^ bit; |
| 9432 | } |
| 9433 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9434 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 9435 | struct x86_exception *fault) |
| 9436 | { |
| 9437 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9438 | |
| 9439 | WARN_ON(!is_guest_mode(vcpu)); |
| 9440 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9441 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9442 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
| 9443 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9444 | vmcs_readl(EXIT_QUALIFICATION)); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9445 | else |
| 9446 | kvm_inject_page_fault(vcpu, fault); |
| 9447 | } |
| 9448 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9449 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
| 9450 | struct vmcs12 *vmcs12) |
| 9451 | { |
| 9452 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9453 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9454 | |
| 9455 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9456 | if (!PAGE_ALIGNED(vmcs12->apic_access_addr) || |
| 9457 | vmcs12->apic_access_addr >> maxphyaddr) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9458 | return false; |
| 9459 | |
| 9460 | /* |
| 9461 | * Translate L1 physical address to host physical |
| 9462 | * address for vmcs02. Keep the page pinned, so this |
| 9463 | * physical address remains valid. We keep a reference |
| 9464 | * to it so we can release it later. |
| 9465 | */ |
| 9466 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 9467 | nested_release_page(vmx->nested.apic_access_page); |
| 9468 | vmx->nested.apic_access_page = |
| 9469 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 9470 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9471 | |
| 9472 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9473 | if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) || |
| 9474 | vmcs12->virtual_apic_page_addr >> maxphyaddr) |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9475 | return false; |
| 9476 | |
| 9477 | if (vmx->nested.virtual_apic_page) /* shouldn't happen */ |
| 9478 | nested_release_page(vmx->nested.virtual_apic_page); |
| 9479 | vmx->nested.virtual_apic_page = |
| 9480 | nested_get_page(vcpu, vmcs12->virtual_apic_page_addr); |
| 9481 | |
| 9482 | /* |
| 9483 | * Failing the vm entry is _not_ what the processor does |
| 9484 | * but it's basically the only possibility we have. |
| 9485 | * We could still enter the guest if CR8 load exits are |
| 9486 | * enabled, CR8 store exits are enabled, and virtualize APIC |
| 9487 | * access is disabled; in this case the processor would never |
| 9488 | * use the TPR shadow and we could simply clear the bit from |
| 9489 | * the execution control. But such a configuration is useless, |
| 9490 | * so let's keep the code simple. |
| 9491 | */ |
| 9492 | if (!vmx->nested.virtual_apic_page) |
| 9493 | return false; |
| 9494 | } |
| 9495 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9496 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9497 | if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) || |
| 9498 | vmcs12->posted_intr_desc_addr >> maxphyaddr) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9499 | return false; |
| 9500 | |
| 9501 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 9502 | kunmap(vmx->nested.pi_desc_page); |
| 9503 | nested_release_page(vmx->nested.pi_desc_page); |
| 9504 | } |
| 9505 | vmx->nested.pi_desc_page = |
| 9506 | nested_get_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 9507 | if (!vmx->nested.pi_desc_page) |
| 9508 | return false; |
| 9509 | |
| 9510 | vmx->nested.pi_desc = |
| 9511 | (struct pi_desc *)kmap(vmx->nested.pi_desc_page); |
| 9512 | if (!vmx->nested.pi_desc) { |
| 9513 | nested_release_page_clean(vmx->nested.pi_desc_page); |
| 9514 | return false; |
| 9515 | } |
| 9516 | vmx->nested.pi_desc = |
| 9517 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 9518 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9519 | (PAGE_SIZE - 1))); |
| 9520 | } |
| 9521 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9522 | return true; |
| 9523 | } |
| 9524 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9525 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 9526 | { |
| 9527 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 9528 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9529 | |
| 9530 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 9531 | return; |
| 9532 | |
| 9533 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 9534 | * hrtimer_start does not guarantee this. */ |
| 9535 | if (preemption_timeout <= 1) { |
| 9536 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 9537 | return; |
| 9538 | } |
| 9539 | |
| 9540 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 9541 | preemption_timeout *= 1000000; |
| 9542 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 9543 | hrtimer_start(&vmx->nested.preemption_timer, |
| 9544 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 9545 | } |
| 9546 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9547 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 9548 | struct vmcs12 *vmcs12) |
| 9549 | { |
| 9550 | int maxphyaddr; |
| 9551 | u64 addr; |
| 9552 | |
| 9553 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 9554 | return 0; |
| 9555 | |
| 9556 | if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) { |
| 9557 | WARN_ON(1); |
| 9558 | return -EINVAL; |
| 9559 | } |
| 9560 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 9561 | |
| 9562 | if (!PAGE_ALIGNED(vmcs12->msr_bitmap) || |
| 9563 | ((addr + PAGE_SIZE) >> maxphyaddr)) |
| 9564 | return -EINVAL; |
| 9565 | |
| 9566 | return 0; |
| 9567 | } |
| 9568 | |
| 9569 | /* |
| 9570 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 9571 | * we do not use the hardware. |
| 9572 | */ |
| 9573 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
| 9574 | struct vmcs12 *vmcs12) |
| 9575 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9576 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9577 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9578 | unsigned long *msr_bitmap_l1; |
| 9579 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9580 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9581 | /* This shortcut is ok because we support only x2APIC MSRs so far. */ |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9582 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12)) |
| 9583 | return false; |
| 9584 | |
| 9585 | page = nested_get_page(vcpu, vmcs12->msr_bitmap); |
Radim Krčmář | 215df1f | 2017-03-07 17:51:49 +0100 | [diff] [blame] | 9586 | if (!page) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9587 | return false; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9588 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
| 9589 | if (!msr_bitmap_l1) { |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9590 | nested_release_page_clean(page); |
| 9591 | WARN_ON(1); |
| 9592 | return false; |
| 9593 | } |
| 9594 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9595 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
| 9596 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9597 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9598 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
| 9599 | for (msr = 0x800; msr <= 0x8ff; msr++) |
| 9600 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9601 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9602 | msr, MSR_TYPE_R); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9603 | |
| 9604 | nested_vmx_disable_intercept_for_msr( |
| 9605 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9606 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
| 9607 | MSR_TYPE_R | MSR_TYPE_W); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9608 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9609 | if (nested_cpu_has_vid(vmcs12)) { |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9610 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9611 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9612 | APIC_BASE_MSR + (APIC_EOI >> 4), |
| 9613 | MSR_TYPE_W); |
| 9614 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9615 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9616 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
| 9617 | MSR_TYPE_W); |
| 9618 | } |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9619 | } |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9620 | kunmap(page); |
| 9621 | nested_release_page_clean(page); |
| 9622 | |
| 9623 | return true; |
| 9624 | } |
| 9625 | |
| 9626 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 9627 | struct vmcs12 *vmcs12) |
| 9628 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9629 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9630 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9631 | !nested_cpu_has_vid(vmcs12) && |
| 9632 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9633 | return 0; |
| 9634 | |
| 9635 | /* |
| 9636 | * If virtualize x2apic mode is enabled, |
| 9637 | * virtualize apic access must be disabled. |
| 9638 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9639 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 9640 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9641 | return -EINVAL; |
| 9642 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9643 | /* |
| 9644 | * If virtual interrupt delivery is enabled, |
| 9645 | * we must exit on external interrupts. |
| 9646 | */ |
| 9647 | if (nested_cpu_has_vid(vmcs12) && |
| 9648 | !nested_exit_on_intr(vcpu)) |
| 9649 | return -EINVAL; |
| 9650 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9651 | /* |
| 9652 | * bits 15:8 should be zero in posted_intr_nv, |
| 9653 | * the descriptor address has been already checked |
| 9654 | * in nested_get_vmcs12_pages. |
| 9655 | */ |
| 9656 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 9657 | (!nested_cpu_has_vid(vmcs12) || |
| 9658 | !nested_exit_intr_ack_set(vcpu) || |
| 9659 | vmcs12->posted_intr_nv & 0xff00)) |
| 9660 | return -EINVAL; |
| 9661 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9662 | /* tpr shadow is needed by all apicv features. */ |
| 9663 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9664 | return -EINVAL; |
| 9665 | |
| 9666 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9667 | } |
| 9668 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9669 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 9670 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9671 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9672 | { |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9673 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9674 | u64 count, addr; |
| 9675 | |
| 9676 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 9677 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 9678 | WARN_ON(1); |
| 9679 | return -EINVAL; |
| 9680 | } |
| 9681 | if (count == 0) |
| 9682 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9683 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9684 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 9685 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9686 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9687 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 9688 | addr_field, maxphyaddr, count, addr); |
| 9689 | return -EINVAL; |
| 9690 | } |
| 9691 | return 0; |
| 9692 | } |
| 9693 | |
| 9694 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 9695 | struct vmcs12 *vmcs12) |
| 9696 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9697 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 9698 | vmcs12->vm_exit_msr_store_count == 0 && |
| 9699 | vmcs12->vm_entry_msr_load_count == 0) |
| 9700 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9701 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9702 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9703 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9704 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9705 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9706 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9707 | return -EINVAL; |
| 9708 | return 0; |
| 9709 | } |
| 9710 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9711 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 9712 | struct vmx_msr_entry *e) |
| 9713 | { |
| 9714 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 9715 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9716 | return -EINVAL; |
| 9717 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 9718 | e->index == MSR_IA32_UCODE_REV) |
| 9719 | return -EINVAL; |
| 9720 | if (e->reserved != 0) |
| 9721 | return -EINVAL; |
| 9722 | return 0; |
| 9723 | } |
| 9724 | |
| 9725 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 9726 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9727 | { |
| 9728 | if (e->index == MSR_FS_BASE || |
| 9729 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9730 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 9731 | nested_vmx_msr_check_common(vcpu, e)) |
| 9732 | return -EINVAL; |
| 9733 | return 0; |
| 9734 | } |
| 9735 | |
| 9736 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 9737 | struct vmx_msr_entry *e) |
| 9738 | { |
| 9739 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 9740 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9741 | return -EINVAL; |
| 9742 | return 0; |
| 9743 | } |
| 9744 | |
| 9745 | /* |
| 9746 | * Load guest's/host's msr at nested entry/exit. |
| 9747 | * return 0 for success, entry index for failure. |
| 9748 | */ |
| 9749 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 9750 | { |
| 9751 | u32 i; |
| 9752 | struct vmx_msr_entry e; |
| 9753 | struct msr_data msr; |
| 9754 | |
| 9755 | msr.host_initiated = false; |
| 9756 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9757 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 9758 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9759 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9760 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 9761 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9762 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9763 | } |
| 9764 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9765 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9766 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 9767 | __func__, i, e.index, e.reserved); |
| 9768 | goto fail; |
| 9769 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9770 | msr.index = e.index; |
| 9771 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9772 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9773 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9774 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 9775 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9776 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9777 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9778 | } |
| 9779 | return 0; |
| 9780 | fail: |
| 9781 | return i + 1; |
| 9782 | } |
| 9783 | |
| 9784 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 9785 | { |
| 9786 | u32 i; |
| 9787 | struct vmx_msr_entry e; |
| 9788 | |
| 9789 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9790 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9791 | if (kvm_vcpu_read_guest(vcpu, |
| 9792 | gpa + i * sizeof(e), |
| 9793 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9794 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9795 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 9796 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9797 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9798 | } |
| 9799 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9800 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9801 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 9802 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9803 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9804 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9805 | msr_info.host_initiated = false; |
| 9806 | msr_info.index = e.index; |
| 9807 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9808 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9809 | "%s cannot read MSR (%u, 0x%x)\n", |
| 9810 | __func__, i, e.index); |
| 9811 | return -EINVAL; |
| 9812 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9813 | if (kvm_vcpu_write_guest(vcpu, |
| 9814 | gpa + i * sizeof(e) + |
| 9815 | offsetof(struct vmx_msr_entry, value), |
| 9816 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9817 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9818 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9819 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9820 | return -EINVAL; |
| 9821 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9822 | } |
| 9823 | return 0; |
| 9824 | } |
| 9825 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9826 | /* |
| 9827 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 9828 | * 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] | 9829 | * 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] | 9830 | * guest in a way that will both be appropriate to L1's requests, and our |
| 9831 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 9832 | * function also has additional necessary side-effects, like setting various |
| 9833 | * vcpu->arch fields. |
| 9834 | */ |
| 9835 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 9836 | { |
| 9837 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9838 | u32 exec_control; |
| 9839 | |
| 9840 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 9841 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 9842 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 9843 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 9844 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 9845 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 9846 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 9847 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 9848 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 9849 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 9850 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 9851 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 9852 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 9853 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 9854 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 9855 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 9856 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 9857 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 9858 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 9859 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 9860 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 9861 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 9862 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 9863 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 9864 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 9865 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 9866 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 9867 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 9868 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 9869 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 9870 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 9871 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 9872 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 9873 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 9874 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 9875 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 9876 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 9877 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 9878 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 9879 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 9880 | } else { |
| 9881 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 9882 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 9883 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9884 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 9885 | vmcs12->vm_entry_intr_info_field); |
| 9886 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 9887 | vmcs12->vm_entry_exception_error_code); |
| 9888 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 9889 | vmcs12->vm_entry_instruction_len); |
| 9890 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 9891 | vmcs12->guest_interruptibility_info); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9892 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 9893 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9894 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 9895 | vmcs12->guest_pending_dbg_exceptions); |
| 9896 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 9897 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 9898 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9899 | if (nested_cpu_has_xsaves(vmcs12)) |
| 9900 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9901 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 9902 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9903 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9904 | |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 9905 | /* Preemption timer setting is only taken from vmcs01. */ |
| 9906 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 9907 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 9908 | if (vmx->hv_deadline_tsc == -1) |
| 9909 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 9910 | |
| 9911 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9912 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 9913 | /* |
| 9914 | * Note that we use L0's vector here and in |
| 9915 | * vmx_deliver_nested_posted_interrupt. |
| 9916 | */ |
| 9917 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 9918 | vmx->nested.pi_pending = false; |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 9919 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9920 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 9921 | page_to_phys(vmx->nested.pi_desc_page) + |
| 9922 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9923 | (PAGE_SIZE - 1))); |
| 9924 | } else |
| 9925 | exec_control &= ~PIN_BASED_POSTED_INTR; |
| 9926 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9927 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9928 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9929 | vmx->nested.preemption_timer_expired = false; |
| 9930 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 9931 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 9932 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9933 | /* |
| 9934 | * Whether page-faults are trapped is determined by a combination of |
| 9935 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 9936 | * If enable_ept, L0 doesn't care about page faults and we should |
| 9937 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 9938 | * care about (at least some) page faults, and because it is not easy |
| 9939 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 9940 | * to exit on each and every L2 page fault. This is done by setting |
| 9941 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 9942 | * Note that below we don't need special code to set EB.PF beyond the |
| 9943 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 9944 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 9945 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 9946 | * |
| 9947 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 9948 | * injected with more page faults than it asked for. This could have |
| 9949 | * caused problems, but in practice existing hypervisors don't care. |
| 9950 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 9951 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 9952 | */ |
| 9953 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 9954 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 9955 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 9956 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 9957 | |
| 9958 | if (cpu_has_secondary_exec_ctrls()) { |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9959 | exec_control = vmx_secondary_exec_control(vmx); |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 9960 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9961 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 9962 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 9963 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 9964 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 9965 | SECONDARY_EXEC_APIC_REGISTER_VIRT); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9966 | if (nested_cpu_has(vmcs12, |
| 9967 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 9968 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 9969 | |
| 9970 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 9971 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9972 | * If translation failed, no matter: This feature asks |
| 9973 | * to exit when accessing the given address, and if it |
| 9974 | * can never be accessed, this feature won't do |
| 9975 | * anything anyway. |
| 9976 | */ |
| 9977 | if (!vmx->nested.apic_access_page) |
| 9978 | exec_control &= |
| 9979 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9980 | else |
| 9981 | vmcs_write64(APIC_ACCESS_ADDR, |
| 9982 | page_to_phys(vmx->nested.apic_access_page)); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9983 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9984 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | ca3f257 | 2013-12-16 12:55:46 +0100 | [diff] [blame] | 9985 | exec_control |= |
| 9986 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9987 | kvm_vcpu_reload_apic_access_page(vcpu); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9988 | } |
| 9989 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9990 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
| 9991 | vmcs_write64(EOI_EXIT_BITMAP0, |
| 9992 | vmcs12->eoi_exit_bitmap0); |
| 9993 | vmcs_write64(EOI_EXIT_BITMAP1, |
| 9994 | vmcs12->eoi_exit_bitmap1); |
| 9995 | vmcs_write64(EOI_EXIT_BITMAP2, |
| 9996 | vmcs12->eoi_exit_bitmap2); |
| 9997 | vmcs_write64(EOI_EXIT_BITMAP3, |
| 9998 | vmcs12->eoi_exit_bitmap3); |
| 9999 | vmcs_write16(GUEST_INTR_STATUS, |
| 10000 | vmcs12->guest_intr_status); |
| 10001 | } |
| 10002 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10003 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 10004 | } |
| 10005 | |
| 10006 | |
| 10007 | /* |
| 10008 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 10009 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 10010 | * Other fields are different per CPU, and will be set later when |
| 10011 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 10012 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10013 | vmx_set_constant_host_state(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10014 | |
| 10015 | /* |
| 10016 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 10017 | * entry, but only if the current (host) sp changed from the value |
| 10018 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 10019 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 10020 | * here we just force the write to happen on entry. |
| 10021 | */ |
| 10022 | vmx->host_rsp = 0; |
| 10023 | |
| 10024 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 10025 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 10026 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 10027 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 10028 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10029 | |
| 10030 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
| 10031 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
| 10032 | page_to_phys(vmx->nested.virtual_apic_page)); |
| 10033 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 86ef97b | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 10034 | } else { |
| 10035 | #ifdef CONFIG_X86_64 |
| 10036 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 10037 | CPU_BASED_CR8_STORE_EXITING; |
| 10038 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10039 | } |
| 10040 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10041 | if (cpu_has_vmx_msr_bitmap() && |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10042 | exec_control & CPU_BASED_USE_MSR_BITMAPS && |
| 10043 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) |
| 10044 | ; /* MSR_BITMAP will be set by following vmx_set_efer. */ |
| 10045 | else |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10046 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 10047 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10048 | /* |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10049 | * Merging of IO bitmap not currently supported. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10050 | * Rather, exit every time. |
| 10051 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10052 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 10053 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 10054 | |
| 10055 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 10056 | |
| 10057 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 10058 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 10059 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 10060 | */ |
| 10061 | update_exception_bitmap(vcpu); |
| 10062 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 10063 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10064 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10065 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 10066 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 10067 | * bits are further modified by vmx_set_efer() below. |
| 10068 | */ |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10069 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10070 | |
| 10071 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 10072 | * emulated by vmx_set_efer(), below. |
| 10073 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10074 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10075 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 10076 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10077 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 10078 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10079 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10080 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10081 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 10082 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10083 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 10084 | |
| 10085 | |
| 10086 | set_cr4_guest_host_mask(vmx); |
| 10087 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10088 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) |
| 10089 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 10090 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10091 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 10092 | vmcs_write64(TSC_OFFSET, |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10093 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10094 | else |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10095 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 10096 | if (kvm_has_tsc_control) |
| 10097 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10098 | |
| 10099 | if (enable_vpid) { |
| 10100 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10101 | * There is no direct mapping between vpid02 and vpid12, the |
| 10102 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 10103 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 10104 | * The vpid12 is allocated by L1 for L2, so it will not |
| 10105 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 10106 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10107 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10108 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
| 10109 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 10110 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 10111 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 10112 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); |
| 10113 | } |
| 10114 | } else { |
| 10115 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 10116 | vmx_flush_tlb(vcpu); |
| 10117 | } |
| 10118 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10119 | } |
| 10120 | |
Ladi Prosek | 560a979 | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 10121 | if (enable_pml) { |
| 10122 | /* |
| 10123 | * Conceptually we want to copy the PML address and index from |
| 10124 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 10125 | * since we always flush the log on each vmexit, this happens |
| 10126 | * to be equivalent to simply resetting the fields in vmcs02. |
| 10127 | */ |
| 10128 | ASSERT(vmx->pml_pg); |
| 10129 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 10130 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 10131 | } |
| 10132 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10133 | if (nested_cpu_has_ept(vmcs12)) { |
| 10134 | kvm_mmu_unload(vcpu); |
| 10135 | nested_ept_init_mmu_context(vcpu); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 10136 | } else if (nested_cpu_has2(vmcs12, |
| 10137 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 10138 | vmx_flush_tlb_ept_only(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10139 | } |
| 10140 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10141 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 10142 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 10143 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10144 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10145 | else |
| 10146 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10147 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 10148 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10149 | |
| 10150 | /* |
| 10151 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 10152 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 10153 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 10154 | * the specifications by L1; It's not enough to take |
| 10155 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 10156 | * have more bits than L1 expected. |
| 10157 | */ |
| 10158 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 10159 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 10160 | |
| 10161 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 10162 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 10163 | |
| 10164 | /* shadow page tables on either EPT or shadow page tables */ |
| 10165 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 10166 | kvm_mmu_reset_context(vcpu); |
| 10167 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10168 | if (!enable_ept) |
| 10169 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 10170 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10171 | /* |
| 10172 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 10173 | */ |
| 10174 | if (enable_ept) { |
| 10175 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 10176 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 10177 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 10178 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 10179 | } |
| 10180 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10181 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 10182 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 10183 | } |
| 10184 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10185 | /* |
| 10186 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 10187 | * for running an L2 nested guest. |
| 10188 | */ |
| 10189 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 10190 | { |
| 10191 | struct vmcs12 *vmcs12; |
| 10192 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10193 | int cpu; |
| 10194 | struct loaded_vmcs *vmcs02; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 10195 | bool ia32e; |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10196 | u32 msr_entry_idx; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10197 | |
| 10198 | if (!nested_vmx_check_permission(vcpu) || |
| 10199 | !nested_vmx_check_vmcs12(vcpu)) |
| 10200 | return 1; |
| 10201 | |
| 10202 | skip_emulated_instruction(vcpu); |
| 10203 | vmcs12 = get_vmcs12(vcpu); |
| 10204 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10205 | if (enable_shadow_vmcs) |
| 10206 | copy_shadow_to_vmcs12(vmx); |
| 10207 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10208 | /* |
| 10209 | * The nested entry process starts with enforcing various prerequisites |
| 10210 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 10211 | * they fail: As the SDM explains, some conditions should cause the |
| 10212 | * instruction to fail, while others will cause the instruction to seem |
| 10213 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 10214 | * To speed up the normal (success) code path, we should avoid checking |
| 10215 | * for misconfigurations which will anyway be caught by the processor |
| 10216 | * when using the merged vmcs02. |
| 10217 | */ |
| 10218 | if (vmcs12->launch_state == launch) { |
| 10219 | nested_vmx_failValid(vcpu, |
| 10220 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 10221 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 10222 | return 1; |
| 10223 | } |
| 10224 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10225 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 10226 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) { |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 10227 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10228 | return 1; |
| 10229 | } |
| 10230 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10231 | if (!nested_get_vmcs12_pages(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10232 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10233 | return 1; |
| 10234 | } |
| 10235 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10236 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10237 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10238 | return 1; |
| 10239 | } |
| 10240 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10241 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) { |
| 10242 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10243 | return 1; |
| 10244 | } |
| 10245 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10246 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) { |
| 10247 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10248 | return 1; |
| 10249 | } |
| 10250 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10251 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10252 | vmx->nested.nested_vmx_true_procbased_ctls_low, |
| 10253 | vmx->nested.nested_vmx_procbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10254 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10255 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 10256 | vmx->nested.nested_vmx_secondary_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10257 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10258 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 10259 | vmx->nested.nested_vmx_pinbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10260 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10261 | vmx->nested.nested_vmx_true_exit_ctls_low, |
| 10262 | vmx->nested.nested_vmx_exit_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10263 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10264 | vmx->nested.nested_vmx_true_entry_ctls_low, |
| 10265 | vmx->nested.nested_vmx_entry_ctls_high)) |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10266 | { |
| 10267 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10268 | return 1; |
| 10269 | } |
| 10270 | |
| 10271 | if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 10272 | ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 10273 | nested_vmx_failValid(vcpu, |
| 10274 | VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 10275 | return 1; |
| 10276 | } |
| 10277 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10278 | if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10279 | ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 10280 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10281 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10282 | return 1; |
| 10283 | } |
| 10284 | if (vmcs12->vmcs_link_pointer != -1ull) { |
| 10285 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10286 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR); |
| 10287 | return 1; |
| 10288 | } |
| 10289 | |
| 10290 | /* |
Jan Kiszka | cb0c8cda | 2013-04-27 12:58:00 +0200 | [diff] [blame] | 10291 | * 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] | 10292 | * are performed on the field for the IA32_EFER MSR: |
| 10293 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 10294 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 10295 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 10296 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 10297 | * CR0.PG) is 1. |
| 10298 | */ |
| 10299 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) { |
| 10300 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 10301 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 10302 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 10303 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 10304 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) { |
| 10305 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10306 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10307 | return 1; |
| 10308 | } |
| 10309 | } |
| 10310 | |
| 10311 | /* |
| 10312 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 10313 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 10314 | * the values of the LMA and LME bits in the field must each be that of |
| 10315 | * the host address-space size VM-exit control. |
| 10316 | */ |
| 10317 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 10318 | ia32e = (vmcs12->vm_exit_controls & |
| 10319 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 10320 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 10321 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 10322 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) { |
| 10323 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10324 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10325 | return 1; |
| 10326 | } |
| 10327 | } |
| 10328 | |
| 10329 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10330 | * We're finally done with prerequisite checking, and can start with |
| 10331 | * the nested entry. |
| 10332 | */ |
| 10333 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10334 | vmcs02 = nested_get_current_vmcs02(vmx); |
| 10335 | if (!vmcs02) |
| 10336 | return -ENOMEM; |
| 10337 | |
| 10338 | enter_guest_mode(vcpu); |
| 10339 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10340 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 10341 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10342 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10343 | cpu = get_cpu(); |
| 10344 | vmx->loaded_vmcs = vmcs02; |
| 10345 | vmx_vcpu_put(vcpu); |
| 10346 | vmx_vcpu_load(vcpu, cpu); |
| 10347 | vcpu->cpu = cpu; |
| 10348 | put_cpu(); |
| 10349 | |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 10350 | vmx_segment_cache_clear(vmx); |
| 10351 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10352 | prepare_vmcs02(vcpu, vmcs12); |
| 10353 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10354 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 10355 | vmcs12->vm_entry_msr_load_addr, |
| 10356 | vmcs12->vm_entry_msr_load_count); |
| 10357 | if (msr_entry_idx) { |
| 10358 | leave_guest_mode(vcpu); |
| 10359 | vmx_load_vmcs01(vcpu); |
| 10360 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10361 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); |
| 10362 | return 1; |
| 10363 | } |
| 10364 | |
| 10365 | vmcs12->launch_state = 1; |
| 10366 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10367 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 10368 | return kvm_vcpu_halt(vcpu); |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10369 | |
Jan Kiszka | 7af40ad3 | 2014-01-04 18:47:23 +0100 | [diff] [blame] | 10370 | vmx->nested.nested_run_pending = 1; |
| 10371 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10372 | /* |
| 10373 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 10374 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 10375 | * returned as far as L1 is concerned. It will only return (and set |
| 10376 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 10377 | */ |
| 10378 | return 1; |
| 10379 | } |
| 10380 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10381 | /* |
| 10382 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 10383 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 10384 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 10385 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 10386 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 10387 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 10388 | * didn't trap the bit, because if L1 did, so would L0). |
| 10389 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 10390 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 10391 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 10392 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 10393 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 10394 | * changed these bits, and therefore they need to be updated, but L0 |
| 10395 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 10396 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 10397 | */ |
| 10398 | static inline unsigned long |
| 10399 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10400 | { |
| 10401 | return |
| 10402 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 10403 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 10404 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 10405 | vcpu->arch.cr0_guest_owned_bits)); |
| 10406 | } |
| 10407 | |
| 10408 | static inline unsigned long |
| 10409 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10410 | { |
| 10411 | return |
| 10412 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 10413 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 10414 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 10415 | vcpu->arch.cr4_guest_owned_bits)); |
| 10416 | } |
| 10417 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10418 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 10419 | struct vmcs12 *vmcs12) |
| 10420 | { |
| 10421 | u32 idt_vectoring; |
| 10422 | unsigned int nr; |
| 10423 | |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10424 | if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10425 | nr = vcpu->arch.exception.nr; |
| 10426 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10427 | |
| 10428 | if (kvm_exception_is_soft(nr)) { |
| 10429 | vmcs12->vm_exit_instruction_len = |
| 10430 | vcpu->arch.event_exit_inst_len; |
| 10431 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 10432 | } else |
| 10433 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 10434 | |
| 10435 | if (vcpu->arch.exception.has_error_code) { |
| 10436 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 10437 | vmcs12->idt_vectoring_error_code = |
| 10438 | vcpu->arch.exception.error_code; |
| 10439 | } |
| 10440 | |
| 10441 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 10442 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10443 | vmcs12->idt_vectoring_info_field = |
| 10444 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 10445 | } else if (vcpu->arch.interrupt.pending) { |
| 10446 | nr = vcpu->arch.interrupt.nr; |
| 10447 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10448 | |
| 10449 | if (vcpu->arch.interrupt.soft) { |
| 10450 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 10451 | vmcs12->vm_entry_instruction_len = |
| 10452 | vcpu->arch.event_exit_inst_len; |
| 10453 | } else |
| 10454 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 10455 | |
| 10456 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 10457 | } |
| 10458 | } |
| 10459 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10460 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 10461 | { |
| 10462 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10463 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10464 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 10465 | vmx->nested.preemption_timer_expired) { |
| 10466 | if (vmx->nested.nested_run_pending) |
| 10467 | return -EBUSY; |
| 10468 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 10469 | return 0; |
| 10470 | } |
| 10471 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10472 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Jan Kiszka | 220c567 | 2014-03-07 20:03:14 +0100 | [diff] [blame] | 10473 | if (vmx->nested.nested_run_pending || |
| 10474 | vcpu->arch.interrupt.pending) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10475 | return -EBUSY; |
| 10476 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10477 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 10478 | INTR_INFO_VALID_MASK, 0); |
| 10479 | /* |
| 10480 | * The NMI-triggered VM exit counts as injection: |
| 10481 | * clear this one and block further NMIs. |
| 10482 | */ |
| 10483 | vcpu->arch.nmi_pending = 0; |
| 10484 | vmx_set_nmi_mask(vcpu, true); |
| 10485 | return 0; |
| 10486 | } |
| 10487 | |
| 10488 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 10489 | nested_exit_on_intr(vcpu)) { |
| 10490 | if (vmx->nested.nested_run_pending) |
| 10491 | return -EBUSY; |
| 10492 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10493 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10494 | } |
| 10495 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10496 | return vmx_complete_nested_posted_interrupt(vcpu); |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10497 | } |
| 10498 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10499 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 10500 | { |
| 10501 | ktime_t remaining = |
| 10502 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 10503 | u64 value; |
| 10504 | |
| 10505 | if (ktime_to_ns(remaining) <= 0) |
| 10506 | return 0; |
| 10507 | |
| 10508 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 10509 | do_div(value, 1000000); |
| 10510 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10511 | } |
| 10512 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10513 | /* |
| 10514 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 10515 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 10516 | * and this function updates it to reflect the changes to the guest state while |
| 10517 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 10518 | * without going back to L1), and to reflect the exit reason. |
| 10519 | * Note that we do not have to copy here all VMCS fields, just those that |
| 10520 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 10521 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 10522 | * which already writes to vmcs12 directly. |
| 10523 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10524 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 10525 | u32 exit_reason, u32 exit_intr_info, |
| 10526 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10527 | { |
| 10528 | /* update guest state fields: */ |
| 10529 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 10530 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 10531 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10532 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 10533 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 10534 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 10535 | |
| 10536 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 10537 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 10538 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 10539 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 10540 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 10541 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 10542 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 10543 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 10544 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 10545 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 10546 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 10547 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 10548 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 10549 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 10550 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 10551 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 10552 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 10553 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 10554 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 10555 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 10556 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 10557 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 10558 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 10559 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 10560 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 10561 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 10562 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 10563 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 10564 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 10565 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 10566 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 10567 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 10568 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 10569 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 10570 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 10571 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 10572 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10573 | vmcs12->guest_interruptibility_info = |
| 10574 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 10575 | vmcs12->guest_pending_dbg_exceptions = |
| 10576 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 10577 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 10578 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 10579 | else |
| 10580 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10581 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10582 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 10583 | if (vmcs12->vm_exit_controls & |
| 10584 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 10585 | vmcs12->vmx_preemption_timer_value = |
| 10586 | vmx_get_preemption_timer_value(vcpu); |
| 10587 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 10588 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 10589 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10590 | /* |
| 10591 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 10592 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 10593 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 10594 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 10595 | * |
| 10596 | * Additionally, restore L2's PDPTR to vmcs12. |
| 10597 | */ |
| 10598 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 10599 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10600 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 10601 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 10602 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 10603 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 10604 | } |
| 10605 | |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 10606 | if (nested_cpu_has_ept(vmcs12)) |
| 10607 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 10608 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10609 | if (nested_cpu_has_vid(vmcs12)) |
| 10610 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 10611 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10612 | vmcs12->vm_entry_controls = |
| 10613 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10614 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10615 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10616 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 10617 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 10618 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10619 | } |
| 10620 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10621 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 10622 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 10623 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10624 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 10625 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 10626 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10627 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 10628 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 10629 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 10630 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10631 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 10632 | if (nested_cpu_has_xsaves(vmcs12)) |
| 10633 | vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10634 | |
| 10635 | /* update exit information fields: */ |
| 10636 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10637 | vmcs12->vm_exit_reason = exit_reason; |
| 10638 | vmcs12->exit_qualification = exit_qualification; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10639 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10640 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Jan Kiszka | c0d1c77 | 2013-04-14 12:12:50 +0200 | [diff] [blame] | 10641 | if ((vmcs12->vm_exit_intr_info & |
| 10642 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 10643 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) |
| 10644 | vmcs12->vm_exit_intr_error_code = |
| 10645 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10646 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10647 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 10648 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 10649 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10650 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 10651 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 10652 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10653 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10654 | |
| 10655 | /* |
| 10656 | * Transfer the event that L0 or L1 may wanted to inject into |
| 10657 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 10658 | */ |
| 10659 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 10660 | } |
| 10661 | |
| 10662 | /* |
| 10663 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 10664 | * preserved above and would only end up incorrectly in L1. |
| 10665 | */ |
| 10666 | vcpu->arch.nmi_injected = false; |
| 10667 | kvm_clear_exception_queue(vcpu); |
| 10668 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10669 | } |
| 10670 | |
| 10671 | /* |
| 10672 | * A part of what we need to when the nested L2 guest exits and we want to |
| 10673 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 10674 | * in vmcs12. |
| 10675 | * This function is to be called not only on normal nested exit, but also on |
| 10676 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 10677 | * Failures During or After Loading Guest State"). |
| 10678 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 10679 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 10680 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 10681 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10682 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10683 | struct kvm_segment seg; |
| 10684 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10685 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 10686 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 10687 | 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] | 10688 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10689 | else |
| 10690 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10691 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10692 | |
| 10693 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 10694 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 10695 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10696 | /* |
| 10697 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 10698 | * actually changed, because it depends on the current state of |
| 10699 | * fpu_active (which may have changed). |
| 10700 | * Note that vmx_set_cr0 refers to efer set above. |
| 10701 | */ |
Jan Kiszka | 9e3e4db | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 10702 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10703 | /* |
| 10704 | * If we did fpu_activate()/fpu_deactivate() during L2's run, we need |
| 10705 | * to apply the same changes to L1's vmcs. We just set cr0 correctly, |
| 10706 | * but we also need to update cr0_guest_host_mask and exception_bitmap. |
| 10707 | */ |
| 10708 | update_exception_bitmap(vcpu); |
| 10709 | vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0); |
| 10710 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10711 | |
| 10712 | /* |
| 10713 | * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01 |
| 10714 | * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask(); |
| 10715 | */ |
| 10716 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 08e1674 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 10717 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10718 | |
Jan Kiszka | 29bf08f | 2013-12-28 16:31:52 +0100 | [diff] [blame] | 10719 | nested_ept_uninit_mmu_context(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10720 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10721 | kvm_set_cr3(vcpu, vmcs12->host_cr3); |
| 10722 | kvm_mmu_reset_context(vcpu); |
| 10723 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10724 | if (!enable_ept) |
| 10725 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 10726 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10727 | if (enable_vpid) { |
| 10728 | /* |
| 10729 | * Trivially support vpid by letting L2s share their parent |
| 10730 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 10731 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 10732 | */ |
| 10733 | vmx_flush_tlb(vcpu); |
| 10734 | } |
| 10735 | |
| 10736 | |
| 10737 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 10738 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 10739 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 10740 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 10741 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 1be0c0e | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 10742 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 10743 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10744 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10745 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 10746 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 10747 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 10748 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10749 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10750 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10751 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 10752 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10753 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 10754 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 10755 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 10756 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10757 | /* Set L1 segment info according to Intel SDM |
| 10758 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 10759 | seg = (struct kvm_segment) { |
| 10760 | .base = 0, |
| 10761 | .limit = 0xFFFFFFFF, |
| 10762 | .selector = vmcs12->host_cs_selector, |
| 10763 | .type = 11, |
| 10764 | .present = 1, |
| 10765 | .s = 1, |
| 10766 | .g = 1 |
| 10767 | }; |
| 10768 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 10769 | seg.l = 1; |
| 10770 | else |
| 10771 | seg.db = 1; |
| 10772 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 10773 | seg = (struct kvm_segment) { |
| 10774 | .base = 0, |
| 10775 | .limit = 0xFFFFFFFF, |
| 10776 | .type = 3, |
| 10777 | .present = 1, |
| 10778 | .s = 1, |
| 10779 | .db = 1, |
| 10780 | .g = 1 |
| 10781 | }; |
| 10782 | seg.selector = vmcs12->host_ds_selector; |
| 10783 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 10784 | seg.selector = vmcs12->host_es_selector; |
| 10785 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 10786 | seg.selector = vmcs12->host_ss_selector; |
| 10787 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 10788 | seg.selector = vmcs12->host_fs_selector; |
| 10789 | seg.base = vmcs12->host_fs_base; |
| 10790 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 10791 | seg.selector = vmcs12->host_gs_selector; |
| 10792 | seg.base = vmcs12->host_gs_base; |
| 10793 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 10794 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 10795 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10796 | .limit = 0x67, |
| 10797 | .selector = vmcs12->host_tr_selector, |
| 10798 | .type = 11, |
| 10799 | .present = 1 |
| 10800 | }; |
| 10801 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 10802 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 10803 | kvm_set_dr(vcpu, 7, 0x400); |
| 10804 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10805 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10806 | if (cpu_has_vmx_msr_bitmap()) |
| 10807 | vmx_set_msr_bitmap(vcpu); |
| 10808 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10809 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 10810 | vmcs12->vm_exit_msr_load_count)) |
| 10811 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10812 | } |
| 10813 | |
| 10814 | /* |
| 10815 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 10816 | * and modify vmcs12 to make it see what it would expect to see there if |
| 10817 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 10818 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10819 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 10820 | u32 exit_intr_info, |
| 10821 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10822 | { |
| 10823 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10824 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10825 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10826 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 10827 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 10828 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10829 | leave_guest_mode(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10830 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 10831 | exit_qualification); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10832 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10833 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 10834 | vmcs12->vm_exit_msr_store_count)) |
| 10835 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
| 10836 | |
Wanpeng Li | f3380ca | 2014-08-05 12:42:23 +0800 | [diff] [blame] | 10837 | vmx_load_vmcs01(vcpu); |
| 10838 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 10839 | if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) |
| 10840 | && nested_exit_intr_ack_set(vcpu)) { |
| 10841 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 10842 | WARN_ON(irq < 0); |
| 10843 | vmcs12->vm_exit_intr_info = irq | |
| 10844 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 10845 | } |
| 10846 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 10847 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 10848 | vmcs12->exit_qualification, |
| 10849 | vmcs12->idt_vectoring_info_field, |
| 10850 | vmcs12->vm_exit_intr_info, |
| 10851 | vmcs12->vm_exit_intr_error_code, |
| 10852 | KVM_ISA_VMX); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10853 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 10854 | vm_entry_controls_reset_shadow(vmx); |
| 10855 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 10856 | vmx_segment_cache_clear(vmx); |
| 10857 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10858 | /* if no vmcs02 cache requested, remove the one we used */ |
| 10859 | if (VMCS02_POOL_SIZE == 0) |
| 10860 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); |
| 10861 | |
| 10862 | load_vmcs12_host_state(vcpu, vmcs12); |
| 10863 | |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 10864 | /* Update any VMCS fields that might have changed while L2 ran */ |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10865 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 10866 | if (vmx->hv_deadline_tsc == -1) |
| 10867 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 10868 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 10869 | else |
| 10870 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 10871 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 10872 | if (kvm_has_tsc_control) |
| 10873 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10874 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 10875 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
| 10876 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; |
| 10877 | vmx_set_virtual_x2apic_mode(vcpu, |
| 10878 | vcpu->arch.apic_base & X2APIC_ENABLE); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 10879 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 10880 | nested_cpu_has2(vmcs12, |
| 10881 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 10882 | vmx_flush_tlb_ept_only(vcpu); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 10883 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10884 | |
| 10885 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 10886 | vmx->host_rsp = 0; |
| 10887 | |
| 10888 | /* Unpin physical memory we referred to in vmcs02 */ |
| 10889 | if (vmx->nested.apic_access_page) { |
| 10890 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 10891 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10892 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10893 | if (vmx->nested.virtual_apic_page) { |
| 10894 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 10895 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10896 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10897 | if (vmx->nested.pi_desc_page) { |
| 10898 | kunmap(vmx->nested.pi_desc_page); |
| 10899 | nested_release_page(vmx->nested.pi_desc_page); |
| 10900 | vmx->nested.pi_desc_page = NULL; |
| 10901 | vmx->nested.pi_desc = NULL; |
| 10902 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10903 | |
| 10904 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10905 | * We are now running in L2, mmu_notifier will force to reload the |
| 10906 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 10907 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 10908 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10909 | |
| 10910 | /* |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10911 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 10912 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 10913 | * success or failure flag accordingly. |
| 10914 | */ |
| 10915 | if (unlikely(vmx->fail)) { |
| 10916 | vmx->fail = 0; |
| 10917 | nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 10918 | } else |
| 10919 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10920 | if (enable_shadow_vmcs) |
| 10921 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10922 | |
| 10923 | /* in case we halted in L2 */ |
| 10924 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10925 | } |
| 10926 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10927 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 10928 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 10929 | */ |
| 10930 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 10931 | { |
Wanpeng Li | c886f28 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 10932 | if (is_guest_mode(vcpu)) { |
| 10933 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10934 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | c886f28 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 10935 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 10936 | free_nested(to_vmx(vcpu)); |
| 10937 | } |
| 10938 | |
| 10939 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10940 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 10941 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 10942 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 10943 | * It should only be called before L2 actually succeeded to run, and when |
| 10944 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 10945 | */ |
| 10946 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 10947 | struct vmcs12 *vmcs12, |
| 10948 | u32 reason, unsigned long qualification) |
| 10949 | { |
| 10950 | load_vmcs12_host_state(vcpu, vmcs12); |
| 10951 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 10952 | vmcs12->exit_qualification = qualification; |
| 10953 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10954 | if (enable_shadow_vmcs) |
| 10955 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10956 | } |
| 10957 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 10958 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 10959 | struct x86_instruction_info *info, |
| 10960 | enum x86_intercept_stage stage) |
| 10961 | { |
| 10962 | return X86EMUL_CONTINUE; |
| 10963 | } |
| 10964 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10965 | #ifdef CONFIG_X86_64 |
| 10966 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 10967 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 10968 | u64 divisor, u64 *result) |
| 10969 | { |
| 10970 | u64 low = a << shift, high = a >> (64 - shift); |
| 10971 | |
| 10972 | /* To avoid the overflow on divq */ |
| 10973 | if (high >= divisor) |
| 10974 | return 1; |
| 10975 | |
| 10976 | /* Low hold the result, high hold rem which is discarded */ |
| 10977 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 10978 | "rm" (divisor), "0" (low), "1" (high)); |
| 10979 | *result = low; |
| 10980 | |
| 10981 | return 0; |
| 10982 | } |
| 10983 | |
| 10984 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 10985 | { |
| 10986 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 9175d2e | 2016-06-27 15:08:01 +0200 | [diff] [blame] | 10987 | u64 tscl = rdtsc(); |
| 10988 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 10989 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10990 | |
| 10991 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 10992 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 10993 | u64_shl_div_u64(delta_tsc, |
| 10994 | kvm_tsc_scaling_ratio_frac_bits, |
| 10995 | vcpu->arch.tsc_scaling_ratio, |
| 10996 | &delta_tsc)) |
| 10997 | return -ERANGE; |
| 10998 | |
| 10999 | /* |
| 11000 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 11001 | * we can't use the preemption timer. |
| 11002 | * It's possible that it fits on later vmentries, but checking |
| 11003 | * on every vmentry is costly so we just use an hrtimer. |
| 11004 | */ |
| 11005 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 11006 | return -ERANGE; |
| 11007 | |
| 11008 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 11009 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11010 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11011 | return 0; |
| 11012 | } |
| 11013 | |
| 11014 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 11015 | { |
| 11016 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11017 | vmx->hv_deadline_tsc = -1; |
| 11018 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11019 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11020 | } |
| 11021 | #endif |
| 11022 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 11023 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11024 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 11025 | if (ple_gap) |
| 11026 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11027 | } |
| 11028 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11029 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 11030 | struct kvm_memory_slot *slot) |
| 11031 | { |
| 11032 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 11033 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 11034 | } |
| 11035 | |
| 11036 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 11037 | struct kvm_memory_slot *slot) |
| 11038 | { |
| 11039 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 11040 | } |
| 11041 | |
| 11042 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 11043 | { |
| 11044 | kvm_flush_pml_buffers(kvm); |
| 11045 | } |
| 11046 | |
| 11047 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 11048 | struct kvm_memory_slot *memslot, |
| 11049 | gfn_t offset, unsigned long mask) |
| 11050 | { |
| 11051 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 11052 | } |
| 11053 | |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11054 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 11055 | { |
| 11056 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11057 | struct pi_desc old, new; |
| 11058 | unsigned int dest; |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11059 | |
| 11060 | do { |
| 11061 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11062 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 11063 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11064 | |
| 11065 | dest = cpu_physical_id(vcpu->cpu); |
| 11066 | |
| 11067 | if (x2apic_enabled()) |
| 11068 | new.ndst = dest; |
| 11069 | else |
| 11070 | new.ndst = (dest << 8) & 0xFF00; |
| 11071 | |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11072 | /* set 'NV' to 'notification vector' */ |
| 11073 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 11074 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 11075 | new.control) != old.control); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11076 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11077 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 11078 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11079 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11080 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11081 | vcpu->pre_pcpu = -1; |
| 11082 | } |
| 11083 | } |
| 11084 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11085 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11086 | * This routine does the following things for vCPU which is going |
| 11087 | * to be blocked if VT-d PI is enabled. |
| 11088 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 11089 | * we can find the right vCPU to wake up. |
| 11090 | * - Change the Posted-interrupt descriptor as below: |
| 11091 | * 'NDST' <-- vcpu->pre_pcpu |
| 11092 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 11093 | * - If 'ON' is set during this process, which means at least one |
| 11094 | * interrupt is posted for this vCPU, we cannot block it, in |
| 11095 | * this case, return 1, otherwise, return 0. |
| 11096 | * |
| 11097 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11098 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11099 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11100 | unsigned int dest; |
| 11101 | struct pi_desc old, new; |
| 11102 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11103 | |
| 11104 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11105 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11106 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11107 | return 0; |
| 11108 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11109 | WARN_ON(irqs_disabled()); |
| 11110 | local_irq_disable(); |
| 11111 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 11112 | vcpu->pre_pcpu = vcpu->cpu; |
| 11113 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 11114 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 11115 | &per_cpu(blocked_vcpu_on_cpu, |
| 11116 | vcpu->pre_pcpu)); |
| 11117 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 11118 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11119 | |
| 11120 | do { |
| 11121 | old.control = new.control = pi_desc->control; |
| 11122 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11123 | WARN((pi_desc->sn == 1), |
| 11124 | "Warning: SN field of posted-interrupts " |
| 11125 | "is set before blocking\n"); |
| 11126 | |
| 11127 | /* |
| 11128 | * Since vCPU can be preempted during this process, |
| 11129 | * vcpu->cpu could be different with pre_pcpu, we |
| 11130 | * need to set pre_pcpu as the destination of wakeup |
| 11131 | * notification event, then we can find the right vCPU |
| 11132 | * to wakeup in wakeup handler if interrupts happen |
| 11133 | * when the vCPU is in blocked state. |
| 11134 | */ |
| 11135 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 11136 | |
| 11137 | if (x2apic_enabled()) |
| 11138 | new.ndst = dest; |
| 11139 | else |
| 11140 | new.ndst = (dest << 8) & 0xFF00; |
| 11141 | |
| 11142 | /* set 'NV' to 'wakeup vector' */ |
| 11143 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 11144 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 11145 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11146 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11147 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 11148 | if (pi_test_on(pi_desc) == 1) |
| 11149 | __pi_post_block(vcpu); |
| 11150 | |
| 11151 | local_irq_enable(); |
| 11152 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11153 | } |
| 11154 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11155 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 11156 | { |
| 11157 | if (pi_pre_block(vcpu)) |
| 11158 | return 1; |
| 11159 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11160 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 11161 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 11162 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11163 | return 0; |
| 11164 | } |
| 11165 | |
| 11166 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11167 | { |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11168 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11169 | return; |
| 11170 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11171 | WARN_ON(irqs_disabled()); |
| 11172 | local_irq_disable(); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11173 | __pi_post_block(vcpu); |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11174 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11175 | } |
| 11176 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11177 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 11178 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11179 | if (kvm_x86_ops->set_hv_timer) |
| 11180 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 11181 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11182 | pi_post_block(vcpu); |
| 11183 | } |
| 11184 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11185 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11186 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 11187 | * |
| 11188 | * @kvm: kvm |
| 11189 | * @host_irq: host irq of the interrupt |
| 11190 | * @guest_irq: gsi of the interrupt |
| 11191 | * @set: set or unset PI |
| 11192 | * returns 0 on success, < 0 on failure |
| 11193 | */ |
| 11194 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 11195 | uint32_t guest_irq, bool set) |
| 11196 | { |
| 11197 | struct kvm_kernel_irq_routing_entry *e; |
| 11198 | struct kvm_irq_routing_table *irq_rt; |
| 11199 | struct kvm_lapic_irq irq; |
| 11200 | struct kvm_vcpu *vcpu; |
| 11201 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3d4213f | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 11202 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11203 | |
| 11204 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11205 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11206 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11207 | return 0; |
| 11208 | |
| 11209 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 11210 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3d4213f | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 11211 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 11212 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 11213 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 11214 | guest_irq, irq_rt->nr_rt_entries); |
| 11215 | goto out; |
| 11216 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11217 | |
| 11218 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 11219 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 11220 | continue; |
| 11221 | /* |
| 11222 | * VT-d PI cannot support posting multicast/broadcast |
| 11223 | * interrupts to a vCPU, we still use interrupt remapping |
| 11224 | * for these kind of interrupts. |
| 11225 | * |
| 11226 | * For lowest-priority interrupts, we only support |
| 11227 | * those with single CPU as the destination, e.g. user |
| 11228 | * configures the interrupts via /proc/irq or uses |
| 11229 | * irqbalance to make the interrupts single-CPU. |
| 11230 | * |
| 11231 | * We will support full lowest-priority interrupt later. |
| 11232 | */ |
| 11233 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 11234 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11235 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 11236 | /* |
| 11237 | * Make sure the IRTE is in remapped mode if |
| 11238 | * we don't handle it in posted mode. |
| 11239 | */ |
| 11240 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 11241 | if (ret < 0) { |
| 11242 | printk(KERN_INFO |
| 11243 | "failed to back to remapped mode, irq: %u\n", |
| 11244 | host_irq); |
| 11245 | goto out; |
| 11246 | } |
| 11247 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11248 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11249 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11250 | |
| 11251 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 11252 | vcpu_info.vector = irq.vector; |
| 11253 | |
Feng Wu | b6ce978 | 2016-01-25 16:53:35 +0800 | [diff] [blame] | 11254 | trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11255 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 11256 | |
| 11257 | if (set) |
| 11258 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | 0c4e39c | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 11259 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11260 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11261 | |
| 11262 | if (ret < 0) { |
| 11263 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 11264 | __func__); |
| 11265 | goto out; |
| 11266 | } |
| 11267 | } |
| 11268 | |
| 11269 | ret = 0; |
| 11270 | out: |
| 11271 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 11272 | return ret; |
| 11273 | } |
| 11274 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11275 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 11276 | { |
| 11277 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 11278 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 11279 | FEATURE_CONTROL_LMCE; |
| 11280 | else |
| 11281 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 11282 | ~FEATURE_CONTROL_LMCE; |
| 11283 | } |
| 11284 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 11285 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11286 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 11287 | .disabled_by_bios = vmx_disabled_by_bios, |
| 11288 | .hardware_setup = hardware_setup, |
| 11289 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 11290 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11291 | .hardware_enable = hardware_enable, |
| 11292 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 11293 | .cpu_has_accelerated_tpr = report_flexpriority, |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 11294 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11295 | |
| 11296 | .vcpu_create = vmx_create_vcpu, |
| 11297 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11298 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11299 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11300 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11301 | .vcpu_load = vmx_vcpu_load, |
| 11302 | .vcpu_put = vmx_vcpu_put, |
| 11303 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 11304 | .update_bp_intercept = update_exception_bitmap, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11305 | .get_msr = vmx_get_msr, |
| 11306 | .set_msr = vmx_set_msr, |
| 11307 | .get_segment_base = vmx_get_segment_base, |
| 11308 | .get_segment = vmx_get_segment, |
| 11309 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 11310 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11311 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 11312 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 11313 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 11314 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11315 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11316 | .set_cr3 = vmx_set_cr3, |
| 11317 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11318 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11319 | .get_idt = vmx_get_idt, |
| 11320 | .set_idt = vmx_set_idt, |
| 11321 | .get_gdt = vmx_get_gdt, |
| 11322 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 11323 | .get_dr6 = vmx_get_dr6, |
| 11324 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 11325 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 11326 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 11327 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11328 | .get_rflags = vmx_get_rflags, |
| 11329 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 11330 | |
| 11331 | .get_pkru = vmx_get_pkru, |
| 11332 | |
Paolo Bonzini | 0fdd74f | 2015-05-20 11:33:43 +0200 | [diff] [blame] | 11333 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 11334 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11335 | |
| 11336 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11337 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11338 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 11339 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11340 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 11341 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 11342 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 11343 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 11344 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11345 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 11346 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 11347 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 11348 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11349 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 11350 | .get_nmi_mask = vmx_get_nmi_mask, |
| 11351 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11352 | .enable_nmi_window = enable_nmi_window, |
| 11353 | .enable_irq_window = enable_irq_window, |
| 11354 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 11355 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11356 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 11357 | .get_enable_apicv = vmx_get_enable_apicv, |
| 11358 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 11359 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
| 11360 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 11361 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 11362 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 11363 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11364 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 11365 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 11366 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 11367 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 11368 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11369 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11370 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 11371 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 11372 | |
| 11373 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 11374 | |
| 11375 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 11376 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 11377 | |
| 11378 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 11379 | |
| 11380 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 11381 | |
| 11382 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 11383 | |
| 11384 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 11385 | |
| 11386 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 11387 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 11388 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 11389 | .xsaves_supported = vmx_xsaves_supported, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11390 | |
| 11391 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11392 | |
| 11393 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11394 | |
| 11395 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 11396 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 11397 | .flush_log_dirty = vmx_flush_log_dirty, |
| 11398 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11399 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11400 | .pre_block = vmx_pre_block, |
| 11401 | .post_block = vmx_post_block, |
| 11402 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11403 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11404 | |
| 11405 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11406 | |
| 11407 | #ifdef CONFIG_X86_64 |
| 11408 | .set_hv_timer = vmx_set_hv_timer, |
| 11409 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 11410 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11411 | |
| 11412 | .setup_mce = vmx_setup_mce, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11413 | }; |
| 11414 | |
| 11415 | static int __init vmx_init(void) |
| 11416 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11417 | int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 11418 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11419 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11420 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 11421 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11422 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11423 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 11424 | crash_vmclear_local_loaded_vmcss); |
| 11425 | #endif |
| 11426 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11427 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11428 | } |
| 11429 | |
| 11430 | static void __exit vmx_exit(void) |
| 11431 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11432 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 11433 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11434 | synchronize_rcu(); |
| 11435 | #endif |
| 11436 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 11437 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11438 | } |
| 11439 | |
| 11440 | module_init(vmx_init) |
| 11441 | module_exit(vmx_exit) |