Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / arch / x86 / xen / enlighten.c
blob2eeddd81465330f43e269d4e1d2449f82772bfd1
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>
8 #include <xen/features.h>
9 #include <xen/page.h>
10 #include <xen/interface/memory.h>
12 #include <asm/xen/hypercall.h>
13 #include <asm/xen/hypervisor.h>
14 #include <asm/cpu.h>
15 #include <asm/e820/api.h>
17 #include "xen-ops.h"
18 #include "smp.h"
19 #include "pmu.h"
21 EXPORT_SYMBOL_GPL(hypercall_page);
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.
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.
39 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
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.
46 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
48 /* Linux <-> Xen vCPU id mapping */
49 DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
50 EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
52 enum xen_domain_type xen_domain_type = XEN_NATIVE;
53 EXPORT_SYMBOL_GPL(xen_domain_type);
55 unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
56 EXPORT_SYMBOL(machine_to_phys_mapping);
57 unsigned long machine_to_phys_nr;
58 EXPORT_SYMBOL(machine_to_phys_nr);
60 struct start_info *xen_start_info;
61 EXPORT_SYMBOL_GPL(xen_start_info);
63 struct shared_info xen_dummy_shared_info;
65 __read_mostly int xen_have_vector_callback;
66 EXPORT_SYMBOL_GPL(xen_have_vector_callback);
69 * NB: needs to live in .data because it's used by xen_prepare_pvh which runs
70 * before clearing the bss.
72 uint32_t xen_start_flags __attribute__((section(".data"))) = 0;
73 EXPORT_SYMBOL(xen_start_flags);
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.
79 struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
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).
89 * Note that any particular CPU may be using a placed vcpu structure,
90 * but we can only optimise if the all are.
92 * 0: not available, 1: available
94 int xen_have_vcpu_info_placement = 1;
96 static int xen_cpu_up_online(unsigned int cpu)
98 xen_init_lock_cpu(cpu);
99 return 0;
102 int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
103 int (*cpu_dead_cb)(unsigned int))
105 int rc;
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);
118 return rc >= 0 ? 0 : rc;
121 static int xen_vcpu_setup_restore(int cpu)
123 int rc = 0;
125 /* Any per_cpu(xen_vcpu) is stale, so reset it */
126 xen_vcpu_info_reset(cpu);
129 * For PVH and PVHVM, setup online VCPUs only. The rest will
130 * be handled by hotplug.
132 if (xen_pv_domain() ||
133 (xen_hvm_domain() && cpu_online(cpu))) {
134 rc = xen_vcpu_setup(cpu);
137 return rc;
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...
145 void xen_vcpu_restore(void)
147 int cpu, rc;
149 for_each_possible_cpu(cpu) {
150 bool other_cpu = (cpu != smp_processor_id());
151 bool is_up;
153 if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
154 continue;
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;
160 if (other_cpu && is_up &&
161 HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
162 BUG();
164 if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
165 xen_setup_runstate_info(cpu);
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);
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.
180 if (other_cpu && is_up && (rc == 0) &&
181 HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
182 BUG();
186 void xen_vcpu_info_reset(int cpu)
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;
197 int xen_vcpu_setup(int cpu)
199 struct vcpu_register_vcpu_info info;
200 int err;
201 struct vcpu_info *vcpup;
203 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
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.
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.
216 if (xen_hvm_domain()) {
217 if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
218 return 0;
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);
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.
236 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
237 xen_vcpu_nr(cpu), &info);
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 {
245 * This cpu is using the registered vcpu info, even if
246 * later ones fail to.
248 per_cpu(xen_vcpu, cpu) = vcpup;
252 if (!xen_have_vcpu_info_placement)
253 xen_vcpu_info_reset(cpu);
255 return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0);
258 void xen_reboot(int reason)
260 struct sched_shutdown r = { .reason = reason };
261 int cpu;
263 for_each_online_cpu(cpu)
264 xen_pmu_finish(cpu);
266 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
267 BUG();
270 void xen_emergency_restart(void)
272 xen_reboot(SHUTDOWN_reboot);
275 static int
276 xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
278 if (!kexec_crash_loaded())
279 xen_reboot(SHUTDOWN_crash);
280 return NOTIFY_DONE;
283 static struct notifier_block xen_panic_block = {
284 .notifier_call = xen_panic_event,
285 .priority = INT_MIN
288 int xen_panic_handler_init(void)
290 atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
291 return 0;
294 void xen_pin_vcpu(int cpu)
296 static bool disable_pinning;
297 struct sched_pin_override pin_override;
298 int ret;
300 if (disable_pinning)
301 return;
303 pin_override.pcpu = cpu;
304 ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
306 /* Ignore errors when removing override. */
307 if (cpu < 0)
308 return;
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;
333 #ifdef CONFIG_HOTPLUG_CPU
334 void xen_arch_register_cpu(int num)
336 arch_register_cpu(num);
338 EXPORT_SYMBOL(xen_arch_register_cpu);
340 void xen_arch_unregister_cpu(int num)
342 arch_unregister_cpu(num);
344 EXPORT_SYMBOL(xen_arch_unregister_cpu);
345 #endif
347 #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
348 void __init arch_xen_balloon_init(struct resource *hostmem_resource)
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;
358 if (!xen_initial_domain())
359 return;
361 xen_e820_table = kmalloc(sizeof(*xen_e820_table), GFP_KERNEL);
362 if (!xen_e820_table)
363 return;
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;
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;
381 entry = &xen_e820_table->entries[last_guest_ram];
382 if (max_addr >= entry->addr + entry->size)
383 goto out; /* No unallocated host RAM. */
385 hostmem_resource->start = max_addr;
386 hostmem_resource->end = entry->addr + entry->size;
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.
393 for (; i < memmap.nr_entries; i++) {
394 entry = &xen_e820_table->entries[i];
396 if (entry->type == E820_TYPE_RAM)
397 continue;
399 if (entry->addr >= hostmem_resource->end)
400 break;
402 res = kzalloc(sizeof(*res), GFP_KERNEL);
403 if (!res)
404 goto out;
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;
419 out:
420 kfree(xen_e820_table);
422 #endif /* CONFIG_XEN_BALLOON_MEMORY_HOTPLUG */