cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

hv_common.c (8389B)


      1// SPDX-License-Identifier: GPL-2.0
      2
      3/*
      4 * Architecture neutral utility routines for interacting with
      5 * Hyper-V. This file is specifically for code that must be
      6 * built-in to the kernel image when CONFIG_HYPERV is set
      7 * (vs. being in a module) because it is called from architecture
      8 * specific code under arch/.
      9 *
     10 * Copyright (C) 2021, Microsoft, Inc.
     11 *
     12 * Author : Michael Kelley <mikelley@microsoft.com>
     13 */
     14
     15#include <linux/types.h>
     16#include <linux/acpi.h>
     17#include <linux/export.h>
     18#include <linux/bitfield.h>
     19#include <linux/cpumask.h>
     20#include <linux/panic_notifier.h>
     21#include <linux/ptrace.h>
     22#include <linux/slab.h>
     23#include <linux/dma-map-ops.h>
     24#include <asm/hyperv-tlfs.h>
     25#include <asm/mshyperv.h>
     26
     27/*
     28 * hv_root_partition and ms_hyperv are defined here with other Hyper-V
     29 * specific globals so they are shared across all architectures and are
     30 * built only when CONFIG_HYPERV is defined.  But on x86,
     31 * ms_hyperv_init_platform() is built even when CONFIG_HYPERV is not
     32 * defined, and it uses these two variables.  So mark them as __weak
     33 * here, allowing for an overriding definition in the module containing
     34 * ms_hyperv_init_platform().
     35 */
     36bool __weak hv_root_partition;
     37EXPORT_SYMBOL_GPL(hv_root_partition);
     38
     39struct ms_hyperv_info __weak ms_hyperv;
     40EXPORT_SYMBOL_GPL(ms_hyperv);
     41
     42u32 *hv_vp_index;
     43EXPORT_SYMBOL_GPL(hv_vp_index);
     44
     45u32 hv_max_vp_index;
     46EXPORT_SYMBOL_GPL(hv_max_vp_index);
     47
     48void * __percpu *hyperv_pcpu_input_arg;
     49EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
     50
     51void * __percpu *hyperv_pcpu_output_arg;
     52EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg);
     53
     54/*
     55 * Hyper-V specific initialization and shutdown code that is
     56 * common across all architectures.  Called from architecture
     57 * specific initialization functions.
     58 */
     59
     60void __init hv_common_free(void)
     61{
     62	kfree(hv_vp_index);
     63	hv_vp_index = NULL;
     64
     65	free_percpu(hyperv_pcpu_output_arg);
     66	hyperv_pcpu_output_arg = NULL;
     67
     68	free_percpu(hyperv_pcpu_input_arg);
     69	hyperv_pcpu_input_arg = NULL;
     70}
     71
     72int __init hv_common_init(void)
     73{
     74	int i;
     75
     76	/*
     77	 * Hyper-V expects to get crash register data or kmsg when
     78	 * crash enlightment is available and system crashes. Set
     79	 * crash_kexec_post_notifiers to be true to make sure that
     80	 * calling crash enlightment interface before running kdump
     81	 * kernel.
     82	 */
     83	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
     84		crash_kexec_post_notifiers = true;
     85		pr_info("Hyper-V: enabling crash_kexec_post_notifiers\n");
     86	}
     87
     88	/*
     89	 * Allocate the per-CPU state for the hypercall input arg.
     90	 * If this allocation fails, we will not be able to setup
     91	 * (per-CPU) hypercall input page and thus this failure is
     92	 * fatal on Hyper-V.
     93	 */
     94	hyperv_pcpu_input_arg = alloc_percpu(void  *);
     95	BUG_ON(!hyperv_pcpu_input_arg);
     96
     97	/* Allocate the per-CPU state for output arg for root */
     98	if (hv_root_partition) {
     99		hyperv_pcpu_output_arg = alloc_percpu(void *);
    100		BUG_ON(!hyperv_pcpu_output_arg);
    101	}
    102
    103	hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
    104				    GFP_KERNEL);
    105	if (!hv_vp_index) {
    106		hv_common_free();
    107		return -ENOMEM;
    108	}
    109
    110	for (i = 0; i < num_possible_cpus(); i++)
    111		hv_vp_index[i] = VP_INVAL;
    112
    113	return 0;
    114}
    115
    116/*
    117 * Hyper-V specific initialization and die code for
    118 * individual CPUs that is common across all architectures.
    119 * Called by the CPU hotplug mechanism.
    120 */
    121
    122int hv_common_cpu_init(unsigned int cpu)
    123{
    124	void **inputarg, **outputarg;
    125	u64 msr_vp_index;
    126	gfp_t flags;
    127	int pgcount = hv_root_partition ? 2 : 1;
    128
    129	/* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
    130	flags = irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL;
    131
    132	inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
    133	*inputarg = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags);
    134	if (!(*inputarg))
    135		return -ENOMEM;
    136
    137	if (hv_root_partition) {
    138		outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
    139		*outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE;
    140	}
    141
    142	msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);
    143
    144	hv_vp_index[cpu] = msr_vp_index;
    145
    146	if (msr_vp_index > hv_max_vp_index)
    147		hv_max_vp_index = msr_vp_index;
    148
    149	return 0;
    150}
    151
    152int hv_common_cpu_die(unsigned int cpu)
    153{
    154	unsigned long flags;
    155	void **inputarg, **outputarg;
    156	void *mem;
    157
    158	local_irq_save(flags);
    159
    160	inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
    161	mem = *inputarg;
    162	*inputarg = NULL;
    163
    164	if (hv_root_partition) {
    165		outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
    166		*outputarg = NULL;
    167	}
    168
    169	local_irq_restore(flags);
    170
    171	kfree(mem);
    172
    173	return 0;
    174}
    175
    176/* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */
    177bool hv_query_ext_cap(u64 cap_query)
    178{
    179	/*
    180	 * The address of the 'hv_extended_cap' variable will be used as an
    181	 * output parameter to the hypercall below and so it should be
    182	 * compatible with 'virt_to_phys'. Which means, it's address should be
    183	 * directly mapped. Use 'static' to keep it compatible; stack variables
    184	 * can be virtually mapped, making them incompatible with
    185	 * 'virt_to_phys'.
    186	 * Hypercall input/output addresses should also be 8-byte aligned.
    187	 */
    188	static u64 hv_extended_cap __aligned(8);
    189	static bool hv_extended_cap_queried;
    190	u64 status;
    191
    192	/*
    193	 * Querying extended capabilities is an extended hypercall. Check if the
    194	 * partition supports extended hypercall, first.
    195	 */
    196	if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS))
    197		return false;
    198
    199	/* Extended capabilities do not change at runtime. */
    200	if (hv_extended_cap_queried)
    201		return hv_extended_cap & cap_query;
    202
    203	status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL,
    204				 &hv_extended_cap);
    205
    206	/*
    207	 * The query extended capabilities hypercall should not fail under
    208	 * any normal circumstances. Avoid repeatedly making the hypercall, on
    209	 * error.
    210	 */
    211	hv_extended_cap_queried = true;
    212	if (!hv_result_success(status)) {
    213		pr_err("Hyper-V: Extended query capabilities hypercall failed 0x%llx\n",
    214		       status);
    215		return false;
    216	}
    217
    218	return hv_extended_cap & cap_query;
    219}
    220EXPORT_SYMBOL_GPL(hv_query_ext_cap);
    221
    222void hv_setup_dma_ops(struct device *dev, bool coherent)
    223{
    224	/*
    225	 * Hyper-V does not offer a vIOMMU in the guest
    226	 * VM, so pass 0/NULL for the IOMMU settings
    227	 */
    228	arch_setup_dma_ops(dev, 0, 0, NULL, coherent);
    229}
    230EXPORT_SYMBOL_GPL(hv_setup_dma_ops);
    231
    232bool hv_is_hibernation_supported(void)
    233{
    234	return !hv_root_partition && acpi_sleep_state_supported(ACPI_STATE_S4);
    235}
    236EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
    237
    238/*
    239 * Default function to read the Hyper-V reference counter, independent
    240 * of whether Hyper-V enlightened clocks/timers are being used. But on
    241 * architectures where it is used, Hyper-V enlightenment code in
    242 * hyperv_timer.c may override this function.
    243 */
    244static u64 __hv_read_ref_counter(void)
    245{
    246	return hv_get_register(HV_REGISTER_TIME_REF_COUNT);
    247}
    248
    249u64 (*hv_read_reference_counter)(void) = __hv_read_ref_counter;
    250EXPORT_SYMBOL_GPL(hv_read_reference_counter);
    251
    252/* These __weak functions provide default "no-op" behavior and
    253 * may be overridden by architecture specific versions. Architectures
    254 * for which the default "no-op" behavior is sufficient can leave
    255 * them unimplemented and not be cluttered with a bunch of stub
    256 * functions in arch-specific code.
    257 */
    258
    259bool __weak hv_is_isolation_supported(void)
    260{
    261	return false;
    262}
    263EXPORT_SYMBOL_GPL(hv_is_isolation_supported);
    264
    265bool __weak hv_isolation_type_snp(void)
    266{
    267	return false;
    268}
    269EXPORT_SYMBOL_GPL(hv_isolation_type_snp);
    270
    271void __weak hv_setup_vmbus_handler(void (*handler)(void))
    272{
    273}
    274EXPORT_SYMBOL_GPL(hv_setup_vmbus_handler);
    275
    276void __weak hv_remove_vmbus_handler(void)
    277{
    278}
    279EXPORT_SYMBOL_GPL(hv_remove_vmbus_handler);
    280
    281void __weak hv_setup_kexec_handler(void (*handler)(void))
    282{
    283}
    284EXPORT_SYMBOL_GPL(hv_setup_kexec_handler);
    285
    286void __weak hv_remove_kexec_handler(void)
    287{
    288}
    289EXPORT_SYMBOL_GPL(hv_remove_kexec_handler);
    290
    291void __weak hv_setup_crash_handler(void (*handler)(struct pt_regs *regs))
    292{
    293}
    294EXPORT_SYMBOL_GPL(hv_setup_crash_handler);
    295
    296void __weak hv_remove_crash_handler(void)
    297{
    298}
    299EXPORT_SYMBOL_GPL(hv_remove_crash_handler);
    300
    301void __weak hyperv_cleanup(void)
    302{
    303}
    304EXPORT_SYMBOL_GPL(hyperv_cleanup);
    305
    306u64 __weak hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
    307{
    308	return HV_STATUS_INVALID_PARAMETER;
    309}
    310EXPORT_SYMBOL_GPL(hv_ghcb_hypercall);
    311
    312void __weak *hv_map_memory(void *addr, unsigned long size)
    313{
    314	return NULL;
    315}
    316EXPORT_SYMBOL_GPL(hv_map_memory);
    317
    318void __weak hv_unmap_memory(void *addr)
    319{
    320}
    321EXPORT_SYMBOL_GPL(hv_unmap_memory);