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