Thomas Gleixner | 43aa313 | 2019-05-29 07:17:54 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 2 | /* |
| 3 | * X86 specific Hyper-V initialization code. |
| 4 | * |
| 5 | * Copyright (C) 2016, Microsoft, Inc. |
| 6 | * |
| 7 | * Author : K. Y. Srinivasan <kys@microsoft.com> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Dexuan Cui | 2f285f4 | 2018-09-18 22:29:50 +0000 | [diff] [blame] | 10 | #include <linux/efi.h> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
Andrea Parri (Microsoft) | a6c76bb0 | 2021-02-01 15:48:11 +0100 | [diff] [blame] | 12 | #include <linux/bitfield.h> |
Michael Kelley | 6dc77fa | 2021-07-14 11:34:47 -0700 | [diff] [blame] | 13 | #include <linux/io.h> |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 14 | #include <asm/apic.h> |
| 15 | #include <asm/desc.h> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 16 | #include <asm/hypervisor.h> |
Vitaly Kuznetsov | 5a48580 | 2018-03-20 15:02:05 +0100 | [diff] [blame] | 17 | #include <asm/hyperv-tlfs.h> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 18 | #include <asm/mshyperv.h> |
Thomas Gleixner | a16be36 | 2020-05-21 22:05:43 +0200 | [diff] [blame] | 19 | #include <asm/idtentry.h> |
Dexuan Cui | dfe94d4 | 2020-12-21 22:55:41 -0800 | [diff] [blame] | 20 | #include <linux/kexec.h> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 21 | #include <linux/version.h> |
| 22 | #include <linux/vmalloc.h> |
| 23 | #include <linux/mm.h> |
Stephen Hemminger | 6707181 | 2017-03-04 18:27:11 -0700 | [diff] [blame] | 24 | #include <linux/hyperv.h> |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 25 | #include <linux/slab.h> |
Tianyu Lan | f3a99e7 | 2020-04-06 08:53:31 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 27 | #include <linux/cpuhotplug.h> |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 28 | #include <linux/syscore_ops.h> |
Michael Kelley | dd2cb34 | 2019-07-01 04:26:06 +0000 | [diff] [blame] | 29 | #include <clocksource/hyperv_timer.h> |
Wei Liu | 80f73c9 | 2021-02-03 15:04:26 +0000 | [diff] [blame] | 30 | #include <linux/highmem.h> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 31 | |
Dexuan Cui | dfe94d4 | 2020-12-21 22:55:41 -0800 | [diff] [blame] | 32 | int hyperv_init_cpuhp; |
Wei Liu | 99a0f46 | 2021-02-03 15:04:25 +0000 | [diff] [blame] | 33 | u64 hv_current_partition_id = ~0ull; |
| 34 | EXPORT_SYMBOL_GPL(hv_current_partition_id); |
Dexuan Cui | dfe94d4 | 2020-12-21 22:55:41 -0800 | [diff] [blame] | 35 | |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 36 | void *hv_hypercall_pg; |
| 37 | EXPORT_SYMBOL_GPL(hv_hypercall_pg); |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 38 | |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 39 | union hv_ghcb __percpu **hv_ghcb_pg; |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 40 | |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 41 | /* Storage to save the hypercall page temporarily for hibernation */ |
| 42 | static void *hv_hypercall_pg_saved; |
| 43 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 44 | struct hv_vp_assist_page **hv_vp_assist_page; |
| 45 | EXPORT_SYMBOL_GPL(hv_vp_assist_page); |
| 46 | |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 47 | static int hyperv_init_ghcb(void) |
| 48 | { |
| 49 | u64 ghcb_gpa; |
| 50 | void *ghcb_va; |
| 51 | void **ghcb_base; |
| 52 | |
| 53 | if (!hv_isolation_type_snp()) |
| 54 | return 0; |
| 55 | |
| 56 | if (!hv_ghcb_pg) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | /* |
| 60 | * GHCB page is allocated by paravisor. The address |
| 61 | * returned by MSR_AMD64_SEV_ES_GHCB is above shared |
| 62 | * memory boundary and map it here. |
| 63 | */ |
| 64 | rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa); |
| 65 | ghcb_va = memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB); |
| 66 | if (!ghcb_va) |
| 67 | return -ENOMEM; |
| 68 | |
| 69 | ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg); |
| 70 | *ghcb_base = ghcb_va; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 75 | static int hv_cpu_init(unsigned int cpu) |
| 76 | { |
Praveen Kumar | e5d9b71 | 2021-07-31 17:35:19 +0530 | [diff] [blame] | 77 | union hv_vp_assist_msr_contents msr = { 0 }; |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 78 | struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()]; |
Michael Kelley | afca4d9 | 2021-07-14 11:34:45 -0700 | [diff] [blame] | 79 | int ret; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 80 | |
Michael Kelley | afca4d9 | 2021-07-14 11:34:45 -0700 | [diff] [blame] | 81 | ret = hv_common_cpu_init(cpu); |
| 82 | if (ret) |
| 83 | return ret; |
Vitaly Kuznetsov | a3b7424 | 2017-10-06 17:48:54 +0200 | [diff] [blame] | 84 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 85 | if (!hv_vp_assist_page) |
| 86 | return 0; |
| 87 | |
Dexuan Cui | e320ab3 | 2019-07-19 03:22:35 +0000 | [diff] [blame] | 88 | if (!*hvp) { |
Praveen Kumar | e5d9b71 | 2021-07-31 17:35:19 +0530 | [diff] [blame] | 89 | if (hv_root_partition) { |
| 90 | /* |
| 91 | * For root partition we get the hypervisor provided VP assist |
| 92 | * page, instead of allocating a new page. |
| 93 | */ |
| 94 | rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); |
| 95 | *hvp = memremap(msr.pfn << |
| 96 | HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT, |
| 97 | PAGE_SIZE, MEMREMAP_WB); |
| 98 | } else { |
| 99 | /* |
| 100 | * The VP assist page is an "overlay" page (see Hyper-V TLFS's |
| 101 | * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed |
| 102 | * out to make sure we always write the EOI MSR in |
| 103 | * hv_apic_eoi_write() *after* the EOI optimization is disabled |
| 104 | * in hv_cpu_die(), otherwise a CPU may not be stopped in the |
| 105 | * case of CPU offlining and the VM will hang. |
| 106 | */ |
| 107 | *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO); |
| 108 | if (*hvp) |
| 109 | msr.pfn = vmalloc_to_pfn(*hvp); |
| 110 | } |
| 111 | WARN_ON(!(*hvp)); |
| 112 | if (*hvp) { |
| 113 | msr.enable = 1; |
| 114 | wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); |
| 115 | } |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 118 | return hyperv_init_ghcb(); |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 121 | static void (*hv_reenlightenment_cb)(void); |
| 122 | |
| 123 | static void hv_reenlightenment_notify(struct work_struct *dummy) |
| 124 | { |
| 125 | struct hv_tsc_emulation_status emu_status; |
| 126 | |
| 127 | rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 128 | |
| 129 | /* Don't issue the callback if TSC accesses are not emulated */ |
| 130 | if (hv_reenlightenment_cb && emu_status.inprogress) |
| 131 | hv_reenlightenment_cb(); |
| 132 | } |
| 133 | static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify); |
| 134 | |
| 135 | void hyperv_stop_tsc_emulation(void) |
| 136 | { |
| 137 | u64 freq; |
| 138 | struct hv_tsc_emulation_status emu_status; |
| 139 | |
| 140 | rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 141 | emu_status.inprogress = 0; |
| 142 | wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 143 | |
| 144 | rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq); |
| 145 | tsc_khz = div64_u64(freq, 1000); |
| 146 | } |
| 147 | EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation); |
| 148 | |
| 149 | static inline bool hv_reenlightenment_available(void) |
| 150 | { |
| 151 | /* |
Ingo Molnar | d9f6e12 | 2021-03-18 15:28:01 +0100 | [diff] [blame] | 152 | * Check for required features and privileges to make TSC frequency |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 153 | * change notifications work. |
| 154 | */ |
Joseph Salisbury | dfc53ba | 2020-09-26 07:26:26 -0700 | [diff] [blame] | 155 | return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS && |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 156 | ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE && |
Joseph Salisbury | dfc53ba | 2020-09-26 07:26:26 -0700 | [diff] [blame] | 157 | ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT; |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Thomas Gleixner | a16be36 | 2020-05-21 22:05:43 +0200 | [diff] [blame] | 160 | DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment) |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 161 | { |
Thomas Gleixner | a16be36 | 2020-05-21 22:05:43 +0200 | [diff] [blame] | 162 | ack_APIC_irq(); |
Vitaly Kuznetsov | 51d4e5d | 2018-01-24 14:23:35 +0100 | [diff] [blame] | 163 | inc_irq_stat(irq_hv_reenlightenment_count); |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 164 | schedule_delayed_work(&hv_reenlightenment_work, HZ/10); |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void set_hv_tscchange_cb(void (*cb)(void)) |
| 168 | { |
| 169 | struct hv_reenlightenment_control re_ctrl = { |
| 170 | .vector = HYPERV_REENLIGHTENMENT_VECTOR, |
| 171 | .enabled = 1, |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 172 | }; |
| 173 | struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1}; |
| 174 | |
| 175 | if (!hv_reenlightenment_available()) { |
| 176 | pr_warn("Hyper-V: reenlightenment support is unavailable\n"); |
| 177 | return; |
| 178 | } |
| 179 | |
Sean Christopherson | daf9721 | 2021-11-04 18:22:38 +0000 | [diff] [blame] | 180 | if (!hv_vp_index) |
| 181 | return; |
| 182 | |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 183 | hv_reenlightenment_cb = cb; |
| 184 | |
| 185 | /* Make sure callback is registered before we write to MSRs */ |
| 186 | wmb(); |
| 187 | |
Vitaly Kuznetsov | 285f68a | 2021-10-12 17:50:05 +0200 | [diff] [blame] | 188 | re_ctrl.target_vp = hv_vp_index[get_cpu()]; |
| 189 | |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 190 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 191 | wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl)); |
Vitaly Kuznetsov | 285f68a | 2021-10-12 17:50:05 +0200 | [diff] [blame] | 192 | |
| 193 | put_cpu(); |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 194 | } |
| 195 | EXPORT_SYMBOL_GPL(set_hv_tscchange_cb); |
| 196 | |
| 197 | void clear_hv_tscchange_cb(void) |
| 198 | { |
| 199 | struct hv_reenlightenment_control re_ctrl; |
| 200 | |
| 201 | if (!hv_reenlightenment_available()) |
| 202 | return; |
| 203 | |
| 204 | rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); |
| 205 | re_ctrl.enabled = 0; |
| 206 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); |
| 207 | |
| 208 | hv_reenlightenment_cb = NULL; |
| 209 | } |
| 210 | EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb); |
| 211 | |
Vitaly Kuznetsov | e7c4e36 | 2018-01-24 14:23:34 +0100 | [diff] [blame] | 212 | static int hv_cpu_die(unsigned int cpu) |
| 213 | { |
| 214 | struct hv_reenlightenment_control re_ctrl; |
| 215 | unsigned int new_cpu; |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 216 | void **ghcb_va; |
| 217 | |
| 218 | if (hv_ghcb_pg) { |
| 219 | ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg); |
| 220 | if (*ghcb_va) |
| 221 | memunmap(*ghcb_va); |
| 222 | *ghcb_va = NULL; |
| 223 | } |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 224 | |
Michael Kelley | afca4d9 | 2021-07-14 11:34:45 -0700 | [diff] [blame] | 225 | hv_common_cpu_die(cpu); |
Vitaly Kuznetsov | e7c4e36 | 2018-01-24 14:23:34 +0100 | [diff] [blame] | 226 | |
Praveen Kumar | e5d9b71 | 2021-07-31 17:35:19 +0530 | [diff] [blame] | 227 | if (hv_vp_assist_page && hv_vp_assist_page[cpu]) { |
| 228 | union hv_vp_assist_msr_contents msr = { 0 }; |
| 229 | if (hv_root_partition) { |
| 230 | /* |
| 231 | * For root partition the VP assist page is mapped to |
| 232 | * hypervisor provided page, and thus we unmap the |
| 233 | * page here and nullify it, so that in future we have |
| 234 | * correct page address mapped in hv_cpu_init. |
| 235 | */ |
| 236 | memunmap(hv_vp_assist_page[cpu]); |
| 237 | hv_vp_assist_page[cpu] = NULL; |
| 238 | rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); |
| 239 | msr.enable = 0; |
| 240 | } |
| 241 | wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); |
| 242 | } |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 243 | |
Vitaly Kuznetsov | e7c4e36 | 2018-01-24 14:23:34 +0100 | [diff] [blame] | 244 | if (hv_reenlightenment_cb == NULL) |
| 245 | return 0; |
| 246 | |
| 247 | rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 248 | if (re_ctrl.target_vp == hv_vp_index[cpu]) { |
Vitaly Kuznetsov | 38dce41 | 2020-05-12 18:01:53 +0200 | [diff] [blame] | 249 | /* |
| 250 | * Reassign reenlightenment notifications to some other online |
| 251 | * CPU or just disable the feature if there are no online CPUs |
| 252 | * left (happens on hibernation). |
| 253 | */ |
Vitaly Kuznetsov | e7c4e36 | 2018-01-24 14:23:34 +0100 | [diff] [blame] | 254 | new_cpu = cpumask_any_but(cpu_online_mask, cpu); |
| 255 | |
Vitaly Kuznetsov | 38dce41 | 2020-05-12 18:01:53 +0200 | [diff] [blame] | 256 | if (new_cpu < nr_cpu_ids) |
| 257 | re_ctrl.target_vp = hv_vp_index[new_cpu]; |
| 258 | else |
| 259 | re_ctrl.enabled = 0; |
| 260 | |
Vitaly Kuznetsov | e7c4e36 | 2018-01-24 14:23:34 +0100 | [diff] [blame] | 261 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Dexuan Cui | 2f285f4 | 2018-09-18 22:29:50 +0000 | [diff] [blame] | 267 | static int __init hv_pci_init(void) |
| 268 | { |
| 269 | int gen2vm = efi_enabled(EFI_BOOT); |
| 270 | |
| 271 | /* |
| 272 | * For Generation-2 VM, we exit from pci_arch_init() by returning 0. |
| 273 | * The purpose is to suppress the harmless warning: |
| 274 | * "PCI: Fatal: No config space access function found" |
| 275 | */ |
| 276 | if (gen2vm) |
| 277 | return 0; |
| 278 | |
| 279 | /* For Generation-1 VM, we'll proceed in pci_arch_init(). */ |
| 280 | return 1; |
| 281 | } |
| 282 | |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 283 | static int hv_suspend(void) |
| 284 | { |
| 285 | union hv_x64_msr_hypercall_contents hypercall_msr; |
Dexuan Cui | 421f090 | 2020-04-20 19:46:11 -0700 | [diff] [blame] | 286 | int ret; |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 287 | |
Wei Liu | 80f73c9 | 2021-02-03 15:04:26 +0000 | [diff] [blame] | 288 | if (hv_root_partition) |
| 289 | return -EPERM; |
| 290 | |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 291 | /* |
| 292 | * Reset the hypercall page as it is going to be invalidated |
Ingo Molnar | d9f6e12 | 2021-03-18 15:28:01 +0100 | [diff] [blame] | 293 | * across hibernation. Setting hv_hypercall_pg to NULL ensures |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 294 | * that any subsequent hypercall operation fails safely instead of |
| 295 | * crashing due to an access of an invalid page. The hypercall page |
| 296 | * pointer is restored on resume. |
| 297 | */ |
| 298 | hv_hypercall_pg_saved = hv_hypercall_pg; |
| 299 | hv_hypercall_pg = NULL; |
| 300 | |
| 301 | /* Disable the hypercall page in the hypervisor */ |
| 302 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 303 | hypercall_msr.enable = 0; |
| 304 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 305 | |
Dexuan Cui | 421f090 | 2020-04-20 19:46:11 -0700 | [diff] [blame] | 306 | ret = hv_cpu_die(0); |
| 307 | return ret; |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void hv_resume(void) |
| 311 | { |
| 312 | union hv_x64_msr_hypercall_contents hypercall_msr; |
Dexuan Cui | 421f090 | 2020-04-20 19:46:11 -0700 | [diff] [blame] | 313 | int ret; |
| 314 | |
| 315 | ret = hv_cpu_init(0); |
| 316 | WARN_ON(ret); |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 317 | |
| 318 | /* Re-enable the hypercall page */ |
| 319 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 320 | hypercall_msr.enable = 1; |
| 321 | hypercall_msr.guest_physical_address = |
| 322 | vmalloc_to_pfn(hv_hypercall_pg_saved); |
| 323 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 324 | |
| 325 | hv_hypercall_pg = hv_hypercall_pg_saved; |
| 326 | hv_hypercall_pg_saved = NULL; |
Vitaly Kuznetsov | 38dce41 | 2020-05-12 18:01:53 +0200 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * Reenlightenment notifications are disabled by hv_cpu_die(0), |
| 330 | * reenable them here if hv_reenlightenment_cb was previously set. |
| 331 | */ |
| 332 | if (hv_reenlightenment_cb) |
| 333 | set_hv_tscchange_cb(hv_reenlightenment_cb); |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Dexuan Cui | 421f090 | 2020-04-20 19:46:11 -0700 | [diff] [blame] | 336 | /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */ |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 337 | static struct syscore_ops hv_syscore_ops = { |
| 338 | .suspend = hv_suspend, |
| 339 | .resume = hv_resume, |
| 340 | }; |
| 341 | |
Dexuan Cui | fff7b5e | 2021-01-16 14:31:36 -0800 | [diff] [blame] | 342 | static void (* __initdata old_setup_percpu_clockev)(void); |
| 343 | |
| 344 | static void __init hv_stimer_setup_percpu_clockev(void) |
| 345 | { |
| 346 | /* |
| 347 | * Ignore any errors in setting up stimer clockevents |
| 348 | * as we can run with the LAPIC timer as a fallback. |
| 349 | */ |
Michael Kelley | ec866be6 | 2021-03-02 13:38:22 -0800 | [diff] [blame] | 350 | (void)hv_stimer_alloc(false); |
Dexuan Cui | fff7b5e | 2021-01-16 14:31:36 -0800 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * Still register the LAPIC timer, because the direct-mode STIMER is |
| 354 | * not supported by old versions of Hyper-V. This also allows users |
| 355 | * to switch to LAPIC timer via /sys, if they want to. |
| 356 | */ |
| 357 | if (old_setup_percpu_clockev) |
| 358 | old_setup_percpu_clockev(); |
| 359 | } |
| 360 | |
Wei Liu | 99a0f46 | 2021-02-03 15:04:25 +0000 | [diff] [blame] | 361 | static void __init hv_get_partition_id(void) |
| 362 | { |
| 363 | struct hv_get_partition_id *output_page; |
| 364 | u64 status; |
| 365 | unsigned long flags; |
| 366 | |
| 367 | local_irq_save(flags); |
| 368 | output_page = *this_cpu_ptr(hyperv_pcpu_output_arg); |
| 369 | status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page); |
Joseph Salisbury | 753ed9c | 2021-04-16 17:43:03 -0700 | [diff] [blame] | 370 | if (!hv_result_success(status)) { |
Wei Liu | 99a0f46 | 2021-02-03 15:04:25 +0000 | [diff] [blame] | 371 | /* No point in proceeding if this failed */ |
| 372 | pr_err("Failed to get partition ID: %lld\n", status); |
| 373 | BUG(); |
| 374 | } |
| 375 | hv_current_partition_id = output_page->partition_id; |
| 376 | local_irq_restore(flags); |
| 377 | } |
| 378 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 379 | /* |
| 380 | * This function is to be invoked early in the boot sequence after the |
| 381 | * hypervisor has been detected. |
| 382 | * |
| 383 | * 1. Setup the hypercall page. |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 384 | * 2. Register Hyper-V specific clocksource. |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 385 | * 3. Setup Hyper-V specific APIC entry points. |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 386 | */ |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 387 | void __init hyperv_init(void) |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 388 | { |
Sean Christopherson | f3e613e | 2021-11-04 18:22:39 +0000 | [diff] [blame] | 389 | u64 guest_id; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 390 | union hv_x64_msr_hypercall_contents hypercall_msr; |
Michael Kelley | afca4d9 | 2021-07-14 11:34:45 -0700 | [diff] [blame] | 391 | int cpuhp; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 392 | |
Juergen Gross | 03b2a32 | 2017-11-09 14:27:36 +0100 | [diff] [blame] | 393 | if (x86_hyper_type != X86_HYPER_MS_HYPERV) |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 394 | return; |
| 395 | |
Michael Kelley | afca4d9 | 2021-07-14 11:34:45 -0700 | [diff] [blame] | 396 | if (hv_common_init()) |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 397 | return; |
| 398 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 399 | hv_vp_assist_page = kcalloc(num_possible_cpus(), |
| 400 | sizeof(*hv_vp_assist_page), GFP_KERNEL); |
| 401 | if (!hv_vp_assist_page) { |
| 402 | ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; |
Michael Kelley | afca4d9 | 2021-07-14 11:34:45 -0700 | [diff] [blame] | 403 | goto common_free; |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 406 | if (hv_isolation_type_snp()) { |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 407 | hv_ghcb_pg = alloc_percpu(union hv_ghcb *); |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 408 | if (!hv_ghcb_pg) |
| 409 | goto free_vp_assist_page; |
| 410 | } |
| 411 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 412 | cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online", |
| 413 | hv_cpu_init, hv_cpu_die); |
| 414 | if (cpuhp < 0) |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 415 | goto free_ghcb_page; |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 416 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 417 | /* |
| 418 | * Setup the hypercall page and enable hypercalls. |
| 419 | * 1. Register the guest ID |
| 420 | * 2. Enable the hypercall and register the hypercall page |
| 421 | */ |
| 422 | guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0); |
| 423 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 424 | |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 425 | /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */ |
| 426 | hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 427 | |
Christoph Hellwig | 800e26b | 2020-06-25 20:30:40 -0700 | [diff] [blame] | 428 | hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, |
| 429 | VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, |
Christoph Hellwig | a3a66c3 | 2020-07-03 15:15:27 -0700 | [diff] [blame] | 430 | VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, |
| 431 | __builtin_return_address(0)); |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 432 | if (hv_hypercall_pg == NULL) |
| 433 | goto clean_guest_os_id; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 434 | |
| 435 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 436 | hypercall_msr.enable = 1; |
Wei Liu | 80f73c9 | 2021-02-03 15:04:26 +0000 | [diff] [blame] | 437 | |
| 438 | if (hv_root_partition) { |
| 439 | struct page *pg; |
| 440 | void *src, *dst; |
| 441 | |
| 442 | /* |
| 443 | * For the root partition, the hypervisor will set up its |
| 444 | * hypercall page. The hypervisor guarantees it will not show |
| 445 | * up in the root's address space. The root can't change the |
| 446 | * location of the hypercall page. |
| 447 | * |
| 448 | * Order is important here. We must enable the hypercall page |
| 449 | * so it is populated with code, then copy the code to an |
| 450 | * executable page. |
| 451 | */ |
| 452 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 453 | |
| 454 | pg = vmalloc_to_page(hv_hypercall_pg); |
| 455 | dst = kmap(pg); |
| 456 | src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE, |
| 457 | MEMREMAP_WB); |
| 458 | BUG_ON(!(src && dst)); |
| 459 | memcpy(dst, src, HV_HYP_PAGE_SIZE); |
| 460 | memunmap(src); |
| 461 | kunmap(pg); |
| 462 | } else { |
| 463 | hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); |
| 464 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 465 | } |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 466 | |
Michael Kelley | 4df4cb9e9 | 2019-11-13 01:11:49 +0000 | [diff] [blame] | 467 | /* |
Dexuan Cui | fff7b5e | 2021-01-16 14:31:36 -0800 | [diff] [blame] | 468 | * hyperv_init() is called before LAPIC is initialized: see |
| 469 | * apic_intr_mode_init() -> x86_platform.apic_post_init() and |
| 470 | * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER |
| 471 | * depends on LAPIC, so hv_stimer_alloc() should be called from |
| 472 | * x86_init.timers.setup_percpu_clockev. |
Michael Kelley | 4df4cb9e9 | 2019-11-13 01:11:49 +0000 | [diff] [blame] | 473 | */ |
Dexuan Cui | fff7b5e | 2021-01-16 14:31:36 -0800 | [diff] [blame] | 474 | old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev; |
| 475 | x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev; |
Michael Kelley | 4df4cb9e9 | 2019-11-13 01:11:49 +0000 | [diff] [blame] | 476 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 477 | hv_apic_init(); |
| 478 | |
Dexuan Cui | 2f285f4 | 2018-09-18 22:29:50 +0000 | [diff] [blame] | 479 | x86_init.pci.arch_init = hv_pci_init; |
| 480 | |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 481 | register_syscore_ops(&hv_syscore_ops); |
| 482 | |
Dexuan Cui | dfe94d4 | 2020-12-21 22:55:41 -0800 | [diff] [blame] | 483 | hyperv_init_cpuhp = cpuhp; |
Wei Liu | 99a0f46 | 2021-02-03 15:04:25 +0000 | [diff] [blame] | 484 | |
| 485 | if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID) |
| 486 | hv_get_partition_id(); |
| 487 | |
| 488 | BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull); |
| 489 | |
Wei Liu | e39397d | 2021-02-03 15:04:34 +0000 | [diff] [blame] | 490 | #ifdef CONFIG_PCI_MSI |
| 491 | /* |
| 492 | * If we're running as root, we want to create our own PCI MSI domain. |
| 493 | * We can't set this in hv_pci_init because that would be too late. |
| 494 | */ |
| 495 | if (hv_root_partition) |
| 496 | x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain; |
| 497 | #endif |
| 498 | |
Sunil Muthuswamy | 6dc2a77 | 2021-03-23 18:47:16 +0000 | [diff] [blame] | 499 | /* Query the VMs extended capability once, so that it can be cached. */ |
| 500 | hv_query_ext_cap(0); |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 501 | return; |
| 502 | |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 503 | clean_guest_os_id: |
| 504 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 505 | hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 506 | cpuhp_remove_state(cpuhp); |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 507 | free_ghcb_page: |
| 508 | free_percpu(hv_ghcb_pg); |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 509 | free_vp_assist_page: |
| 510 | kfree(hv_vp_assist_page); |
| 511 | hv_vp_assist_page = NULL; |
Michael Kelley | afca4d9 | 2021-07-14 11:34:45 -0700 | [diff] [blame] | 512 | common_free: |
| 513 | hv_common_free(); |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 514 | } |
K. Y. Srinivasan | 6ab42a6 | 2017-01-18 16:45:03 -0700 | [diff] [blame] | 515 | |
| 516 | /* |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 517 | * This routine is called before kexec/kdump, it does the required cleanup. |
| 518 | */ |
| 519 | void hyperv_cleanup(void) |
| 520 | { |
| 521 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 522 | |
Dexuan Cui | 05bd330 | 2020-01-06 14:42:39 -0800 | [diff] [blame] | 523 | unregister_syscore_ops(&hv_syscore_ops); |
| 524 | |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 525 | /* Reset our OS id */ |
| 526 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 527 | hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 528 | |
Kairui Song | 179fb36 | 2019-03-06 19:18:27 +0800 | [diff] [blame] | 529 | /* |
| 530 | * Reset hypercall page reference before reset the page, |
| 531 | * let hypercall operations fail safely rather than |
| 532 | * panic the kernel for using invalid hypercall page |
| 533 | */ |
| 534 | hv_hypercall_pg = NULL; |
| 535 | |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 536 | /* Reset the hypercall page */ |
| 537 | hypercall_msr.as_uint64 = 0; |
| 538 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
Vitaly Kuznetsov | 5647dbf | 2017-01-28 12:37:15 -0700 | [diff] [blame] | 539 | |
| 540 | /* Reset the TSC page */ |
| 541 | hypercall_msr.as_uint64 = 0; |
| 542 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64); |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 543 | } |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 544 | |
Tianyu Lan | f3a99e7 | 2020-04-06 08:53:31 -0700 | [diff] [blame] | 545 | void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die) |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 546 | { |
| 547 | static bool panic_reported; |
K. Y. Srinivasan | 7ed4325 | 2017-10-29 11:33:41 -0700 | [diff] [blame] | 548 | u64 guest_id; |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 549 | |
Tianyu Lan | f3a99e7 | 2020-04-06 08:53:31 -0700 | [diff] [blame] | 550 | if (in_die && !panic_on_oops) |
| 551 | return; |
| 552 | |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 553 | /* |
| 554 | * We prefer to report panic on 'die' chain as we have proper |
| 555 | * registers to report, but if we miss it (e.g. on BUG()) we need |
| 556 | * to report it on 'panic'. |
| 557 | */ |
| 558 | if (panic_reported) |
| 559 | return; |
| 560 | panic_reported = true; |
| 561 | |
K. Y. Srinivasan | 7ed4325 | 2017-10-29 11:33:41 -0700 | [diff] [blame] | 562 | rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 563 | |
| 564 | wrmsrl(HV_X64_MSR_CRASH_P0, err); |
| 565 | wrmsrl(HV_X64_MSR_CRASH_P1, guest_id); |
| 566 | wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip); |
| 567 | wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax); |
| 568 | wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp); |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 569 | |
| 570 | /* |
| 571 | * Let Hyper-V know there is crash data available |
| 572 | */ |
| 573 | wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); |
| 574 | } |
| 575 | EXPORT_SYMBOL_GPL(hyperv_report_panic); |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 576 | |
Michael Kelley | 4a5f3cd | 2017-12-22 11:19:02 -0700 | [diff] [blame] | 577 | bool hv_is_hyperv_initialized(void) |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 578 | { |
| 579 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 580 | |
Michael Kelley | 4a5f3cd | 2017-12-22 11:19:02 -0700 | [diff] [blame] | 581 | /* |
| 582 | * Ensure that we're really on Hyper-V, and not a KVM or Xen |
| 583 | * emulation of Hyper-V |
| 584 | */ |
| 585 | if (x86_hyper_type != X86_HYPER_MS_HYPERV) |
| 586 | return false; |
| 587 | |
| 588 | /* |
| 589 | * Verify that earlier initialization succeeded by checking |
| 590 | * that the hypercall page is setup |
| 591 | */ |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 592 | hypercall_msr.as_uint64 = 0; |
| 593 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 594 | |
Michael Kelley | 4a5f3cd | 2017-12-22 11:19:02 -0700 | [diff] [blame] | 595 | return hypercall_msr.enable; |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 596 | } |
Michael Kelley | 4a5f3cd | 2017-12-22 11:19:02 -0700 | [diff] [blame] | 597 | EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); |