Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 1 | #include <linux/cpu.h> |
Vitaly Kuznetsov | 0b34a16 | 2015-09-25 11:59:52 +0200 | [diff] [blame] | 2 | #include <linux/kexec.h> |
Vitaly Kuznetsov | 0b34a16 | 2015-09-25 11:59:52 +0200 | [diff] [blame] | 3 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 4 | #include <xen/features.h> |
| 5 | #include <xen/page.h> |
| 6 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 7 | #include <asm/xen/hypercall.h> |
| 8 | #include <asm/xen/hypervisor.h> |
Stefano Stabellini | a314e3e | 2015-10-22 16:20:46 +0000 | [diff] [blame] | 9 | #include <asm/cpu.h> |
Ingo Molnar | 687d77a | 2017-03-01 08:49:47 +0100 | [diff] [blame] | 10 | #include <asm/e820/api.h> |
Konrad Rzeszutek Wilk | 73c154c | 2012-02-13 22:26:32 -0500 | [diff] [blame] | 11 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 12 | #include "xen-ops.h" |
Ben Guthro | f447d56 | 2012-04-21 00:11:04 +0800 | [diff] [blame] | 13 | #include "smp.h" |
Boris Ostrovsky | 65d0cf0 | 2015-08-10 16:34:34 -0400 | [diff] [blame] | 14 | #include "pmu.h" |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 15 | |
| 16 | EXPORT_SYMBOL_GPL(hypercall_page); |
| 17 | |
Konrad Rzeszutek Wilk | a520996 | 2013-05-05 08:51:47 -0400 | [diff] [blame] | 18 | /* |
| 19 | * Pointer to the xen_vcpu_info structure or |
| 20 | * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info |
| 21 | * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info |
| 22 | * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point |
| 23 | * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to |
| 24 | * acknowledge pending events. |
| 25 | * Also more subtly it is used by the patched version of irq enable/disable |
| 26 | * e.g. xen_irq_enable_direct and xen_iret in PV mode. |
| 27 | * |
| 28 | * The desire to be able to do those mask/unmask operations as a single |
| 29 | * instruction by using the per-cpu offset held in %gs is the real reason |
| 30 | * vcpu info is in a per-cpu pointer and the original reason for this |
| 31 | * hypercall. |
| 32 | * |
| 33 | */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 34 | DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu); |
Konrad Rzeszutek Wilk | a520996 | 2013-05-05 08:51:47 -0400 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info |
| 38 | * hypercall. This can be used both in PV and PVHVM mode. The structure |
| 39 | * overrides the default per_cpu(xen_vcpu, cpu) value. |
| 40 | */ |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 41 | DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info); |
Jeremy Fitzhardinge | 9f79991 | 2007-10-16 11:51:30 -0700 | [diff] [blame] | 42 | |
Vitaly Kuznetsov | 88e957d | 2016-06-30 17:56:37 +0200 | [diff] [blame] | 43 | /* Linux <-> Xen vCPU id mapping */ |
Vitaly Kuznetsov | 55467de | 2016-07-29 11:06:48 +0200 | [diff] [blame] | 44 | DEFINE_PER_CPU(uint32_t, xen_vcpu_id); |
Vitaly Kuznetsov | 88e957d | 2016-06-30 17:56:37 +0200 | [diff] [blame] | 45 | EXPORT_PER_CPU_SYMBOL(xen_vcpu_id); |
| 46 | |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 47 | enum xen_domain_type xen_domain_type = XEN_NATIVE; |
| 48 | EXPORT_SYMBOL_GPL(xen_domain_type); |
| 49 | |
Ian Campbell | 7e77506 | 2010-09-30 12:37:26 +0100 | [diff] [blame] | 50 | unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START; |
| 51 | EXPORT_SYMBOL(machine_to_phys_mapping); |
Jan Beulich | ccbcdf7 | 2011-08-16 15:07:41 +0100 | [diff] [blame] | 52 | unsigned long machine_to_phys_nr; |
| 53 | EXPORT_SYMBOL(machine_to_phys_nr); |
Ian Campbell | 7e77506 | 2010-09-30 12:37:26 +0100 | [diff] [blame] | 54 | |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 55 | struct start_info *xen_start_info; |
| 56 | EXPORT_SYMBOL_GPL(xen_start_info); |
| 57 | |
Jeremy Fitzhardinge | a0d695c | 2008-05-26 23:31:21 +0100 | [diff] [blame] | 58 | struct shared_info xen_dummy_shared_info; |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 59 | |
Boris Ostrovsky | 3dbd820 | 2017-05-02 13:23:06 -0400 | [diff] [blame] | 60 | __read_mostly int xen_have_vector_callback; |
| 61 | EXPORT_SYMBOL_GPL(xen_have_vector_callback); |
| 62 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 63 | /* |
| 64 | * Point at some empty memory to start with. We map the real shared_info |
| 65 | * page as soon as fixmap is up and running. |
| 66 | */ |
Olaf Hering | 4648da7 | 2012-07-17 11:57:41 +0200 | [diff] [blame] | 67 | struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info; |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Flag to determine whether vcpu info placement is available on all |
| 71 | * VCPUs. We assume it is to start with, and then set it to zero on |
| 72 | * the first failure. This is because it can succeed on some VCPUs |
| 73 | * and not others, since it can involve hypervisor memory allocation, |
| 74 | * or because the guest failed to guarantee all the appropriate |
| 75 | * constraints on all VCPUs (ie buffer can't cross a page boundary). |
| 76 | * |
| 77 | * Note that any particular CPU may be using a placed vcpu structure, |
| 78 | * but we can only optimise if the all are. |
| 79 | * |
| 80 | * 0: not available, 1: available |
| 81 | */ |
Vitaly Kuznetsov | 52519f2 | 2017-03-14 18:35:37 +0100 | [diff] [blame] | 82 | int xen_have_vcpu_info_placement = 1; |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 83 | |
Vitaly Kuznetsov | e1dab14 | 2017-03-14 18:35:41 +0100 | [diff] [blame] | 84 | static int xen_cpu_up_online(unsigned int cpu) |
| 85 | { |
| 86 | xen_init_lock_cpu(cpu); |
| 87 | return 0; |
| 88 | } |
David Vrabel | 1c32cdc | 2012-07-09 11:39:08 +0100 | [diff] [blame] | 89 | |
Vitaly Kuznetsov | e1dab14 | 2017-03-14 18:35:41 +0100 | [diff] [blame] | 90 | int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int), |
| 91 | int (*cpu_dead_cb)(unsigned int)) |
| 92 | { |
| 93 | int rc; |
| 94 | |
| 95 | rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE, |
| 96 | "x86/xen/hvm_guest:prepare", |
| 97 | cpu_up_prepare_cb, cpu_dead_cb); |
| 98 | if (rc >= 0) { |
| 99 | rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 100 | "x86/xen/hvm_guest:online", |
| 101 | xen_cpu_up_online, NULL); |
| 102 | if (rc < 0) |
| 103 | cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE); |
| 104 | } |
| 105 | |
| 106 | return rc >= 0 ? 0 : rc; |
| 107 | } |
David Vrabel | 1c32cdc | 2012-07-09 11:39:08 +0100 | [diff] [blame] | 108 | |
Mukesh Rathor | c06ee78 | 2010-07-19 10:25:08 -0700 | [diff] [blame] | 109 | static void clamp_max_cpus(void) |
| 110 | { |
| 111 | #ifdef CONFIG_SMP |
| 112 | if (setup_max_cpus > MAX_VIRT_CPUS) |
| 113 | setup_max_cpus = MAX_VIRT_CPUS; |
| 114 | #endif |
| 115 | } |
| 116 | |
Vitaly Kuznetsov | ee42d66 | 2016-06-30 17:56:43 +0200 | [diff] [blame] | 117 | void xen_vcpu_setup(int cpu) |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 118 | { |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 119 | struct vcpu_register_vcpu_info info; |
| 120 | int err; |
| 121 | struct vcpu_info *vcpup; |
| 122 | |
Jeremy Fitzhardinge | a0d695c | 2008-05-26 23:31:21 +0100 | [diff] [blame] | 123 | BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info); |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 124 | |
Konrad Rzeszutek Wilk | 7f1fc26 | 2013-05-05 09:30:09 -0400 | [diff] [blame] | 125 | /* |
| 126 | * This path is called twice on PVHVM - first during bootup via |
| 127 | * smp_init -> xen_hvm_cpu_notify, and then if the VCPU is being |
| 128 | * hotplugged: cpu_up -> xen_hvm_cpu_notify. |
| 129 | * As we can only do the VCPUOP_register_vcpu_info once lets |
| 130 | * not over-write its result. |
| 131 | * |
| 132 | * For PV it is called during restore (xen_vcpu_restore) and bootup |
| 133 | * (xen_setup_vcpu_info_placement). The hotplug mechanism does not |
| 134 | * use this function. |
| 135 | */ |
| 136 | if (xen_hvm_domain()) { |
| 137 | if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu)) |
| 138 | return; |
| 139 | } |
Vitaly Kuznetsov | e15a862 | 2016-06-30 17:56:39 +0200 | [diff] [blame] | 140 | if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) |
| 141 | per_cpu(xen_vcpu, cpu) = |
| 142 | &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)]; |
Mukesh Rathor | c06ee78 | 2010-07-19 10:25:08 -0700 | [diff] [blame] | 143 | |
Vitaly Kuznetsov | 52519f2 | 2017-03-14 18:35:37 +0100 | [diff] [blame] | 144 | if (!xen_have_vcpu_info_placement) { |
Mukesh Rathor | c06ee78 | 2010-07-19 10:25:08 -0700 | [diff] [blame] | 145 | if (cpu >= MAX_VIRT_CPUS) |
| 146 | clamp_max_cpus(); |
| 147 | return; |
| 148 | } |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 149 | |
| 150 | vcpup = &per_cpu(xen_vcpu_info, cpu); |
Jeremy Fitzhardinge | 9976b39 | 2009-02-27 09:19:26 -0800 | [diff] [blame] | 151 | info.mfn = arbitrary_virt_to_mfn(vcpup); |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 152 | info.offset = offset_in_page(vcpup); |
| 153 | |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 154 | /* Check to see if the hypervisor will put the vcpu_info |
| 155 | structure where we want it, which allows direct access via |
Konrad Rzeszutek Wilk | a520996 | 2013-05-05 08:51:47 -0400 | [diff] [blame] | 156 | a percpu-variable. |
| 157 | N.B. This hypercall can _only_ be called once per CPU. Subsequent |
| 158 | calls will error out with -EINVAL. This is due to the fact that |
| 159 | hypervisor has no unregister variant and this hypercall does not |
| 160 | allow to over-write info.mfn and info.offset. |
| 161 | */ |
Vitaly Kuznetsov | ad5475f | 2016-06-30 17:56:38 +0200 | [diff] [blame] | 162 | err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, xen_vcpu_nr(cpu), |
| 163 | &info); |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 164 | |
| 165 | if (err) { |
| 166 | printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err); |
Vitaly Kuznetsov | 52519f2 | 2017-03-14 18:35:37 +0100 | [diff] [blame] | 167 | xen_have_vcpu_info_placement = 0; |
Mukesh Rathor | c06ee78 | 2010-07-19 10:25:08 -0700 | [diff] [blame] | 168 | clamp_max_cpus(); |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 169 | } else { |
| 170 | /* This cpu is using the registered vcpu info, even if |
| 171 | later ones fail to. */ |
| 172 | per_cpu(xen_vcpu, cpu) = vcpup; |
Jeremy Fitzhardinge | 60223a3 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 173 | } |
Jeremy Fitzhardinge | 5ead97c | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Vitaly Kuznetsov | 98f2a47 | 2017-03-14 18:35:40 +0100 | [diff] [blame] | 176 | void xen_reboot(int reason) |
Jeremy Fitzhardinge | fefa629 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 177 | { |
Jeremy Fitzhardinge | 349c709f | 2008-05-26 23:31:02 +0100 | [diff] [blame] | 178 | struct sched_shutdown r = { .reason = reason }; |
Boris Ostrovsky | 65d0cf0 | 2015-08-10 16:34:34 -0400 | [diff] [blame] | 179 | int cpu; |
| 180 | |
| 181 | for_each_online_cpu(cpu) |
| 182 | xen_pmu_finish(cpu); |
Jeremy Fitzhardinge | 349c709f | 2008-05-26 23:31:02 +0100 | [diff] [blame] | 183 | |
Jeremy Fitzhardinge | 349c709f | 2008-05-26 23:31:02 +0100 | [diff] [blame] | 184 | if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r)) |
Jeremy Fitzhardinge | fefa629 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 185 | BUG(); |
| 186 | } |
| 187 | |
Vitaly Kuznetsov | 98f2a47 | 2017-03-14 18:35:40 +0100 | [diff] [blame] | 188 | void xen_emergency_restart(void) |
Jeremy Fitzhardinge | fefa629 | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 189 | { |
| 190 | xen_reboot(SHUTDOWN_reboot); |
| 191 | } |
| 192 | |
Donald Dutile | f09f6d1 | 2010-07-15 14:56:49 -0400 | [diff] [blame] | 193 | static int |
| 194 | xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 195 | { |
Petr Tesarik | c025311 | 2016-08-02 14:06:19 -0700 | [diff] [blame] | 196 | if (!kexec_crash_loaded()) |
| 197 | xen_reboot(SHUTDOWN_crash); |
Donald Dutile | f09f6d1 | 2010-07-15 14:56:49 -0400 | [diff] [blame] | 198 | return NOTIFY_DONE; |
| 199 | } |
| 200 | |
| 201 | static struct notifier_block xen_panic_block = { |
Vitaly Kuznetsov | e1dab14 | 2017-03-14 18:35:41 +0100 | [diff] [blame] | 202 | .notifier_call = xen_panic_event, |
Radim Krčmář | bc5eb20 | 2014-05-13 18:56:25 +0200 | [diff] [blame] | 203 | .priority = INT_MIN |
Donald Dutile | f09f6d1 | 2010-07-15 14:56:49 -0400 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | int xen_panic_handler_init(void) |
| 207 | { |
| 208 | atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block); |
| 209 | return 0; |
| 210 | } |
| 211 | |
Vitaly Kuznetsov | 98f2a47 | 2017-03-14 18:35:40 +0100 | [diff] [blame] | 212 | void xen_pin_vcpu(int cpu) |
Juergen Gross | 99bc675 | 2016-08-29 08:48:45 +0200 | [diff] [blame] | 213 | { |
| 214 | static bool disable_pinning; |
| 215 | struct sched_pin_override pin_override; |
| 216 | int ret; |
| 217 | |
| 218 | if (disable_pinning) |
| 219 | return; |
| 220 | |
| 221 | pin_override.pcpu = cpu; |
| 222 | ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override); |
| 223 | |
| 224 | /* Ignore errors when removing override. */ |
| 225 | if (cpu < 0) |
| 226 | return; |
| 227 | |
| 228 | switch (ret) { |
| 229 | case -ENOSYS: |
| 230 | pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n", |
| 231 | cpu); |
| 232 | disable_pinning = true; |
| 233 | break; |
| 234 | case -EPERM: |
| 235 | WARN(1, "Trying to pin vcpu without having privilege to do so\n"); |
| 236 | disable_pinning = true; |
| 237 | break; |
| 238 | case -EINVAL: |
| 239 | case -EBUSY: |
| 240 | pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n", |
| 241 | cpu); |
| 242 | break; |
| 243 | case 0: |
| 244 | break; |
| 245 | default: |
| 246 | WARN(1, "rc %d while trying to pin vcpu\n", ret); |
| 247 | disable_pinning = true; |
| 248 | } |
| 249 | } |
| 250 | |
Stefano Stabellini | a314e3e | 2015-10-22 16:20:46 +0000 | [diff] [blame] | 251 | #ifdef CONFIG_HOTPLUG_CPU |
| 252 | void xen_arch_register_cpu(int num) |
| 253 | { |
| 254 | arch_register_cpu(num); |
| 255 | } |
| 256 | EXPORT_SYMBOL(xen_arch_register_cpu); |
| 257 | |
| 258 | void xen_arch_unregister_cpu(int num) |
| 259 | { |
| 260 | arch_unregister_cpu(num); |
| 261 | } |
| 262 | EXPORT_SYMBOL(xen_arch_unregister_cpu); |
| 263 | #endif |