linux/arch/x86/hyperv/hv_init.c
<<
>>
Prefs
   1/*
   2 * X86 specific Hyper-V initialization code.
   3 *
   4 * Copyright (C) 2016, Microsoft, Inc.
   5 *
   6 * Author : K. Y. Srinivasan <kys@microsoft.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of the GNU General Public License version 2 as published
  10 * by the Free Software Foundation.
  11 *
  12 * This program is distributed in the hope that it will be useful, but
  13 * WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  15 * NON INFRINGEMENT.  See the GNU General Public License for more
  16 * details.
  17 *
  18 */
  19
  20#include <linux/types.h>
  21#include <asm/hypervisor.h>
  22#include <asm/hyperv.h>
  23#include <asm/mshyperv.h>
  24#include <asm/fixmap.h>
  25#include <linux/version.h>
  26#include <linux/vmalloc.h>
  27#include <linux/mm.h>
  28#include <linux/clockchips.h>
  29#include <linux/hyperv.h>
  30#include <linux/slab.h>
  31#include <linux/cpu.h>
  32#include <linux/kaiser.h>
  33
  34#ifdef CONFIG_HYPERV_TSCPAGE
  35
  36static struct ms_hyperv_tsc_page *tsc_pg;
  37
  38struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
  39{
  40        return tsc_pg;
  41}
  42
  43static u64 read_hv_clock_tsc(struct clocksource *arg)
  44{
  45        u64 current_tick = hv_read_tsc_page(tsc_pg);
  46
  47        if (current_tick == U64_MAX)
  48                rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  49
  50        return current_tick;
  51}
  52
  53static struct clocksource hyperv_cs_tsc = {
  54                .name           = "hyperv_clocksource_tsc_page",
  55                .rating         = 400,
  56                .read           = read_hv_clock_tsc,
  57                .mask           = CLOCKSOURCE_MASK(64),
  58                .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
  59};
  60#endif
  61
  62static u64 read_hv_clock_msr(struct clocksource *arg)
  63{
  64        u64 current_tick;
  65        /*
  66         * Read the partition counter to get the current tick count. This count
  67         * is set to 0 when the partition is created and is incremented in
  68         * 100 nanosecond units.
  69         */
  70        rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
  71        return current_tick;
  72}
  73
  74static struct clocksource hyperv_cs_msr = {
  75        .name           = "hyperv_clocksource_msr",
  76        .rating         = 400,
  77        .read           = read_hv_clock_msr,
  78        .mask           = CLOCKSOURCE_MASK(64),
  79        .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
  80};
  81
  82void *hv_hypercall_pg;
  83EXPORT_SYMBOL_GPL(hv_hypercall_pg);
  84struct clocksource *hyperv_cs;
  85EXPORT_SYMBOL_GPL(hyperv_cs);
  86
  87u32 *hv_vp_index;
  88EXPORT_SYMBOL_GPL(hv_vp_index);
  89
  90u32 hv_max_vp_index;
  91EXPORT_SYMBOL_GPL(hv_max_vp_index);
  92
  93static void hv_cpu_init(void *arg)
  94{
  95        u64 msr_vp_index;
  96
  97        hv_get_vp_index(msr_vp_index);
  98
  99        hv_vp_index[smp_processor_id()] = msr_vp_index;
 100
 101        if (msr_vp_index > hv_max_vp_index)
 102                hv_max_vp_index = msr_vp_index;
 103}
 104
 105/* RHEL-only: old CPUHP infrastructure */
 106static int hv_newcpu_cb(struct notifier_block *nfb,
 107                        unsigned long action, void *hcpu)
 108{
 109        switch (action & ~CPU_TASKS_FROZEN) {
 110        case CPU_STARTING:
 111                hv_cpu_init(hcpu);
 112                break;
 113        default:
 114                break;
 115        }
 116
 117        return NOTIFY_OK;
 118}
 119
 120static struct notifier_block hv_newcpu_notifier __refdata = {
 121       .notifier_call = hv_newcpu_cb,
 122       .priority = INT_MAX,
 123};
 124
 125/*
 126 * This function is to be invoked early in the boot sequence after the
 127 * hypervisor has been detected.
 128 *
 129 * 1. Setup the hypercall page.
 130 * 2. Register Hyper-V specific clocksource.
 131 */
 132void hyperv_init(void)
 133{
 134        u64 guest_id;
 135        union hv_x64_msr_hypercall_contents hypercall_msr;
 136        int  i;
 137
 138        if (x86_hyper != &x86_hyper_ms_hyperv)
 139                return;
 140
 141        /* Allocate percpu VP index */
 142        hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
 143                                    GFP_KERNEL);
 144        if (!hv_vp_index)
 145                return;
 146
 147        for (i = 0; i < num_possible_cpus(); i++)
 148                hv_vp_index[i] = VP_INVAL;
 149
 150        cpu_notifier_register_begin();
 151        on_each_cpu(hv_cpu_init, NULL, 1);
 152        __register_hotcpu_notifier(&hv_newcpu_notifier);
 153        cpu_notifier_register_done();
 154
 155        /*
 156         * Setup the hypercall page and enable hypercalls.
 157         * 1. Register the guest ID
 158         * 2. Enable the hypercall and register the hypercall page
 159         */
 160        guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
 161        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 162
 163        hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
 164        if (hv_hypercall_pg == NULL) {
 165                wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 166                goto free_vp_index;
 167        }
 168
 169        rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 170        hypercall_msr.enable = 1;
 171        hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
 172        wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 173
 174        hyper_alloc_mmu();
 175
 176        /*
 177         * Register Hyper-V specific clocksource.
 178         */
 179#ifdef CONFIG_HYPERV_TSCPAGE
 180        if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
 181                union hv_x64_msr_hypercall_contents tsc_msr;
 182
 183                tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
 184                if (!tsc_pg)
 185                        goto register_msr_cs;
 186
 187                hyperv_cs = &hyperv_cs_tsc;
 188
 189                rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
 190
 191                tsc_msr.enable = 1;
 192                tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
 193
 194                wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
 195
 196                __set_fixmap(HVCLOCK_TSC_PAGE,
 197                             tsc_msr.guest_physical_address << PAGE_SHIFT,
 198                             PAGE_KERNEL_VVAR);
 199                kaiser_add_mapping(__fix_to_virt(HVCLOCK_TSC_PAGE), PAGE_SIZE,
 200                                   __PAGE_KERNEL_VVAR | _PAGE_GLOBAL);
 201                /* set fixmap before switching vclock mode */
 202                wmb();
 203                hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
 204
 205                clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
 206                return;
 207        }
 208register_msr_cs:
 209#endif
 210        /*
 211         * For 32 bit guests just use the MSR based mechanism for reading
 212         * the partition counter.
 213         */
 214
 215        hyperv_cs = &hyperv_cs_msr;
 216        if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
 217                clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
 218
 219        return;
 220
 221free_vp_index:
 222        kfree(hv_vp_index);
 223        hv_vp_index = NULL;
 224}
 225
 226/*
 227 * This routine is called before kexec/kdump, it does the required cleanup.
 228 */
 229void hyperv_cleanup(void)
 230{
 231        union hv_x64_msr_hypercall_contents hypercall_msr;
 232
 233        /* Reset our OS id */
 234        wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 235
 236        /* Reset the hypercall page */
 237        hypercall_msr.as_uint64 = 0;
 238        wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 239
 240        /* Reset the TSC page */
 241        hypercall_msr.as_uint64 = 0;
 242        wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
 243}
 244EXPORT_SYMBOL_GPL(hyperv_cleanup);
 245
 246void hyperv_report_panic(struct pt_regs *regs, long err)
 247{
 248        static bool panic_reported;
 249        u64 guest_id;
 250
 251        /*
 252         * We prefer to report panic on 'die' chain as we have proper
 253         * registers to report, but if we miss it (e.g. on BUG()) we need
 254         * to report it on 'panic'.
 255         */
 256        if (panic_reported)
 257                return;
 258        panic_reported = true;
 259
 260        rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 261
 262        wrmsrl(HV_X64_MSR_CRASH_P0, err);
 263        wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
 264        wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
 265        wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
 266        wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
 267
 268        /*
 269         * Let Hyper-V know there is crash data available
 270         */
 271        wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
 272}
 273EXPORT_SYMBOL_GPL(hyperv_report_panic);
 274
 275bool hv_is_hyperv_initialized(void)
 276{
 277        union hv_x64_msr_hypercall_contents hypercall_msr;
 278
 279        /*
 280         * Ensure that we're really on Hyper-V, and not a KVM or Xen
 281         * emulation of Hyper-V
 282         */
 283        if (x86_hyper != &x86_hyper_ms_hyperv)
 284                return false;
 285
 286        /*
 287         * Verify that earlier initialization succeeded by checking
 288         * that the hypercall page is setup
 289         */
 290        hypercall_msr.as_uint64 = 0;
 291        rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 292
 293        return hypercall_msr.enable;
 294}
 295EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
 296