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