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