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