0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/efi.h>
0011 #include <linux/types.h>
0012 #include <linux/bitfield.h>
0013 #include <linux/io.h>
0014 #include <asm/apic.h>
0015 #include <asm/desc.h>
0016 #include <asm/sev.h>
0017 #include <asm/hypervisor.h>
0018 #include <asm/hyperv-tlfs.h>
0019 #include <asm/mshyperv.h>
0020 #include <asm/idtentry.h>
0021 #include <linux/kexec.h>
0022 #include <linux/version.h>
0023 #include <linux/vmalloc.h>
0024 #include <linux/mm.h>
0025 #include <linux/hyperv.h>
0026 #include <linux/slab.h>
0027 #include <linux/kernel.h>
0028 #include <linux/cpuhotplug.h>
0029 #include <linux/syscore_ops.h>
0030 #include <clocksource/hyperv_timer.h>
0031 #include <linux/highmem.h>
0032 #include <linux/swiotlb.h>
0033
0034 int hyperv_init_cpuhp;
0035 u64 hv_current_partition_id = ~0ull;
0036 EXPORT_SYMBOL_GPL(hv_current_partition_id);
0037
0038 void *hv_hypercall_pg;
0039 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
0040
0041 union hv_ghcb * __percpu *hv_ghcb_pg;
0042
0043
0044 static void *hv_hypercall_pg_saved;
0045
0046 struct hv_vp_assist_page **hv_vp_assist_page;
0047 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
0048
0049 static int hyperv_init_ghcb(void)
0050 {
0051 u64 ghcb_gpa;
0052 void *ghcb_va;
0053 void **ghcb_base;
0054
0055 if (!hv_isolation_type_snp())
0056 return 0;
0057
0058 if (!hv_ghcb_pg)
0059 return -EINVAL;
0060
0061
0062
0063
0064
0065
0066 rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
0067 ghcb_va = memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB);
0068 if (!ghcb_va)
0069 return -ENOMEM;
0070
0071 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
0072 *ghcb_base = ghcb_va;
0073
0074 return 0;
0075 }
0076
0077 static int hv_cpu_init(unsigned int cpu)
0078 {
0079 union hv_vp_assist_msr_contents msr = { 0 };
0080 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
0081 int ret;
0082
0083 ret = hv_common_cpu_init(cpu);
0084 if (ret)
0085 return ret;
0086
0087 if (!hv_vp_assist_page)
0088 return 0;
0089
0090 if (!*hvp) {
0091 if (hv_root_partition) {
0092
0093
0094
0095
0096 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
0097 *hvp = memremap(msr.pfn <<
0098 HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
0099 PAGE_SIZE, MEMREMAP_WB);
0100 } else {
0101
0102
0103
0104
0105
0106
0107
0108
0109 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
0110 if (*hvp)
0111 msr.pfn = vmalloc_to_pfn(*hvp);
0112 }
0113 WARN_ON(!(*hvp));
0114 if (*hvp) {
0115 msr.enable = 1;
0116 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
0117 }
0118 }
0119
0120 return hyperv_init_ghcb();
0121 }
0122
0123 static void (*hv_reenlightenment_cb)(void);
0124
0125 static void hv_reenlightenment_notify(struct work_struct *dummy)
0126 {
0127 struct hv_tsc_emulation_status emu_status;
0128
0129 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
0130
0131
0132 if (hv_reenlightenment_cb && emu_status.inprogress)
0133 hv_reenlightenment_cb();
0134 }
0135 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
0136
0137 void hyperv_stop_tsc_emulation(void)
0138 {
0139 u64 freq;
0140 struct hv_tsc_emulation_status emu_status;
0141
0142 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
0143 emu_status.inprogress = 0;
0144 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
0145
0146 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
0147 tsc_khz = div64_u64(freq, 1000);
0148 }
0149 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
0150
0151 static inline bool hv_reenlightenment_available(void)
0152 {
0153
0154
0155
0156
0157 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
0158 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
0159 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
0160 }
0161
0162 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
0163 {
0164 ack_APIC_irq();
0165 inc_irq_stat(irq_hv_reenlightenment_count);
0166 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
0167 }
0168
0169 void set_hv_tscchange_cb(void (*cb)(void))
0170 {
0171 struct hv_reenlightenment_control re_ctrl = {
0172 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
0173 .enabled = 1,
0174 };
0175 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
0176
0177 if (!hv_reenlightenment_available()) {
0178 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
0179 return;
0180 }
0181
0182 if (!hv_vp_index)
0183 return;
0184
0185 hv_reenlightenment_cb = cb;
0186
0187
0188 wmb();
0189
0190 re_ctrl.target_vp = hv_vp_index[get_cpu()];
0191
0192 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
0193 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
0194
0195 put_cpu();
0196 }
0197 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
0198
0199 void clear_hv_tscchange_cb(void)
0200 {
0201 struct hv_reenlightenment_control re_ctrl;
0202
0203 if (!hv_reenlightenment_available())
0204 return;
0205
0206 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
0207 re_ctrl.enabled = 0;
0208 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
0209
0210 hv_reenlightenment_cb = NULL;
0211 }
0212 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
0213
0214 static int hv_cpu_die(unsigned int cpu)
0215 {
0216 struct hv_reenlightenment_control re_ctrl;
0217 unsigned int new_cpu;
0218 void **ghcb_va;
0219
0220 if (hv_ghcb_pg) {
0221 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
0222 if (*ghcb_va)
0223 memunmap(*ghcb_va);
0224 *ghcb_va = NULL;
0225 }
0226
0227 hv_common_cpu_die(cpu);
0228
0229 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
0230 union hv_vp_assist_msr_contents msr = { 0 };
0231 if (hv_root_partition) {
0232
0233
0234
0235
0236
0237
0238 memunmap(hv_vp_assist_page[cpu]);
0239 hv_vp_assist_page[cpu] = NULL;
0240 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
0241 msr.enable = 0;
0242 }
0243 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
0244 }
0245
0246 if (hv_reenlightenment_cb == NULL)
0247 return 0;
0248
0249 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
0250 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
0251
0252
0253
0254
0255
0256 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
0257
0258 if (new_cpu < nr_cpu_ids)
0259 re_ctrl.target_vp = hv_vp_index[new_cpu];
0260 else
0261 re_ctrl.enabled = 0;
0262
0263 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
0264 }
0265
0266 return 0;
0267 }
0268
0269 static int __init hv_pci_init(void)
0270 {
0271 int gen2vm = efi_enabled(EFI_BOOT);
0272
0273
0274
0275
0276
0277
0278 if (gen2vm)
0279 return 0;
0280
0281
0282 return 1;
0283 }
0284
0285 static int hv_suspend(void)
0286 {
0287 union hv_x64_msr_hypercall_contents hypercall_msr;
0288 int ret;
0289
0290 if (hv_root_partition)
0291 return -EPERM;
0292
0293
0294
0295
0296
0297
0298
0299
0300 hv_hypercall_pg_saved = hv_hypercall_pg;
0301 hv_hypercall_pg = NULL;
0302
0303
0304 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0305 hypercall_msr.enable = 0;
0306 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0307
0308 ret = hv_cpu_die(0);
0309 return ret;
0310 }
0311
0312 static void hv_resume(void)
0313 {
0314 union hv_x64_msr_hypercall_contents hypercall_msr;
0315 int ret;
0316
0317 ret = hv_cpu_init(0);
0318 WARN_ON(ret);
0319
0320
0321 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0322 hypercall_msr.enable = 1;
0323 hypercall_msr.guest_physical_address =
0324 vmalloc_to_pfn(hv_hypercall_pg_saved);
0325 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0326
0327 hv_hypercall_pg = hv_hypercall_pg_saved;
0328 hv_hypercall_pg_saved = NULL;
0329
0330
0331
0332
0333
0334 if (hv_reenlightenment_cb)
0335 set_hv_tscchange_cb(hv_reenlightenment_cb);
0336 }
0337
0338
0339 static struct syscore_ops hv_syscore_ops = {
0340 .suspend = hv_suspend,
0341 .resume = hv_resume,
0342 };
0343
0344 static void (* __initdata old_setup_percpu_clockev)(void);
0345
0346 static void __init hv_stimer_setup_percpu_clockev(void)
0347 {
0348
0349
0350
0351
0352 (void)hv_stimer_alloc(false);
0353
0354
0355
0356
0357
0358
0359 if (old_setup_percpu_clockev)
0360 old_setup_percpu_clockev();
0361 }
0362
0363 static void __init hv_get_partition_id(void)
0364 {
0365 struct hv_get_partition_id *output_page;
0366 u64 status;
0367 unsigned long flags;
0368
0369 local_irq_save(flags);
0370 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
0371 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
0372 if (!hv_result_success(status)) {
0373
0374 pr_err("Failed to get partition ID: %lld\n", status);
0375 BUG();
0376 }
0377 hv_current_partition_id = output_page->partition_id;
0378 local_irq_restore(flags);
0379 }
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389 void __init hyperv_init(void)
0390 {
0391 u64 guest_id;
0392 union hv_x64_msr_hypercall_contents hypercall_msr;
0393 int cpuhp;
0394
0395 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
0396 return;
0397
0398 if (hv_common_init())
0399 return;
0400
0401 hv_vp_assist_page = kcalloc(num_possible_cpus(),
0402 sizeof(*hv_vp_assist_page), GFP_KERNEL);
0403 if (!hv_vp_assist_page) {
0404 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
0405 goto common_free;
0406 }
0407
0408 if (hv_isolation_type_snp()) {
0409
0410 if (!hv_ghcb_negotiate_protocol())
0411 hv_ghcb_terminate(SEV_TERM_SET_GEN,
0412 GHCB_SEV_ES_PROT_UNSUPPORTED);
0413
0414 hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
0415 if (!hv_ghcb_pg)
0416 goto free_vp_assist_page;
0417 }
0418
0419 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
0420 hv_cpu_init, hv_cpu_die);
0421 if (cpuhp < 0)
0422 goto free_ghcb_page;
0423
0424
0425
0426
0427
0428
0429 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
0430 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
0431
0432
0433 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
0434
0435 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
0436 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
0437 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
0438 __builtin_return_address(0));
0439 if (hv_hypercall_pg == NULL)
0440 goto clean_guest_os_id;
0441
0442 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0443 hypercall_msr.enable = 1;
0444
0445 if (hv_root_partition) {
0446 struct page *pg;
0447 void *src, *dst;
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0460
0461 pg = vmalloc_to_page(hv_hypercall_pg);
0462 dst = kmap(pg);
0463 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
0464 MEMREMAP_WB);
0465 BUG_ON(!(src && dst));
0466 memcpy(dst, src, HV_HYP_PAGE_SIZE);
0467 memunmap(src);
0468 kunmap(pg);
0469 } else {
0470 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
0471 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0472 }
0473
0474
0475
0476
0477
0478
0479
0480
0481 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
0482 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
0483
0484 hv_apic_init();
0485
0486 x86_init.pci.arch_init = hv_pci_init;
0487
0488 register_syscore_ops(&hv_syscore_ops);
0489
0490 hyperv_init_cpuhp = cpuhp;
0491
0492 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
0493 hv_get_partition_id();
0494
0495 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
0496
0497 #ifdef CONFIG_PCI_MSI
0498
0499
0500
0501
0502 if (hv_root_partition)
0503 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
0504 #endif
0505
0506
0507 hv_query_ext_cap(0);
0508
0509 #ifdef CONFIG_SWIOTLB
0510
0511
0512
0513
0514
0515 if (hv_is_isolation_supported())
0516 swiotlb_update_mem_attributes();
0517 #endif
0518
0519 return;
0520
0521 clean_guest_os_id:
0522 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
0523 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
0524 cpuhp_remove_state(cpuhp);
0525 free_ghcb_page:
0526 free_percpu(hv_ghcb_pg);
0527 free_vp_assist_page:
0528 kfree(hv_vp_assist_page);
0529 hv_vp_assist_page = NULL;
0530 common_free:
0531 hv_common_free();
0532 }
0533
0534
0535
0536
0537 void hyperv_cleanup(void)
0538 {
0539 union hv_x64_msr_hypercall_contents hypercall_msr;
0540
0541 unregister_syscore_ops(&hv_syscore_ops);
0542
0543
0544 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
0545 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
0546
0547
0548
0549
0550
0551
0552 hv_hypercall_pg = NULL;
0553
0554
0555 hypercall_msr.as_uint64 = 0;
0556 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0557
0558
0559 hypercall_msr.as_uint64 = 0;
0560 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
0561 }
0562
0563 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
0564 {
0565 static bool panic_reported;
0566 u64 guest_id;
0567
0568 if (in_die && !panic_on_oops)
0569 return;
0570
0571
0572
0573
0574
0575
0576 if (panic_reported)
0577 return;
0578 panic_reported = true;
0579
0580 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
0581
0582 wrmsrl(HV_X64_MSR_CRASH_P0, err);
0583 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
0584 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
0585 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
0586 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
0587
0588
0589
0590
0591 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
0592 }
0593 EXPORT_SYMBOL_GPL(hyperv_report_panic);
0594
0595 bool hv_is_hyperv_initialized(void)
0596 {
0597 union hv_x64_msr_hypercall_contents hypercall_msr;
0598
0599
0600
0601
0602
0603 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
0604 return false;
0605
0606
0607
0608
0609
0610 hypercall_msr.as_uint64 = 0;
0611 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
0612
0613 return hypercall_msr.enable;
0614 }
0615 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);