Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1 | #define pr_fmt(fmt) "SVM: " fmt |
| 2 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 3 | #include <linux/kvm_host.h> |
| 4 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 6 | #include "mmu.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 7 | #include "kvm_cache_regs.h" |
Gleb Natapov | fe4c7b1 | 2009-03-23 11:23:18 +0200 | [diff] [blame] | 8 | #include "x86.h" |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 9 | #include "cpuid.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 10 | #include "pmu.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 11 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12 | #include <linux/module.h> |
Josh Triplett | ae75954 | 2012-03-28 11:32:28 -0700 | [diff] [blame] | 13 | #include <linux/mod_devicetable.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/highmem.h> |
Joerg Roedel | ef0f649 | 2020-03-31 12:17:38 -0400 | [diff] [blame] | 17 | #include <linux/amd-iommu.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Steven Rostedt (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 19 | #include <linux/trace_events.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 21 | #include <linux/hashtable.h> |
Julien Thierry | 00089c0 | 2020-09-04 16:30:25 +0100 | [diff] [blame] | 22 | #include <linux/objtool.h> |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 23 | #include <linux/psp-sev.h> |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 24 | #include <linux/file.h> |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/swap.h> |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 27 | #include <linux/rwsem.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 28 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 29 | #include <asm/apic.h> |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 30 | #include <asm/perf_event.h> |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 31 | #include <asm/tlbflush.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 32 | #include <asm/desc.h> |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 33 | #include <asm/debugreg.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 34 | #include <asm/kvm_para.h> |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 35 | #include <asm/irq_remapping.h> |
Thomas Gleixner | 28a2775 | 2018-04-29 15:01:37 +0200 | [diff] [blame] | 36 | #include <asm/spec-ctrl.h> |
Thomas Gleixner | ba5bade | 2020-03-20 14:13:46 +0100 | [diff] [blame] | 37 | #include <asm/cpu_device_id.h> |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 38 | #include <asm/traps.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 39 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 40 | #include <asm/virtext.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 41 | #include "trace.h" |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 42 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 43 | #include "svm.h" |
Sean Christopherson | 35a7831 | 2020-12-30 16:27:00 -0800 | [diff] [blame] | 44 | #include "svm_ops.h" |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 45 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 46 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 47 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 48 | MODULE_AUTHOR("Qumranet"); |
| 49 | MODULE_LICENSE("GPL"); |
| 50 | |
Valdis Klētnieks | 575b255 | 2020-02-27 21:49:52 -0500 | [diff] [blame] | 51 | #ifdef MODULE |
Josh Triplett | ae75954 | 2012-03-28 11:32:28 -0700 | [diff] [blame] | 52 | static const struct x86_cpu_id svm_cpu_id[] = { |
Thomas Gleixner | 320debe | 2020-03-20 14:13:50 +0100 | [diff] [blame] | 53 | X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL), |
Josh Triplett | ae75954 | 2012-03-28 11:32:28 -0700 | [diff] [blame] | 54 | {} |
| 55 | }; |
| 56 | MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); |
Valdis Klētnieks | 575b255 | 2020-02-27 21:49:52 -0500 | [diff] [blame] | 57 | #endif |
Josh Triplett | ae75954 | 2012-03-28 11:32:28 -0700 | [diff] [blame] | 58 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 59 | #define SEG_TYPE_LDT 2 |
| 60 | #define SEG_TYPE_BUSY_TSS16 3 |
| 61 | |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 62 | #define SVM_FEATURE_LBRV (1 << 1) |
| 63 | #define SVM_FEATURE_SVML (1 << 2) |
Andre Przywara | ddce97a | 2010-12-21 11:12:03 +0100 | [diff] [blame] | 64 | #define SVM_FEATURE_TSC_RATE (1 << 4) |
| 65 | #define SVM_FEATURE_VMCB_CLEAN (1 << 5) |
| 66 | #define SVM_FEATURE_FLUSH_ASID (1 << 6) |
| 67 | #define SVM_FEATURE_DECODE_ASSIST (1 << 7) |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 68 | #define SVM_FEATURE_PAUSE_FILTER (1 << 10) |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 69 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 70 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
| 71 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 72 | #define TSC_RATIO_RSVD 0xffffff0000000000ULL |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 73 | #define TSC_RATIO_MIN 0x0000000000000001ULL |
| 74 | #define TSC_RATIO_MAX 0x000000ffffffffffULL |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 75 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 76 | static bool erratum_383_found __read_mostly; |
| 77 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 78 | u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 79 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 80 | /* |
| 81 | * Set osvw_len to higher value when updated Revision Guides |
| 82 | * are published and we know what the new status bits are |
| 83 | */ |
| 84 | static uint64_t osvw_len = 4, osvw_status; |
| 85 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 86 | static DEFINE_PER_CPU(u64, current_tsc_ratio); |
| 87 | #define TSC_RATIO_DEFAULT 0x0100000000ULL |
| 88 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 89 | static const struct svm_direct_access_msrs { |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 90 | u32 index; /* Index of the MSR */ |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 91 | bool always; /* True if intercept is initially cleared */ |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 92 | } direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = { |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 93 | { .index = MSR_STAR, .always = true }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 94 | { .index = MSR_IA32_SYSENTER_CS, .always = true }, |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 95 | { .index = MSR_IA32_SYSENTER_EIP, .always = false }, |
| 96 | { .index = MSR_IA32_SYSENTER_ESP, .always = false }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 97 | #ifdef CONFIG_X86_64 |
| 98 | { .index = MSR_GS_BASE, .always = true }, |
| 99 | { .index = MSR_FS_BASE, .always = true }, |
| 100 | { .index = MSR_KERNEL_GS_BASE, .always = true }, |
| 101 | { .index = MSR_LSTAR, .always = true }, |
| 102 | { .index = MSR_CSTAR, .always = true }, |
| 103 | { .index = MSR_SYSCALL_MASK, .always = true }, |
| 104 | #endif |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 105 | { .index = MSR_IA32_SPEC_CTRL, .always = false }, |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 106 | { .index = MSR_IA32_PRED_CMD, .always = false }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 107 | { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, |
| 108 | { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, |
| 109 | { .index = MSR_IA32_LASTINTFROMIP, .always = false }, |
| 110 | { .index = MSR_IA32_LASTINTTOIP, .always = false }, |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 111 | { .index = MSR_EFER, .always = false }, |
| 112 | { .index = MSR_IA32_CR_PAT, .always = false }, |
| 113 | { .index = MSR_AMD64_SEV_ES_GHCB, .always = true }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 114 | { .index = MSR_INVALID, .always = false }, |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 115 | }; |
| 116 | |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 117 | /* |
| 118 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 119 | * pause_filter_count: On processors that support Pause filtering(indicated |
| 120 | * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter |
| 121 | * count value. On VMRUN this value is loaded into an internal counter. |
| 122 | * Each time a pause instruction is executed, this counter is decremented |
| 123 | * until it reaches zero at which time a #VMEXIT is generated if pause |
| 124 | * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause |
| 125 | * Intercept Filtering for more details. |
| 126 | * This also indicate if ple logic enabled. |
| 127 | * |
| 128 | * pause_filter_thresh: In addition, some processor families support advanced |
| 129 | * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on |
| 130 | * the amount of time a guest is allowed to execute in a pause loop. |
| 131 | * In this mode, a 16-bit pause filter threshold field is added in the |
| 132 | * VMCB. The threshold value is a cycle count that is used to reset the |
| 133 | * pause counter. As with simple pause filtering, VMRUN loads the pause |
| 134 | * count value from VMCB into an internal counter. Then, on each pause |
| 135 | * instruction the hardware checks the elapsed number of cycles since |
| 136 | * the most recent pause instruction against the pause filter threshold. |
| 137 | * If the elapsed cycle count is greater than the pause filter threshold, |
| 138 | * then the internal pause count is reloaded from the VMCB and execution |
| 139 | * continues. If the elapsed cycle count is less than the pause filter |
| 140 | * threshold, then the internal pause count is decremented. If the count |
| 141 | * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is |
| 142 | * triggered. If advanced pause filtering is supported and pause filter |
| 143 | * threshold field is set to zero, the filter will operate in the simpler, |
| 144 | * count only mode. |
| 145 | */ |
| 146 | |
| 147 | static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP; |
| 148 | module_param(pause_filter_thresh, ushort, 0444); |
| 149 | |
| 150 | static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW; |
| 151 | module_param(pause_filter_count, ushort, 0444); |
| 152 | |
| 153 | /* Default doubles per-vcpu window every exit. */ |
| 154 | static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
| 155 | module_param(pause_filter_count_grow, ushort, 0444); |
| 156 | |
| 157 | /* Default resets per-vcpu window every exit to pause_filter_count. */ |
| 158 | static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
| 159 | module_param(pause_filter_count_shrink, ushort, 0444); |
| 160 | |
| 161 | /* Default is to compute the maximum so we can never overflow. */ |
| 162 | static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX; |
| 163 | module_param(pause_filter_count_max, ushort, 0444); |
| 164 | |
Sean Christopherson | 99840a7 | 2021-03-04 18:16:37 -0800 | [diff] [blame] | 165 | /* |
| 166 | * Use nested page tables by default. Note, NPT may get forced off by |
| 167 | * svm_hardware_setup() if it's unsupported by hardware or the host kernel. |
| 168 | */ |
| 169 | bool npt_enabled = true; |
| 170 | module_param_named(npt, npt_enabled, bool, 0444); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 171 | |
Davidlohr Bueso | e235885 | 2012-01-17 14:09:50 +0100 | [diff] [blame] | 172 | /* allow nested virtualization in KVM/SVM */ |
| 173 | static int nested = true; |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 174 | module_param(nested, int, S_IRUGO); |
| 175 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 176 | /* enable/disable Next RIP Save */ |
| 177 | static int nrips = true; |
| 178 | module_param(nrips, int, 0444); |
| 179 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 180 | /* enable/disable Virtual VMLOAD VMSAVE */ |
| 181 | static int vls = true; |
| 182 | module_param(vls, int, 0444); |
| 183 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 184 | /* enable/disable Virtual GIF */ |
| 185 | static int vgif = true; |
| 186 | module_param(vgif, int, 0444); |
Suravee Suthikulpanit | 5ea11f2 | 2016-08-23 13:52:41 -0500 | [diff] [blame] | 187 | |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 188 | bool __read_mostly dump_invalid_vmcb; |
Paolo Bonzini | 6f2f845 | 2019-05-20 15:34:35 +0200 | [diff] [blame] | 189 | module_param(dump_invalid_vmcb, bool, 0644); |
| 190 | |
Wei Yongjun | 2e21521 | 2021-02-10 07:59:58 +0000 | [diff] [blame] | 191 | static bool svm_gp_erratum_intercept = true; |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 192 | |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 193 | static u8 rsm_ins_bytes[] = "\x0f\xaa"; |
| 194 | |
Harvey Harrison | 4866d5e | 2008-02-19 10:32:02 -0800 | [diff] [blame] | 195 | static unsigned long iopm_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 196 | |
| 197 | struct kvm_ldttss_desc { |
| 198 | u16 limit0; |
| 199 | u16 base0; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 200 | unsigned base1:8, type:5, dpl:2, p:1; |
| 201 | unsigned limit1:4, zero0:3, g:1, base2:8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 202 | u32 base3; |
| 203 | u32 zero1; |
| 204 | } __attribute__((packed)); |
| 205 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 206 | DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 207 | |
Sean Christopherson | 844d69c | 2021-04-23 15:34:04 -0700 | [diff] [blame] | 208 | /* |
| 209 | * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via |
| 210 | * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE. |
| 211 | * |
| 212 | * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to |
| 213 | * defer the restoration of TSC_AUX until the CPU returns to userspace. |
| 214 | */ |
| 215 | #define TSC_AUX_URET_SLOT 0 |
| 216 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 217 | static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 218 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 219 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 220 | #define MSRS_RANGE_SIZE 2048 |
| 221 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 222 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 223 | u32 svm_msrpm_offset(u32 msr) |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 224 | { |
| 225 | u32 offset; |
| 226 | int i; |
| 227 | |
| 228 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 229 | if (msr < msrpm_ranges[i] || |
| 230 | msr >= msrpm_ranges[i] + MSRS_IN_RANGE) |
| 231 | continue; |
| 232 | |
| 233 | offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ |
| 234 | offset += (i * MSRS_RANGE_SIZE); /* add range offset */ |
| 235 | |
| 236 | /* Now we have the u8 offset - but need the u32 offset */ |
| 237 | return offset / 4; |
| 238 | } |
| 239 | |
| 240 | /* MSR not in any range */ |
| 241 | return MSR_INVALID; |
| 242 | } |
| 243 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 244 | #define MAX_INST_SIZE 15 |
| 245 | |
Sean Christopherson | d468d94 | 2020-07-15 20:41:20 -0700 | [diff] [blame] | 246 | static int get_max_npt_level(void) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 247 | { |
| 248 | #ifdef CONFIG_X86_64 |
Yu Zhang | 2a7266a | 2017-08-24 20:27:54 +0800 | [diff] [blame] | 249 | return PT64_ROOT_4LEVEL; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 250 | #else |
| 251 | return PT32E_ROOT_LEVEL; |
| 252 | #endif |
| 253 | } |
| 254 | |
Maxim Levitsky | 72f211e | 2020-10-01 14:29:53 +0300 | [diff] [blame] | 255 | int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 256 | { |
Paolo Bonzini | c513f48 | 2020-05-18 13:08:37 -0400 | [diff] [blame] | 257 | struct vcpu_svm *svm = to_svm(vcpu); |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 258 | u64 old_efer = vcpu->arch.efer; |
Zachary Amsden | 6dc696d | 2010-05-26 15:09:43 -1000 | [diff] [blame] | 259 | vcpu->arch.efer = efer; |
Paolo Bonzini | 9167ab7 | 2019-10-27 16:23:23 +0100 | [diff] [blame] | 260 | |
| 261 | if (!npt_enabled) { |
| 262 | /* Shadow paging assumes NX to be available. */ |
| 263 | efer |= EFER_NX; |
| 264 | |
| 265 | if (!(efer & EFER_LMA)) |
| 266 | efer &= ~EFER_LME; |
| 267 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 268 | |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 269 | if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) { |
| 270 | if (!(efer & EFER_SVME)) { |
| 271 | svm_leave_nested(svm); |
| 272 | svm_set_gif(svm, true); |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 273 | /* #GP intercept is still needed for vmware backdoor */ |
| 274 | if (!enable_vmware_backdoor) |
| 275 | clr_exception_intercept(svm, GP_VECTOR); |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * Free the nested guest state, unless we are in SMM. |
| 279 | * In this case we will return to the nested guest |
| 280 | * as soon as we leave SMM. |
| 281 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 282 | if (!is_smm(vcpu)) |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 283 | svm_free_nested(svm); |
| 284 | |
| 285 | } else { |
| 286 | int ret = svm_allocate_nested(svm); |
| 287 | |
| 288 | if (ret) { |
| 289 | vcpu->arch.efer = old_efer; |
| 290 | return ret; |
| 291 | } |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 292 | |
| 293 | if (svm_gp_erratum_intercept) |
| 294 | set_exception_intercept(svm, GP_VECTOR); |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 295 | } |
Paolo Bonzini | c513f48 | 2020-05-18 13:08:37 -0400 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | svm->vmcb->save.efer = efer | EFER_SVME; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 299 | vmcb_mark_dirty(svm->vmcb, VMCB_CR); |
Maxim Levitsky | 72f211e | 2020-10-01 14:29:53 +0300 | [diff] [blame] | 300 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 303 | static int is_external_interrupt(u32 info) |
| 304 | { |
| 305 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 306 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 307 | } |
| 308 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 309 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 310 | { |
| 311 | struct vcpu_svm *svm = to_svm(vcpu); |
| 312 | u32 ret = 0; |
| 313 | |
| 314 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 315 | ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; |
| 316 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 320 | { |
| 321 | struct vcpu_svm *svm = to_svm(vcpu); |
| 322 | |
| 323 | if (mask == 0) |
| 324 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
| 325 | else |
| 326 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; |
| 327 | |
| 328 | } |
| 329 | |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 330 | static int skip_emulated_instruction(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 331 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 332 | struct vcpu_svm *svm = to_svm(vcpu); |
| 333 | |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 334 | /* |
| 335 | * SEV-ES does not expose the next RIP. The RIP update is controlled by |
| 336 | * the type of exit and the #VC handler in the guest. |
| 337 | */ |
| 338 | if (sev_es_guest(vcpu->kvm)) |
| 339 | goto done; |
| 340 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 341 | if (nrips && svm->vmcb->control.next_rip != 0) { |
Dirk Müller | d292242 | 2015-10-01 13:43:42 +0200 | [diff] [blame] | 342 | WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS)); |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 343 | svm->next_rip = svm->vmcb->control.next_rip; |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 344 | } |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 345 | |
Sean Christopherson | 1957aa6 | 2019-08-27 14:40:39 -0700 | [diff] [blame] | 346 | if (!svm->next_rip) { |
| 347 | if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP)) |
| 348 | return 0; |
| 349 | } else { |
Sean Christopherson | 1957aa6 | 2019-08-27 14:40:39 -0700 | [diff] [blame] | 350 | kvm_rip_write(vcpu, svm->next_rip); |
| 351 | } |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 352 | |
| 353 | done: |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 354 | svm_set_interrupt_shadow(vcpu, 0); |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 355 | |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 356 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 359 | static void svm_queue_exception(struct kvm_vcpu *vcpu) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 360 | { |
| 361 | struct vcpu_svm *svm = to_svm(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 362 | unsigned nr = vcpu->arch.exception.nr; |
| 363 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 364 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 365 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 366 | kvm_deliver_exception_payload(vcpu); |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 367 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 368 | if (nr == BP_VECTOR && !nrips) { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 369 | unsigned long rip, old_rip = kvm_rip_read(vcpu); |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * For guest debugging where we have to reinject #BP if some |
| 373 | * INT3 is guest-owned: |
| 374 | * Emulate nRIP by moving RIP forward. Will fail if injection |
| 375 | * raises a fault that is not intercepted. Still better than |
| 376 | * failing in all cases. |
| 377 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 378 | (void)skip_emulated_instruction(vcpu); |
| 379 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 380 | svm->int3_rip = rip + svm->vmcb->save.cs.base; |
| 381 | svm->int3_injected = rip - old_rip; |
| 382 | } |
| 383 | |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 384 | svm->vmcb->control.event_inj = nr |
| 385 | | SVM_EVTINJ_VALID |
| 386 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 387 | | SVM_EVTINJ_TYPE_EXEPT; |
| 388 | svm->vmcb->control.event_inj_err = error_code; |
| 389 | } |
| 390 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 391 | static void svm_init_erratum_383(void) |
| 392 | { |
| 393 | u32 low, high; |
| 394 | int err; |
| 395 | u64 val; |
| 396 | |
Borislav Petkov | e6ee94d | 2013-03-20 15:07:27 +0100 | [diff] [blame] | 397 | if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH)) |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 398 | return; |
| 399 | |
| 400 | /* Use _safe variants to not break nested virtualization */ |
| 401 | val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); |
| 402 | if (err) |
| 403 | return; |
| 404 | |
| 405 | val |= (1ULL << 47); |
| 406 | |
| 407 | low = lower_32_bits(val); |
| 408 | high = upper_32_bits(val); |
| 409 | |
| 410 | native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); |
| 411 | |
| 412 | erratum_383_found = true; |
| 413 | } |
| 414 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 415 | static void svm_init_osvw(struct kvm_vcpu *vcpu) |
| 416 | { |
| 417 | /* |
| 418 | * Guests should see errata 400 and 415 as fixed (assuming that |
| 419 | * HLT and IO instructions are intercepted). |
| 420 | */ |
| 421 | vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; |
| 422 | vcpu->arch.osvw.status = osvw_status & ~(6ULL); |
| 423 | |
| 424 | /* |
| 425 | * By increasing VCPU's osvw.length to 3 we are telling the guest that |
| 426 | * all osvw.status bits inside that length, including bit 0 (which is |
| 427 | * reserved for erratum 298), are valid. However, if host processor's |
| 428 | * osvw_len is 0 then osvw_status[0] carries no information. We need to |
| 429 | * be conservative here and therefore we tell the guest that erratum 298 |
| 430 | * is present (because we really don't know). |
| 431 | */ |
| 432 | if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) |
| 433 | vcpu->arch.osvw.status |= 1; |
| 434 | } |
| 435 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 436 | static int has_svm(void) |
| 437 | { |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 438 | const char *msg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 439 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 440 | if (!cpu_has_svm(&msg)) { |
Joe Perches | ff81ff1 | 2009-01-08 11:05:17 -0800 | [diff] [blame] | 441 | printk(KERN_INFO "has_svm: %s\n", msg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
Sean Christopherson | ccd85d9 | 2021-02-02 13:20:17 -0800 | [diff] [blame] | 445 | if (sev_active()) { |
| 446 | pr_info("KVM is unsupported when running as an SEV guest\n"); |
| 447 | return 0; |
| 448 | } |
| 449 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 450 | return 1; |
| 451 | } |
| 452 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 453 | static void svm_hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 454 | { |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 455 | /* Make sure we clean up behind us */ |
| 456 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) |
| 457 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
| 458 | |
Eduardo Habkost | 2c8dcee | 2008-11-17 19:03:21 -0200 | [diff] [blame] | 459 | cpu_svm_disable(); |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 460 | |
| 461 | amd_pmu_disable_virt(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 464 | static int svm_hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 465 | { |
| 466 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 467 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 468 | uint64_t efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 469 | struct desc_struct *gdt; |
| 470 | int me = raw_smp_processor_id(); |
| 471 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 472 | rdmsrl(MSR_EFER, efer); |
| 473 | if (efer & EFER_SVME) |
| 474 | return -EBUSY; |
| 475 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 476 | if (!has_svm()) { |
Borislav Petkov | 1f5b77f | 2012-10-20 20:20:04 +0200 | [diff] [blame] | 477 | pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 478 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 479 | } |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 480 | sd = per_cpu(svm_data, me); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 481 | if (!sd) { |
Borislav Petkov | 1f5b77f | 2012-10-20 20:20:04 +0200 | [diff] [blame] | 482 | pr_err("%s: svm_data is NULL on %d\n", __func__, me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 483 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 486 | sd->asid_generation = 1; |
| 487 | sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 488 | sd->next_asid = sd->max_asid + 1; |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 489 | sd->min_asid = max_sev_asid + 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 490 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 491 | gdt = get_current_gdt_rw(); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 492 | sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 493 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 494 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 495 | |
Tom Lendacky | 85ca8be | 2020-12-10 11:10:04 -0600 | [diff] [blame] | 496 | wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area)); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 497 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 498 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 499 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 500 | __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 503 | |
| 504 | /* |
| 505 | * Get OSVW bits. |
| 506 | * |
| 507 | * Note that it is possible to have a system with mixed processor |
| 508 | * revisions and therefore different OSVW bits. If bits are not the same |
| 509 | * on different processors then choose the worst case (i.e. if erratum |
| 510 | * is present on one processor and not on another then assume that the |
| 511 | * erratum is present everywhere). |
| 512 | */ |
| 513 | if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { |
| 514 | uint64_t len, status = 0; |
| 515 | int err; |
| 516 | |
| 517 | len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err); |
| 518 | if (!err) |
| 519 | status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, |
| 520 | &err); |
| 521 | |
| 522 | if (err) |
| 523 | osvw_status = osvw_len = 0; |
| 524 | else { |
| 525 | if (len < osvw_len) |
| 526 | osvw_len = len; |
| 527 | osvw_status |= status; |
| 528 | osvw_status &= (1ULL << osvw_len) - 1; |
| 529 | } |
| 530 | } else |
| 531 | osvw_status = osvw_len = 0; |
| 532 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 533 | svm_init_erratum_383(); |
| 534 | |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 535 | amd_pmu_enable_virt(); |
| 536 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 537 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 540 | static void svm_cpu_uninit(int cpu) |
| 541 | { |
Jacob Xu | a2b2d4b | 2020-12-03 12:59:39 -0800 | [diff] [blame] | 542 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 543 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 544 | if (!sd) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 545 | return; |
| 546 | |
Jacob Xu | a2b2d4b | 2020-12-03 12:59:39 -0800 | [diff] [blame] | 547 | per_cpu(svm_data, cpu) = NULL; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 548 | kfree(sd->sev_vmcbs); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 549 | __free_page(sd->save_area); |
| 550 | kfree(sd); |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 553 | static int svm_cpu_init(int cpu) |
| 554 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 555 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 556 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 557 | sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 558 | if (!sd) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 559 | return -ENOMEM; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 560 | sd->cpu = cpu; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 561 | sd->save_area = alloc_page(GFP_KERNEL); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 562 | if (!sd->save_area) |
Miaohe Lin | d80b64f | 2020-01-04 16:56:49 +0800 | [diff] [blame] | 563 | goto free_cpu_data; |
Tom Lendacky | 85ca8be | 2020-12-10 11:10:04 -0600 | [diff] [blame] | 564 | clear_page(page_address(sd->save_area)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 565 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 566 | if (svm_sev_enabled()) { |
Sean Christopherson | 3b1902b8 | 2021-04-21 19:11:11 -0700 | [diff] [blame] | 567 | sd->sev_vmcbs = kcalloc(max_sev_asid + 1, sizeof(void *), |
| 568 | GFP_KERNEL); |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 569 | if (!sd->sev_vmcbs) |
Miaohe Lin | d80b64f | 2020-01-04 16:56:49 +0800 | [diff] [blame] | 570 | goto free_save_area; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 571 | } |
| 572 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 573 | per_cpu(svm_data, cpu) = sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 574 | |
| 575 | return 0; |
| 576 | |
Miaohe Lin | d80b64f | 2020-01-04 16:56:49 +0800 | [diff] [blame] | 577 | free_save_area: |
| 578 | __free_page(sd->save_area); |
| 579 | free_cpu_data: |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 580 | kfree(sd); |
Miaohe Lin | d80b64f | 2020-01-04 16:56:49 +0800 | [diff] [blame] | 581 | return -ENOMEM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 582 | |
| 583 | } |
| 584 | |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 585 | static int direct_access_msr_slot(u32 msr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 586 | { |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 587 | u32 i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 588 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 589 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 590 | if (direct_access_msrs[i].index == msr) |
| 591 | return i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 592 | |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 593 | return -ENOENT; |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 596 | static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read, |
| 597 | int write) |
| 598 | { |
| 599 | struct vcpu_svm *svm = to_svm(vcpu); |
| 600 | int slot = direct_access_msr_slot(msr); |
| 601 | |
| 602 | if (slot == -ENOENT) |
| 603 | return; |
| 604 | |
| 605 | /* Set the shadow bitmaps to the desired intercept states */ |
| 606 | if (read) |
| 607 | set_bit(slot, svm->shadow_msr_intercept.read); |
| 608 | else |
| 609 | clear_bit(slot, svm->shadow_msr_intercept.read); |
| 610 | |
| 611 | if (write) |
| 612 | set_bit(slot, svm->shadow_msr_intercept.write); |
| 613 | else |
| 614 | clear_bit(slot, svm->shadow_msr_intercept.write); |
| 615 | } |
| 616 | |
| 617 | static bool valid_msr_intercept(u32 index) |
| 618 | { |
| 619 | return direct_access_msr_slot(index) != -ENOENT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 622 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 623 | { |
| 624 | u8 bit_write; |
| 625 | unsigned long tmp; |
| 626 | u32 offset; |
| 627 | u32 *msrpm; |
| 628 | |
| 629 | msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm: |
| 630 | to_svm(vcpu)->msrpm; |
| 631 | |
| 632 | offset = svm_msrpm_offset(msr); |
| 633 | bit_write = 2 * (msr & 0x0f) + 1; |
| 634 | tmp = msrpm[offset]; |
| 635 | |
| 636 | BUG_ON(offset == MSR_INVALID); |
| 637 | |
| 638 | return !!test_bit(bit_write, &tmp); |
| 639 | } |
| 640 | |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 641 | static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm, |
| 642 | u32 msr, int read, int write) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 643 | { |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 644 | u8 bit_read, bit_write; |
| 645 | unsigned long tmp; |
| 646 | u32 offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 647 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 648 | /* |
| 649 | * If this warning triggers extend the direct_access_msrs list at the |
| 650 | * beginning of the file |
| 651 | */ |
| 652 | WARN_ON(!valid_msr_intercept(msr)); |
| 653 | |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 654 | /* Enforce non allowed MSRs to trap */ |
| 655 | if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) |
| 656 | read = 0; |
| 657 | |
| 658 | if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) |
| 659 | write = 0; |
| 660 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 661 | offset = svm_msrpm_offset(msr); |
| 662 | bit_read = 2 * (msr & 0x0f); |
| 663 | bit_write = 2 * (msr & 0x0f) + 1; |
| 664 | tmp = msrpm[offset]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 665 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 666 | BUG_ON(offset == MSR_INVALID); |
| 667 | |
| 668 | read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); |
| 669 | write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); |
| 670 | |
| 671 | msrpm[offset] = tmp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 674 | void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr, |
| 675 | int read, int write) |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 676 | { |
| 677 | set_shadow_msr_intercept(vcpu, msr, read, write); |
| 678 | set_msr_interception_bitmap(vcpu, msrpm, msr, read, write); |
| 679 | } |
| 680 | |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 681 | u32 *svm_vcpu_alloc_msrpm(void) |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 682 | { |
Krish Sadhukhan | 47903dc | 2021-04-12 17:56:05 -0400 | [diff] [blame] | 683 | unsigned int order = get_order(MSRPM_SIZE); |
| 684 | struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order); |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 685 | u32 *msrpm; |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 686 | |
Maxim Levitsky | f4c847a | 2020-08-27 20:11:40 +0300 | [diff] [blame] | 687 | if (!pages) |
| 688 | return NULL; |
| 689 | |
| 690 | msrpm = page_address(pages); |
Krish Sadhukhan | 47903dc | 2021-04-12 17:56:05 -0400 | [diff] [blame] | 691 | memset(msrpm, 0xff, PAGE_SIZE * (1 << order)); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 692 | |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 693 | return msrpm; |
| 694 | } |
| 695 | |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 696 | void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 697 | { |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 698 | int i; |
| 699 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 700 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 701 | if (!direct_access_msrs[i].always) |
| 702 | continue; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 703 | set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1); |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 704 | } |
Maxim Levitsky | f4c847a | 2020-08-27 20:11:40 +0300 | [diff] [blame] | 705 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 706 | |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 707 | |
| 708 | void svm_vcpu_free_msrpm(u32 *msrpm) |
Maxim Levitsky | f4c847a | 2020-08-27 20:11:40 +0300 | [diff] [blame] | 709 | { |
Krish Sadhukhan | 47903dc | 2021-04-12 17:56:05 -0400 | [diff] [blame] | 710 | __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 713 | static void svm_msr_filter_changed(struct kvm_vcpu *vcpu) |
| 714 | { |
| 715 | struct vcpu_svm *svm = to_svm(vcpu); |
| 716 | u32 i; |
| 717 | |
| 718 | /* |
| 719 | * Set intercept permissions for all direct access MSRs again. They |
| 720 | * will automatically get filtered through the MSR filter, so we are |
| 721 | * back in sync after this. |
| 722 | */ |
| 723 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 724 | u32 msr = direct_access_msrs[i].index; |
| 725 | u32 read = test_bit(i, svm->shadow_msr_intercept.read); |
| 726 | u32 write = test_bit(i, svm->shadow_msr_intercept.write); |
| 727 | |
| 728 | set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write); |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 732 | static void add_msr_offset(u32 offset) |
| 733 | { |
| 734 | int i; |
| 735 | |
| 736 | for (i = 0; i < MSRPM_OFFSETS; ++i) { |
| 737 | |
| 738 | /* Offset already in list? */ |
| 739 | if (msrpm_offsets[i] == offset) |
| 740 | return; |
| 741 | |
| 742 | /* Slot used by another offset? */ |
| 743 | if (msrpm_offsets[i] != MSR_INVALID) |
| 744 | continue; |
| 745 | |
| 746 | /* Add offset to list */ |
| 747 | msrpm_offsets[i] = offset; |
| 748 | |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * If this BUG triggers the msrpm_offsets table has an overflow. Just |
| 754 | * increase MSRPM_OFFSETS in this case. |
| 755 | */ |
| 756 | BUG(); |
| 757 | } |
| 758 | |
| 759 | static void init_msrpm_offsets(void) |
| 760 | { |
| 761 | int i; |
| 762 | |
| 763 | memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); |
| 764 | |
| 765 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 766 | u32 offset; |
| 767 | |
| 768 | offset = svm_msrpm_offset(direct_access_msrs[i].index); |
| 769 | BUG_ON(offset == MSR_INVALID); |
| 770 | |
| 771 | add_msr_offset(offset); |
| 772 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 775 | static void svm_enable_lbrv(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 776 | { |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 777 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 778 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 779 | svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 780 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 781 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 782 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 783 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 786 | static void svm_disable_lbrv(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 787 | { |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 788 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 789 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 790 | svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 791 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); |
| 792 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); |
| 793 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); |
| 794 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 797 | void disable_nmi_singlestep(struct vcpu_svm *svm) |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 798 | { |
| 799 | svm->nmi_singlestep = false; |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 800 | |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 801 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) { |
| 802 | /* Clear our flags if they were not set by the guest */ |
| 803 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 804 | svm->vmcb->save.rflags &= ~X86_EFLAGS_TF; |
| 805 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 806 | svm->vmcb->save.rflags &= ~X86_EFLAGS_RF; |
| 807 | } |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 808 | } |
| 809 | |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 810 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 811 | { |
| 812 | struct vcpu_svm *svm = to_svm(vcpu); |
| 813 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 814 | int old = control->pause_filter_count; |
| 815 | |
| 816 | control->pause_filter_count = __grow_ple_window(old, |
| 817 | pause_filter_count, |
| 818 | pause_filter_count_grow, |
| 819 | pause_filter_count_max); |
| 820 | |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 821 | if (control->pause_filter_count != old) { |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 822 | vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 823 | trace_kvm_ple_window_update(vcpu->vcpu_id, |
| 824 | control->pause_filter_count, old); |
| 825 | } |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 829 | { |
| 830 | struct vcpu_svm *svm = to_svm(vcpu); |
| 831 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 832 | int old = control->pause_filter_count; |
| 833 | |
| 834 | control->pause_filter_count = |
| 835 | __shrink_ple_window(old, |
| 836 | pause_filter_count, |
| 837 | pause_filter_count_shrink, |
| 838 | pause_filter_count); |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 839 | if (control->pause_filter_count != old) { |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 840 | vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 841 | trace_kvm_ple_window_update(vcpu->vcpu_id, |
| 842 | control->pause_filter_count, old); |
| 843 | } |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 844 | } |
| 845 | |
Tom Lendacky | 52918ed | 2020-01-09 17:42:16 -0600 | [diff] [blame] | 846 | /* |
| 847 | * The default MMIO mask is a single bit (excluding the present bit), |
| 848 | * which could conflict with the memory encryption bit. Check for |
| 849 | * memory encryption support and override the default MMIO mask if |
| 850 | * memory encryption is enabled. |
| 851 | */ |
| 852 | static __init void svm_adjust_mmio_mask(void) |
| 853 | { |
| 854 | unsigned int enc_bit, mask_bit; |
| 855 | u64 msr, mask; |
| 856 | |
| 857 | /* If there is no memory encryption support, use existing mask */ |
| 858 | if (cpuid_eax(0x80000000) < 0x8000001f) |
| 859 | return; |
| 860 | |
| 861 | /* If memory encryption is not enabled, use existing mask */ |
| 862 | rdmsrl(MSR_K8_SYSCFG, msr); |
| 863 | if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT)) |
| 864 | return; |
| 865 | |
| 866 | enc_bit = cpuid_ebx(0x8000001f) & 0x3f; |
| 867 | mask_bit = boot_cpu_data.x86_phys_bits; |
| 868 | |
| 869 | /* Increment the mask bit if it is the same as the encryption bit */ |
| 870 | if (enc_bit == mask_bit) |
| 871 | mask_bit++; |
| 872 | |
| 873 | /* |
| 874 | * If the mask bit location is below 52, then some bits above the |
| 875 | * physical addressing limit will always be reserved, so use the |
| 876 | * rsvd_bits() function to generate the mask. This mask, along with |
| 877 | * the present bit, will be used to generate a page fault with |
| 878 | * PFER.RSV = 1. |
| 879 | * |
| 880 | * If the mask bit location is 52 (or above), then clear the mask. |
| 881 | */ |
| 882 | mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0; |
| 883 | |
Sean Christopherson | 8120337 | 2021-02-25 12:47:35 -0800 | [diff] [blame] | 884 | kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK); |
Tom Lendacky | 52918ed | 2020-01-09 17:42:16 -0600 | [diff] [blame] | 885 | } |
| 886 | |
Li RongQing | dd58f3c | 2020-02-23 16:13:12 +0800 | [diff] [blame] | 887 | static void svm_hardware_teardown(void) |
| 888 | { |
| 889 | int cpu; |
| 890 | |
Sean Christopherson | 4cafd0c | 2021-04-21 19:11:20 -0700 | [diff] [blame^] | 891 | sev_hardware_teardown(); |
Li RongQing | dd58f3c | 2020-02-23 16:13:12 +0800 | [diff] [blame] | 892 | |
| 893 | for_each_possible_cpu(cpu) |
| 894 | svm_cpu_uninit(cpu); |
| 895 | |
Krish Sadhukhan | 47903dc | 2021-04-12 17:56:05 -0400 | [diff] [blame] | 896 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), |
| 897 | get_order(IOPM_SIZE)); |
Li RongQing | dd58f3c | 2020-02-23 16:13:12 +0800 | [diff] [blame] | 898 | iopm_base = 0; |
| 899 | } |
| 900 | |
Sean Christopherson | 9b58b98 | 2020-03-02 15:56:42 -0800 | [diff] [blame] | 901 | static __init void svm_set_cpu_caps(void) |
| 902 | { |
| 903 | kvm_set_cpu_caps(); |
| 904 | |
Paolo Bonzini | 408e9a3 | 2020-03-05 16:11:56 +0100 | [diff] [blame] | 905 | supported_xss = 0; |
| 906 | |
Sean Christopherson | a50718c | 2020-03-02 15:57:07 -0800 | [diff] [blame] | 907 | /* CPUID 0x80000001 and 0x8000000A (SVM features) */ |
| 908 | if (nested) { |
Sean Christopherson | 9b58b98 | 2020-03-02 15:56:42 -0800 | [diff] [blame] | 909 | kvm_cpu_cap_set(X86_FEATURE_SVM); |
| 910 | |
Sean Christopherson | 4eb8746 | 2020-03-02 15:57:08 -0800 | [diff] [blame] | 911 | if (nrips) |
Sean Christopherson | a50718c | 2020-03-02 15:57:07 -0800 | [diff] [blame] | 912 | kvm_cpu_cap_set(X86_FEATURE_NRIPS); |
| 913 | |
| 914 | if (npt_enabled) |
| 915 | kvm_cpu_cap_set(X86_FEATURE_NPT); |
Wei Huang | 14c2bf8 | 2021-01-26 03:18:31 -0500 | [diff] [blame] | 916 | |
| 917 | /* Nested VM can receive #VMEXIT instead of triggering #GP */ |
| 918 | kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK); |
Sean Christopherson | a50718c | 2020-03-02 15:57:07 -0800 | [diff] [blame] | 919 | } |
| 920 | |
Sean Christopherson | 93c380e | 2020-03-02 15:56:54 -0800 | [diff] [blame] | 921 | /* CPUID 0x80000008 */ |
| 922 | if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) || |
| 923 | boot_cpu_has(X86_FEATURE_AMD_SSBD)) |
| 924 | kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD); |
Paolo Bonzini | d9db0fd | 2021-04-21 19:11:15 -0700 | [diff] [blame] | 925 | |
| 926 | /* CPUID 0x8000001F (SME/SEV features) */ |
| 927 | sev_set_cpu_caps(); |
Sean Christopherson | 9b58b98 | 2020-03-02 15:56:42 -0800 | [diff] [blame] | 928 | } |
| 929 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 930 | static __init int svm_hardware_setup(void) |
| 931 | { |
| 932 | int cpu; |
| 933 | struct page *iopm_pages; |
| 934 | void *iopm_va; |
| 935 | int r; |
Krish Sadhukhan | 47903dc | 2021-04-12 17:56:05 -0400 | [diff] [blame] | 936 | unsigned int order = get_order(IOPM_SIZE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 937 | |
Krish Sadhukhan | 47903dc | 2021-04-12 17:56:05 -0400 | [diff] [blame] | 938 | iopm_pages = alloc_pages(GFP_KERNEL, order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 939 | |
| 940 | if (!iopm_pages) |
| 941 | return -ENOMEM; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 942 | |
| 943 | iopm_va = page_address(iopm_pages); |
Krish Sadhukhan | 47903dc | 2021-04-12 17:56:05 -0400 | [diff] [blame] | 944 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << order)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 945 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 946 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 947 | init_msrpm_offsets(); |
| 948 | |
Sean Christopherson | cfc4818 | 2020-03-02 15:56:23 -0800 | [diff] [blame] | 949 | supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR); |
| 950 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 951 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 952 | kvm_enable_efer_bits(EFER_NX); |
| 953 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 954 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
| 955 | kvm_enable_efer_bits(EFER_FFXSR); |
| 956 | |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 957 | if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 958 | kvm_has_tsc_control = true; |
Haozhong Zhang | bc9b961 | 2015-10-20 15:39:01 +0800 | [diff] [blame] | 959 | kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX; |
| 960 | kvm_tsc_scaling_ratio_frac_bits = 32; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 961 | } |
| 962 | |
Sean Christopherson | 844d69c | 2021-04-23 15:34:04 -0700 | [diff] [blame] | 963 | if (boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 964 | kvm_define_user_return_msr(TSC_AUX_URET_SLOT, MSR_TSC_AUX); |
| 965 | |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 966 | /* Check for pause filtering support */ |
| 967 | if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { |
| 968 | pause_filter_count = 0; |
| 969 | pause_filter_thresh = 0; |
| 970 | } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) { |
| 971 | pause_filter_thresh = 0; |
| 972 | } |
| 973 | |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 974 | if (nested) { |
| 975 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); |
Joerg Roedel | eec4b14 | 2010-05-05 16:04:44 +0200 | [diff] [blame] | 976 | kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 977 | } |
| 978 | |
Sean Christopherson | 99840a7 | 2021-03-04 18:16:37 -0800 | [diff] [blame] | 979 | /* |
| 980 | * KVM's MMU doesn't support using 2-level paging for itself, and thus |
| 981 | * NPT isn't supported if the host is using 2-level paging since host |
| 982 | * CR4 is unchanged on VMRUN. |
| 983 | */ |
| 984 | if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE)) |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 985 | npt_enabled = false; |
| 986 | |
Sean Christopherson | 99840a7 | 2021-03-04 18:16:37 -0800 | [diff] [blame] | 987 | if (!boot_cpu_has(X86_FEATURE_NPT)) |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 988 | npt_enabled = false; |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 989 | |
Sean Christopherson | 8301305 | 2020-07-15 20:41:22 -0700 | [diff] [blame] | 990 | kvm_configure_mmu(npt_enabled, get_max_npt_level(), PG_LEVEL_1G); |
Sean Christopherson | 213e0e1 | 2020-03-02 15:57:01 -0800 | [diff] [blame] | 991 | pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis"); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 992 | |
Sean Christopherson | e8126bd | 2021-04-21 19:11:14 -0700 | [diff] [blame] | 993 | /* Note, SEV setup consumes npt_enabled. */ |
| 994 | sev_hardware_setup(); |
Sean Christopherson | fa13680 | 2021-04-21 19:11:13 -0700 | [diff] [blame] | 995 | |
| 996 | svm_adjust_mmio_mask(); |
| 997 | |
| 998 | for_each_possible_cpu(cpu) { |
| 999 | r = svm_cpu_init(cpu); |
| 1000 | if (r) |
| 1001 | goto err; |
| 1002 | } |
| 1003 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 1004 | if (nrips) { |
| 1005 | if (!boot_cpu_has(X86_FEATURE_NRIPS)) |
| 1006 | nrips = false; |
| 1007 | } |
| 1008 | |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1009 | if (avic) { |
| 1010 | if (!npt_enabled || |
| 1011 | !boot_cpu_has(X86_FEATURE_AVIC) || |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1012 | !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) { |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1013 | avic = false; |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1014 | } else { |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1015 | pr_info("AVIC enabled\n"); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1016 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1017 | amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); |
| 1018 | } |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1019 | } |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1020 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1021 | if (vls) { |
| 1022 | if (!npt_enabled || |
Borislav Petkov | 5442c26 | 2017-08-01 20:55:52 +0200 | [diff] [blame] | 1023 | !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) || |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1024 | !IS_ENABLED(CONFIG_X86_64)) { |
| 1025 | vls = false; |
| 1026 | } else { |
| 1027 | pr_info("Virtual VMLOAD VMSAVE supported\n"); |
| 1028 | } |
| 1029 | } |
| 1030 | |
Wei Huang | 3b9c723 | 2021-01-26 03:18:30 -0500 | [diff] [blame] | 1031 | if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK)) |
| 1032 | svm_gp_erratum_intercept = false; |
| 1033 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1034 | if (vgif) { |
| 1035 | if (!boot_cpu_has(X86_FEATURE_VGIF)) |
| 1036 | vgif = false; |
| 1037 | else |
| 1038 | pr_info("Virtual GIF supported\n"); |
| 1039 | } |
| 1040 | |
Sean Christopherson | 9b58b98 | 2020-03-02 15:56:42 -0800 | [diff] [blame] | 1041 | svm_set_cpu_caps(); |
Sean Christopherson | 66a6950 | 2020-03-02 15:56:41 -0800 | [diff] [blame] | 1042 | |
Mohammed Gamal | 3edd683 | 2020-07-10 17:48:11 +0200 | [diff] [blame] | 1043 | /* |
| 1044 | * It seems that on AMD processors PTE's accessed bit is |
| 1045 | * being set by the CPU hardware before the NPF vmexit. |
| 1046 | * This is not expected behaviour and our tests fail because |
| 1047 | * of it. |
| 1048 | * A workaround here is to disable support for |
| 1049 | * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled. |
| 1050 | * In this case userspace can know if there is support using |
| 1051 | * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle |
| 1052 | * it |
| 1053 | * If future AMD CPU models change the behaviour described above, |
| 1054 | * this variable can be changed accordingly |
| 1055 | */ |
| 1056 | allow_smaller_maxphyaddr = !npt_enabled; |
| 1057 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1058 | return 0; |
| 1059 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1060 | err: |
Li RongQing | dd58f3c | 2020-02-23 16:13:12 +0800 | [diff] [blame] | 1061 | svm_hardware_teardown(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1062 | return r; |
| 1063 | } |
| 1064 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1065 | static void init_seg(struct vmcb_seg *seg) |
| 1066 | { |
| 1067 | seg->selector = 0; |
| 1068 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1069 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1070 | seg->limit = 0xffff; |
| 1071 | seg->base = 0; |
| 1072 | } |
| 1073 | |
| 1074 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 1075 | { |
| 1076 | seg->selector = 0; |
| 1077 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 1078 | seg->limit = 0xffff; |
| 1079 | seg->base = 0; |
| 1080 | } |
| 1081 | |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1082 | static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1083 | { |
| 1084 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1085 | u64 g_tsc_offset = 0; |
| 1086 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 1087 | if (is_guest_mode(vcpu)) { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1088 | /* Write L1's TSC offset. */ |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1089 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1090 | svm->vmcb01.ptr->control.tsc_offset; |
| 1091 | svm->vmcb01.ptr->control.tsc_offset = offset; |
Paolo Bonzini | 45c3af9 | 2018-11-25 18:45:35 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 1095 | svm->vmcb->control.tsc_offset - g_tsc_offset, |
| 1096 | offset); |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1097 | |
| 1098 | svm->vmcb->control.tsc_offset = offset + g_tsc_offset; |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 1099 | |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1100 | vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1101 | return svm->vmcb->control.tsc_offset; |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1102 | } |
| 1103 | |
Babu Moger | 4407a79 | 2020-09-11 14:29:19 -0500 | [diff] [blame] | 1104 | static void svm_check_invpcid(struct vcpu_svm *svm) |
| 1105 | { |
| 1106 | /* |
Sean Christopherson | 0a8ed2e | 2021-02-11 16:34:09 -0800 | [diff] [blame] | 1107 | * Intercept INVPCID if shadow paging is enabled to sync/free shadow |
| 1108 | * roots, or if INVPCID is disabled in the guest to inject #UD. |
Babu Moger | 4407a79 | 2020-09-11 14:29:19 -0500 | [diff] [blame] | 1109 | */ |
| 1110 | if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) { |
Sean Christopherson | 0a8ed2e | 2021-02-11 16:34:09 -0800 | [diff] [blame] | 1111 | if (!npt_enabled || |
| 1112 | !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID)) |
Babu Moger | 4407a79 | 2020-09-11 14:29:19 -0500 | [diff] [blame] | 1113 | svm_set_intercept(svm, INTERCEPT_INVPCID); |
| 1114 | else |
| 1115 | svm_clr_intercept(svm, INTERCEPT_INVPCID); |
| 1116 | } |
| 1117 | } |
| 1118 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1119 | static void init_vmcb(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1120 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1121 | struct vcpu_svm *svm = to_svm(vcpu); |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 1122 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1123 | struct vmcb_save_area *save = &svm->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1124 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1125 | vcpu->arch.hflags = 0; |
Avi Kivity | bff7827 | 2010-01-07 13:16:08 +0200 | [diff] [blame] | 1126 | |
Babu Moger | 830bd71 | 2020-09-11 14:28:50 -0500 | [diff] [blame] | 1127 | svm_set_intercept(svm, INTERCEPT_CR0_READ); |
| 1128 | svm_set_intercept(svm, INTERCEPT_CR3_READ); |
| 1129 | svm_set_intercept(svm, INTERCEPT_CR4_READ); |
| 1130 | svm_set_intercept(svm, INTERCEPT_CR0_WRITE); |
| 1131 | svm_set_intercept(svm, INTERCEPT_CR3_WRITE); |
| 1132 | svm_set_intercept(svm, INTERCEPT_CR4_WRITE); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1133 | if (!kvm_vcpu_apicv_active(vcpu)) |
Babu Moger | 830bd71 | 2020-09-11 14:28:50 -0500 | [diff] [blame] | 1134 | svm_set_intercept(svm, INTERCEPT_CR8_WRITE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1135 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 1136 | set_dr_intercepts(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1137 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1138 | set_exception_intercept(svm, PF_VECTOR); |
| 1139 | set_exception_intercept(svm, UD_VECTOR); |
| 1140 | set_exception_intercept(svm, MC_VECTOR); |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1141 | set_exception_intercept(svm, AC_VECTOR); |
Paolo Bonzini | cbdb967 | 2015-11-10 09:14:39 +0100 | [diff] [blame] | 1142 | set_exception_intercept(svm, DB_VECTOR); |
Liran Alon | 9718420 | 2018-03-12 13:12:52 +0200 | [diff] [blame] | 1143 | /* |
| 1144 | * Guest access to VMware backdoor ports could legitimately |
| 1145 | * trigger #GP because of TSS I/O permission bitmap. |
| 1146 | * We intercept those #GP and allow access to them anyway |
| 1147 | * as VMware does. |
| 1148 | */ |
| 1149 | if (enable_vmware_backdoor) |
| 1150 | set_exception_intercept(svm, GP_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1151 | |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1152 | svm_set_intercept(svm, INTERCEPT_INTR); |
| 1153 | svm_set_intercept(svm, INTERCEPT_NMI); |
| 1154 | svm_set_intercept(svm, INTERCEPT_SMI); |
| 1155 | svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0); |
| 1156 | svm_set_intercept(svm, INTERCEPT_RDPMC); |
| 1157 | svm_set_intercept(svm, INTERCEPT_CPUID); |
| 1158 | svm_set_intercept(svm, INTERCEPT_INVD); |
| 1159 | svm_set_intercept(svm, INTERCEPT_INVLPG); |
| 1160 | svm_set_intercept(svm, INTERCEPT_INVLPGA); |
| 1161 | svm_set_intercept(svm, INTERCEPT_IOIO_PROT); |
| 1162 | svm_set_intercept(svm, INTERCEPT_MSR_PROT); |
| 1163 | svm_set_intercept(svm, INTERCEPT_TASK_SWITCH); |
| 1164 | svm_set_intercept(svm, INTERCEPT_SHUTDOWN); |
| 1165 | svm_set_intercept(svm, INTERCEPT_VMRUN); |
| 1166 | svm_set_intercept(svm, INTERCEPT_VMMCALL); |
| 1167 | svm_set_intercept(svm, INTERCEPT_VMLOAD); |
| 1168 | svm_set_intercept(svm, INTERCEPT_VMSAVE); |
| 1169 | svm_set_intercept(svm, INTERCEPT_STGI); |
| 1170 | svm_set_intercept(svm, INTERCEPT_CLGI); |
| 1171 | svm_set_intercept(svm, INTERCEPT_SKINIT); |
| 1172 | svm_set_intercept(svm, INTERCEPT_WBINVD); |
| 1173 | svm_set_intercept(svm, INTERCEPT_XSETBV); |
| 1174 | svm_set_intercept(svm, INTERCEPT_RDPRU); |
| 1175 | svm_set_intercept(svm, INTERCEPT_RSM); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1176 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1177 | if (!kvm_mwait_in_guest(vcpu->kvm)) { |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1178 | svm_set_intercept(svm, INTERCEPT_MONITOR); |
| 1179 | svm_set_intercept(svm, INTERCEPT_MWAIT); |
Michael S. Tsirkin | 668fffa | 2017-04-21 12:27:17 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1182 | if (!kvm_hlt_in_guest(vcpu->kvm)) |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1183 | svm_set_intercept(svm, INTERCEPT_HLT); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 1184 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1185 | control->iopm_base_pa = __sme_set(iopm_base); |
| 1186 | control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1187 | control->int_ctl = V_INTR_MASKING_MASK; |
| 1188 | |
| 1189 | init_seg(&save->es); |
| 1190 | init_seg(&save->ss); |
| 1191 | init_seg(&save->ds); |
| 1192 | init_seg(&save->fs); |
| 1193 | init_seg(&save->gs); |
| 1194 | |
| 1195 | save->cs.selector = 0xf000; |
Paolo Bonzini | 04b6683 | 2013-03-19 16:30:26 +0100 | [diff] [blame] | 1196 | save->cs.base = 0xffff0000; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1197 | /* Executable/Readable Code Segment */ |
| 1198 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 1199 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 1200 | save->cs.limit = 0xffff; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1201 | |
| 1202 | save->gdtr.limit = 0xffff; |
| 1203 | save->idtr.limit = 0xffff; |
| 1204 | |
| 1205 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 1206 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 1207 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1208 | svm_set_cr4(vcpu, 0); |
| 1209 | svm_set_efer(vcpu, 0); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1210 | save->dr6 = 0xffff0ff0; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1211 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1212 | save->rip = 0x0000fff0; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1213 | vcpu->arch.regs[VCPU_REGS_RIP] = save->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1214 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1215 | /* |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 1216 | * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0. |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1217 | * It also updates the guest-visible cr0 value. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1218 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1219 | svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); |
| 1220 | kvm_mmu_reset_context(vcpu); |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 1221 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1222 | save->cr4 = X86_CR4_PAE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1223 | /* rdx = ?? */ |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1224 | |
| 1225 | if (npt_enabled) { |
| 1226 | /* Setup VMCB for Nested Paging */ |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 1227 | control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE; |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1228 | svm_clr_intercept(svm, INTERCEPT_INVLPG); |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1229 | clr_exception_intercept(svm, PF_VECTOR); |
Babu Moger | 830bd71 | 2020-09-11 14:28:50 -0500 | [diff] [blame] | 1230 | svm_clr_intercept(svm, INTERCEPT_CR3_READ); |
| 1231 | svm_clr_intercept(svm, INTERCEPT_CR3_WRITE); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1232 | save->g_pat = vcpu->arch.pat; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1233 | save->cr3 = 0; |
| 1234 | save->cr4 = 0; |
| 1235 | } |
Cathy Avery | 193015a | 2021-01-12 11:43:13 -0500 | [diff] [blame] | 1236 | svm->current_vmcb->asid_generation = 0; |
Cathy Avery | 7e8e6ee | 2020-10-11 14:48:17 -0400 | [diff] [blame] | 1237 | svm->asid = 0; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1238 | |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 1239 | svm->nested.vmcb12_gpa = 0; |
Cathy Avery | 8173396 | 2021-03-01 15:08:44 -0500 | [diff] [blame] | 1240 | svm->nested.last_vmcb12_gpa = 0; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1241 | vcpu->arch.hflags = 0; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1242 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1243 | if (!kvm_pause_in_guest(vcpu->kvm)) { |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1244 | control->pause_filter_count = pause_filter_count; |
| 1245 | if (pause_filter_thresh) |
| 1246 | control->pause_filter_thresh = pause_filter_thresh; |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1247 | svm_set_intercept(svm, INTERCEPT_PAUSE); |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1248 | } else { |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1249 | svm_clr_intercept(svm, INTERCEPT_PAUSE); |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 1250 | } |
| 1251 | |
Babu Moger | 4407a79 | 2020-09-11 14:29:19 -0500 | [diff] [blame] | 1252 | svm_check_invpcid(svm); |
| 1253 | |
Babu Moger | d00b99c | 2021-02-17 10:56:04 -0500 | [diff] [blame] | 1254 | /* |
| 1255 | * If the host supports V_SPEC_CTRL then disable the interception |
| 1256 | * of MSR_IA32_SPEC_CTRL. |
| 1257 | */ |
| 1258 | if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) |
| 1259 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); |
| 1260 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1261 | if (kvm_vcpu_apicv_active(vcpu)) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1262 | avic_init_vmcb(svm); |
| 1263 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1264 | if (vgif) { |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1265 | svm_clr_intercept(svm, INTERCEPT_STGI); |
| 1266 | svm_clr_intercept(svm, INTERCEPT_CLGI); |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1267 | svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK; |
| 1268 | } |
| 1269 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1270 | if (sev_guest(vcpu->kvm)) { |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1271 | svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE; |
Brijesh Singh | 35c6f649 | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 1272 | clr_exception_intercept(svm, UD_VECTOR); |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 1273 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1274 | if (sev_es_guest(vcpu->kvm)) { |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 1275 | /* Perform SEV-ES specific VMCB updates */ |
| 1276 | sev_es_init_vmcb(svm); |
| 1277 | } |
Brijesh Singh | 35c6f649 | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 1278 | } |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1279 | |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1280 | vmcb_mark_all_dirty(svm->vmcb); |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 1281 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1282 | enable_gif(svm); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1283 | |
| 1284 | } |
| 1285 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1286 | static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1287 | { |
| 1288 | struct vcpu_svm *svm = to_svm(vcpu); |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 1289 | u32 dummy; |
| 1290 | u32 eax = 1; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1291 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 1292 | svm->spec_ctrl = 0; |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 1293 | svm->virt_spec_ctrl = 0; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 1294 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1295 | if (!init_event) { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1296 | vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE | |
| 1297 | MSR_IA32_APICBASE_ENABLE; |
| 1298 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 1299 | vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1300 | } |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1301 | init_vmcb(vcpu); |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 1302 | |
Sean Christopherson | f91af51 | 2020-03-04 17:34:37 -0800 | [diff] [blame] | 1303 | kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, false); |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1304 | kvm_rdx_write(vcpu, eax); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1305 | |
| 1306 | if (kvm_vcpu_apicv_active(vcpu) && !init_event) |
| 1307 | avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1308 | } |
| 1309 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1310 | void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb) |
| 1311 | { |
| 1312 | svm->current_vmcb = target_vmcb; |
| 1313 | svm->vmcb = target_vmcb->ptr; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1314 | } |
| 1315 | |
Sean Christopherson | 987b259 | 2019-12-18 13:54:55 -0800 | [diff] [blame] | 1316 | static int svm_create_vcpu(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1317 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1318 | struct vcpu_svm *svm; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1319 | struct page *vmcb01_page; |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1320 | struct page *vmsa_page = NULL; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1321 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1322 | |
Sean Christopherson | a9dd6f0 | 2019-12-18 13:54:52 -0800 | [diff] [blame] | 1323 | BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0); |
| 1324 | svm = to_svm(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1325 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1326 | err = -ENOMEM; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1327 | vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); |
| 1328 | if (!vmcb01_page) |
Sean Christopherson | 987b259 | 2019-12-18 13:54:55 -0800 | [diff] [blame] | 1329 | goto out; |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1330 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1331 | if (sev_es_guest(vcpu->kvm)) { |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1332 | /* |
| 1333 | * SEV-ES guests require a separate VMSA page used to contain |
| 1334 | * the encrypted register state of the guest. |
| 1335 | */ |
| 1336 | vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); |
| 1337 | if (!vmsa_page) |
| 1338 | goto error_free_vmcb_page; |
Tom Lendacky | ed02b21 | 2020-12-10 11:10:01 -0600 | [diff] [blame] | 1339 | |
| 1340 | /* |
| 1341 | * SEV-ES guests maintain an encrypted version of their FPU |
| 1342 | * state which is restored and saved on VMRUN and VMEXIT. |
| 1343 | * Free the fpu structure to prevent KVM from attempting to |
| 1344 | * access the FPU state. |
| 1345 | */ |
| 1346 | kvm_free_guest_fpu(vcpu); |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1347 | } |
| 1348 | |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 1349 | err = avic_init_vcpu(svm); |
| 1350 | if (err) |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1351 | goto error_free_vmsa_page; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1352 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1353 | /* We initialize this flag to true to make sure that the is_running |
| 1354 | * bit would be set the first time the vcpu is loaded. |
| 1355 | */ |
Suravee Suthikulpanit | 6c3e442 | 2019-11-14 14:15:12 -0600 | [diff] [blame] | 1356 | if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm)) |
| 1357 | svm->avic_is_running = true; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1358 | |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 1359 | svm->msrpm = svm_vcpu_alloc_msrpm(); |
Chen Zhou | 054409a | 2020-11-17 10:54:26 +0800 | [diff] [blame] | 1360 | if (!svm->msrpm) { |
| 1361 | err = -ENOMEM; |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1362 | goto error_free_vmsa_page; |
Chen Zhou | 054409a | 2020-11-17 10:54:26 +0800 | [diff] [blame] | 1363 | } |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 1364 | |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 1365 | svm_vcpu_init_msrpm(vcpu, svm->msrpm); |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1366 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1367 | svm->vmcb01.ptr = page_address(vmcb01_page); |
| 1368 | svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT); |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1369 | |
| 1370 | if (vmsa_page) |
| 1371 | svm->vmsa = page_address(vmsa_page); |
| 1372 | |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1373 | svm->guest_state_loaded = false; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1374 | |
| 1375 | svm_switch_vmcb(svm, &svm->vmcb01); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1376 | init_vmcb(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1377 | |
Sean Christopherson | 7f27179 | 2019-12-18 13:54:51 -0800 | [diff] [blame] | 1378 | svm_init_osvw(vcpu); |
Paolo Bonzini | bab0c31 | 2020-02-11 18:40:58 +0100 | [diff] [blame] | 1379 | vcpu->arch.microcode_version = 0x01000065; |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 1380 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1381 | if (sev_es_guest(vcpu->kvm)) |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 1382 | /* Perform SEV-ES specific VMCB creation updates */ |
| 1383 | sev_es_create_vcpu(svm); |
| 1384 | |
Sean Christopherson | a9dd6f0 | 2019-12-18 13:54:52 -0800 | [diff] [blame] | 1385 | return 0; |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 1386 | |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1387 | error_free_vmsa_page: |
| 1388 | if (vmsa_page) |
| 1389 | __free_page(vmsa_page); |
Maxim Levitsky | 8d22b90 | 2020-08-27 20:11:42 +0300 | [diff] [blame] | 1390 | error_free_vmcb_page: |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1391 | __free_page(vmcb01_page); |
Sean Christopherson | 987b259 | 2019-12-18 13:54:55 -0800 | [diff] [blame] | 1392 | out: |
Sean Christopherson | a9dd6f0 | 2019-12-18 13:54:52 -0800 | [diff] [blame] | 1393 | return err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
Jim Mattson | fd65d31 | 2018-05-22 09:54:20 -0700 | [diff] [blame] | 1396 | static void svm_clear_current_vmcb(struct vmcb *vmcb) |
| 1397 | { |
| 1398 | int i; |
| 1399 | |
| 1400 | for_each_online_cpu(i) |
| 1401 | cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL); |
| 1402 | } |
| 1403 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1404 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 1405 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1406 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1407 | |
Jim Mattson | fd65d31 | 2018-05-22 09:54:20 -0700 | [diff] [blame] | 1408 | /* |
| 1409 | * The vmcb page can be recycled, causing a false negative in |
| 1410 | * svm_vcpu_load(). So, ensure that no logical CPU has this |
| 1411 | * vmcb page recorded as its current vmcb. |
| 1412 | */ |
| 1413 | svm_clear_current_vmcb(svm->vmcb); |
| 1414 | |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 1415 | svm_free_nested(svm); |
| 1416 | |
Tom Lendacky | add5e2f | 2020-12-10 11:09:40 -0600 | [diff] [blame] | 1417 | sev_free_vcpu(vcpu); |
| 1418 | |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1419 | __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT)); |
Krish Sadhukhan | 47903dc | 2021-04-12 17:56:05 -0400 | [diff] [blame] | 1420 | __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1423 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1424 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1425 | struct vcpu_svm *svm = to_svm(vcpu); |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1426 | struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu); |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 1427 | |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1428 | if (svm->guest_state_loaded) |
| 1429 | return; |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1430 | |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1431 | /* |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1432 | * Save additional host state that will be restored on VMEXIT (sev-es) |
| 1433 | * or subsequent vmload of host save area. |
| 1434 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1435 | if (sev_es_guest(vcpu->kvm)) { |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1436 | sev_es_prepare_guest_switch(svm, vcpu->cpu); |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 1437 | } else { |
Michael Roth | e79b91b | 2021-02-02 13:01:24 -0600 | [diff] [blame] | 1438 | vmsave(__sme_page_pa(sd->save_area)); |
Tom Lendacky | 8613777 | 2020-12-10 11:10:07 -0600 | [diff] [blame] | 1439 | } |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 1440 | |
Haozhong Zhang | ad721883 | 2015-10-20 15:39:02 +0800 | [diff] [blame] | 1441 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 1442 | u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio; |
| 1443 | if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) { |
| 1444 | __this_cpu_write(current_tsc_ratio, tsc_ratio); |
| 1445 | wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio); |
| 1446 | } |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 1447 | } |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1448 | |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 1449 | if (static_cpu_has(X86_FEATURE_RDTSCP)) |
Sean Christopherson | 844d69c | 2021-04-23 15:34:04 -0700 | [diff] [blame] | 1450 | kvm_set_user_return_msr(TSC_AUX_URET_SLOT, svm->tsc_aux, -1ull); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1451 | |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1452 | svm->guest_state_loaded = true; |
| 1453 | } |
| 1454 | |
| 1455 | static void svm_prepare_host_switch(struct kvm_vcpu *vcpu) |
| 1456 | { |
Sean Christopherson | 844d69c | 2021-04-23 15:34:04 -0700 | [diff] [blame] | 1457 | to_svm(vcpu)->guest_state_loaded = false; |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1461 | { |
| 1462 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1463 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 1464 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1465 | if (sd->current_vmcb != svm->vmcb) { |
| 1466 | sd->current_vmcb = svm->vmcb; |
| 1467 | indirect_branch_prediction_barrier(); |
| 1468 | } |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1469 | avic_vcpu_load(vcpu, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 1473 | { |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1474 | avic_vcpu_put(vcpu); |
Michael Roth | a7fc06d | 2021-02-02 13:01:26 -0600 | [diff] [blame] | 1475 | svm_prepare_host_switch(vcpu); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1476 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1477 | ++vcpu->stat.host_state_reload; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1478 | } |
| 1479 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1480 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 1481 | { |
Ladi Prosek | 9b61174 | 2017-06-21 09:06:59 +0200 | [diff] [blame] | 1482 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1483 | unsigned long rflags = svm->vmcb->save.rflags; |
| 1484 | |
| 1485 | if (svm->nmi_singlestep) { |
| 1486 | /* Hide our flags if they were not set by the guest */ |
| 1487 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 1488 | rflags &= ~X86_EFLAGS_TF; |
| 1489 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 1490 | rflags &= ~X86_EFLAGS_RF; |
| 1491 | } |
| 1492 | return rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 1496 | { |
Ladi Prosek | 9b61174 | 2017-06-21 09:06:59 +0200 | [diff] [blame] | 1497 | if (to_svm(vcpu)->nmi_singlestep) |
| 1498 | rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1499 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 1500 | /* |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 1501 | * Any change of EFLAGS.VM is accompanied by a reload of SS |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 1502 | * (caused by either a task switch or an inter-privilege IRET), |
| 1503 | * so we do not need to update the CPL here. |
| 1504 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1505 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1508 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
| 1509 | { |
| 1510 | switch (reg) { |
| 1511 | case VCPU_EXREG_PDPTR: |
| 1512 | BUG_ON(!npt_enabled); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 1513 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1514 | break; |
| 1515 | default: |
Sean Christopherson | 34059c2 | 2019-09-27 14:45:23 -0700 | [diff] [blame] | 1516 | WARN_ON_ONCE(1); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Suravee Suthikulpanit | e14b778 | 2020-05-06 08:17:55 -0500 | [diff] [blame] | 1520 | static void svm_set_vintr(struct vcpu_svm *svm) |
Paolo Bonzini | 64b5bd2 | 2020-03-04 13:12:35 -0500 | [diff] [blame] | 1521 | { |
| 1522 | struct vmcb_control_area *control; |
| 1523 | |
| 1524 | /* The following fields are ignored when AVIC is enabled */ |
| 1525 | WARN_ON(kvm_vcpu_apicv_active(&svm->vcpu)); |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1526 | svm_set_intercept(svm, INTERCEPT_VINTR); |
Paolo Bonzini | 64b5bd2 | 2020-03-04 13:12:35 -0500 | [diff] [blame] | 1527 | |
| 1528 | /* |
| 1529 | * This is just a dummy VINTR to actually cause a vmexit to happen. |
| 1530 | * Actual injection of virtual interrupts happens through EVENTINJ. |
| 1531 | */ |
| 1532 | control = &svm->vmcb->control; |
| 1533 | control->int_vector = 0x0; |
| 1534 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 1535 | control->int_ctl |= V_IRQ_MASK | |
| 1536 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1537 | vmcb_mark_dirty(svm->vmcb, VMCB_INTR); |
Paolo Bonzini | 64b5bd2 | 2020-03-04 13:12:35 -0500 | [diff] [blame] | 1538 | } |
| 1539 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 1540 | static void svm_clear_vintr(struct vcpu_svm *svm) |
| 1541 | { |
Paolo Bonzini | d8e4e58 | 2020-05-22 07:38:20 -0400 | [diff] [blame] | 1542 | const u32 mask = V_TPR_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK | V_INTR_MASKING_MASK; |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 1543 | svm_clr_intercept(svm, INTERCEPT_VINTR); |
Paolo Bonzini | 64b5bd2 | 2020-03-04 13:12:35 -0500 | [diff] [blame] | 1544 | |
Paolo Bonzini | d8e4e58 | 2020-05-22 07:38:20 -0400 | [diff] [blame] | 1545 | /* Drop int_ctl fields related to VINTR injection. */ |
| 1546 | svm->vmcb->control.int_ctl &= mask; |
| 1547 | if (is_guest_mode(&svm->vcpu)) { |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 1548 | svm->vmcb01.ptr->control.int_ctl &= mask; |
Paolo Bonzini | fb7333d | 2020-06-08 07:11:47 -0400 | [diff] [blame] | 1549 | |
Paolo Bonzini | d8e4e58 | 2020-05-22 07:38:20 -0400 | [diff] [blame] | 1550 | WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) != |
| 1551 | (svm->nested.ctl.int_ctl & V_TPR_MASK)); |
| 1552 | svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl & ~mask; |
| 1553 | } |
| 1554 | |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1555 | vmcb_mark_dirty(svm->vmcb, VMCB_INTR); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 1556 | } |
| 1557 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1558 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 1559 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1560 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 1561 | struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1562 | |
| 1563 | switch (seg) { |
| 1564 | case VCPU_SREG_CS: return &save->cs; |
| 1565 | case VCPU_SREG_DS: return &save->ds; |
| 1566 | case VCPU_SREG_ES: return &save->es; |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 1567 | case VCPU_SREG_FS: return &save01->fs; |
| 1568 | case VCPU_SREG_GS: return &save01->gs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1569 | case VCPU_SREG_SS: return &save->ss; |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 1570 | case VCPU_SREG_TR: return &save01->tr; |
| 1571 | case VCPU_SREG_LDTR: return &save01->ldtr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1572 | } |
| 1573 | BUG(); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1574 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1578 | { |
| 1579 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1580 | |
| 1581 | return s->base; |
| 1582 | } |
| 1583 | |
| 1584 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 1585 | struct kvm_segment *var, int seg) |
| 1586 | { |
| 1587 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1588 | |
| 1589 | var->base = s->base; |
| 1590 | var->limit = s->limit; |
| 1591 | var->selector = s->selector; |
| 1592 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 1593 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 1594 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 1595 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 1596 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 1597 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 1598 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
Jim Mattson | 80112c8 | 2014-07-08 09:47:41 +0530 | [diff] [blame] | 1599 | |
| 1600 | /* |
| 1601 | * AMD CPUs circa 2014 track the G bit for all segments except CS. |
| 1602 | * However, the SVM spec states that the G bit is not observed by the |
| 1603 | * CPU, and some VMware virtual CPUs drop the G bit for all segments. |
| 1604 | * So let's synthesize a legal G bit for all segments, this helps |
| 1605 | * running KVM nested. It also helps cross-vendor migration, because |
| 1606 | * Intel's vmentry has a check on the 'G' bit. |
| 1607 | */ |
| 1608 | var->g = s->limit > 0xfffff; |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 1609 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1610 | /* |
| 1611 | * AMD's VMCB does not have an explicit unusable field, so emulate it |
Andre Przywara | 19bca6a | 2009-04-28 12:45:30 +0200 | [diff] [blame] | 1612 | * for cross vendor migration purposes by "not present" |
| 1613 | */ |
Gioh Kim | 8eae957 | 2017-05-30 15:24:45 +0200 | [diff] [blame] | 1614 | var->unusable = !var->present; |
Andre Przywara | 19bca6a | 2009-04-28 12:45:30 +0200 | [diff] [blame] | 1615 | |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 1616 | switch (seg) { |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 1617 | case VCPU_SREG_TR: |
| 1618 | /* |
| 1619 | * Work around a bug where the busy flag in the tr selector |
| 1620 | * isn't exposed |
| 1621 | */ |
Amit Shah | c0d0982 | 2008-10-27 09:04:18 +0000 | [diff] [blame] | 1622 | var->type |= 0x2; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 1623 | break; |
| 1624 | case VCPU_SREG_DS: |
| 1625 | case VCPU_SREG_ES: |
| 1626 | case VCPU_SREG_FS: |
| 1627 | case VCPU_SREG_GS: |
| 1628 | /* |
| 1629 | * The accessed bit must always be set in the segment |
| 1630 | * descriptor cache, although it can be cleared in the |
| 1631 | * descriptor, the cached bit always remains at 1. Since |
| 1632 | * Intel has a check on this, set it here to support |
| 1633 | * cross-vendor migration. |
| 1634 | */ |
| 1635 | if (!var->unusable) |
| 1636 | var->type |= 0x1; |
| 1637 | break; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 1638 | case VCPU_SREG_SS: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1639 | /* |
| 1640 | * On AMD CPUs sometimes the DB bit in the segment |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 1641 | * descriptor is left as 1, although the whole segment has |
| 1642 | * been made unusable. Clear it here to pass an Intel VMX |
| 1643 | * entry check when cross vendor migrating. |
| 1644 | */ |
| 1645 | if (var->unusable) |
| 1646 | var->db = 0; |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 1647 | /* This is symmetric with svm_set_segment() */ |
Jan Kiszka | 33b458d | 2014-06-29 17:12:43 +0200 | [diff] [blame] | 1648 | var->dpl = to_svm(vcpu)->vmcb->save.cpl; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 1649 | break; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 1650 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1651 | } |
| 1652 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 1653 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
| 1654 | { |
| 1655 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 1656 | |
| 1657 | return save->cpl; |
| 1658 | } |
| 1659 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1660 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1661 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1662 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1663 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1664 | dt->size = svm->vmcb->save.idtr.limit; |
| 1665 | dt->address = svm->vmcb->save.idtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1666 | } |
| 1667 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1668 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1669 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1670 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1671 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1672 | svm->vmcb->save.idtr.limit = dt->size; |
| 1673 | svm->vmcb->save.idtr.base = dt->address ; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1674 | vmcb_mark_dirty(svm->vmcb, VMCB_DT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1675 | } |
| 1676 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1677 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1678 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1679 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1680 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1681 | dt->size = svm->vmcb->save.gdtr.limit; |
| 1682 | dt->address = svm->vmcb->save.gdtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1683 | } |
| 1684 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1685 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1686 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1687 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1688 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 1689 | svm->vmcb->save.gdtr.limit = dt->size; |
| 1690 | svm->vmcb->save.gdtr.base = dt->address ; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1691 | vmcb_mark_dirty(svm->vmcb, VMCB_DT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
Joerg Roedel | 883b0a9 | 2020-03-24 10:41:52 +0100 | [diff] [blame] | 1694 | void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1695 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1696 | struct vcpu_svm *svm = to_svm(vcpu); |
Paolo Bonzini | 2a32a77 | 2021-02-18 09:51:06 -0500 | [diff] [blame] | 1697 | u64 hcr0 = cr0; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1698 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1699 | #ifdef CONFIG_X86_64 |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 1700 | if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1701 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1702 | vcpu->arch.efer |= EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 1703 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1704 | } |
| 1705 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1706 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1707 | vcpu->arch.efer &= ~EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 1708 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1712 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 888f9f3 | 2010-01-10 12:14:04 +0200 | [diff] [blame] | 1713 | |
| 1714 | if (!npt_enabled) |
Paolo Bonzini | 2a32a77 | 2021-02-18 09:51:06 -0500 | [diff] [blame] | 1715 | hcr0 |= X86_CR0_PG | X86_CR0_WP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1716 | |
Paolo Bonzini | bcf166a | 2015-10-01 13:19:55 +0200 | [diff] [blame] | 1717 | /* |
| 1718 | * re-enable caching here because the QEMU bios |
| 1719 | * does not do it - this results in some delay at |
| 1720 | * reboot |
| 1721 | */ |
| 1722 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
Paolo Bonzini | 2a32a77 | 2021-02-18 09:51:06 -0500 | [diff] [blame] | 1723 | hcr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
| 1724 | |
| 1725 | svm->vmcb->save.cr0 = hcr0; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1726 | vmcb_mark_dirty(svm->vmcb, VMCB_CR); |
Paolo Bonzini | 2a32a77 | 2021-02-18 09:51:06 -0500 | [diff] [blame] | 1727 | |
| 1728 | /* |
| 1729 | * SEV-ES guests must always keep the CR intercepts cleared. CR |
| 1730 | * tracking is done using the CR write traps. |
| 1731 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1732 | if (sev_es_guest(vcpu->kvm)) |
Paolo Bonzini | 2a32a77 | 2021-02-18 09:51:06 -0500 | [diff] [blame] | 1733 | return; |
| 1734 | |
| 1735 | if (hcr0 == cr0) { |
| 1736 | /* Selective CR0 write remains on. */ |
| 1737 | svm_clr_intercept(svm, INTERCEPT_CR0_READ); |
| 1738 | svm_clr_intercept(svm, INTERCEPT_CR0_WRITE); |
| 1739 | } else { |
| 1740 | svm_set_intercept(svm, INTERCEPT_CR0_READ); |
| 1741 | svm_set_intercept(svm, INTERCEPT_CR0_WRITE); |
| 1742 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 1745 | static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 1746 | { |
| 1747 | return true; |
| 1748 | } |
| 1749 | |
| 1750 | void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1751 | { |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 1752 | unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE; |
Paolo Bonzini | dc924b0 | 2020-11-15 09:44:18 -0500 | [diff] [blame] | 1753 | unsigned long old_cr4 = vcpu->arch.cr4; |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 1754 | |
| 1755 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
Sean Christopherson | f55ac30 | 2020-03-20 14:28:12 -0700 | [diff] [blame] | 1756 | svm_flush_tlb(vcpu); |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 1757 | |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 1758 | vcpu->arch.cr4 = cr4; |
| 1759 | if (!npt_enabled) |
| 1760 | cr4 |= X86_CR4_PAE; |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 1761 | cr4 |= host_cr4_mce; |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 1762 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1763 | vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
Jim Mattson | 2259c17 | 2020-10-29 10:06:48 -0700 | [diff] [blame] | 1764 | |
| 1765 | if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) |
| 1766 | kvm_update_cpuid_runtime(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 1770 | struct kvm_segment *var, int seg) |
| 1771 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1772 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1773 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1774 | |
| 1775 | s->base = var->base; |
| 1776 | s->limit = var->limit; |
| 1777 | s->selector = var->selector; |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 1778 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 1779 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 1780 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 1781 | s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT; |
| 1782 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 1783 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 1784 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 1785 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 1786 | |
| 1787 | /* |
| 1788 | * This is always accurate, except if SYSRET returned to a segment |
| 1789 | * with SS.DPL != 3. Intel does not have this quirk, and always |
| 1790 | * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it |
| 1791 | * would entail passing the CPL to userspace and back. |
| 1792 | */ |
| 1793 | if (seg == VCPU_SREG_SS) |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 1794 | /* This is symmetric with svm_get_segment() */ |
| 1795 | svm->vmcb->save.cpl = (var->dpl & 3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1796 | |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1797 | vmcb_mark_dirty(svm->vmcb, VMCB_SEG); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1798 | } |
| 1799 | |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 1800 | static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1801 | { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1802 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1803 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1804 | clr_exception_intercept(svm, BP_VECTOR); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1805 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1806 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1807 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1808 | set_exception_intercept(svm, BP_VECTOR); |
Paolo Bonzini | 6986982 | 2020-07-10 17:48:06 +0200 | [diff] [blame] | 1809 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1810 | } |
| 1811 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1812 | static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1813 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1814 | if (sd->next_asid > sd->max_asid) { |
| 1815 | ++sd->asid_generation; |
Brijesh Singh | 4faefff | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 1816 | sd->next_asid = sd->min_asid; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1817 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
Cathy Avery | 7e8e6ee | 2020-10-11 14:48:17 -0400 | [diff] [blame] | 1818 | vmcb_mark_dirty(svm->vmcb, VMCB_ASID); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1819 | } |
| 1820 | |
Cathy Avery | 193015a | 2021-01-12 11:43:13 -0500 | [diff] [blame] | 1821 | svm->current_vmcb->asid_generation = sd->asid_generation; |
Cathy Avery | 7e8e6ee | 2020-10-11 14:48:17 -0400 | [diff] [blame] | 1822 | svm->asid = sd->next_asid++; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1823 | } |
| 1824 | |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 1825 | static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value) |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 1826 | { |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 1827 | struct vmcb *vmcb = svm->vmcb; |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 1828 | |
Tom Lendacky | 8d4846b | 2020-12-10 11:09:43 -0600 | [diff] [blame] | 1829 | if (svm->vcpu.arch.guest_state_protected) |
| 1830 | return; |
| 1831 | |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 1832 | if (unlikely(value != vmcb->save.dr6)) { |
| 1833 | vmcb->save.dr6 = value; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1834 | vmcb_mark_dirty(vmcb, VMCB_DR); |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 1835 | } |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 1838 | static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 1839 | { |
| 1840 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1841 | |
Tom Lendacky | 8d4846b | 2020-12-10 11:09:43 -0600 | [diff] [blame] | 1842 | if (vcpu->arch.guest_state_protected) |
| 1843 | return; |
| 1844 | |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 1845 | get_debugreg(vcpu->arch.db[0], 0); |
| 1846 | get_debugreg(vcpu->arch.db[1], 1); |
| 1847 | get_debugreg(vcpu->arch.db[2], 2); |
| 1848 | get_debugreg(vcpu->arch.db[3], 3); |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 1849 | /* |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 1850 | * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here, |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 1851 | * because db_interception might need it. We can do it before vmentry. |
| 1852 | */ |
Paolo Bonzini | 5679b80 | 2020-05-04 11:28:25 -0400 | [diff] [blame] | 1853 | vcpu->arch.dr6 = svm->vmcb->save.dr6; |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 1854 | vcpu->arch.dr7 = svm->vmcb->save.dr7; |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 1855 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
| 1856 | set_dr_intercepts(svm); |
| 1857 | } |
| 1858 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1859 | static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1860 | { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1861 | struct vcpu_svm *svm = to_svm(vcpu); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1862 | |
Tom Lendacky | 8d4846b | 2020-12-10 11:09:43 -0600 | [diff] [blame] | 1863 | if (vcpu->arch.guest_state_protected) |
| 1864 | return; |
| 1865 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 1866 | svm->vmcb->save.dr7 = value; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 1867 | vmcb_mark_dirty(svm->vmcb, VMCB_DR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1868 | } |
| 1869 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1870 | static int pf_interception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1871 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1872 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1873 | |
Sean Christopherson | 6d1b867 | 2021-03-04 17:10:56 -0800 | [diff] [blame] | 1874 | u64 fault_address = svm->vmcb->control.exit_info_2; |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 1875 | u64 error_code = svm->vmcb->control.exit_info_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1876 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1877 | return kvm_handle_page_fault(vcpu, error_code, fault_address, |
Brijesh Singh | 00b10fe | 2017-12-04 10:57:40 -0600 | [diff] [blame] | 1878 | static_cpu_has(X86_FEATURE_DECODEASSISTS) ? |
| 1879 | svm->vmcb->control.insn_bytes : NULL, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 1880 | svm->vmcb->control.insn_len); |
| 1881 | } |
| 1882 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1883 | static int npf_interception(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 1884 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1885 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1886 | |
Brijesh Singh | 0ede79e | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 1887 | u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 1888 | u64 error_code = svm->vmcb->control.exit_info_1; |
| 1889 | |
| 1890 | trace_kvm_page_fault(fault_address, error_code); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1891 | return kvm_mmu_page_fault(vcpu, fault_address, error_code, |
Brijesh Singh | 00b10fe | 2017-12-04 10:57:40 -0600 | [diff] [blame] | 1892 | static_cpu_has(X86_FEATURE_DECODEASSISTS) ? |
| 1893 | svm->vmcb->control.insn_bytes : NULL, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 1894 | svm->vmcb->control.insn_len); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1895 | } |
| 1896 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1897 | static int db_interception(struct kvm_vcpu *vcpu) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1898 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1899 | struct kvm_run *kvm_run = vcpu->run; |
| 1900 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1901 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1902 | if (!(vcpu->guest_debug & |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1903 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 1904 | !svm->nmi_singlestep) { |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 1905 | u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1906 | kvm_queue_exception_p(vcpu, DB_VECTOR, payload); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1907 | return 1; |
| 1908 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1909 | |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 1910 | if (svm->nmi_singlestep) { |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 1911 | disable_nmi_singlestep(svm); |
Vitaly Kuznetsov | 99c2217 | 2019-04-03 16:06:42 +0200 | [diff] [blame] | 1912 | /* Make sure we check for pending NMIs upon entry */ |
| 1913 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1914 | } |
| 1915 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1916 | if (vcpu->guest_debug & |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1917 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1918 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Paolo Bonzini | dee919d | 2020-05-04 09:34:10 -0400 | [diff] [blame] | 1919 | kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6; |
| 1920 | kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1921 | kvm_run->debug.arch.pc = |
| 1922 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 1923 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 1924 | return 0; |
| 1925 | } |
| 1926 | |
| 1927 | return 1; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1928 | } |
| 1929 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1930 | static int bp_interception(struct kvm_vcpu *vcpu) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1931 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1932 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1933 | struct kvm_run *kvm_run = vcpu->run; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1934 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1935 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 1936 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 1937 | kvm_run->debug.arch.exception = BP_VECTOR; |
| 1938 | return 0; |
| 1939 | } |
| 1940 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1941 | static int ud_interception(struct kvm_vcpu *vcpu) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1942 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1943 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1944 | } |
| 1945 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1946 | static int ac_interception(struct kvm_vcpu *vcpu) |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1947 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1948 | kvm_queue_exception_e(vcpu, AC_VECTOR, 0); |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1949 | return 1; |
| 1950 | } |
| 1951 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 1952 | static bool is_erratum_383(void) |
| 1953 | { |
| 1954 | int err, i; |
| 1955 | u64 value; |
| 1956 | |
| 1957 | if (!erratum_383_found) |
| 1958 | return false; |
| 1959 | |
| 1960 | value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); |
| 1961 | if (err) |
| 1962 | return false; |
| 1963 | |
| 1964 | /* Bit 62 may or may not be set for this mce */ |
| 1965 | value &= ~(1ULL << 62); |
| 1966 | |
| 1967 | if (value != 0xb600000000010015ULL) |
| 1968 | return false; |
| 1969 | |
| 1970 | /* Clear MCi_STATUS registers */ |
| 1971 | for (i = 0; i < 6; ++i) |
| 1972 | native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); |
| 1973 | |
| 1974 | value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); |
| 1975 | if (!err) { |
| 1976 | u32 low, high; |
| 1977 | |
| 1978 | value &= ~(1ULL << 2); |
| 1979 | low = lower_32_bits(value); |
| 1980 | high = upper_32_bits(value); |
| 1981 | |
| 1982 | native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); |
| 1983 | } |
| 1984 | |
| 1985 | /* Flush tlb to evict multi-match entries */ |
| 1986 | __flush_tlb_all(); |
| 1987 | |
| 1988 | return true; |
| 1989 | } |
| 1990 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 1991 | static void svm_handle_mce(struct kvm_vcpu *vcpu) |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 1992 | { |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 1993 | if (is_erratum_383()) { |
| 1994 | /* |
| 1995 | * Erratum 383 triggered. Guest state is corrupt so kill the |
| 1996 | * guest. |
| 1997 | */ |
| 1998 | pr_err("KVM: Guest triggered AMD Erratum 383\n"); |
| 1999 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2000 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2001 | |
| 2002 | return; |
| 2003 | } |
| 2004 | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2005 | /* |
| 2006 | * On an #MC intercept the MCE handler is not called automatically in |
| 2007 | * the host. So do it by hand here. |
| 2008 | */ |
Uros Bizjak | 1c164cb | 2020-04-11 17:36:27 +0200 | [diff] [blame] | 2009 | kvm_machine_check(); |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 2010 | } |
| 2011 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2012 | static int mc_interception(struct kvm_vcpu *vcpu) |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 2013 | { |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2014 | return 1; |
| 2015 | } |
| 2016 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2017 | static int shutdown_interception(struct kvm_vcpu *vcpu) |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2018 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2019 | struct kvm_run *kvm_run = vcpu->run; |
| 2020 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2021 | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2022 | /* |
Tom Lendacky | 8164a5f | 2020-12-10 11:09:45 -0600 | [diff] [blame] | 2023 | * The VM save area has already been encrypted so it |
| 2024 | * cannot be reinitialized - just terminate. |
| 2025 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2026 | if (sev_es_guest(vcpu->kvm)) |
Tom Lendacky | 8164a5f | 2020-12-10 11:09:45 -0600 | [diff] [blame] | 2027 | return -EINVAL; |
| 2028 | |
| 2029 | /* |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2030 | * VMCB is undefined after a SHUTDOWN intercept |
| 2031 | * so reinitialize it. |
| 2032 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2033 | clear_page(svm->vmcb); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2034 | init_vmcb(vcpu); |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2035 | |
| 2036 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2037 | return 0; |
| 2038 | } |
| 2039 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2040 | static int io_interception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2041 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2042 | struct vcpu_svm *svm = to_svm(vcpu); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2043 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 2044 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2045 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2046 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2047 | ++vcpu->stat.io_exits; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2048 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2049 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
| 2050 | port = io_info >> 16; |
| 2051 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
Tom Lendacky | 7ed9abf | 2020-12-10 11:09:54 -0600 | [diff] [blame] | 2052 | |
| 2053 | if (string) { |
| 2054 | if (sev_es_guest(vcpu->kvm)) |
| 2055 | return sev_es_string_io(svm, size, port, in); |
| 2056 | else |
| 2057 | return kvm_emulate_instruction(vcpu, 0); |
| 2058 | } |
| 2059 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2060 | svm->next_rip = svm->vmcb->control.exit_info_2; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2061 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2062 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2063 | } |
| 2064 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2065 | static int nmi_interception(struct kvm_vcpu *vcpu) |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 2066 | { |
| 2067 | return 1; |
| 2068 | } |
| 2069 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2070 | static int intr_interception(struct kvm_vcpu *vcpu) |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 2071 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2072 | ++vcpu->stat.irq_exits; |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 2073 | return 1; |
| 2074 | } |
| 2075 | |
Sean Christopherson | 2ac636a | 2021-02-04 16:57:45 -0800 | [diff] [blame] | 2076 | static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2077 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2078 | struct vcpu_svm *svm = to_svm(vcpu); |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 2079 | struct vmcb *vmcb12; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 2080 | struct kvm_host_map map; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2081 | int ret; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2082 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2083 | if (nested_svm_check_permissions(vcpu)) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2084 | return 1; |
| 2085 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2086 | ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map); |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 2087 | if (ret) { |
| 2088 | if (ret == -EINVAL) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2089 | kvm_inject_gp(vcpu, 0); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2090 | return 1; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 2091 | } |
| 2092 | |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 2093 | vmcb12 = map.hva; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 2094 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2095 | ret = kvm_skip_emulated_instruction(vcpu); |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 2096 | |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 2097 | if (vmload) { |
Sean Christopherson | 2ac636a | 2021-02-04 16:57:45 -0800 | [diff] [blame] | 2098 | nested_svm_vmloadsave(vmcb12, svm->vmcb); |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 2099 | svm->sysenter_eip_hi = 0; |
| 2100 | svm->sysenter_esp_hi = 0; |
| 2101 | } else |
Sean Christopherson | 2ac636a | 2021-02-04 16:57:45 -0800 | [diff] [blame] | 2102 | nested_svm_vmloadsave(svm->vmcb, vmcb12); |
| 2103 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2104 | kvm_vcpu_unmap(vcpu, &map, true); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2105 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2106 | return ret; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2107 | } |
| 2108 | |
Sean Christopherson | 2ac636a | 2021-02-04 16:57:45 -0800 | [diff] [blame] | 2109 | static int vmload_interception(struct kvm_vcpu *vcpu) |
| 2110 | { |
| 2111 | return vmload_vmsave_interception(vcpu, true); |
| 2112 | } |
| 2113 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2114 | static int vmsave_interception(struct kvm_vcpu *vcpu) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2115 | { |
Sean Christopherson | 2ac636a | 2021-02-04 16:57:45 -0800 | [diff] [blame] | 2116 | return vmload_vmsave_interception(vcpu, false); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2119 | static int vmrun_interception(struct kvm_vcpu *vcpu) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2120 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2121 | if (nested_svm_check_permissions(vcpu)) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2122 | return 1; |
| 2123 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2124 | return nested_svm_vmrun(vcpu); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 2127 | enum { |
| 2128 | NONE_SVM_INSTR, |
| 2129 | SVM_INSTR_VMRUN, |
| 2130 | SVM_INSTR_VMLOAD, |
| 2131 | SVM_INSTR_VMSAVE, |
| 2132 | }; |
| 2133 | |
| 2134 | /* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */ |
| 2135 | static int svm_instr_opcode(struct kvm_vcpu *vcpu) |
| 2136 | { |
| 2137 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
| 2138 | |
| 2139 | if (ctxt->b != 0x1 || ctxt->opcode_len != 2) |
| 2140 | return NONE_SVM_INSTR; |
| 2141 | |
| 2142 | switch (ctxt->modrm) { |
| 2143 | case 0xd8: /* VMRUN */ |
| 2144 | return SVM_INSTR_VMRUN; |
| 2145 | case 0xda: /* VMLOAD */ |
| 2146 | return SVM_INSTR_VMLOAD; |
| 2147 | case 0xdb: /* VMSAVE */ |
| 2148 | return SVM_INSTR_VMSAVE; |
| 2149 | default: |
| 2150 | break; |
| 2151 | } |
| 2152 | |
| 2153 | return NONE_SVM_INSTR; |
| 2154 | } |
| 2155 | |
| 2156 | static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode) |
| 2157 | { |
Wei Huang | 14c2bf8 | 2021-01-26 03:18:31 -0500 | [diff] [blame] | 2158 | const int guest_mode_exit_codes[] = { |
| 2159 | [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN, |
| 2160 | [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD, |
| 2161 | [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE, |
| 2162 | }; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2163 | int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = { |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 2164 | [SVM_INSTR_VMRUN] = vmrun_interception, |
| 2165 | [SVM_INSTR_VMLOAD] = vmload_interception, |
| 2166 | [SVM_INSTR_VMSAVE] = vmsave_interception, |
| 2167 | }; |
| 2168 | struct vcpu_svm *svm = to_svm(vcpu); |
Sean Christopherson | 2df8d38 | 2021-02-23 16:56:26 -0800 | [diff] [blame] | 2169 | int ret; |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 2170 | |
Wei Huang | 14c2bf8 | 2021-01-26 03:18:31 -0500 | [diff] [blame] | 2171 | if (is_guest_mode(vcpu)) { |
Sean Christopherson | 2df8d38 | 2021-02-23 16:56:26 -0800 | [diff] [blame] | 2172 | /* Returns '1' or -errno on failure, '0' on success. */ |
Sean Christopherson | 3a87c7e | 2021-03-02 09:45:15 -0800 | [diff] [blame] | 2173 | ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]); |
Sean Christopherson | 2df8d38 | 2021-02-23 16:56:26 -0800 | [diff] [blame] | 2174 | if (ret) |
| 2175 | return ret; |
| 2176 | return 1; |
| 2177 | } |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2178 | return svm_instr_handlers[opcode](vcpu); |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /* |
| 2182 | * #GP handling code. Note that #GP can be triggered under the following two |
| 2183 | * cases: |
| 2184 | * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on |
| 2185 | * some AMD CPUs when EAX of these instructions are in the reserved memory |
| 2186 | * regions (e.g. SMM memory on host). |
| 2187 | * 2) VMware backdoor |
| 2188 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2189 | static int gp_interception(struct kvm_vcpu *vcpu) |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 2190 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2191 | struct vcpu_svm *svm = to_svm(vcpu); |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 2192 | u32 error_code = svm->vmcb->control.exit_info_1; |
| 2193 | int opcode; |
| 2194 | |
| 2195 | /* Both #GP cases have zero error_code */ |
| 2196 | if (error_code) |
| 2197 | goto reinject; |
| 2198 | |
| 2199 | /* Decode the instruction for usage later */ |
| 2200 | if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK) |
| 2201 | goto reinject; |
| 2202 | |
| 2203 | opcode = svm_instr_opcode(vcpu); |
| 2204 | |
| 2205 | if (opcode == NONE_SVM_INSTR) { |
| 2206 | if (!enable_vmware_backdoor) |
| 2207 | goto reinject; |
| 2208 | |
| 2209 | /* |
| 2210 | * VMware backdoor emulation on #GP interception only handles |
| 2211 | * IN{S}, OUT{S}, and RDPMC. |
| 2212 | */ |
Wei Huang | 14c2bf8 | 2021-01-26 03:18:31 -0500 | [diff] [blame] | 2213 | if (!is_guest_mode(vcpu)) |
| 2214 | return kvm_emulate_instruction(vcpu, |
Bandan Das | 82a11e9c | 2021-01-26 03:18:29 -0500 | [diff] [blame] | 2215 | EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE); |
| 2216 | } else |
| 2217 | return emulate_svm_instr(vcpu, opcode); |
| 2218 | |
| 2219 | reinject: |
| 2220 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 2221 | return 1; |
| 2222 | } |
| 2223 | |
Paolo Bonzini | ffdf7f9 | 2020-05-22 12:18:27 -0400 | [diff] [blame] | 2224 | void svm_set_gif(struct vcpu_svm *svm, bool value) |
| 2225 | { |
| 2226 | if (value) { |
| 2227 | /* |
| 2228 | * If VGIF is enabled, the STGI intercept is only added to |
| 2229 | * detect the opening of the SMI/NMI window; remove it now. |
| 2230 | * Likewise, clear the VINTR intercept, we will set it |
| 2231 | * again while processing KVM_REQ_EVENT if needed. |
| 2232 | */ |
| 2233 | if (vgif_enabled(svm)) |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 2234 | svm_clr_intercept(svm, INTERCEPT_STGI); |
| 2235 | if (svm_is_intercept(svm, INTERCEPT_VINTR)) |
Paolo Bonzini | ffdf7f9 | 2020-05-22 12:18:27 -0400 | [diff] [blame] | 2236 | svm_clear_vintr(svm); |
| 2237 | |
| 2238 | enable_gif(svm); |
| 2239 | if (svm->vcpu.arch.smi_pending || |
| 2240 | svm->vcpu.arch.nmi_pending || |
| 2241 | kvm_cpu_has_injectable_intr(&svm->vcpu)) |
| 2242 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 2243 | } else { |
| 2244 | disable_gif(svm); |
| 2245 | |
| 2246 | /* |
| 2247 | * After a CLGI no interrupts should come. But if vGIF is |
| 2248 | * in use, we still rely on the VINTR intercept (rather than |
| 2249 | * STGI) to detect an open interrupt window. |
| 2250 | */ |
| 2251 | if (!vgif_enabled(svm)) |
| 2252 | svm_clear_vintr(svm); |
| 2253 | } |
| 2254 | } |
| 2255 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2256 | static int stgi_interception(struct kvm_vcpu *vcpu) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2257 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2258 | int ret; |
| 2259 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2260 | if (nested_svm_check_permissions(vcpu)) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2261 | return 1; |
| 2262 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2263 | ret = kvm_skip_emulated_instruction(vcpu); |
| 2264 | svm_set_gif(to_svm(vcpu), true); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2265 | return ret; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2268 | static int clgi_interception(struct kvm_vcpu *vcpu) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2269 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2270 | int ret; |
| 2271 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2272 | if (nested_svm_check_permissions(vcpu)) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2273 | return 1; |
| 2274 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2275 | ret = kvm_skip_emulated_instruction(vcpu); |
| 2276 | svm_set_gif(to_svm(vcpu), false); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2277 | return ret; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2278 | } |
| 2279 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2280 | static int invlpga_interception(struct kvm_vcpu *vcpu) |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2281 | { |
Sean Christopherson | bc9eff6 | 2021-04-21 19:21:27 -0700 | [diff] [blame] | 2282 | gva_t gva = kvm_rax_read(vcpu); |
| 2283 | u32 asid = kvm_rcx_read(vcpu); |
| 2284 | |
| 2285 | /* FIXME: Handle an address size prefix. */ |
| 2286 | if (!is_long_mode(vcpu)) |
| 2287 | gva = (u32)gva; |
| 2288 | |
| 2289 | trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 2290 | |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2291 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
Sean Christopherson | bc9eff6 | 2021-04-21 19:21:27 -0700 | [diff] [blame] | 2292 | kvm_mmu_invlpg(vcpu, gva); |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2293 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2294 | return kvm_skip_emulated_instruction(vcpu); |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2295 | } |
| 2296 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2297 | static int skinit_interception(struct kvm_vcpu *vcpu) |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 2298 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2299 | trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu)); |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 2300 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2301 | kvm_queue_exception(vcpu, UD_VECTOR); |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 2302 | return 1; |
| 2303 | } |
| 2304 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2305 | static int task_switch_interception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2306 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2307 | struct vcpu_svm *svm = to_svm(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2308 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2309 | int reason; |
| 2310 | int int_type = svm->vmcb->control.exit_int_info & |
| 2311 | SVM_EXITINTINFO_TYPE_MASK; |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 2312 | int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2313 | uint32_t type = |
| 2314 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; |
| 2315 | uint32_t idt_v = |
| 2316 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2317 | bool has_error_code = false; |
| 2318 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2319 | |
| 2320 | tss_selector = (u16)svm->vmcb->control.exit_info_1; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2321 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2322 | if (svm->vmcb->control.exit_info_2 & |
| 2323 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2324 | reason = TASK_SWITCH_IRET; |
| 2325 | else if (svm->vmcb->control.exit_info_2 & |
| 2326 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) |
| 2327 | reason = TASK_SWITCH_JMP; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2328 | else if (idt_v) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2329 | reason = TASK_SWITCH_GATE; |
| 2330 | else |
| 2331 | reason = TASK_SWITCH_CALL; |
| 2332 | |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2333 | if (reason == TASK_SWITCH_GATE) { |
| 2334 | switch (type) { |
| 2335 | case SVM_EXITINTINFO_TYPE_NMI: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2336 | vcpu->arch.nmi_injected = false; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2337 | break; |
| 2338 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 2339 | if (svm->vmcb->control.exit_info_2 & |
| 2340 | (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { |
| 2341 | has_error_code = true; |
| 2342 | error_code = |
| 2343 | (u32)svm->vmcb->control.exit_info_2; |
| 2344 | } |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2345 | kvm_clear_exception_queue(vcpu); |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2346 | break; |
| 2347 | case SVM_EXITINTINFO_TYPE_INTR: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2348 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2349 | break; |
| 2350 | default: |
| 2351 | break; |
| 2352 | } |
| 2353 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2354 | |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 2355 | if (reason != TASK_SWITCH_GATE || |
| 2356 | int_type == SVM_EXITINTINFO_TYPE_SOFT || |
| 2357 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 2358 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2359 | if (!skip_emulated_instruction(vcpu)) |
Sean Christopherson | 738fece | 2019-08-27 14:40:34 -0700 | [diff] [blame] | 2360 | return 0; |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 2361 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2362 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 2363 | if (int_type != SVM_EXITINTINFO_TYPE_SOFT) |
| 2364 | int_vec = -1; |
| 2365 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2366 | return kvm_task_switch(vcpu, tss_selector, int_vec, reason, |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 2367 | has_error_code, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2368 | } |
| 2369 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2370 | static int iret_interception(struct kvm_vcpu *vcpu) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2371 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2372 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2373 | |
| 2374 | ++vcpu->stat.nmi_window_exits; |
| 2375 | vcpu->arch.hflags |= HF_IRET_MASK; |
| 2376 | if (!sev_es_guest(vcpu->kvm)) { |
Tom Lendacky | 4444dfe | 2020-12-14 11:16:03 -0500 | [diff] [blame] | 2377 | svm_clr_intercept(svm, INTERCEPT_IRET); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2378 | svm->nmi_iret_rip = kvm_rip_read(vcpu); |
Tom Lendacky | 4444dfe | 2020-12-14 11:16:03 -0500 | [diff] [blame] | 2379 | } |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2380 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2381 | return 1; |
| 2382 | } |
| 2383 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2384 | static int invlpg_interception(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2385 | { |
Andre Przywara | df4f3108 | 2010-12-21 11:12:06 +0100 | [diff] [blame] | 2386 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2387 | return kvm_emulate_instruction(vcpu, 0); |
Andre Przywara | df4f3108 | 2010-12-21 11:12:06 +0100 | [diff] [blame] | 2388 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2389 | kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1); |
| 2390 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2391 | } |
| 2392 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2393 | static int emulate_on_interception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2394 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2395 | return kvm_emulate_instruction(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2396 | } |
| 2397 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2398 | static int rsm_interception(struct kvm_vcpu *vcpu) |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 2399 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2400 | return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2); |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 2401 | } |
| 2402 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2403 | static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu, |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 2404 | unsigned long val) |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 2405 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2406 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2407 | unsigned long cr0 = vcpu->arch.cr0; |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 2408 | bool ret = false; |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 2409 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2410 | if (!is_guest_mode(vcpu) || |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 2411 | (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0)))) |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 2412 | return false; |
| 2413 | |
| 2414 | cr0 &= ~SVM_CR0_SELECTIVE_MASK; |
| 2415 | val &= ~SVM_CR0_SELECTIVE_MASK; |
| 2416 | |
| 2417 | if (cr0 ^ val) { |
| 2418 | svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 2419 | ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); |
| 2420 | } |
| 2421 | |
| 2422 | return ret; |
| 2423 | } |
| 2424 | |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2425 | #define CR_VALID (1ULL << 63) |
| 2426 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2427 | static int cr_interception(struct kvm_vcpu *vcpu) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2428 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2429 | struct vcpu_svm *svm = to_svm(vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2430 | int reg, cr; |
| 2431 | unsigned long val; |
| 2432 | int err; |
| 2433 | |
| 2434 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2435 | return emulate_on_interception(vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2436 | |
| 2437 | if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2438 | return emulate_on_interception(vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2439 | |
| 2440 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
David Kaplan | 5e57518 | 2015-03-06 14:44:35 -0600 | [diff] [blame] | 2441 | if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) |
| 2442 | cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0; |
| 2443 | else |
| 2444 | cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2445 | |
| 2446 | err = 0; |
| 2447 | if (cr >= 16) { /* mov to cr */ |
| 2448 | cr -= 16; |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 2449 | val = kvm_register_read(vcpu, reg); |
Haiwei Li | 95b28ac | 2020-09-04 19:25:29 +0800 | [diff] [blame] | 2450 | trace_kvm_cr_write(cr, val); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2451 | switch (cr) { |
| 2452 | case 0: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2453 | if (!check_selective_cr0_intercepted(vcpu, val)) |
| 2454 | err = kvm_set_cr0(vcpu, val); |
Joerg Roedel | 977b2d0 | 2011-04-18 11:42:52 +0200 | [diff] [blame] | 2455 | else |
| 2456 | return 1; |
| 2457 | |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2458 | break; |
| 2459 | case 3: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2460 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2461 | break; |
| 2462 | case 4: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2463 | err = kvm_set_cr4(vcpu, val); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2464 | break; |
| 2465 | case 8: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2466 | err = kvm_set_cr8(vcpu, val); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2467 | break; |
| 2468 | default: |
| 2469 | WARN(1, "unhandled write to CR%d", cr); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2470 | kvm_queue_exception(vcpu, UD_VECTOR); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2471 | return 1; |
| 2472 | } |
| 2473 | } else { /* mov from cr */ |
| 2474 | switch (cr) { |
| 2475 | case 0: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2476 | val = kvm_read_cr0(vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2477 | break; |
| 2478 | case 2: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2479 | val = vcpu->arch.cr2; |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2480 | break; |
| 2481 | case 3: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2482 | val = kvm_read_cr3(vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2483 | break; |
| 2484 | case 4: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2485 | val = kvm_read_cr4(vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2486 | break; |
| 2487 | case 8: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2488 | val = kvm_get_cr8(vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2489 | break; |
| 2490 | default: |
| 2491 | WARN(1, "unhandled read from CR%d", cr); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2492 | kvm_queue_exception(vcpu, UD_VECTOR); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2493 | return 1; |
| 2494 | } |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 2495 | kvm_register_write(vcpu, reg, val); |
Haiwei Li | 95b28ac | 2020-09-04 19:25:29 +0800 | [diff] [blame] | 2496 | trace_kvm_cr_read(cr, val); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2497 | } |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2498 | return kvm_complete_insn_gp(vcpu, err); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2499 | } |
| 2500 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2501 | static int cr_trap(struct kvm_vcpu *vcpu) |
Tom Lendacky | f27ad38 | 2020-12-10 11:09:56 -0600 | [diff] [blame] | 2502 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2503 | struct vcpu_svm *svm = to_svm(vcpu); |
Tom Lendacky | f27ad38 | 2020-12-10 11:09:56 -0600 | [diff] [blame] | 2504 | unsigned long old_value, new_value; |
| 2505 | unsigned int cr; |
Tom Lendacky | d1949b9 | 2020-12-10 11:09:58 -0600 | [diff] [blame] | 2506 | int ret = 0; |
Tom Lendacky | f27ad38 | 2020-12-10 11:09:56 -0600 | [diff] [blame] | 2507 | |
| 2508 | new_value = (unsigned long)svm->vmcb->control.exit_info_1; |
| 2509 | |
| 2510 | cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP; |
| 2511 | switch (cr) { |
| 2512 | case 0: |
| 2513 | old_value = kvm_read_cr0(vcpu); |
| 2514 | svm_set_cr0(vcpu, new_value); |
| 2515 | |
| 2516 | kvm_post_set_cr0(vcpu, old_value, new_value); |
| 2517 | break; |
Tom Lendacky | 5b51cb1 | 2020-12-10 11:09:57 -0600 | [diff] [blame] | 2518 | case 4: |
| 2519 | old_value = kvm_read_cr4(vcpu); |
| 2520 | svm_set_cr4(vcpu, new_value); |
| 2521 | |
| 2522 | kvm_post_set_cr4(vcpu, old_value, new_value); |
| 2523 | break; |
Tom Lendacky | d1949b9 | 2020-12-10 11:09:58 -0600 | [diff] [blame] | 2524 | case 8: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2525 | ret = kvm_set_cr8(vcpu, new_value); |
Tom Lendacky | d1949b9 | 2020-12-10 11:09:58 -0600 | [diff] [blame] | 2526 | break; |
Tom Lendacky | f27ad38 | 2020-12-10 11:09:56 -0600 | [diff] [blame] | 2527 | default: |
| 2528 | WARN(1, "unhandled CR%d write trap", cr); |
| 2529 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 2530 | return 1; |
| 2531 | } |
| 2532 | |
Tom Lendacky | d1949b9 | 2020-12-10 11:09:58 -0600 | [diff] [blame] | 2533 | return kvm_complete_insn_gp(vcpu, ret); |
Tom Lendacky | f27ad38 | 2020-12-10 11:09:56 -0600 | [diff] [blame] | 2534 | } |
| 2535 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2536 | static int dr_interception(struct kvm_vcpu *vcpu) |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 2537 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2538 | struct vcpu_svm *svm = to_svm(vcpu); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 2539 | int reg, dr; |
| 2540 | unsigned long val; |
Paolo Bonzini | 996ff54 | 2020-12-14 07:49:54 -0500 | [diff] [blame] | 2541 | int err = 0; |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 2542 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2543 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 2544 | /* |
| 2545 | * No more DR vmexits; force a reload of the debug registers |
| 2546 | * and reenter on this instruction. The next vmexit will |
| 2547 | * retrieve the full state of the debug registers. |
| 2548 | */ |
| 2549 | clr_dr_intercepts(svm); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2550 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 2551 | return 1; |
| 2552 | } |
| 2553 | |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 2554 | if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2555 | return emulate_on_interception(vcpu); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 2556 | |
| 2557 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
| 2558 | dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; |
Paolo Bonzini | 996ff54 | 2020-12-14 07:49:54 -0500 | [diff] [blame] | 2559 | if (dr >= 16) { /* mov to DRn */ |
| 2560 | dr -= 16; |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 2561 | val = kvm_register_read(vcpu, reg); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2562 | err = kvm_set_dr(vcpu, dr, val); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 2563 | } else { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2564 | kvm_get_dr(vcpu, dr, &val); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 2565 | kvm_register_write(vcpu, reg, val); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 2566 | } |
| 2567 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2568 | return kvm_complete_insn_gp(vcpu, err); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 2569 | } |
| 2570 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2571 | static int cr8_write_interception(struct kvm_vcpu *vcpu) |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2572 | { |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 2573 | int r; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2574 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2575 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 2576 | /* instruction emulation calls kvm_set_cr8() */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2577 | r = cr_interception(vcpu); |
| 2578 | if (lapic_in_kernel(vcpu)) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2579 | return r; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2580 | if (cr8_prev <= kvm_get_cr8(vcpu)) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 2581 | return r; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2582 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2583 | return 0; |
| 2584 | } |
| 2585 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2586 | static int efer_trap(struct kvm_vcpu *vcpu) |
Tom Lendacky | 2985afb | 2020-12-10 11:09:55 -0600 | [diff] [blame] | 2587 | { |
| 2588 | struct msr_data msr_info; |
| 2589 | int ret; |
| 2590 | |
| 2591 | /* |
| 2592 | * Clear the EFER_SVME bit from EFER. The SVM code always sets this |
| 2593 | * bit in svm_set_efer(), but __kvm_valid_efer() checks it against |
| 2594 | * whether the guest has X86_FEATURE_SVM - this avoids a failure if |
| 2595 | * the guest doesn't have X86_FEATURE_SVM. |
| 2596 | */ |
| 2597 | msr_info.host_initiated = false; |
| 2598 | msr_info.index = MSR_EFER; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2599 | msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME; |
| 2600 | ret = kvm_set_msr_common(vcpu, &msr_info); |
Tom Lendacky | 2985afb | 2020-12-10 11:09:55 -0600 | [diff] [blame] | 2601 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2602 | return kvm_complete_insn_gp(vcpu, ret); |
Tom Lendacky | 2985afb | 2020-12-10 11:09:55 -0600 | [diff] [blame] | 2603 | } |
| 2604 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 2605 | static int svm_get_msr_feature(struct kvm_msr_entry *msr) |
| 2606 | { |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 2607 | msr->data = 0; |
| 2608 | |
| 2609 | switch (msr->index) { |
| 2610 | case MSR_F10H_DECFG: |
| 2611 | if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) |
| 2612 | msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE; |
| 2613 | break; |
Vitaly Kuznetsov | d574c53 | 2020-07-10 17:25:59 +0200 | [diff] [blame] | 2614 | case MSR_IA32_PERF_CAPABILITIES: |
| 2615 | return 0; |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 2616 | default: |
Peter Xu | 12bc213 | 2020-06-22 18:04:42 -0400 | [diff] [blame] | 2617 | return KVM_MSR_RET_INVALID; |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 2621 | } |
| 2622 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2623 | static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2624 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2625 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2626 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2627 | switch (msr_info->index) { |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2628 | case MSR_STAR: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2629 | msr_info->data = svm->vmcb01.ptr->save.star; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2630 | break; |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 2631 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2632 | case MSR_LSTAR: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2633 | msr_info->data = svm->vmcb01.ptr->save.lstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2634 | break; |
| 2635 | case MSR_CSTAR: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2636 | msr_info->data = svm->vmcb01.ptr->save.cstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2637 | break; |
| 2638 | case MSR_KERNEL_GS_BASE: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2639 | msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2640 | break; |
| 2641 | case MSR_SYSCALL_MASK: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2642 | msr_info->data = svm->vmcb01.ptr->save.sfmask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2643 | break; |
| 2644 | #endif |
| 2645 | case MSR_IA32_SYSENTER_CS: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2646 | msr_info->data = svm->vmcb01.ptr->save.sysenter_cs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2647 | break; |
| 2648 | case MSR_IA32_SYSENTER_EIP: |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 2649 | msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip; |
| 2650 | if (guest_cpuid_is_intel(vcpu)) |
| 2651 | msr_info->data |= (u64)svm->sysenter_eip_hi << 32; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2652 | break; |
| 2653 | case MSR_IA32_SYSENTER_ESP: |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 2654 | msr_info->data = svm->vmcb01.ptr->save.sysenter_esp; |
| 2655 | if (guest_cpuid_is_intel(vcpu)) |
| 2656 | msr_info->data |= (u64)svm->sysenter_esp_hi << 32; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2657 | break; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2658 | case MSR_TSC_AUX: |
| 2659 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 2660 | return 1; |
Sean Christopherson | 6f2b296 | 2021-04-23 15:34:01 -0700 | [diff] [blame] | 2661 | if (!msr_info->host_initiated && |
| 2662 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
| 2663 | return 1; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2664 | msr_info->data = svm->tsc_aux; |
| 2665 | break; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2666 | /* |
| 2667 | * Nobody will change the following 5 values in the VMCB so we can |
| 2668 | * safely return them on rdmsr. They will always be 0 until LBRV is |
| 2669 | * implemented. |
| 2670 | */ |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2671 | case MSR_IA32_DEBUGCTLMSR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2672 | msr_info->data = svm->vmcb->save.dbgctl; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2673 | break; |
| 2674 | case MSR_IA32_LASTBRANCHFROMIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2675 | msr_info->data = svm->vmcb->save.br_from; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2676 | break; |
| 2677 | case MSR_IA32_LASTBRANCHTOIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2678 | msr_info->data = svm->vmcb->save.br_to; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2679 | break; |
| 2680 | case MSR_IA32_LASTINTFROMIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2681 | msr_info->data = svm->vmcb->save.last_excp_from; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2682 | break; |
| 2683 | case MSR_IA32_LASTINTTOIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2684 | msr_info->data = svm->vmcb->save.last_excp_to; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2685 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2686 | case MSR_VM_HSAVE_PA: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2687 | msr_info->data = svm->nested.hsave_msr; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2688 | break; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 2689 | case MSR_VM_CR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2690 | msr_info->data = svm->nested.vm_cr_msr; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 2691 | break; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2692 | case MSR_IA32_SPEC_CTRL: |
| 2693 | if (!msr_info->host_initiated && |
Paolo Bonzini | 39485ed | 2020-12-03 09:40:15 -0500 | [diff] [blame] | 2694 | !guest_has_spec_ctrl_msr(vcpu)) |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2695 | return 1; |
| 2696 | |
Babu Moger | d00b99c | 2021-02-17 10:56:04 -0500 | [diff] [blame] | 2697 | if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) |
| 2698 | msr_info->data = svm->vmcb->save.spec_ctrl; |
| 2699 | else |
| 2700 | msr_info->data = svm->spec_ctrl; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2701 | break; |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 2702 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 2703 | if (!msr_info->host_initiated && |
| 2704 | !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD)) |
| 2705 | return 1; |
| 2706 | |
| 2707 | msr_info->data = svm->virt_spec_ctrl; |
| 2708 | break; |
Borislav Petkov | ae8b787 | 2015-11-23 11:12:23 +0100 | [diff] [blame] | 2709 | case MSR_F15H_IC_CFG: { |
| 2710 | |
| 2711 | int family, model; |
| 2712 | |
| 2713 | family = guest_cpuid_family(vcpu); |
| 2714 | model = guest_cpuid_model(vcpu); |
| 2715 | |
| 2716 | if (family < 0 || model < 0) |
| 2717 | return kvm_get_msr_common(vcpu, msr_info); |
| 2718 | |
| 2719 | msr_info->data = 0; |
| 2720 | |
| 2721 | if (family == 0x15 && |
| 2722 | (model >= 0x2 && model < 0x20)) |
| 2723 | msr_info->data = 0x1E; |
| 2724 | } |
| 2725 | break; |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 2726 | case MSR_F10H_DECFG: |
| 2727 | msr_info->data = svm->msr_decfg; |
| 2728 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2729 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2730 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2731 | } |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 2735 | static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err) |
| 2736 | { |
| 2737 | struct vcpu_svm *svm = to_svm(vcpu); |
Tom Lendacky | a3ba26e | 2021-04-09 09:38:42 -0500 | [diff] [blame] | 2738 | if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->ghcb)) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2739 | return kvm_complete_insn_gp(vcpu, err); |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 2740 | |
| 2741 | ghcb_set_sw_exit_info_1(svm->ghcb, 1); |
| 2742 | ghcb_set_sw_exit_info_2(svm->ghcb, |
| 2743 | X86_TRAP_GP | |
| 2744 | SVM_EVTINJ_TYPE_EXEPT | |
| 2745 | SVM_EVTINJ_VALID); |
| 2746 | return 1; |
| 2747 | } |
| 2748 | |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 2749 | static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) |
| 2750 | { |
| 2751 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2752 | int svm_dis, chg_mask; |
| 2753 | |
| 2754 | if (data & ~SVM_VM_CR_VALID_MASK) |
| 2755 | return 1; |
| 2756 | |
| 2757 | chg_mask = SVM_VM_CR_VALID_MASK; |
| 2758 | |
| 2759 | if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) |
| 2760 | chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); |
| 2761 | |
| 2762 | svm->nested.vm_cr_msr &= ~chg_mask; |
| 2763 | svm->nested.vm_cr_msr |= (data & chg_mask); |
| 2764 | |
| 2765 | svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; |
| 2766 | |
| 2767 | /* check for svm_disable while efer.svme is set */ |
| 2768 | if (svm_dis && (vcpu->arch.efer & EFER_SVME)) |
| 2769 | return 1; |
| 2770 | |
| 2771 | return 0; |
| 2772 | } |
| 2773 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2774 | static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2775 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2776 | struct vcpu_svm *svm = to_svm(vcpu); |
Sean Christopherson | 844d69c | 2021-04-23 15:34:04 -0700 | [diff] [blame] | 2777 | int r; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2778 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2779 | u32 ecx = msr->index; |
| 2780 | u64 data = msr->data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2781 | switch (ecx) { |
Paolo Bonzini | 15038e1 | 2017-10-26 09:13:27 +0200 | [diff] [blame] | 2782 | case MSR_IA32_CR_PAT: |
| 2783 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 2784 | return 1; |
| 2785 | vcpu->arch.pat = data; |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 2786 | svm->vmcb01.ptr->save.g_pat = data; |
| 2787 | if (is_guest_mode(vcpu)) |
| 2788 | nested_vmcb02_compute_g_pat(svm); |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 2789 | vmcb_mark_dirty(svm->vmcb, VMCB_NPT); |
Paolo Bonzini | 15038e1 | 2017-10-26 09:13:27 +0200 | [diff] [blame] | 2790 | break; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2791 | case MSR_IA32_SPEC_CTRL: |
| 2792 | if (!msr->host_initiated && |
Paolo Bonzini | 39485ed | 2020-12-03 09:40:15 -0500 | [diff] [blame] | 2793 | !guest_has_spec_ctrl_msr(vcpu)) |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2794 | return 1; |
| 2795 | |
Maxim Levitsky | 841c2be | 2020-07-08 14:57:31 +0300 | [diff] [blame] | 2796 | if (kvm_spec_ctrl_test_value(data)) |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2797 | return 1; |
| 2798 | |
Babu Moger | d00b99c | 2021-02-17 10:56:04 -0500 | [diff] [blame] | 2799 | if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) |
| 2800 | svm->vmcb->save.spec_ctrl = data; |
| 2801 | else |
| 2802 | svm->spec_ctrl = data; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2803 | if (!data) |
| 2804 | break; |
| 2805 | |
| 2806 | /* |
| 2807 | * For non-nested: |
| 2808 | * When it's written (to non-zero) for the first time, pass |
| 2809 | * it through. |
| 2810 | * |
| 2811 | * For nested: |
| 2812 | * The handling of the MSR bitmap for L2 guests is done in |
| 2813 | * nested_svm_vmrun_msrpm. |
| 2814 | * We update the L1 MSR bit as well since it will end up |
| 2815 | * touching the MSR anyway now. |
| 2816 | */ |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 2817 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2818 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2819 | case MSR_IA32_PRED_CMD: |
| 2820 | if (!msr->host_initiated && |
Paolo Bonzini | 39485ed | 2020-12-03 09:40:15 -0500 | [diff] [blame] | 2821 | !guest_has_pred_cmd_msr(vcpu)) |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2822 | return 1; |
| 2823 | |
| 2824 | if (data & ~PRED_CMD_IBPB) |
| 2825 | return 1; |
Paolo Bonzini | 39485ed | 2020-12-03 09:40:15 -0500 | [diff] [blame] | 2826 | if (!boot_cpu_has(X86_FEATURE_IBPB)) |
Paolo Bonzini | 6441fa6 | 2020-01-20 16:33:06 +0100 | [diff] [blame] | 2827 | return 1; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2828 | if (!data) |
| 2829 | break; |
| 2830 | |
| 2831 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 2832 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2833 | break; |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 2834 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 2835 | if (!msr->host_initiated && |
| 2836 | !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD)) |
| 2837 | return 1; |
| 2838 | |
| 2839 | if (data & ~SPEC_CTRL_SSBD) |
| 2840 | return 1; |
| 2841 | |
| 2842 | svm->virt_spec_ctrl = data; |
| 2843 | break; |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2844 | case MSR_STAR: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2845 | svm->vmcb01.ptr->save.star = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2846 | break; |
Robert P. J. Day | 49b14f2 | 2007-01-29 13:19:50 -0800 | [diff] [blame] | 2847 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2848 | case MSR_LSTAR: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2849 | svm->vmcb01.ptr->save.lstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2850 | break; |
| 2851 | case MSR_CSTAR: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2852 | svm->vmcb01.ptr->save.cstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2853 | break; |
| 2854 | case MSR_KERNEL_GS_BASE: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2855 | svm->vmcb01.ptr->save.kernel_gs_base = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2856 | break; |
| 2857 | case MSR_SYSCALL_MASK: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2858 | svm->vmcb01.ptr->save.sfmask = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2859 | break; |
| 2860 | #endif |
| 2861 | case MSR_IA32_SYSENTER_CS: |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 2862 | svm->vmcb01.ptr->save.sysenter_cs = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2863 | break; |
| 2864 | case MSR_IA32_SYSENTER_EIP: |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 2865 | svm->vmcb01.ptr->save.sysenter_eip = (u32)data; |
| 2866 | /* |
| 2867 | * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs |
| 2868 | * when we spoof an Intel vendor ID (for cross vendor migration). |
| 2869 | * In this case we use this intercept to track the high |
| 2870 | * 32 bit part of these msrs to support Intel's |
| 2871 | * implementation of SYSENTER/SYSEXIT. |
| 2872 | */ |
| 2873 | svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2874 | break; |
| 2875 | case MSR_IA32_SYSENTER_ESP: |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 2876 | svm->vmcb01.ptr->save.sysenter_esp = (u32)data; |
| 2877 | svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2878 | break; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2879 | case MSR_TSC_AUX: |
| 2880 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 2881 | return 1; |
| 2882 | |
Sean Christopherson | 6f2b296 | 2021-04-23 15:34:01 -0700 | [diff] [blame] | 2883 | if (!msr->host_initiated && |
| 2884 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
| 2885 | return 1; |
| 2886 | |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2887 | /* |
Sean Christopherson | dbd6127 | 2021-04-23 15:34:02 -0700 | [diff] [blame] | 2888 | * Per Intel's SDM, bits 63:32 are reserved, but AMD's APM has |
| 2889 | * incomplete and conflicting architectural behavior. Current |
| 2890 | * AMD CPUs completely ignore bits 63:32, i.e. they aren't |
| 2891 | * reserved and always read as zeros. Emulate AMD CPU behavior |
| 2892 | * to avoid explosions if the vCPU is migrated from an AMD host |
| 2893 | * to an Intel host. |
| 2894 | */ |
| 2895 | data = (u32)data; |
| 2896 | |
| 2897 | /* |
Sean Christopherson | 844d69c | 2021-04-23 15:34:04 -0700 | [diff] [blame] | 2898 | * TSC_AUX is usually changed only during boot and never read |
| 2899 | * directly. Intercept TSC_AUX instead of exposing it to the |
| 2900 | * guest via direct_access_msrs, and switch it via user return. |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2901 | */ |
Sean Christopherson | 844d69c | 2021-04-23 15:34:04 -0700 | [diff] [blame] | 2902 | preempt_disable(); |
| 2903 | r = kvm_set_user_return_msr(TSC_AUX_URET_SLOT, data, -1ull); |
| 2904 | preempt_enable(); |
| 2905 | if (r) |
| 2906 | return 1; |
| 2907 | |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2908 | svm->tsc_aux = data; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2909 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2910 | case MSR_IA32_DEBUGCTLMSR: |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 2911 | if (!boot_cpu_has(X86_FEATURE_LBRV)) { |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 2912 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
| 2913 | __func__, data); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2914 | break; |
| 2915 | } |
| 2916 | if (data & DEBUGCTL_RESERVED_BITS) |
| 2917 | return 1; |
| 2918 | |
| 2919 | svm->vmcb->save.dbgctl = data; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 2920 | vmcb_mark_dirty(svm->vmcb, VMCB_LBR); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2921 | if (data & (1ULL<<0)) |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 2922 | svm_enable_lbrv(vcpu); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2923 | else |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 2924 | svm_disable_lbrv(vcpu); |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2925 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2926 | case MSR_VM_HSAVE_PA: |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2927 | svm->nested.hsave_msr = data; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2928 | break; |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 2929 | case MSR_VM_CR: |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 2930 | return svm_set_vm_cr(vcpu, data); |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 2931 | case MSR_VM_IGNNE: |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 2932 | vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 2933 | break; |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 2934 | case MSR_F10H_DECFG: { |
| 2935 | struct kvm_msr_entry msr_entry; |
| 2936 | |
| 2937 | msr_entry.index = msr->index; |
| 2938 | if (svm_get_msr_feature(&msr_entry)) |
| 2939 | return 1; |
| 2940 | |
| 2941 | /* Check the supported bits */ |
| 2942 | if (data & ~msr_entry.data) |
| 2943 | return 1; |
| 2944 | |
| 2945 | /* Don't allow the guest to change a bit, #GP */ |
| 2946 | if (!msr->host_initiated && (data ^ msr_entry.data)) |
| 2947 | return 1; |
| 2948 | |
| 2949 | svm->msr_decfg = data; |
| 2950 | break; |
| 2951 | } |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 2952 | case MSR_IA32_APICBASE: |
| 2953 | if (kvm_vcpu_apicv_active(vcpu)) |
| 2954 | avic_update_vapic_bar(to_svm(vcpu), data); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2955 | fallthrough; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2956 | default: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2957 | return kvm_set_msr_common(vcpu, msr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2958 | } |
| 2959 | return 0; |
| 2960 | } |
| 2961 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2962 | static int msr_interception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2963 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2964 | if (to_svm(vcpu)->vmcb->control.exit_info_1) |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 2965 | return kvm_emulate_wrmsr(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2966 | else |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 2967 | return kvm_emulate_rdmsr(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2968 | } |
| 2969 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2970 | static int interrupt_window_interception(struct kvm_vcpu *vcpu) |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2971 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2972 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 2973 | svm_clear_vintr(to_svm(vcpu)); |
Suravee Suthikulpanit | f3515dc | 2019-11-14 14:15:15 -0600 | [diff] [blame] | 2974 | |
| 2975 | /* |
| 2976 | * For AVIC, the only reason to end up here is ExtINTs. |
| 2977 | * In this case AVIC was temporarily disabled for |
| 2978 | * requesting the IRQ window and we have to re-enable it. |
| 2979 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2980 | svm_toggle_avic_for_irq_window(vcpu, true); |
Suravee Suthikulpanit | f3515dc | 2019-11-14 14:15:15 -0600 | [diff] [blame] | 2981 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2982 | ++vcpu->stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2983 | return 1; |
| 2984 | } |
| 2985 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2986 | static int pause_interception(struct kvm_vcpu *vcpu) |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 2987 | { |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 2988 | bool in_kernel; |
| 2989 | |
| 2990 | /* |
| 2991 | * CPL is not made available for an SEV-ES guest, therefore |
| 2992 | * vcpu->arch.preempted_in_kernel can never be true. Just |
| 2993 | * set in_kernel to false as well. |
| 2994 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 2995 | in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0; |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 2996 | |
Wanpeng Li | 830f01b | 2020-07-31 11:12:21 +0800 | [diff] [blame] | 2997 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 2998 | grow_ple_window(vcpu); |
| 2999 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 3000 | kvm_vcpu_on_spin(vcpu, in_kernel); |
Sean Christopherson | c8781fe | 2021-02-04 16:57:50 -0800 | [diff] [blame] | 3001 | return kvm_skip_emulated_instruction(vcpu); |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 3002 | } |
| 3003 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3004 | static int invpcid_interception(struct kvm_vcpu *vcpu) |
Babu Moger | 4407a79 | 2020-09-11 14:29:19 -0500 | [diff] [blame] | 3005 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3006 | struct vcpu_svm *svm = to_svm(vcpu); |
Babu Moger | 4407a79 | 2020-09-11 14:29:19 -0500 | [diff] [blame] | 3007 | unsigned long type; |
| 3008 | gva_t gva; |
| 3009 | |
| 3010 | if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { |
| 3011 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3012 | return 1; |
| 3013 | } |
| 3014 | |
| 3015 | /* |
| 3016 | * For an INVPCID intercept: |
| 3017 | * EXITINFO1 provides the linear address of the memory operand. |
| 3018 | * EXITINFO2 provides the contents of the register operand. |
| 3019 | */ |
| 3020 | type = svm->vmcb->control.exit_info_2; |
| 3021 | gva = svm->vmcb->control.exit_info_1; |
| 3022 | |
| 3023 | if (type > 3) { |
| 3024 | kvm_inject_gp(vcpu, 0); |
| 3025 | return 1; |
| 3026 | } |
| 3027 | |
| 3028 | return kvm_handle_invpcid(vcpu, type, gva); |
| 3029 | } |
| 3030 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3031 | static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3032 | [SVM_EXIT_READ_CR0] = cr_interception, |
| 3033 | [SVM_EXIT_READ_CR3] = cr_interception, |
| 3034 | [SVM_EXIT_READ_CR4] = cr_interception, |
| 3035 | [SVM_EXIT_READ_CR8] = cr_interception, |
David Kaplan | 5e57518 | 2015-03-06 14:44:35 -0600 | [diff] [blame] | 3036 | [SVM_EXIT_CR0_SEL_WRITE] = cr_interception, |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 3037 | [SVM_EXIT_WRITE_CR0] = cr_interception, |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3038 | [SVM_EXIT_WRITE_CR3] = cr_interception, |
| 3039 | [SVM_EXIT_WRITE_CR4] = cr_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3040 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3041 | [SVM_EXIT_READ_DR0] = dr_interception, |
| 3042 | [SVM_EXIT_READ_DR1] = dr_interception, |
| 3043 | [SVM_EXIT_READ_DR2] = dr_interception, |
| 3044 | [SVM_EXIT_READ_DR3] = dr_interception, |
| 3045 | [SVM_EXIT_READ_DR4] = dr_interception, |
| 3046 | [SVM_EXIT_READ_DR5] = dr_interception, |
| 3047 | [SVM_EXIT_READ_DR6] = dr_interception, |
| 3048 | [SVM_EXIT_READ_DR7] = dr_interception, |
| 3049 | [SVM_EXIT_WRITE_DR0] = dr_interception, |
| 3050 | [SVM_EXIT_WRITE_DR1] = dr_interception, |
| 3051 | [SVM_EXIT_WRITE_DR2] = dr_interception, |
| 3052 | [SVM_EXIT_WRITE_DR3] = dr_interception, |
| 3053 | [SVM_EXIT_WRITE_DR4] = dr_interception, |
| 3054 | [SVM_EXIT_WRITE_DR5] = dr_interception, |
| 3055 | [SVM_EXIT_WRITE_DR6] = dr_interception, |
| 3056 | [SVM_EXIT_WRITE_DR7] = dr_interception, |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3057 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
| 3058 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3059 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3060 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3061 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 3062 | [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception, |
Liran Alon | 9718420 | 2018-03-12 13:12:52 +0200 | [diff] [blame] | 3063 | [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3064 | [SVM_EXIT_INTR] = intr_interception, |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 3065 | [SVM_EXIT_NMI] = nmi_interception, |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 3066 | [SVM_EXIT_SMI] = kvm_emulate_as_nop, |
| 3067 | [SVM_EXIT_INIT] = kvm_emulate_as_nop, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 3068 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
Sean Christopherson | 32c23c7 | 2021-02-04 16:57:49 -0800 | [diff] [blame] | 3069 | [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc, |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 3070 | [SVM_EXIT_CPUID] = kvm_emulate_cpuid, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3071 | [SVM_EXIT_IRET] = iret_interception, |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 3072 | [SVM_EXIT_INVD] = kvm_emulate_invd, |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 3073 | [SVM_EXIT_PAUSE] = pause_interception, |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 3074 | [SVM_EXIT_HLT] = kvm_emulate_halt, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3075 | [SVM_EXIT_INVLPG] = invlpg_interception, |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3076 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3077 | [SVM_EXIT_IOIO] = io_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3078 | [SVM_EXIT_MSR] = msr_interception, |
| 3079 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 3080 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3081 | [SVM_EXIT_VMRUN] = vmrun_interception, |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 3082 | [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall, |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3083 | [SVM_EXIT_VMLOAD] = vmload_interception, |
| 3084 | [SVM_EXIT_VMSAVE] = vmsave_interception, |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3085 | [SVM_EXIT_STGI] = stgi_interception, |
| 3086 | [SVM_EXIT_CLGI] = clgi_interception, |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 3087 | [SVM_EXIT_SKINIT] = skinit_interception, |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 3088 | [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd, |
| 3089 | [SVM_EXIT_MONITOR] = kvm_emulate_monitor, |
| 3090 | [SVM_EXIT_MWAIT] = kvm_emulate_mwait, |
Sean Christopherson | 92f9895 | 2021-02-04 16:57:46 -0800 | [diff] [blame] | 3091 | [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv, |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 3092 | [SVM_EXIT_RDPRU] = kvm_handle_invalid_op, |
Tom Lendacky | 2985afb | 2020-12-10 11:09:55 -0600 | [diff] [blame] | 3093 | [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap, |
Tom Lendacky | f27ad38 | 2020-12-10 11:09:56 -0600 | [diff] [blame] | 3094 | [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap, |
Tom Lendacky | 5b51cb1 | 2020-12-10 11:09:57 -0600 | [diff] [blame] | 3095 | [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap, |
Tom Lendacky | d1949b9 | 2020-12-10 11:09:58 -0600 | [diff] [blame] | 3096 | [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap, |
Babu Moger | 4407a79 | 2020-09-11 14:29:19 -0500 | [diff] [blame] | 3097 | [SVM_EXIT_INVPCID] = invpcid_interception, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 3098 | [SVM_EXIT_NPF] = npf_interception, |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 3099 | [SVM_EXIT_RSM] = rsm_interception, |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 3100 | [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, |
| 3101 | [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 3102 | [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3103 | }; |
| 3104 | |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3105 | static void dump_vmcb(struct kvm_vcpu *vcpu) |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 3106 | { |
| 3107 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3108 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 3109 | struct vmcb_save_area *save = &svm->vmcb->save; |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3110 | struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save; |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 3111 | |
Paolo Bonzini | 6f2f845 | 2019-05-20 15:34:35 +0200 | [diff] [blame] | 3112 | if (!dump_invalid_vmcb) { |
| 3113 | pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n"); |
| 3114 | return; |
| 3115 | } |
| 3116 | |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 3117 | pr_err("VMCB Control Area:\n"); |
Babu Moger | 03bfeeb | 2020-09-11 14:28:05 -0500 | [diff] [blame] | 3118 | pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff); |
| 3119 | pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16); |
Babu Moger | 30abaa88 | 2020-09-11 14:28:12 -0500 | [diff] [blame] | 3120 | pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff); |
| 3121 | pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16); |
Babu Moger | 9780d51 | 2020-09-11 14:28:20 -0500 | [diff] [blame] | 3122 | pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]); |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 3123 | pr_err("%-20s%08x %08x\n", "intercepts:", |
| 3124 | control->intercepts[INTERCEPT_WORD3], |
| 3125 | control->intercepts[INTERCEPT_WORD4]); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3126 | pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); |
Babu Moger | 1d8fb44 | 2018-03-16 16:37:25 -0400 | [diff] [blame] | 3127 | pr_err("%-20s%d\n", "pause filter threshold:", |
| 3128 | control->pause_filter_thresh); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3129 | pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); |
| 3130 | pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); |
| 3131 | pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); |
| 3132 | pr_err("%-20s%d\n", "asid:", control->asid); |
| 3133 | pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); |
| 3134 | pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); |
| 3135 | pr_err("%-20s%08x\n", "int_vector:", control->int_vector); |
| 3136 | pr_err("%-20s%08x\n", "int_state:", control->int_state); |
| 3137 | pr_err("%-20s%08x\n", "exit_code:", control->exit_code); |
| 3138 | pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); |
| 3139 | pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); |
| 3140 | pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); |
| 3141 | pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); |
| 3142 | pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); |
| 3143 | pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 3144 | pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); |
Tom Lendacky | 291bd20 | 2020-12-10 11:09:47 -0600 | [diff] [blame] | 3145 | pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3146 | pr_err("%-20s%08x\n", "event_inj:", control->event_inj); |
| 3147 | pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 3148 | pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3149 | pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 3150 | pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page); |
| 3151 | pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id); |
| 3152 | pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id); |
Tom Lendacky | 376c6d2 | 2020-12-10 11:10:06 -0600 | [diff] [blame] | 3153 | pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 3154 | pr_err("VMCB State Save Area:\n"); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3155 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3156 | "es:", |
| 3157 | save->es.selector, save->es.attrib, |
| 3158 | save->es.limit, save->es.base); |
| 3159 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3160 | "cs:", |
| 3161 | save->cs.selector, save->cs.attrib, |
| 3162 | save->cs.limit, save->cs.base); |
| 3163 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3164 | "ss:", |
| 3165 | save->ss.selector, save->ss.attrib, |
| 3166 | save->ss.limit, save->ss.base); |
| 3167 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3168 | "ds:", |
| 3169 | save->ds.selector, save->ds.attrib, |
| 3170 | save->ds.limit, save->ds.base); |
| 3171 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3172 | "fs:", |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3173 | save01->fs.selector, save01->fs.attrib, |
| 3174 | save01->fs.limit, save01->fs.base); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3175 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3176 | "gs:", |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3177 | save01->gs.selector, save01->gs.attrib, |
| 3178 | save01->gs.limit, save01->gs.base); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3179 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3180 | "gdtr:", |
| 3181 | save->gdtr.selector, save->gdtr.attrib, |
| 3182 | save->gdtr.limit, save->gdtr.base); |
| 3183 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3184 | "ldtr:", |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3185 | save01->ldtr.selector, save01->ldtr.attrib, |
| 3186 | save01->ldtr.limit, save01->ldtr.base); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3187 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3188 | "idtr:", |
| 3189 | save->idtr.selector, save->idtr.attrib, |
| 3190 | save->idtr.limit, save->idtr.base); |
| 3191 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 3192 | "tr:", |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3193 | save01->tr.selector, save01->tr.attrib, |
| 3194 | save01->tr.limit, save01->tr.base); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 3195 | pr_err("cpl: %d efer: %016llx\n", |
| 3196 | save->cpl, save->efer); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3197 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 3198 | "cr0:", save->cr0, "cr2:", save->cr2); |
| 3199 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 3200 | "cr3:", save->cr3, "cr4:", save->cr4); |
| 3201 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 3202 | "dr6:", save->dr6, "dr7:", save->dr7); |
| 3203 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 3204 | "rip:", save->rip, "rflags:", save->rflags); |
| 3205 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 3206 | "rsp:", save->rsp, "rax:", save->rax); |
| 3207 | pr_err("%-15s %016llx %-13s %016llx\n", |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3208 | "star:", save01->star, "lstar:", save01->lstar); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3209 | pr_err("%-15s %016llx %-13s %016llx\n", |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3210 | "cstar:", save01->cstar, "sfmask:", save01->sfmask); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3211 | pr_err("%-15s %016llx %-13s %016llx\n", |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3212 | "kernel_gs_base:", save01->kernel_gs_base, |
| 3213 | "sysenter_cs:", save01->sysenter_cs); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3214 | pr_err("%-15s %016llx %-13s %016llx\n", |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3215 | "sysenter_esp:", save01->sysenter_esp, |
| 3216 | "sysenter_eip:", save01->sysenter_eip); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 3217 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 3218 | "gpat:", save->g_pat, "dbgctl:", save->dbgctl); |
| 3219 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 3220 | "br_from:", save->br_from, "br_to:", save->br_to); |
| 3221 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 3222 | "excp_from:", save->last_excp_from, |
| 3223 | "excp_to:", save->last_excp_to); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 3224 | } |
| 3225 | |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3226 | static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code) |
| 3227 | { |
| 3228 | if (exit_code < ARRAY_SIZE(svm_exit_handlers) && |
| 3229 | svm_exit_handlers[exit_code]) |
| 3230 | return 0; |
| 3231 | |
| 3232 | vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code); |
| 3233 | dump_vmcb(vcpu); |
| 3234 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3235 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; |
| 3236 | vcpu->run->internal.ndata = 2; |
| 3237 | vcpu->run->internal.data[0] = exit_code; |
| 3238 | vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; |
| 3239 | |
| 3240 | return -EINVAL; |
| 3241 | } |
| 3242 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3243 | int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code) |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3244 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3245 | if (svm_handle_invalid_exit(vcpu, exit_code)) |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3246 | return 0; |
| 3247 | |
| 3248 | #ifdef CONFIG_RETPOLINE |
| 3249 | if (exit_code == SVM_EXIT_MSR) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3250 | return msr_interception(vcpu); |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3251 | else if (exit_code == SVM_EXIT_VINTR) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3252 | return interrupt_window_interception(vcpu); |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3253 | else if (exit_code == SVM_EXIT_INTR) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3254 | return intr_interception(vcpu); |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3255 | else if (exit_code == SVM_EXIT_HLT) |
Sean Christopherson | 5ff3a35 | 2021-02-04 16:57:47 -0800 | [diff] [blame] | 3256 | return kvm_emulate_halt(vcpu); |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3257 | else if (exit_code == SVM_EXIT_NPF) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3258 | return npf_interception(vcpu); |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3259 | #endif |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3260 | return svm_exit_handlers[exit_code](vcpu); |
Tom Lendacky | e9093fd4 | 2020-12-10 11:09:46 -0600 | [diff] [blame] | 3261 | } |
| 3262 | |
Sean Christopherson | 235ba74 | 2020-09-23 13:13:46 -0700 | [diff] [blame] | 3263 | static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2, |
| 3264 | u32 *intr_info, u32 *error_code) |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 3265 | { |
| 3266 | struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; |
| 3267 | |
| 3268 | *info1 = control->exit_info_1; |
| 3269 | *info2 = control->exit_info_2; |
Sean Christopherson | 235ba74 | 2020-09-23 13:13:46 -0700 | [diff] [blame] | 3270 | *intr_info = control->exit_int_info; |
| 3271 | if ((*intr_info & SVM_EXITINTINFO_VALID) && |
| 3272 | (*intr_info & SVM_EXITINTINFO_VALID_ERR)) |
| 3273 | *error_code = control->exit_int_info_err; |
| 3274 | else |
| 3275 | *error_code = 0; |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
Wanpeng Li | 404d5d7 | 2020-04-28 14:23:25 +0800 | [diff] [blame] | 3278 | static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3279 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3280 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3281 | struct kvm_run *kvm_run = vcpu->run; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3282 | u32 exit_code = svm->vmcb->control.exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3283 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 3284 | trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM); |
| 3285 | |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 3286 | /* SEV-ES guests must use the CR write traps to track CR registers. */ |
| 3287 | if (!sev_es_guest(vcpu->kvm)) { |
| 3288 | if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE)) |
| 3289 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 3290 | if (npt_enabled) |
| 3291 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
| 3292 | } |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 3293 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3294 | if (is_guest_mode(vcpu)) { |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3295 | int vmexit; |
| 3296 | |
Sean Christopherson | cc167bd | 2020-09-23 13:13:48 -0700 | [diff] [blame] | 3297 | trace_kvm_nested_vmexit(exit_code, vcpu, KVM_ISA_SVM); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 3298 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3299 | vmexit = nested_svm_exit_special(svm); |
| 3300 | |
| 3301 | if (vmexit == NESTED_EXIT_CONTINUE) |
| 3302 | vmexit = nested_svm_exit_handled(svm); |
| 3303 | |
| 3304 | if (vmexit == NESTED_EXIT_DONE) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3305 | return 1; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3306 | } |
| 3307 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3308 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 3309 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 3310 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 3311 | = svm->vmcb->control.exit_code; |
Jim Mattson | 8a14fe4 | 2020-06-03 16:56:22 -0700 | [diff] [blame] | 3312 | kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 3313 | dump_vmcb(vcpu); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3314 | return 0; |
| 3315 | } |
| 3316 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3317 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 3318 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
Joerg Roedel | 55c5e46 | 2010-09-10 17:31:04 +0200 | [diff] [blame] | 3319 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH && |
| 3320 | exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI) |
Borislav Petkov | 6614c7d | 2013-04-26 00:22:01 +0200 | [diff] [blame] | 3321 | printk(KERN_ERR "%s: unexpected exit_int_info 0x%x " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3322 | "exit_code 0x%x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 3323 | __func__, svm->vmcb->control.exit_int_info, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3324 | exit_code); |
| 3325 | |
Wanpeng Li | 404d5d7 | 2020-04-28 14:23:25 +0800 | [diff] [blame] | 3326 | if (exit_fastpath != EXIT_FASTPATH_NONE) |
Wanpeng Li | 1e9e262 | 2019-11-21 11:17:11 +0800 | [diff] [blame] | 3327 | return 1; |
Wanpeng Li | 404d5d7 | 2020-04-28 14:23:25 +0800 | [diff] [blame] | 3328 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3329 | return svm_invoke_exit_handler(vcpu, exit_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 3333 | { |
Jim Mattson | 73cd6e5 | 2020-06-03 16:56:18 -0700 | [diff] [blame] | 3334 | struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3335 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 3336 | sd->tss_desc->type = 9; /* available 32/64-bit TSS */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3337 | load_TR_desc(); |
| 3338 | } |
| 3339 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3340 | static void pre_svm_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3341 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3342 | struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu); |
| 3343 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3344 | |
Cathy Avery | af18fa7 | 2021-01-12 11:43:12 -0500 | [diff] [blame] | 3345 | /* |
Sean Christopherson | 44f1b55 | 2021-04-06 10:18:11 -0700 | [diff] [blame] | 3346 | * If the previous vmrun of the vmcb occurred on a different physical |
| 3347 | * cpu, then mark the vmcb dirty and assign a new asid. Hardware's |
| 3348 | * vmcb clean bits are per logical CPU, as are KVM's asid assignments. |
| 3349 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3350 | if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) { |
Cathy Avery | 193015a | 2021-01-12 11:43:13 -0500 | [diff] [blame] | 3351 | svm->current_vmcb->asid_generation = 0; |
Cathy Avery | af18fa7 | 2021-01-12 11:43:12 -0500 | [diff] [blame] | 3352 | vmcb_mark_all_dirty(svm->vmcb); |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3353 | svm->current_vmcb->cpu = vcpu->cpu; |
Cathy Avery | af18fa7 | 2021-01-12 11:43:12 -0500 | [diff] [blame] | 3354 | } |
| 3355 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3356 | if (sev_guest(vcpu->kvm)) |
| 3357 | return pre_sev_run(svm, vcpu->cpu); |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 3358 | |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 3359 | /* FIXME: handle wraparound of asid_generation */ |
Cathy Avery | 193015a | 2021-01-12 11:43:13 -0500 | [diff] [blame] | 3360 | if (svm->current_vmcb->asid_generation != sd->asid_generation) |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 3361 | new_asid(svm, sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3362 | } |
| 3363 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3364 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
| 3365 | { |
| 3366 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3367 | |
| 3368 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 3369 | vcpu->arch.hflags |= HF_NMI_MASK; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3370 | if (!sev_es_guest(vcpu->kvm)) |
Tom Lendacky | 4444dfe | 2020-12-14 11:16:03 -0500 | [diff] [blame] | 3371 | svm_set_intercept(svm, INTERCEPT_IRET); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3372 | ++vcpu->stat.nmi_injections; |
| 3373 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3374 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3375 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 3376 | { |
| 3377 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3378 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3379 | BUG_ON(!(gif_set(svm))); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3380 | |
Gleb Natapov | 9fb2d2b | 2010-05-23 14:28:26 +0300 | [diff] [blame] | 3381 | trace_kvm_inj_virq(vcpu->arch.interrupt.nr); |
| 3382 | ++vcpu->stat.irq_injections; |
| 3383 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3384 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
| 3385 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 3386 | } |
| 3387 | |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 3388 | static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3389 | { |
| 3390 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3391 | |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 3392 | /* |
| 3393 | * SEV-ES guests must always keep the CR intercepts cleared. CR |
| 3394 | * tracking is done using the CR write traps. |
| 3395 | */ |
| 3396 | if (sev_es_guest(vcpu->kvm)) |
| 3397 | return; |
| 3398 | |
Joerg Roedel | 01c3b2b | 2020-06-25 10:03:25 +0200 | [diff] [blame] | 3399 | if (nested_svm_virtualize_tpr(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3400 | return; |
| 3401 | |
Babu Moger | 830bd71 | 2020-09-11 14:28:50 -0500 | [diff] [blame] | 3402 | svm_clr_intercept(svm, INTERCEPT_CR8_WRITE); |
Radim Krčmář | 596f314 | 2014-03-11 19:11:18 +0100 | [diff] [blame] | 3403 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3404 | if (irr == -1) |
| 3405 | return; |
| 3406 | |
| 3407 | if (tpr >= irr) |
Babu Moger | 830bd71 | 2020-09-11 14:28:50 -0500 | [diff] [blame] | 3408 | svm_set_intercept(svm, INTERCEPT_CR8_WRITE); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3409 | } |
| 3410 | |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3411 | bool svm_nmi_blocked(struct kvm_vcpu *vcpu) |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 3412 | { |
| 3413 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3414 | struct vmcb *vmcb = svm->vmcb; |
Sean Christopherson | 88c604b | 2020-04-22 19:25:41 -0700 | [diff] [blame] | 3415 | bool ret; |
Cathy Avery | 9c3d370 | 2020-04-14 16:11:06 -0400 | [diff] [blame] | 3416 | |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3417 | if (!gif_set(svm)) |
Paolo Bonzini | bbdad0b | 2020-04-23 08:06:43 -0400 | [diff] [blame] | 3418 | return true; |
| 3419 | |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3420 | if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm)) |
| 3421 | return false; |
| 3422 | |
| 3423 | ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) || |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3424 | (vcpu->arch.hflags & HF_NMI_MASK); |
Joerg Roedel | 924584c | 2010-04-22 12:33:07 +0200 | [diff] [blame] | 3425 | |
| 3426 | return ret; |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 3427 | } |
| 3428 | |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 3429 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection) |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3430 | { |
| 3431 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3432 | if (svm->nested.nested_run_pending) |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 3433 | return -EBUSY; |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3434 | |
Paolo Bonzini | c300ab9 | 2020-04-23 14:08:58 -0400 | [diff] [blame] | 3435 | /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */ |
| 3436 | if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm)) |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 3437 | return -EBUSY; |
Paolo Bonzini | c300ab9 | 2020-04-23 14:08:58 -0400 | [diff] [blame] | 3438 | |
| 3439 | return !svm_nmi_blocked(vcpu); |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3440 | } |
| 3441 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3442 | static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 3443 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3444 | return !!(vcpu->arch.hflags & HF_NMI_MASK); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 3448 | { |
| 3449 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3450 | |
| 3451 | if (masked) { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3452 | vcpu->arch.hflags |= HF_NMI_MASK; |
| 3453 | if (!sev_es_guest(vcpu->kvm)) |
Tom Lendacky | 4444dfe | 2020-12-14 11:16:03 -0500 | [diff] [blame] | 3454 | svm_set_intercept(svm, INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3455 | } else { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3456 | vcpu->arch.hflags &= ~HF_NMI_MASK; |
| 3457 | if (!sev_es_guest(vcpu->kvm)) |
Tom Lendacky | 4444dfe | 2020-12-14 11:16:03 -0500 | [diff] [blame] | 3458 | svm_clr_intercept(svm, INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3462 | bool svm_interrupt_blocked(struct kvm_vcpu *vcpu) |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 3463 | { |
| 3464 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3465 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 3466 | |
Paolo Bonzini | fc6f7c0 | 2020-04-23 18:02:45 -0400 | [diff] [blame] | 3467 | if (!gif_set(svm)) |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3468 | return true; |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 3469 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3470 | if (sev_es_guest(vcpu->kvm)) { |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 3471 | /* |
| 3472 | * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask |
| 3473 | * bit to determine the state of the IF flag. |
| 3474 | */ |
| 3475 | if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK)) |
| 3476 | return true; |
| 3477 | } else if (is_guest_mode(vcpu)) { |
Paolo Bonzini | fc6f7c0 | 2020-04-23 18:02:45 -0400 | [diff] [blame] | 3478 | /* As long as interrupts are being delivered... */ |
Paolo Bonzini | e9fd761 | 2020-05-13 13:28:23 -0400 | [diff] [blame] | 3479 | if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK) |
Cathy Avery | 4995a36 | 2021-01-13 07:07:52 -0500 | [diff] [blame] | 3480 | ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF) |
Paolo Bonzini | fc6f7c0 | 2020-04-23 18:02:45 -0400 | [diff] [blame] | 3481 | : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF)) |
| 3482 | return true; |
| 3483 | |
| 3484 | /* ... vmexits aren't blocked by the interrupt shadow */ |
| 3485 | if (nested_exit_on_intr(svm)) |
| 3486 | return false; |
| 3487 | } else { |
| 3488 | if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF)) |
| 3489 | return true; |
| 3490 | } |
| 3491 | |
| 3492 | return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK); |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3493 | } |
| 3494 | |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 3495 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection) |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3496 | { |
| 3497 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3498 | if (svm->nested.nested_run_pending) |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 3499 | return -EBUSY; |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 3500 | |
Paolo Bonzini | c300ab9 | 2020-04-23 14:08:58 -0400 | [diff] [blame] | 3501 | /* |
| 3502 | * An IRQ must not be injected into L2 if it's supposed to VM-Exit, |
| 3503 | * e.g. if the IRQ arrived asynchronously after checking nested events. |
| 3504 | */ |
| 3505 | if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm)) |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 3506 | return -EBUSY; |
Paolo Bonzini | c300ab9 | 2020-04-23 14:08:58 -0400 | [diff] [blame] | 3507 | |
| 3508 | return !svm_interrupt_blocked(vcpu); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 3509 | } |
| 3510 | |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 3511 | static void svm_enable_irq_window(struct kvm_vcpu *vcpu) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3512 | { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3513 | struct vcpu_svm *svm = to_svm(vcpu); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3514 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3515 | /* |
| 3516 | * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes |
| 3517 | * 1, because that's a separate STGI/VMRUN intercept. The next time we |
| 3518 | * get that intercept, this function will be called again though and |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3519 | * we'll get the vintr intercept. However, if the vGIF feature is |
| 3520 | * enabled, the STGI interception will not occur. Enable the irq |
| 3521 | * window under the assumption that the hardware will set the GIF. |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3522 | */ |
Paolo Bonzini | b518ba9 | 2020-03-04 16:46:47 -0500 | [diff] [blame] | 3523 | if (vgif_enabled(svm) || gif_set(svm)) { |
Suravee Suthikulpanit | f3515dc | 2019-11-14 14:15:15 -0600 | [diff] [blame] | 3524 | /* |
| 3525 | * IRQ window is not needed when AVIC is enabled, |
| 3526 | * unless we have pending ExtINT since it cannot be injected |
| 3527 | * via AVIC. In such case, we need to temporarily disable AVIC, |
| 3528 | * and fallback to injecting IRQ via V_IRQ. |
| 3529 | */ |
| 3530 | svm_toggle_avic_for_irq_window(vcpu, false); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3531 | svm_set_vintr(svm); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3532 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3533 | } |
| 3534 | |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 3535 | static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3536 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3537 | struct vcpu_svm *svm = to_svm(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3538 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3539 | if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK) |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 3540 | return; /* IRET will cause a vm exit */ |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3541 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3542 | if (!gif_set(svm)) { |
| 3543 | if (vgif_enabled(svm)) |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 3544 | svm_set_intercept(svm, INTERCEPT_STGI); |
Ladi Prosek | 1a5e185 | 2017-06-21 09:07:01 +0200 | [diff] [blame] | 3545 | return; /* STGI will cause a vm exit */ |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3546 | } |
Ladi Prosek | 1a5e185 | 2017-06-21 09:07:01 +0200 | [diff] [blame] | 3547 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3548 | /* |
| 3549 | * Something prevents NMI from been injected. Single step over possible |
| 3550 | * problem (IRET or exception injection or interrupt shadow) |
| 3551 | */ |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 3552 | svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu); |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 3553 | svm->nmi_singlestep = true; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3554 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3555 | } |
| 3556 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 3557 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 3558 | { |
| 3559 | return 0; |
| 3560 | } |
| 3561 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 3562 | static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 3563 | { |
| 3564 | return 0; |
| 3565 | } |
| 3566 | |
Sean Christopherson | f55ac30 | 2020-03-20 14:28:12 -0700 | [diff] [blame] | 3567 | void svm_flush_tlb(struct kvm_vcpu *vcpu) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 3568 | { |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 3569 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3570 | |
Sean Christopherson | 4a41e43 | 2020-03-20 14:28:17 -0700 | [diff] [blame] | 3571 | /* |
| 3572 | * Flush only the current ASID even if the TLB flush was invoked via |
| 3573 | * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all |
| 3574 | * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and |
| 3575 | * unconditionally does a TLB flush on both nested VM-Enter and nested |
| 3576 | * VM-Exit (via kvm_mmu_reset_context()). |
| 3577 | */ |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 3578 | if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) |
| 3579 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
| 3580 | else |
Cathy Avery | 193015a | 2021-01-12 11:43:13 -0500 | [diff] [blame] | 3581 | svm->current_vmcb->asid_generation--; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 3582 | } |
| 3583 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 3584 | static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva) |
| 3585 | { |
| 3586 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3587 | |
| 3588 | invlpga(gva, svm->vmcb->control.asid); |
| 3589 | } |
| 3590 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 3591 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
| 3592 | { |
| 3593 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3594 | |
Joerg Roedel | 01c3b2b | 2020-06-25 10:03:25 +0200 | [diff] [blame] | 3595 | if (nested_svm_virtualize_tpr(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3596 | return; |
| 3597 | |
Babu Moger | 830bd71 | 2020-09-11 14:28:50 -0500 | [diff] [blame] | 3598 | if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) { |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 3599 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
Gleb Natapov | 615d519 | 2009-04-21 17:45:05 +0300 | [diff] [blame] | 3600 | kvm_set_cr8(vcpu, cr8); |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 3601 | } |
| 3602 | } |
| 3603 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 3604 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
| 3605 | { |
| 3606 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3607 | u64 cr8; |
| 3608 | |
Joerg Roedel | 01c3b2b | 2020-06-25 10:03:25 +0200 | [diff] [blame] | 3609 | if (nested_svm_virtualize_tpr(vcpu) || |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 3610 | kvm_vcpu_apicv_active(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3611 | return; |
| 3612 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 3613 | cr8 = kvm_get_cr8(vcpu); |
| 3614 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 3615 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; |
| 3616 | } |
| 3617 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3618 | static void svm_complete_interrupts(struct kvm_vcpu *vcpu) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3619 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3620 | struct vcpu_svm *svm = to_svm(vcpu); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3621 | u8 vector; |
| 3622 | int type; |
| 3623 | u32 exitintinfo = svm->vmcb->control.exit_int_info; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 3624 | unsigned int3_injected = svm->int3_injected; |
| 3625 | |
| 3626 | svm->int3_injected = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3627 | |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 3628 | /* |
| 3629 | * If we've made progress since setting HF_IRET_MASK, we've |
| 3630 | * executed an IRET and can allow NMI injection. |
| 3631 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3632 | if ((vcpu->arch.hflags & HF_IRET_MASK) && |
| 3633 | (sev_es_guest(vcpu->kvm) || |
| 3634 | kvm_rip_read(vcpu) != svm->nmi_iret_rip)) { |
| 3635 | vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
| 3636 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3637 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3638 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3639 | vcpu->arch.nmi_injected = false; |
| 3640 | kvm_clear_exception_queue(vcpu); |
| 3641 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3642 | |
| 3643 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) |
| 3644 | return; |
| 3645 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3646 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3647 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3648 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
| 3649 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; |
| 3650 | |
| 3651 | switch (type) { |
| 3652 | case SVM_EXITINTINFO_TYPE_NMI: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3653 | vcpu->arch.nmi_injected = true; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3654 | break; |
| 3655 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 3656 | /* |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 3657 | * Never re-inject a #VC exception. |
| 3658 | */ |
| 3659 | if (vector == X86_TRAP_VC) |
| 3660 | break; |
| 3661 | |
| 3662 | /* |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 3663 | * In case of software exceptions, do not reinject the vector, |
| 3664 | * but re-execute the instruction instead. Rewind RIP first |
| 3665 | * if we emulated INT3 before. |
| 3666 | */ |
| 3667 | if (kvm_exception_is_soft(vector)) { |
| 3668 | if (vector == BP_VECTOR && int3_injected && |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3669 | kvm_is_linear_rip(vcpu, svm->int3_rip)) |
| 3670 | kvm_rip_write(vcpu, |
| 3671 | kvm_rip_read(vcpu) - int3_injected); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3672 | break; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 3673 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3674 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
| 3675 | u32 err = svm->vmcb->control.exit_int_info_err; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3676 | kvm_requeue_exception_e(vcpu, vector, err); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3677 | |
| 3678 | } else |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3679 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3680 | break; |
| 3681 | case SVM_EXITINTINFO_TYPE_INTR: |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3682 | kvm_queue_interrupt(vcpu, vector, false); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3683 | break; |
| 3684 | default: |
| 3685 | break; |
| 3686 | } |
| 3687 | } |
| 3688 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 3689 | static void svm_cancel_injection(struct kvm_vcpu *vcpu) |
| 3690 | { |
| 3691 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3692 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 3693 | |
| 3694 | control->exit_int_info = control->event_inj; |
| 3695 | control->exit_int_info_err = control->event_inj_err; |
| 3696 | control->event_inj = 0; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3697 | svm_complete_interrupts(vcpu); |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 3698 | } |
| 3699 | |
Wanpeng Li | 404d5d7 | 2020-04-28 14:23:25 +0800 | [diff] [blame] | 3700 | static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu) |
Wanpeng Li | a9ab13f | 2020-04-10 10:47:03 -0700 | [diff] [blame] | 3701 | { |
Wanpeng Li | 4e810ad | 2020-09-14 14:55:48 +0800 | [diff] [blame] | 3702 | if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR && |
Wanpeng Li | a9ab13f | 2020-04-10 10:47:03 -0700 | [diff] [blame] | 3703 | to_svm(vcpu)->vmcb->control.exit_info_1) |
| 3704 | return handle_fastpath_set_msr_irqoff(vcpu); |
| 3705 | |
| 3706 | return EXIT_FASTPATH_NONE; |
| 3707 | } |
| 3708 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3709 | static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu) |
Thomas Gleixner | 135961e | 2020-07-08 21:51:58 +0200 | [diff] [blame] | 3710 | { |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3711 | struct vcpu_svm *svm = to_svm(vcpu); |
Sean Christopherson | d178819 | 2021-04-06 10:18:09 -0700 | [diff] [blame] | 3712 | unsigned long vmcb_pa = svm->current_vmcb->pa; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3713 | |
Thomas Gleixner | 135961e | 2020-07-08 21:51:58 +0200 | [diff] [blame] | 3714 | /* |
| 3715 | * VMENTER enables interrupts (host state), but the kernel state is |
| 3716 | * interrupts disabled when this is invoked. Also tell RCU about |
| 3717 | * it. This is the same logic as for exit_to_user_mode(). |
| 3718 | * |
| 3719 | * This ensures that e.g. latency analysis on the host observes |
| 3720 | * guest mode as interrupt enabled. |
| 3721 | * |
| 3722 | * guest_enter_irqoff() informs context tracking about the |
| 3723 | * transition to guest mode and if enabled adjusts RCU state |
| 3724 | * accordingly. |
| 3725 | */ |
| 3726 | instrumentation_begin(); |
| 3727 | trace_hardirqs_on_prepare(); |
| 3728 | lockdep_hardirqs_on_prepare(CALLER_ADDR0); |
| 3729 | instrumentation_end(); |
| 3730 | |
| 3731 | guest_enter_irqoff(); |
| 3732 | lockdep_hardirqs_on(CALLER_ADDR0); |
| 3733 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3734 | if (sev_es_guest(vcpu->kvm)) { |
Sean Christopherson | d178819 | 2021-04-06 10:18:09 -0700 | [diff] [blame] | 3735 | __svm_sev_es_vcpu_run(vmcb_pa); |
Tom Lendacky | 16809ec | 2020-12-10 11:10:08 -0600 | [diff] [blame] | 3736 | } else { |
Michael Roth | e79b91b | 2021-02-02 13:01:24 -0600 | [diff] [blame] | 3737 | struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu); |
| 3738 | |
Sean Christopherson | d178819 | 2021-04-06 10:18:09 -0700 | [diff] [blame] | 3739 | /* |
| 3740 | * Use a single vmcb (vmcb01 because it's always valid) for |
| 3741 | * context switching guest state via VMLOAD/VMSAVE, that way |
| 3742 | * the state doesn't need to be copied between vmcb01 and |
| 3743 | * vmcb02 when switching vmcbs for nested virtualization. |
| 3744 | */ |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3745 | vmload(svm->vmcb01.pa); |
Sean Christopherson | d178819 | 2021-04-06 10:18:09 -0700 | [diff] [blame] | 3746 | __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs); |
Maxim Levitsky | cc3ed80 | 2021-02-10 18:54:36 +0200 | [diff] [blame] | 3747 | vmsave(svm->vmcb01.pa); |
Thomas Gleixner | 135961e | 2020-07-08 21:51:58 +0200 | [diff] [blame] | 3748 | |
Michael Roth | e79b91b | 2021-02-02 13:01:24 -0600 | [diff] [blame] | 3749 | vmload(__sme_page_pa(sd->save_area)); |
Tom Lendacky | 16809ec | 2020-12-10 11:10:08 -0600 | [diff] [blame] | 3750 | } |
Thomas Gleixner | 135961e | 2020-07-08 21:51:58 +0200 | [diff] [blame] | 3751 | |
| 3752 | /* |
| 3753 | * VMEXIT disables interrupts (host state), but tracing and lockdep |
| 3754 | * have them in state 'on' as recorded before entering guest mode. |
| 3755 | * Same as enter_from_user_mode(). |
| 3756 | * |
| 3757 | * guest_exit_irqoff() restores host context and reinstates RCU if |
| 3758 | * enabled and required. |
| 3759 | * |
| 3760 | * This needs to be done before the below as native_read_msr() |
| 3761 | * contains a tracepoint and x86_spec_ctrl_restore_host() calls |
| 3762 | * into world and some more. |
| 3763 | */ |
| 3764 | lockdep_hardirqs_off(CALLER_ADDR0); |
| 3765 | guest_exit_irqoff(); |
| 3766 | |
| 3767 | instrumentation_begin(); |
| 3768 | trace_hardirqs_off_finish(); |
| 3769 | instrumentation_end(); |
| 3770 | } |
| 3771 | |
Qian Cai | b95273f | 2020-04-15 11:37:09 -0400 | [diff] [blame] | 3772 | static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3773 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3774 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 3775 | |
Lorenzo Brescia | d95df95 | 2020-12-23 14:45:07 +0000 | [diff] [blame] | 3776 | trace_kvm_entry(vcpu); |
| 3777 | |
Joerg Roedel | 2041a06 | 2010-04-22 12:33:08 +0200 | [diff] [blame] | 3778 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 3779 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 3780 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 3781 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 3782 | /* |
Ladi Prosek | a12713c | 2017-06-21 09:07:00 +0200 | [diff] [blame] | 3783 | * Disable singlestep if we're injecting an interrupt/exception. |
| 3784 | * We don't want our modified rflags to be pushed on the stack where |
| 3785 | * we might not be able to easily reset them if we disabled NMI |
| 3786 | * singlestep later. |
| 3787 | */ |
| 3788 | if (svm->nmi_singlestep && svm->vmcb->control.event_inj) { |
| 3789 | /* |
| 3790 | * Event injection happens before external interrupts cause a |
| 3791 | * vmexit and interrupts are disabled here, so smp_send_reschedule |
| 3792 | * is enough to force an immediate vmexit. |
| 3793 | */ |
| 3794 | disable_nmi_singlestep(svm); |
| 3795 | smp_send_reschedule(vcpu->cpu); |
| 3796 | } |
| 3797 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3798 | pre_svm_run(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3799 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 3800 | sync_lapic_to_cr8(vcpu); |
| 3801 | |
Cathy Avery | 7e8e6ee | 2020-10-11 14:48:17 -0400 | [diff] [blame] | 3802 | if (unlikely(svm->asid != svm->vmcb->control.asid)) { |
| 3803 | svm->vmcb->control.asid = svm->asid; |
| 3804 | vmcb_mark_dirty(svm->vmcb, VMCB_ASID); |
| 3805 | } |
Joerg Roedel | cda0ffd | 2009-08-07 11:49:45 +0200 | [diff] [blame] | 3806 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3807 | |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 3808 | /* |
| 3809 | * Run with all-zero DR6 unless needed, so that we can get the exact cause |
| 3810 | * of a #DB. |
| 3811 | */ |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3812 | if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 3813 | svm_set_dr6(svm, vcpu->arch.dr6); |
| 3814 | else |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 3815 | svm_set_dr6(svm, DR6_ACTIVE_LOW); |
Paolo Bonzini | d67668e | 2020-05-06 06:40:04 -0400 | [diff] [blame] | 3816 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3817 | clgi(); |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 3818 | kvm_load_guest_xsave_state(vcpu); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3819 | |
Wanpeng Li | 010fd37 | 2020-09-10 17:50:41 +0800 | [diff] [blame] | 3820 | kvm_wait_lapic_expire(vcpu); |
Wanpeng Li | b6c4bc6 | 2019-05-20 16:18:09 +0800 | [diff] [blame] | 3821 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 3822 | /* |
| 3823 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 3824 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 3825 | * is no need to worry about the conditional branch over the wrmsr |
| 3826 | * being speculatively taken. |
| 3827 | */ |
Babu Moger | d00b99c | 2021-02-17 10:56:04 -0500 | [diff] [blame] | 3828 | if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL)) |
| 3829 | x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl); |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 3830 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3831 | svm_vcpu_enter_exit(vcpu); |
Thomas Gleixner | 15e6c22 | 2018-05-11 15:21:01 +0200 | [diff] [blame] | 3832 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 3833 | /* |
| 3834 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 3835 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 3836 | * turn it off. This is much more efficient than blindly adding |
| 3837 | * it to the atomic save/restore list. Especially as the former |
| 3838 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 3839 | * |
| 3840 | * For non-nested case: |
| 3841 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 3842 | * save it. |
| 3843 | * |
| 3844 | * For nested case: |
| 3845 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 3846 | * save it. |
| 3847 | */ |
Babu Moger | d00b99c | 2021-02-17 10:56:04 -0500 | [diff] [blame] | 3848 | if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) && |
| 3849 | unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 3850 | svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 3851 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3852 | if (!sev_es_guest(vcpu->kvm)) |
Tom Lendacky | 16809ec | 2020-12-10 11:10:08 -0600 | [diff] [blame] | 3853 | reload_tss(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3854 | |
Babu Moger | d00b99c | 2021-02-17 10:56:04 -0500 | [diff] [blame] | 3855 | if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL)) |
| 3856 | x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl); |
Thomas Gleixner | 024d83c | 2018-08-12 20:41:45 +0200 | [diff] [blame] | 3857 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3858 | if (!sev_es_guest(vcpu->kvm)) { |
Tom Lendacky | 16809ec | 2020-12-10 11:10:08 -0600 | [diff] [blame] | 3859 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
| 3860 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 3861 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 3862 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; |
| 3863 | } |
Avi Kivity | 13c34e0 | 2010-10-21 12:20:31 +0200 | [diff] [blame] | 3864 | |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 3865 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3866 | kvm_before_interrupt(vcpu); |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 3867 | |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 3868 | kvm_load_host_xsave_state(vcpu); |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 3869 | stgi(); |
| 3870 | |
| 3871 | /* Any pending NMI will happen here */ |
| 3872 | |
| 3873 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3874 | kvm_after_interrupt(vcpu); |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 3875 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 3876 | sync_cr8_to_lapic(vcpu); |
| 3877 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3878 | svm->next_rip = 0; |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3879 | if (is_guest_mode(vcpu)) { |
Paolo Bonzini | 9e8f0fb | 2020-11-17 05:15:41 -0500 | [diff] [blame] | 3880 | nested_sync_control_from_vmcb02(svm); |
Paolo Bonzini | 2d8a42b | 2020-05-22 03:50:14 -0400 | [diff] [blame] | 3881 | svm->nested.nested_run_pending = 0; |
| 3882 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 3883 | |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 3884 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
Wanpeng Li | e42c682 | 2020-09-12 02:16:39 -0400 | [diff] [blame] | 3885 | vmcb_mark_all_clean(svm->vmcb); |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 3886 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 3887 | /* if exit due to PF check for async PF */ |
| 3888 | if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3889 | vcpu->arch.apf.host_apf_flags = |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 3890 | kvm_read_and_reset_apf_flags(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 3891 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3892 | if (npt_enabled) { |
| 3893 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); |
| 3894 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); |
| 3895 | } |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 3896 | |
| 3897 | /* |
| 3898 | * We need to handle MC intercepts here before the vcpu has a chance to |
| 3899 | * change the physical cpu |
| 3900 | */ |
| 3901 | if (unlikely(svm->vmcb->control.exit_code == |
| 3902 | SVM_EXIT_EXCP_BASE + MC_VECTOR)) |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3903 | svm_handle_mce(vcpu); |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3904 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 3905 | svm_complete_interrupts(vcpu); |
Wanpeng Li | 4e810ad | 2020-09-14 14:55:48 +0800 | [diff] [blame] | 3906 | |
| 3907 | if (is_guest_mode(vcpu)) |
| 3908 | return EXIT_FASTPATH_NONE; |
| 3909 | |
| 3910 | return svm_exit_handlers_fastpath(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3911 | } |
| 3912 | |
Sean Christopherson | e83bc09 | 2021-03-05 10:31:13 -0800 | [diff] [blame] | 3913 | static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, |
Sean Christopherson | 2a40b90 | 2020-07-15 20:41:18 -0700 | [diff] [blame] | 3914 | int root_level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3915 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3916 | struct vcpu_svm *svm = to_svm(vcpu); |
Paolo Bonzini | 689f3bf | 2020-03-03 10:11:10 +0100 | [diff] [blame] | 3917 | unsigned long cr3; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3918 | |
Paolo Bonzini | 689f3bf | 2020-03-03 10:11:10 +0100 | [diff] [blame] | 3919 | if (npt_enabled) { |
Sean Christopherson | 4a98623 | 2021-03-09 14:42:07 -0800 | [diff] [blame] | 3920 | svm->vmcb->control.nested_cr3 = __sme_set(root_hpa); |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 3921 | vmcb_mark_dirty(svm->vmcb, VMCB_NPT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3922 | |
Paolo Bonzini | 689f3bf | 2020-03-03 10:11:10 +0100 | [diff] [blame] | 3923 | /* Loading L2's CR3 is handled by enter_svm_guest_mode. */ |
Paolo Bonzini | 978ce58 | 2020-05-20 08:37:37 -0400 | [diff] [blame] | 3924 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 3925 | return; |
| 3926 | cr3 = vcpu->arch.cr3; |
Sean Christopherson | e83bc09 | 2021-03-05 10:31:13 -0800 | [diff] [blame] | 3927 | } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) { |
Sean Christopherson | 4a98623 | 2021-03-09 14:42:07 -0800 | [diff] [blame] | 3928 | cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu); |
Sean Christopherson | e83bc09 | 2021-03-05 10:31:13 -0800 | [diff] [blame] | 3929 | } else { |
| 3930 | /* PCID in the guest should be impossible with a 32-bit MMU. */ |
| 3931 | WARN_ON_ONCE(kvm_get_active_pcid(vcpu)); |
| 3932 | cr3 = root_hpa; |
Paolo Bonzini | 689f3bf | 2020-03-03 10:11:10 +0100 | [diff] [blame] | 3933 | } |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 3934 | |
Paolo Bonzini | 978ce58 | 2020-05-20 08:37:37 -0400 | [diff] [blame] | 3935 | svm->vmcb->save.cr3 = cr3; |
Joerg Roedel | 06e7852 | 2020-06-25 10:03:23 +0200 | [diff] [blame] | 3936 | vmcb_mark_dirty(svm->vmcb, VMCB_CR); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 3937 | } |
| 3938 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3939 | static int is_disabled(void) |
| 3940 | { |
Joerg Roedel | 6031a61 | 2007-06-22 12:29:50 +0300 | [diff] [blame] | 3941 | u64 vm_cr; |
| 3942 | |
| 3943 | rdmsrl(MSR_VM_CR, vm_cr); |
| 3944 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 3945 | return 1; |
| 3946 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3947 | return 0; |
| 3948 | } |
| 3949 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3950 | static void |
| 3951 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 3952 | { |
| 3953 | /* |
| 3954 | * Patch in the VMMCALL instruction: |
| 3955 | */ |
| 3956 | hypercall[0] = 0x0f; |
| 3957 | hypercall[1] = 0x01; |
| 3958 | hypercall[2] = 0xd9; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3959 | } |
| 3960 | |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 3961 | static int __init svm_check_processor_compat(void) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3962 | { |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 3963 | return 0; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3964 | } |
| 3965 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 3966 | static bool svm_cpu_has_accelerated_tpr(void) |
| 3967 | { |
| 3968 | return false; |
| 3969 | } |
| 3970 | |
Tom Lendacky | 5719455 | 2020-12-10 11:10:00 -0600 | [diff] [blame] | 3971 | /* |
| 3972 | * The kvm parameter can be NULL (module initialization, or invocation before |
| 3973 | * VM creation). Be sure to check the kvm parameter before using it. |
| 3974 | */ |
| 3975 | static bool svm_has_emulated_msr(struct kvm *kvm, u32 index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 3976 | { |
Vitaly Kuznetsov | e87555e | 2018-12-19 12:06:13 +0100 | [diff] [blame] | 3977 | switch (index) { |
| 3978 | case MSR_IA32_MCG_EXT_CTL: |
Paolo Bonzini | 95c5c7c | 2019-07-02 14:45:24 +0200 | [diff] [blame] | 3979 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
Vitaly Kuznetsov | e87555e | 2018-12-19 12:06:13 +0100 | [diff] [blame] | 3980 | return false; |
Tom Lendacky | 5719455 | 2020-12-10 11:10:00 -0600 | [diff] [blame] | 3981 | case MSR_IA32_SMBASE: |
| 3982 | /* SEV-ES guests do not support SMM, so report false */ |
| 3983 | if (kvm && sev_es_guest(kvm)) |
| 3984 | return false; |
| 3985 | break; |
Vitaly Kuznetsov | e87555e | 2018-12-19 12:06:13 +0100 | [diff] [blame] | 3986 | default: |
| 3987 | break; |
| 3988 | } |
| 3989 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 3990 | return true; |
| 3991 | } |
| 3992 | |
Paolo Bonzini | fc07e76 | 2015-10-01 13:20:22 +0200 | [diff] [blame] | 3993 | static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
| 3994 | { |
| 3995 | return 0; |
| 3996 | } |
| 3997 | |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 3998 | static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 3999 | { |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 4000 | struct vcpu_svm *svm = to_svm(vcpu); |
Babu Moger | 96308b0 | 2020-11-12 16:18:03 -0600 | [diff] [blame] | 4001 | struct kvm_cpuid_entry2 *best; |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 4002 | |
Aaron Lewis | 7204160 | 2019-10-21 16:30:20 -0700 | [diff] [blame] | 4003 | vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
Sean Christopherson | 96be4e0 | 2019-12-10 14:44:15 -0800 | [diff] [blame] | 4004 | boot_cpu_has(X86_FEATURE_XSAVE) && |
Aaron Lewis | 7204160 | 2019-10-21 16:30:20 -0700 | [diff] [blame] | 4005 | boot_cpu_has(X86_FEATURE_XSAVES); |
| 4006 | |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 4007 | /* Update nrips enabled cache */ |
Sean Christopherson | 4eb8746 | 2020-03-02 15:57:08 -0800 | [diff] [blame] | 4008 | svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) && |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 4009 | guest_cpuid_has(vcpu, X86_FEATURE_NRIPS); |
Suravee Suthikulpanit | 46781ea | 2016-05-04 14:09:50 -0500 | [diff] [blame] | 4010 | |
Babu Moger | 4407a79 | 2020-09-11 14:29:19 -0500 | [diff] [blame] | 4011 | /* Check again if INVPCID interception if required */ |
| 4012 | svm_check_invpcid(svm); |
| 4013 | |
Babu Moger | 96308b0 | 2020-11-12 16:18:03 -0600 | [diff] [blame] | 4014 | /* For sev guests, the memory encryption bit is not reserved in CR3. */ |
| 4015 | if (sev_guest(vcpu->kvm)) { |
| 4016 | best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0); |
| 4017 | if (best) |
Sean Christopherson | ca29e14 | 2021-02-03 16:01:12 -0800 | [diff] [blame] | 4018 | vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f)); |
Babu Moger | 96308b0 | 2020-11-12 16:18:03 -0600 | [diff] [blame] | 4019 | } |
| 4020 | |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 4021 | if (kvm_vcpu_apicv_active(vcpu)) { |
| 4022 | /* |
| 4023 | * AVIC does not work with an x2APIC mode guest. If the X2APIC feature |
| 4024 | * is exposed to the guest, disable AVIC. |
| 4025 | */ |
| 4026 | if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC)) |
| 4027 | kvm_request_apicv_update(vcpu->kvm, false, |
| 4028 | APICV_INHIBIT_REASON_X2APIC); |
Suravee Suthikulpanit | 46781ea | 2016-05-04 14:09:50 -0500 | [diff] [blame] | 4029 | |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 4030 | /* |
| 4031 | * Currently, AVIC does not work with nested virtualization. |
| 4032 | * So, we disable AVIC when cpuid for SVM is set in the L1 guest. |
| 4033 | */ |
| 4034 | if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM)) |
| 4035 | kvm_request_apicv_update(vcpu->kvm, false, |
| 4036 | APICV_INHIBIT_REASON_NESTED); |
| 4037 | } |
Suravee Suthikulpanit | 9a0bf05 | 2019-11-14 14:15:14 -0600 | [diff] [blame] | 4038 | |
Maxim Levitsky | adc2a23 | 2021-04-01 14:19:28 +0300 | [diff] [blame] | 4039 | if (guest_cpuid_is_intel(vcpu)) { |
| 4040 | /* |
| 4041 | * We must intercept SYSENTER_EIP and SYSENTER_ESP |
| 4042 | * accesses because the processor only stores 32 bits. |
| 4043 | * For the same reason we cannot use virtual VMLOAD/VMSAVE. |
| 4044 | */ |
| 4045 | svm_set_intercept(svm, INTERCEPT_VMLOAD); |
| 4046 | svm_set_intercept(svm, INTERCEPT_VMSAVE); |
| 4047 | svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; |
| 4048 | |
| 4049 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0); |
| 4050 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0); |
| 4051 | } else { |
| 4052 | /* |
| 4053 | * If hardware supports Virtual VMLOAD VMSAVE then enable it |
| 4054 | * in VMCB and clear intercepts to avoid #VMEXIT. |
| 4055 | */ |
| 4056 | if (vls) { |
| 4057 | svm_clr_intercept(svm, INTERCEPT_VMLOAD); |
| 4058 | svm_clr_intercept(svm, INTERCEPT_VMSAVE); |
| 4059 | svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; |
| 4060 | } |
| 4061 | /* No need to intercept these MSRs */ |
| 4062 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1); |
| 4063 | set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1); |
| 4064 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 4065 | } |
| 4066 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4067 | static bool svm_has_wbinvd_exit(void) |
| 4068 | { |
| 4069 | return true; |
| 4070 | } |
| 4071 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4072 | #define PRE_EX(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 4073 | .stage = X86_ICPT_PRE_EXCEPT, } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4074 | #define POST_EX(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 4075 | .stage = X86_ICPT_POST_EXCEPT, } |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4076 | #define POST_MEM(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 4077 | .stage = X86_ICPT_POST_MEMACCESS, } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4078 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 4079 | static const struct __x86_intercept { |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4080 | u32 exit_code; |
| 4081 | enum x86_intercept_stage stage; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4082 | } x86_intercept_map[] = { |
| 4083 | [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), |
| 4084 | [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 4085 | [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 4086 | [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 4087 | [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4088 | [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), |
| 4089 | [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 4090 | [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), |
| 4091 | [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), |
| 4092 | [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), |
| 4093 | [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), |
| 4094 | [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), |
| 4095 | [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), |
| 4096 | [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), |
| 4097 | [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 4098 | [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), |
| 4099 | [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), |
| 4100 | [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), |
| 4101 | [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), |
| 4102 | [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), |
| 4103 | [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), |
| 4104 | [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), |
| 4105 | [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4106 | [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), |
| 4107 | [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), |
| 4108 | [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4109 | [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), |
| 4110 | [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), |
| 4111 | [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), |
| 4112 | [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), |
| 4113 | [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), |
| 4114 | [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), |
| 4115 | [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), |
| 4116 | [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), |
| 4117 | [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4118 | [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), |
| 4119 | [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), |
| 4120 | [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), |
| 4121 | [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), |
| 4122 | [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), |
| 4123 | [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), |
| 4124 | [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4125 | [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), |
| 4126 | [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), |
| 4127 | [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), |
| 4128 | [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), |
Vitaly Kuznetsov | 02d4160 | 2019-08-13 15:53:32 +0200 | [diff] [blame] | 4129 | [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV), |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4130 | }; |
| 4131 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4132 | #undef PRE_EX |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4133 | #undef POST_EX |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 4134 | #undef POST_MEM |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4135 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4136 | static int svm_check_intercept(struct kvm_vcpu *vcpu, |
| 4137 | struct x86_instruction_info *info, |
Sean Christopherson | 21f1b8f | 2020-02-18 15:29:42 -0800 | [diff] [blame] | 4138 | enum x86_intercept_stage stage, |
| 4139 | struct x86_exception *exception) |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4140 | { |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4141 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4142 | int vmexit, ret = X86EMUL_CONTINUE; |
| 4143 | struct __x86_intercept icpt_info; |
| 4144 | struct vmcb *vmcb = svm->vmcb; |
| 4145 | |
| 4146 | if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) |
| 4147 | goto out; |
| 4148 | |
| 4149 | icpt_info = x86_intercept_map[info->intercept]; |
| 4150 | |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 4151 | if (stage != icpt_info.stage) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4152 | goto out; |
| 4153 | |
| 4154 | switch (icpt_info.exit_code) { |
| 4155 | case SVM_EXIT_READ_CR0: |
| 4156 | if (info->intercept == x86_intercept_cr_read) |
| 4157 | icpt_info.exit_code += info->modrm_reg; |
| 4158 | break; |
| 4159 | case SVM_EXIT_WRITE_CR0: { |
| 4160 | unsigned long cr0, val; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4161 | |
| 4162 | if (info->intercept == x86_intercept_cr_write) |
| 4163 | icpt_info.exit_code += info->modrm_reg; |
| 4164 | |
Jan Kiszka | 62baf44 | 2014-06-29 21:55:53 +0200 | [diff] [blame] | 4165 | if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 || |
| 4166 | info->intercept == x86_intercept_clts) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4167 | break; |
| 4168 | |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 4169 | if (!(vmcb_is_intercept(&svm->nested.ctl, |
| 4170 | INTERCEPT_SELECTIVE_CR0))) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4171 | break; |
| 4172 | |
| 4173 | cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; |
| 4174 | val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; |
| 4175 | |
| 4176 | if (info->intercept == x86_intercept_lmsw) { |
| 4177 | cr0 &= 0xfUL; |
| 4178 | val &= 0xfUL; |
| 4179 | /* lmsw can't clear PE - catch this here */ |
| 4180 | if (cr0 & X86_CR0_PE) |
| 4181 | val |= X86_CR0_PE; |
| 4182 | } |
| 4183 | |
| 4184 | if (cr0 ^ val) |
| 4185 | icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 4186 | |
| 4187 | break; |
| 4188 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 4189 | case SVM_EXIT_READ_DR0: |
| 4190 | case SVM_EXIT_WRITE_DR0: |
| 4191 | icpt_info.exit_code += info->modrm_reg; |
| 4192 | break; |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 4193 | case SVM_EXIT_MSR: |
| 4194 | if (info->intercept == x86_intercept_wrmsr) |
| 4195 | vmcb->control.exit_info_1 = 1; |
| 4196 | else |
| 4197 | vmcb->control.exit_info_1 = 0; |
| 4198 | break; |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 4199 | case SVM_EXIT_PAUSE: |
| 4200 | /* |
| 4201 | * We get this for NOP only, but pause |
| 4202 | * is rep not, check this here |
| 4203 | */ |
| 4204 | if (info->rep_prefix != REPE_PREFIX) |
| 4205 | goto out; |
Jan H. Schönherr | 49a8afc | 2017-09-05 23:58:44 +0200 | [diff] [blame] | 4206 | break; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4207 | case SVM_EXIT_IOIO: { |
| 4208 | u64 exit_info; |
| 4209 | u32 bytes; |
| 4210 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4211 | if (info->intercept == x86_intercept_in || |
| 4212 | info->intercept == x86_intercept_ins) { |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 4213 | exit_info = ((info->src_val & 0xffff) << 16) | |
| 4214 | SVM_IOIO_TYPE_MASK; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4215 | bytes = info->dst_bytes; |
Jan Kiszka | 6493f15 | 2014-06-30 11:07:05 +0200 | [diff] [blame] | 4216 | } else { |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 4217 | exit_info = (info->dst_val & 0xffff) << 16; |
Jan Kiszka | 6493f15 | 2014-06-30 11:07:05 +0200 | [diff] [blame] | 4218 | bytes = info->src_bytes; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | if (info->intercept == x86_intercept_outs || |
| 4222 | info->intercept == x86_intercept_ins) |
| 4223 | exit_info |= SVM_IOIO_STR_MASK; |
| 4224 | |
| 4225 | if (info->rep_prefix) |
| 4226 | exit_info |= SVM_IOIO_REP_MASK; |
| 4227 | |
| 4228 | bytes = min(bytes, 4u); |
| 4229 | |
| 4230 | exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; |
| 4231 | |
| 4232 | exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); |
| 4233 | |
| 4234 | vmcb->control.exit_info_1 = exit_info; |
| 4235 | vmcb->control.exit_info_2 = info->next_rip; |
| 4236 | |
| 4237 | break; |
| 4238 | } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4239 | default: |
| 4240 | break; |
| 4241 | } |
| 4242 | |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 4243 | /* TODO: Advertise NRIPS to guest hypervisor unconditionally */ |
| 4244 | if (static_cpu_has(X86_FEATURE_NRIPS)) |
| 4245 | vmcb->control.next_rip = info->next_rip; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 4246 | vmcb->control.exit_code = icpt_info.exit_code; |
| 4247 | vmexit = nested_svm_exit_handled(svm); |
| 4248 | |
| 4249 | ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED |
| 4250 | : X86EMUL_CONTINUE; |
| 4251 | |
| 4252 | out: |
| 4253 | return ret; |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4254 | } |
| 4255 | |
Wanpeng Li | a9ab13f | 2020-04-10 10:47:03 -0700 | [diff] [blame] | 4256 | static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu) |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4257 | { |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4258 | } |
| 4259 | |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 4260 | static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu) |
| 4261 | { |
Wanpeng Li | 830f01b | 2020-07-31 11:12:21 +0800 | [diff] [blame] | 4262 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 4263 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 4264 | } |
| 4265 | |
Borislav Petkov | 74f1690 | 2017-03-26 23:51:24 +0200 | [diff] [blame] | 4266 | static void svm_setup_mce(struct kvm_vcpu *vcpu) |
| 4267 | { |
| 4268 | /* [63:9] are reserved. */ |
| 4269 | vcpu->arch.mcg_cap &= 0x1ff; |
| 4270 | } |
| 4271 | |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 4272 | bool svm_smi_blocked(struct kvm_vcpu *vcpu) |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 4273 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 4274 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4275 | |
| 4276 | /* Per APM Vol.2 15.22.2 "Response to SMI" */ |
| 4277 | if (!gif_set(svm)) |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 4278 | return true; |
| 4279 | |
| 4280 | return is_smm(vcpu); |
| 4281 | } |
| 4282 | |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 4283 | static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection) |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 4284 | { |
| 4285 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4286 | if (svm->nested.nested_run_pending) |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 4287 | return -EBUSY; |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 4288 | |
Paolo Bonzini | c300ab9 | 2020-04-23 14:08:58 -0400 | [diff] [blame] | 4289 | /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */ |
| 4290 | if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm)) |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 4291 | return -EBUSY; |
Paolo Bonzini | c300ab9 | 2020-04-23 14:08:58 -0400 | [diff] [blame] | 4292 | |
Paolo Bonzini | cae96af | 2020-04-23 14:19:26 -0400 | [diff] [blame] | 4293 | return !svm_smi_blocked(vcpu); |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 4294 | } |
| 4295 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 4296 | static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 4297 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 4298 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4299 | int ret; |
| 4300 | |
| 4301 | if (is_guest_mode(vcpu)) { |
| 4302 | /* FED8h - SVM Guest */ |
| 4303 | put_smstate(u64, smstate, 0x7ed8, 1); |
| 4304 | /* FEE0h - SVM Guest VMCB Physical Address */ |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 4305 | put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa); |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 4306 | |
| 4307 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 4308 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 4309 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 4310 | |
| 4311 | ret = nested_svm_vmexit(svm); |
| 4312 | if (ret) |
| 4313 | return ret; |
| 4314 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 4315 | return 0; |
| 4316 | } |
| 4317 | |
Sean Christopherson | ed19321 | 2019-04-02 08:03:09 -0700 | [diff] [blame] | 4318 | static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate) |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 4319 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 4320 | struct vcpu_svm *svm = to_svm(vcpu); |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 4321 | struct kvm_host_map map; |
Vitaly Kuznetsov | 59cd9bc | 2020-07-10 16:11:52 +0200 | [diff] [blame] | 4322 | int ret = 0; |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 4323 | |
Maxim Levitsky | 3ebb5d2 | 2020-08-27 19:27:20 +0300 | [diff] [blame] | 4324 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) { |
| 4325 | u64 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0); |
| 4326 | u64 guest = GET_SMSTATE(u64, smstate, 0x7ed8); |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 4327 | u64 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0); |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 4328 | |
Maxim Levitsky | 3ebb5d2 | 2020-08-27 19:27:20 +0300 | [diff] [blame] | 4329 | if (guest) { |
| 4330 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM)) |
| 4331 | return 1; |
| 4332 | |
| 4333 | if (!(saved_efer & EFER_SVME)) |
| 4334 | return 1; |
| 4335 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 4336 | if (kvm_vcpu_map(vcpu, |
Maxim Levitsky | 0dd16b5 | 2020-08-27 20:11:39 +0300 | [diff] [blame] | 4337 | gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL) |
Maxim Levitsky | 3ebb5d2 | 2020-08-27 19:27:20 +0300 | [diff] [blame] | 4338 | return 1; |
| 4339 | |
Maxim Levitsky | 2fcf487 | 2020-10-01 14:29:54 +0300 | [diff] [blame] | 4340 | if (svm_allocate_nested(svm)) |
| 4341 | return 1; |
| 4342 | |
Paolo Bonzini | 6312975 | 2021-03-02 14:40:39 -0500 | [diff] [blame] | 4343 | ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, map.hva); |
| 4344 | kvm_vcpu_unmap(vcpu, &map, true); |
Maxim Levitsky | 3ebb5d2 | 2020-08-27 19:27:20 +0300 | [diff] [blame] | 4345 | } |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 4346 | } |
Vitaly Kuznetsov | 59cd9bc | 2020-07-10 16:11:52 +0200 | [diff] [blame] | 4347 | |
| 4348 | return ret; |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 4349 | } |
| 4350 | |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 4351 | static void svm_enable_smi_window(struct kvm_vcpu *vcpu) |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 4352 | { |
| 4353 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4354 | |
| 4355 | if (!gif_set(svm)) { |
| 4356 | if (vgif_enabled(svm)) |
Joerg Roedel | a284ba5 | 2020-06-25 10:03:24 +0200 | [diff] [blame] | 4357 | svm_set_intercept(svm, INTERCEPT_STGI); |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 4358 | /* STGI will cause a vm exit */ |
Paolo Bonzini | c9d4091 | 2020-05-22 11:21:49 -0400 | [diff] [blame] | 4359 | } else { |
| 4360 | /* We must be in SMM; RSM will cause a vmexit anyway. */ |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 4361 | } |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 4362 | } |
| 4363 | |
Sean Christopherson | 09e3e2a | 2020-09-15 16:27:02 -0700 | [diff] [blame] | 4364 | static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len) |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 4365 | { |
Sean Christopherson | 09e3e2a | 2020-09-15 16:27:02 -0700 | [diff] [blame] | 4366 | bool smep, smap, is_user; |
| 4367 | unsigned long cr4; |
Paolo Bonzini | e72436b | 2020-04-17 12:21:06 -0400 | [diff] [blame] | 4368 | |
| 4369 | /* |
Tom Lendacky | bc624d9 | 2020-12-10 11:09:44 -0600 | [diff] [blame] | 4370 | * When the guest is an SEV-ES guest, emulation is not possible. |
| 4371 | */ |
| 4372 | if (sev_es_guest(vcpu->kvm)) |
| 4373 | return false; |
| 4374 | |
| 4375 | /* |
Liran Alon | 118154b | 2019-07-17 02:56:58 +0300 | [diff] [blame] | 4376 | * Detect and workaround Errata 1096 Fam_17h_00_0Fh. |
| 4377 | * |
| 4378 | * Errata: |
| 4379 | * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is |
| 4380 | * possible that CPU microcode implementing DecodeAssist will fail |
| 4381 | * to read bytes of instruction which caused #NPF. In this case, |
| 4382 | * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly |
| 4383 | * return 0 instead of the correct guest instruction bytes. |
| 4384 | * |
| 4385 | * This happens because CPU microcode reading instruction bytes |
| 4386 | * uses a special opcode which attempts to read data using CPL=0 |
| 4387 | * priviledges. The microcode reads CS:RIP and if it hits a SMAP |
| 4388 | * fault, it gives up and returns no instruction bytes. |
| 4389 | * |
| 4390 | * Detection: |
| 4391 | * We reach here in case CPU supports DecodeAssist, raised #NPF and |
| 4392 | * returned 0 in GuestIntrBytes field of the VMCB. |
| 4393 | * First, errata can only be triggered in case vCPU CR4.SMAP=1. |
| 4394 | * Second, if vCPU CR4.SMEP=1, errata could only be triggered |
| 4395 | * in case vCPU CPL==3 (Because otherwise guest would have triggered |
| 4396 | * a SMEP fault instead of #NPF). |
| 4397 | * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL. |
| 4398 | * As most guests enable SMAP if they have also enabled SMEP, use above |
| 4399 | * logic in order to attempt minimize false-positive of detecting errata |
| 4400 | * while still preserving all cases semantic correctness. |
| 4401 | * |
| 4402 | * Workaround: |
| 4403 | * To determine what instruction the guest was executing, the hypervisor |
| 4404 | * will have to decode the instruction at the instruction pointer. |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 4405 | * |
| 4406 | * In non SEV guest, hypervisor will be able to read the guest |
| 4407 | * memory to decode the instruction pointer when insn_len is zero |
| 4408 | * so we return true to indicate that decoding is possible. |
| 4409 | * |
| 4410 | * But in the SEV guest, the guest memory is encrypted with the |
| 4411 | * guest specific key and hypervisor will not be able to decode the |
| 4412 | * instruction pointer so we will not able to workaround it. Lets |
| 4413 | * print the error and request to kill the guest. |
| 4414 | */ |
Sean Christopherson | 09e3e2a | 2020-09-15 16:27:02 -0700 | [diff] [blame] | 4415 | if (likely(!insn || insn_len)) |
| 4416 | return true; |
| 4417 | |
| 4418 | /* |
| 4419 | * If RIP is invalid, go ahead with emulation which will cause an |
| 4420 | * internal error exit. |
| 4421 | */ |
| 4422 | if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT)) |
| 4423 | return true; |
| 4424 | |
| 4425 | cr4 = kvm_read_cr4(vcpu); |
| 4426 | smep = cr4 & X86_CR4_SMEP; |
| 4427 | smap = cr4 & X86_CR4_SMAP; |
| 4428 | is_user = svm_get_cpl(vcpu) == 3; |
Liran Alon | 118154b | 2019-07-17 02:56:58 +0300 | [diff] [blame] | 4429 | if (smap && (!smep || is_user)) { |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 4430 | if (!sev_guest(vcpu->kvm)) |
| 4431 | return true; |
| 4432 | |
Liran Alon | 118154b | 2019-07-17 02:56:58 +0300 | [diff] [blame] | 4433 | pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n"); |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 4434 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 4435 | } |
| 4436 | |
| 4437 | return false; |
| 4438 | } |
| 4439 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4440 | static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu) |
| 4441 | { |
| 4442 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4443 | |
| 4444 | /* |
| 4445 | * TODO: Last condition latch INIT signals on vCPU when |
| 4446 | * vCPU is in guest-mode and vmcb12 defines intercept on INIT. |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 4447 | * To properly emulate the INIT intercept, |
| 4448 | * svm_check_nested_events() should call nested_svm_vmexit() |
| 4449 | * if an INIT signal is pending. |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4450 | */ |
| 4451 | return !gif_set(svm) || |
Babu Moger | c62e2e9 | 2020-09-11 14:28:28 -0500 | [diff] [blame] | 4452 | (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT)); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4453 | } |
| 4454 | |
Tom Lendacky | 647daca | 2021-01-04 14:20:01 -0600 | [diff] [blame] | 4455 | static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) |
| 4456 | { |
| 4457 | if (!sev_es_guest(vcpu->kvm)) |
| 4458 | return kvm_vcpu_deliver_sipi_vector(vcpu, vector); |
| 4459 | |
| 4460 | sev_vcpu_deliver_sipi_vector(vcpu, vector); |
| 4461 | } |
| 4462 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 4463 | static void svm_vm_destroy(struct kvm *kvm) |
| 4464 | { |
| 4465 | avic_vm_destroy(kvm); |
| 4466 | sev_vm_destroy(kvm); |
| 4467 | } |
| 4468 | |
| 4469 | static int svm_vm_init(struct kvm *kvm) |
| 4470 | { |
Wanpeng Li | 830f01b | 2020-07-31 11:12:21 +0800 | [diff] [blame] | 4471 | if (!pause_filter_count || !pause_filter_thresh) |
| 4472 | kvm->arch.pause_in_guest = true; |
| 4473 | |
Joerg Roedel | eaf7826 | 2020-03-24 10:41:54 +0100 | [diff] [blame] | 4474 | if (avic) { |
| 4475 | int ret = avic_vm_init(kvm); |
| 4476 | if (ret) |
| 4477 | return ret; |
| 4478 | } |
| 4479 | |
| 4480 | kvm_apicv_init(kvm, avic); |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
Sean Christopherson | 9c14ee2 | 2020-03-21 13:26:03 -0700 | [diff] [blame] | 4484 | static struct kvm_x86_ops svm_x86_ops __initdata = { |
Li RongQing | dd58f3c | 2020-02-23 16:13:12 +0800 | [diff] [blame] | 4485 | .hardware_unsetup = svm_hardware_teardown, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4486 | .hardware_enable = svm_hardware_enable, |
| 4487 | .hardware_disable = svm_hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 4488 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 4489 | .has_emulated_msr = svm_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4490 | |
| 4491 | .vcpu_create = svm_create_vcpu, |
| 4492 | .vcpu_free = svm_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4493 | .vcpu_reset = svm_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4494 | |
Sean Christopherson | 562b6b0 | 2020-01-26 16:41:13 -0800 | [diff] [blame] | 4495 | .vm_size = sizeof(struct kvm_svm), |
Suravee Suthikulpanit | 4e19c36 | 2019-11-14 14:15:05 -0600 | [diff] [blame] | 4496 | .vm_init = svm_vm_init, |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 4497 | .vm_destroy = svm_vm_destroy, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4498 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4499 | .prepare_guest_switch = svm_prepare_guest_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4500 | .vcpu_load = svm_vcpu_load, |
| 4501 | .vcpu_put = svm_vcpu_put, |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 4502 | .vcpu_blocking = svm_vcpu_blocking, |
| 4503 | .vcpu_unblocking = svm_vcpu_unblocking, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4504 | |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 4505 | .update_exception_bitmap = svm_update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4506 | .get_msr_feature = svm_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4507 | .get_msr = svm_get_msr, |
| 4508 | .set_msr = svm_set_msr, |
| 4509 | .get_segment_base = svm_get_segment_base, |
| 4510 | .get_segment = svm_get_segment, |
| 4511 | .set_segment = svm_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4512 | .get_cpl = svm_get_cpl, |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 4513 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4514 | .set_cr0 = svm_set_cr0, |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 4515 | .is_valid_cr4 = svm_is_valid_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4516 | .set_cr4 = svm_set_cr4, |
| 4517 | .set_efer = svm_set_efer, |
| 4518 | .get_idt = svm_get_idt, |
| 4519 | .set_idt = svm_set_idt, |
| 4520 | .get_gdt = svm_get_gdt, |
| 4521 | .set_gdt = svm_set_gdt, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4522 | .set_dr7 = svm_set_dr7, |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4523 | .sync_dirty_debug_regs = svm_sync_dirty_debug_regs, |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4524 | .cache_reg = svm_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4525 | .get_rflags = svm_get_rflags, |
| 4526 | .set_rflags = svm_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 4527 | |
Sean Christopherson | 7780938 | 2020-03-20 14:28:18 -0700 | [diff] [blame] | 4528 | .tlb_flush_all = svm_flush_tlb, |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 4529 | .tlb_flush_current = svm_flush_tlb, |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 4530 | .tlb_flush_gva = svm_flush_tlb_gva, |
Sean Christopherson | 72b3832 | 2020-03-20 14:28:13 -0700 | [diff] [blame] | 4531 | .tlb_flush_guest = svm_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4532 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4533 | .run = svm_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4534 | .handle_exit = handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4535 | .skip_emulated_instruction = skip_emulated_instruction, |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 4536 | .update_emulated_instruction = NULL, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 4537 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
| 4538 | .get_interrupt_shadow = svm_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4539 | .patch_hypercall = svm_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 4540 | .set_irq = svm_set_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4541 | .set_nmi = svm_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 4542 | .queue_exception = svm_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 4543 | .cancel_injection = svm_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4544 | .interrupt_allowed = svm_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4545 | .nmi_allowed = svm_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4546 | .get_nmi_mask = svm_get_nmi_mask, |
| 4547 | .set_nmi_mask = svm_set_nmi_mask, |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 4548 | .enable_nmi_window = svm_enable_nmi_window, |
| 4549 | .enable_irq_window = svm_enable_irq_window, |
| 4550 | .update_cr8_intercept = svm_update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 4551 | .set_virtual_apic_mode = svm_set_virtual_apic_mode, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4552 | .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl, |
Suravee Suthikulpanit | ef8efd7 | 2019-11-14 14:15:10 -0600 | [diff] [blame] | 4553 | .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons, |
Suravee Suthikulpanit | 2de9d0c | 2019-11-14 14:15:11 -0600 | [diff] [blame] | 4554 | .pre_update_apicv_exec_ctrl = svm_pre_update_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4555 | .load_eoi_exitmap = svm_load_eoi_exitmap, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4556 | .hwapic_irr_update = svm_hwapic_irr_update, |
| 4557 | .hwapic_isr_update = svm_hwapic_isr_update, |
Liran Alon | fa59cc0 | 2017-12-24 18:12:53 +0200 | [diff] [blame] | 4558 | .sync_pir_to_irr = kvm_lapic_find_highest_irr, |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 4559 | .apicv_post_state_restore = avic_post_state_restore, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4560 | |
| 4561 | .set_tss_addr = svm_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 4562 | .set_identity_map_addr = svm_set_identity_map_addr, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4563 | .get_mt_mask = svm_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4564 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 4565 | .get_exit_info = svm_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 4566 | |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 4567 | .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4568 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4569 | .has_wbinvd_exit = svm_has_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 4570 | |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 4571 | .write_l1_tsc_offset = svm_write_l1_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 4572 | |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 4573 | .load_mmu_pgd = svm_load_mmu_pgd, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 4574 | |
| 4575 | .check_intercept = svm_check_intercept, |
Sean Christopherson | 95b5a48 | 2019-04-19 22:50:59 -0700 | [diff] [blame] | 4576 | .handle_exit_irqoff = svm_handle_exit_irqoff, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 4577 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 4578 | .request_immediate_exit = __kvm_request_immediate_exit, |
| 4579 | |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 4580 | .sched_in = svm_sched_in, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 4581 | |
| 4582 | .pmu_ops = &amd_pmu_ops, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 4583 | .nested_ops = &svm_nested_ops, |
| 4584 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 4585 | .deliver_posted_interrupt = svm_deliver_avic_intr, |
Wanpeng Li | 17e433b | 2019-08-05 10:03:19 +0800 | [diff] [blame] | 4586 | .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt, |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 4587 | .update_pi_irte = svm_update_pi_irte, |
Borislav Petkov | 74f1690 | 2017-03-26 23:51:24 +0200 | [diff] [blame] | 4588 | .setup_mce = svm_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 4589 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 4590 | .smi_allowed = svm_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 4591 | .pre_enter_smm = svm_pre_enter_smm, |
| 4592 | .pre_leave_smm = svm_pre_leave_smm, |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 4593 | .enable_smi_window = svm_enable_smi_window, |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 4594 | |
| 4595 | .mem_enc_op = svm_mem_enc_op, |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 4596 | .mem_enc_reg_region = svm_register_enc_region, |
| 4597 | .mem_enc_unreg_region = svm_unregister_enc_region, |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 4598 | |
Nathan Tempelman | 54526d1 | 2021-04-08 22:32:14 +0000 | [diff] [blame] | 4599 | .vm_copy_enc_context_from = svm_vm_copy_asid_from, |
| 4600 | |
Sean Christopherson | 09e3e2a | 2020-09-15 16:27:02 -0700 | [diff] [blame] | 4601 | .can_emulate_instruction = svm_can_emulate_instruction, |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4602 | |
| 4603 | .apic_init_signal_blocked = svm_apic_init_signal_blocked, |
Alexander Graf | fd6fa73 | 2020-09-25 16:34:19 +0200 | [diff] [blame] | 4604 | |
| 4605 | .msr_filter_changed = svm_msr_filter_changed, |
Tom Lendacky | f1c6366 | 2020-12-14 10:29:50 -0500 | [diff] [blame] | 4606 | .complete_emulated_msr = svm_complete_emulated_msr, |
Tom Lendacky | 647daca | 2021-01-04 14:20:01 -0600 | [diff] [blame] | 4607 | |
| 4608 | .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4609 | }; |
| 4610 | |
Sean Christopherson | d008dfd | 2020-03-21 13:25:56 -0700 | [diff] [blame] | 4611 | static struct kvm_x86_init_ops svm_init_ops __initdata = { |
| 4612 | .cpu_has_kvm_support = has_svm, |
| 4613 | .disabled_by_bios = is_disabled, |
| 4614 | .hardware_setup = svm_hardware_setup, |
| 4615 | .check_processor_compatibility = svm_check_processor_compat, |
| 4616 | |
| 4617 | .runtime_ops = &svm_x86_ops, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4618 | }; |
| 4619 | |
| 4620 | static int __init svm_init(void) |
| 4621 | { |
Tom Lendacky | d07f46f | 2020-09-07 15:15:03 +0200 | [diff] [blame] | 4622 | __unused_size_checks(); |
| 4623 | |
Sean Christopherson | d008dfd | 2020-03-21 13:25:56 -0700 | [diff] [blame] | 4624 | return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm), |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 4625 | __alignof__(struct vcpu_svm), THIS_MODULE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | static void __exit svm_exit(void) |
| 4629 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 4630 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4631 | } |
| 4632 | |
| 4633 | module_init(svm_init) |
| 4634 | module_exit(svm_exit) |