linux/arch/x86/xen/enlighten.c
<<
>>
Prefs
   1#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
   2#include <linux/bootmem.h>
   3#endif
   4#include <linux/cpu.h>
   5#include <linux/kexec.h>
   6#include <linux/slab.h>
   7
   8#include <xen/features.h>
   9#include <xen/page.h>
  10#include <xen/interface/memory.h>
  11
  12#include <asm/xen/hypercall.h>
  13#include <asm/xen/hypervisor.h>
  14#include <asm/cpu.h>
  15#include <asm/e820/api.h> 
  16
  17#include "xen-ops.h"
  18#include "smp.h"
  19#include "pmu.h"
  20
  21EXPORT_SYMBOL_GPL(hypercall_page);
  22
  23/*
  24 * Pointer to the xen_vcpu_info structure or
  25 * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
  26 * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
  27 * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point
  28 * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to
  29 * acknowledge pending events.
  30 * Also more subtly it is used by the patched version of irq enable/disable
  31 * e.g. xen_irq_enable_direct and xen_iret in PV mode.
  32 *
  33 * The desire to be able to do those mask/unmask operations as a single
  34 * instruction by using the per-cpu offset held in %gs is the real reason
  35 * vcpu info is in a per-cpu pointer and the original reason for this
  36 * hypercall.
  37 *
  38 */
  39DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  40
  41/*
  42 * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info
  43 * hypercall. This can be used both in PV and PVHVM mode. The structure
  44 * overrides the default per_cpu(xen_vcpu, cpu) value.
  45 */
  46DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  47
  48/* Linux <-> Xen vCPU id mapping */
  49DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
  50EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
  51
  52enum xen_domain_type xen_domain_type = XEN_NATIVE;
  53EXPORT_SYMBOL_GPL(xen_domain_type);
  54
  55unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
  56EXPORT_SYMBOL(machine_to_phys_mapping);
  57unsigned long  machine_to_phys_nr;
  58EXPORT_SYMBOL(machine_to_phys_nr);
  59
  60struct start_info *xen_start_info;
  61EXPORT_SYMBOL_GPL(xen_start_info);
  62
  63struct shared_info xen_dummy_shared_info;
  64
  65__read_mostly int xen_have_vector_callback;
  66EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  67
  68/*
  69 * NB: needs to live in .data because it's used by xen_prepare_pvh which runs
  70 * before clearing the bss.
  71 */
  72uint32_t xen_start_flags __attribute__((section(".data"))) = 0;
  73EXPORT_SYMBOL(xen_start_flags);
  74
  75/*
  76 * Point at some empty memory to start with. We map the real shared_info
  77 * page as soon as fixmap is up and running.
  78 */
  79struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
  80
  81/*
  82 * Flag to determine whether vcpu info placement is available on all
  83 * VCPUs.  We assume it is to start with, and then set it to zero on
  84 * the first failure.  This is because it can succeed on some VCPUs
  85 * and not others, since it can involve hypervisor memory allocation,
  86 * or because the guest failed to guarantee all the appropriate
  87 * constraints on all VCPUs (ie buffer can't cross a page boundary).
  88 *
  89 * Note that any particular CPU may be using a placed vcpu structure,
  90 * but we can only optimise if the all are.
  91 *
  92 * 0: not available, 1: available
  93 */
  94int xen_have_vcpu_info_placement = 1;
  95
  96static int xen_cpu_up_online(unsigned int cpu)
  97{
  98        xen_init_lock_cpu(cpu);
  99        return 0;
 100}
 101
 102int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
 103                    int (*cpu_dead_cb)(unsigned int))
 104{
 105        int rc;
 106
 107        rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
 108                                       "x86/xen/guest:prepare",
 109                                       cpu_up_prepare_cb, cpu_dead_cb);
 110        if (rc >= 0) {
 111                rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
 112                                               "x86/xen/guest:online",
 113                                               xen_cpu_up_online, NULL);
 114                if (rc < 0)
 115                        cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
 116        }
 117
 118        return rc >= 0 ? 0 : rc;
 119}
 120
 121static int xen_vcpu_setup_restore(int cpu)
 122{
 123        int rc = 0;
 124
 125        /* Any per_cpu(xen_vcpu) is stale, so reset it */
 126        xen_vcpu_info_reset(cpu);
 127
 128        /*
 129         * For PVH and PVHVM, setup online VCPUs only. The rest will
 130         * be handled by hotplug.
 131         */
 132        if (xen_pv_domain() ||
 133            (xen_hvm_domain() && cpu_online(cpu))) {
 134                rc = xen_vcpu_setup(cpu);
 135        }
 136
 137        return rc;
 138}
 139
 140/*
 141 * On restore, set the vcpu placement up again.
 142 * If it fails, then we're in a bad state, since
 143 * we can't back out from using it...
 144 */
 145void xen_vcpu_restore(void)
 146{
 147        int cpu, rc;
 148
 149        for_each_possible_cpu(cpu) {
 150                bool other_cpu = (cpu != smp_processor_id());
 151                bool is_up;
 152
 153                if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
 154                        continue;
 155
 156                /* Only Xen 4.5 and higher support this. */
 157                is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
 158                                           xen_vcpu_nr(cpu), NULL) > 0;
 159
 160                if (other_cpu && is_up &&
 161                    HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
 162                        BUG();
 163
 164                if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
 165                        xen_setup_runstate_info(cpu);
 166
 167                rc = xen_vcpu_setup_restore(cpu);
 168                if (rc)
 169                        pr_emerg_once("vcpu restore failed for cpu=%d err=%d. "
 170                                        "System will hang.\n", cpu, rc);
 171                /*
 172                 * In case xen_vcpu_setup_restore() fails, do not bring up the
 173                 * VCPU. This helps us avoid the resulting OOPS when the VCPU
 174                 * accesses pvclock_vcpu_time via xen_vcpu (which is NULL.)
 175                 * Note that this does not improve the situation much -- now the
 176                 * VM hangs instead of OOPSing -- with the VCPUs that did not
 177                 * fail, spinning in stop_machine(), waiting for the failed
 178                 * VCPUs to come up.
 179                 */
 180                if (other_cpu && is_up && (rc == 0) &&
 181                    HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
 182                        BUG();
 183        }
 184}
 185
 186void xen_vcpu_info_reset(int cpu)
 187{
 188        if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
 189                per_cpu(xen_vcpu, cpu) =
 190                        &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
 191        } else {
 192                /* Set to NULL so that if somebody accesses it we get an OOPS */
 193                per_cpu(xen_vcpu, cpu) = NULL;
 194        }
 195}
 196
 197int xen_vcpu_setup(int cpu)
 198{
 199        struct vcpu_register_vcpu_info info;
 200        int err;
 201        struct vcpu_info *vcpup;
 202
 203        BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
 204
 205        /*
 206         * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu)
 207         * and at restore (xen_vcpu_restore). Also called for hotplugged
 208         * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm).
 209         * However, the hypercall can only be done once (see below) so if a VCPU
 210         * is offlined and comes back online then let's not redo the hypercall.
 211         *
 212         * For PV it is called during restore (xen_vcpu_restore) and bootup
 213         * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
 214         * use this function.
 215         */
 216        if (xen_hvm_domain()) {
 217                if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
 218                        return 0;
 219        }
 220
 221        if (xen_have_vcpu_info_placement) {
 222                vcpup = &per_cpu(xen_vcpu_info, cpu);
 223                info.mfn = arbitrary_virt_to_mfn(vcpup);
 224                info.offset = offset_in_page(vcpup);
 225
 226                /*
 227                 * Check to see if the hypervisor will put the vcpu_info
 228                 * structure where we want it, which allows direct access via
 229                 * a percpu-variable.
 230                 * N.B. This hypercall can _only_ be called once per CPU.
 231                 * Subsequent calls will error out with -EINVAL. This is due to
 232                 * the fact that hypervisor has no unregister variant and this
 233                 * hypercall does not allow to over-write info.mfn and
 234                 * info.offset.
 235                 */
 236                err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
 237                                         xen_vcpu_nr(cpu), &info);
 238
 239                if (err) {
 240                        pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
 241                                     cpu, err);
 242                        xen_have_vcpu_info_placement = 0;
 243                } else {
 244                        /*
 245                         * This cpu is using the registered vcpu info, even if
 246                         * later ones fail to.
 247                         */
 248                        per_cpu(xen_vcpu, cpu) = vcpup;
 249                }
 250        }
 251
 252        if (!xen_have_vcpu_info_placement)
 253                xen_vcpu_info_reset(cpu);
 254
 255        return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0);
 256}
 257
 258void xen_reboot(int reason)
 259{
 260        struct sched_shutdown r = { .reason = reason };
 261        int cpu;
 262
 263        for_each_online_cpu(cpu)
 264                xen_pmu_finish(cpu);
 265
 266        if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
 267                BUG();
 268}
 269
 270void xen_emergency_restart(void)
 271{
 272        xen_reboot(SHUTDOWN_reboot);
 273}
 274
 275static int
 276xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
 277{
 278        if (!kexec_crash_loaded())
 279                xen_reboot(SHUTDOWN_crash);
 280        return NOTIFY_DONE;
 281}
 282
 283static struct notifier_block xen_panic_block = {
 284        .notifier_call = xen_panic_event,
 285        .priority = INT_MIN
 286};
 287
 288int xen_panic_handler_init(void)
 289{
 290        atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
 291        return 0;
 292}
 293
 294void xen_pin_vcpu(int cpu)
 295{
 296        static bool disable_pinning;
 297        struct sched_pin_override pin_override;
 298        int ret;
 299
 300        if (disable_pinning)
 301                return;
 302
 303        pin_override.pcpu = cpu;
 304        ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
 305
 306        /* Ignore errors when removing override. */
 307        if (cpu < 0)
 308                return;
 309
 310        switch (ret) {
 311        case -ENOSYS:
 312                pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
 313                        cpu);
 314                disable_pinning = true;
 315                break;
 316        case -EPERM:
 317                WARN(1, "Trying to pin vcpu without having privilege to do so\n");
 318                disable_pinning = true;
 319                break;
 320        case -EINVAL:
 321        case -EBUSY:
 322                pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
 323                        cpu);
 324                break;
 325        case 0:
 326                break;
 327        default:
 328                WARN(1, "rc %d while trying to pin vcpu\n", ret);
 329                disable_pinning = true;
 330        }
 331}
 332
 333#ifdef CONFIG_HOTPLUG_CPU
 334void xen_arch_register_cpu(int num)
 335{
 336        arch_register_cpu(num);
 337}
 338EXPORT_SYMBOL(xen_arch_register_cpu);
 339
 340void xen_arch_unregister_cpu(int num)
 341{
 342        arch_unregister_cpu(num);
 343}
 344EXPORT_SYMBOL(xen_arch_unregister_cpu);
 345#endif
 346
 347#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
 348void __init arch_xen_balloon_init(struct resource *hostmem_resource)
 349{
 350        struct xen_memory_map memmap;
 351        int rc;
 352        unsigned int i, last_guest_ram;
 353        phys_addr_t max_addr = PFN_PHYS(max_pfn);
 354        struct e820_table *xen_e820_table;
 355        const struct e820_entry *entry;
 356        struct resource *res;
 357
 358        if (!xen_initial_domain())
 359                return;
 360
 361        xen_e820_table = kmalloc(sizeof(*xen_e820_table), GFP_KERNEL);
 362        if (!xen_e820_table)
 363                return;
 364
 365        memmap.nr_entries = ARRAY_SIZE(xen_e820_table->entries);
 366        set_xen_guest_handle(memmap.buffer, xen_e820_table->entries);
 367        rc = HYPERVISOR_memory_op(XENMEM_machine_memory_map, &memmap);
 368        if (rc) {
 369                pr_warn("%s: Can't read host e820 (%d)\n", __func__, rc);
 370                goto out;
 371        }
 372
 373        last_guest_ram = 0;
 374        for (i = 0; i < memmap.nr_entries; i++) {
 375                if (xen_e820_table->entries[i].addr >= max_addr)
 376                        break;
 377                if (xen_e820_table->entries[i].type == E820_TYPE_RAM)
 378                        last_guest_ram = i;
 379        }
 380
 381        entry = &xen_e820_table->entries[last_guest_ram];
 382        if (max_addr >= entry->addr + entry->size)
 383                goto out; /* No unallocated host RAM. */
 384
 385        hostmem_resource->start = max_addr;
 386        hostmem_resource->end = entry->addr + entry->size;
 387
 388        /*
 389         * Mark non-RAM regions between the end of dom0 RAM and end of host RAM
 390         * as unavailable. The rest of that region can be used for hotplug-based
 391         * ballooning.
 392         */
 393        for (; i < memmap.nr_entries; i++) {
 394                entry = &xen_e820_table->entries[i];
 395
 396                if (entry->type == E820_TYPE_RAM)
 397                        continue;
 398
 399                if (entry->addr >= hostmem_resource->end)
 400                        break;
 401
 402                res = kzalloc(sizeof(*res), GFP_KERNEL);
 403                if (!res)
 404                        goto out;
 405
 406                res->name = "Unavailable host RAM";
 407                res->start = entry->addr;
 408                res->end = (entry->addr + entry->size < hostmem_resource->end) ?
 409                            entry->addr + entry->size : hostmem_resource->end;
 410                rc = insert_resource(hostmem_resource, res);
 411                if (rc) {
 412                        pr_warn("%s: Can't insert [%llx - %llx) (%d)\n",
 413                                __func__, res->start, res->end, rc);
 414                        kfree(res);
 415                        goto  out;
 416                }
 417        }
 418
 419 out:
 420        kfree(xen_e820_table);
 421}
 422#endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */
 423