enlighten.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <linux/cpu.h>
  2. #include <linux/kexec.h>
  3. #include <xen/features.h>
  4. #include <xen/page.h>
  5. #include <asm/xen/hypercall.h>
  6. #include <asm/xen/hypervisor.h>
  7. #include <asm/cpu.h>
  8. #include <asm/e820/api.h>
  9. #include "xen-ops.h"
  10. #include "smp.h"
  11. #include "pmu.h"
  12. EXPORT_SYMBOL_GPL(hypercall_page);
  13. /*
  14. * Pointer to the xen_vcpu_info structure or
  15. * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info
  16. * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info
  17. * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point
  18. * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to
  19. * acknowledge pending events.
  20. * Also more subtly it is used by the patched version of irq enable/disable
  21. * e.g. xen_irq_enable_direct and xen_iret in PV mode.
  22. *
  23. * The desire to be able to do those mask/unmask operations as a single
  24. * instruction by using the per-cpu offset held in %gs is the real reason
  25. * vcpu info is in a per-cpu pointer and the original reason for this
  26. * hypercall.
  27. *
  28. */
  29. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  30. /*
  31. * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info
  32. * hypercall. This can be used both in PV and PVHVM mode. The structure
  33. * overrides the default per_cpu(xen_vcpu, cpu) value.
  34. */
  35. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  36. /* Linux <-> Xen vCPU id mapping */
  37. DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
  38. EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
  39. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  40. EXPORT_SYMBOL_GPL(xen_domain_type);
  41. unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
  42. EXPORT_SYMBOL(machine_to_phys_mapping);
  43. unsigned long machine_to_phys_nr;
  44. EXPORT_SYMBOL(machine_to_phys_nr);
  45. struct start_info *xen_start_info;
  46. EXPORT_SYMBOL_GPL(xen_start_info);
  47. struct shared_info xen_dummy_shared_info;
  48. __read_mostly int xen_have_vector_callback;
  49. EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  50. /*
  51. * Point at some empty memory to start with. We map the real shared_info
  52. * page as soon as fixmap is up and running.
  53. */
  54. struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
  55. /*
  56. * Flag to determine whether vcpu info placement is available on all
  57. * VCPUs. We assume it is to start with, and then set it to zero on
  58. * the first failure. This is because it can succeed on some VCPUs
  59. * and not others, since it can involve hypervisor memory allocation,
  60. * or because the guest failed to guarantee all the appropriate
  61. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  62. *
  63. * Note that any particular CPU may be using a placed vcpu structure,
  64. * but we can only optimise if the all are.
  65. *
  66. * 0: not available, 1: available
  67. */
  68. int xen_have_vcpu_info_placement = 1;
  69. static int xen_cpu_up_online(unsigned int cpu)
  70. {
  71. xen_init_lock_cpu(cpu);
  72. return 0;
  73. }
  74. int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
  75. int (*cpu_dead_cb)(unsigned int))
  76. {
  77. int rc;
  78. rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
  79. "x86/xen/hvm_guest:prepare",
  80. cpu_up_prepare_cb, cpu_dead_cb);
  81. if (rc >= 0) {
  82. rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  83. "x86/xen/hvm_guest:online",
  84. xen_cpu_up_online, NULL);
  85. if (rc < 0)
  86. cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
  87. }
  88. return rc >= 0 ? 0 : rc;
  89. }
  90. static void clamp_max_cpus(void)
  91. {
  92. #ifdef CONFIG_SMP
  93. if (setup_max_cpus > MAX_VIRT_CPUS)
  94. setup_max_cpus = MAX_VIRT_CPUS;
  95. #endif
  96. }
  97. void xen_vcpu_setup(int cpu)
  98. {
  99. struct vcpu_register_vcpu_info info;
  100. int err;
  101. struct vcpu_info *vcpup;
  102. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  103. /*
  104. * This path is called twice on PVHVM - first during bootup via
  105. * smp_init -> xen_hvm_cpu_notify, and then if the VCPU is being
  106. * hotplugged: cpu_up -> xen_hvm_cpu_notify.
  107. * As we can only do the VCPUOP_register_vcpu_info once lets
  108. * not over-write its result.
  109. *
  110. * For PV it is called during restore (xen_vcpu_restore) and bootup
  111. * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
  112. * use this function.
  113. */
  114. if (xen_hvm_domain()) {
  115. if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
  116. return;
  117. }
  118. if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS)
  119. per_cpu(xen_vcpu, cpu) =
  120. &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
  121. if (!xen_have_vcpu_info_placement) {
  122. if (cpu >= MAX_VIRT_CPUS)
  123. clamp_max_cpus();
  124. return;
  125. }
  126. vcpup = &per_cpu(xen_vcpu_info, cpu);
  127. info.mfn = arbitrary_virt_to_mfn(vcpup);
  128. info.offset = offset_in_page(vcpup);
  129. /* Check to see if the hypervisor will put the vcpu_info
  130. structure where we want it, which allows direct access via
  131. a percpu-variable.
  132. N.B. This hypercall can _only_ be called once per CPU. Subsequent
  133. calls will error out with -EINVAL. This is due to the fact that
  134. hypervisor has no unregister variant and this hypercall does not
  135. allow to over-write info.mfn and info.offset.
  136. */
  137. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, xen_vcpu_nr(cpu),
  138. &info);
  139. if (err) {
  140. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  141. xen_have_vcpu_info_placement = 0;
  142. clamp_max_cpus();
  143. } else {
  144. /* This cpu is using the registered vcpu info, even if
  145. later ones fail to. */
  146. per_cpu(xen_vcpu, cpu) = vcpup;
  147. }
  148. }
  149. void xen_reboot(int reason)
  150. {
  151. struct sched_shutdown r = { .reason = reason };
  152. int cpu;
  153. for_each_online_cpu(cpu)
  154. xen_pmu_finish(cpu);
  155. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  156. BUG();
  157. }
  158. void xen_emergency_restart(void)
  159. {
  160. xen_reboot(SHUTDOWN_reboot);
  161. }
  162. static int
  163. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  164. {
  165. if (!kexec_crash_loaded())
  166. xen_reboot(SHUTDOWN_crash);
  167. return NOTIFY_DONE;
  168. }
  169. static struct notifier_block xen_panic_block = {
  170. .notifier_call = xen_panic_event,
  171. .priority = INT_MIN
  172. };
  173. int xen_panic_handler_init(void)
  174. {
  175. atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
  176. return 0;
  177. }
  178. void xen_pin_vcpu(int cpu)
  179. {
  180. static bool disable_pinning;
  181. struct sched_pin_override pin_override;
  182. int ret;
  183. if (disable_pinning)
  184. return;
  185. pin_override.pcpu = cpu;
  186. ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
  187. /* Ignore errors when removing override. */
  188. if (cpu < 0)
  189. return;
  190. switch (ret) {
  191. case -ENOSYS:
  192. pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
  193. cpu);
  194. disable_pinning = true;
  195. break;
  196. case -EPERM:
  197. WARN(1, "Trying to pin vcpu without having privilege to do so\n");
  198. disable_pinning = true;
  199. break;
  200. case -EINVAL:
  201. case -EBUSY:
  202. pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
  203. cpu);
  204. break;
  205. case 0:
  206. break;
  207. default:
  208. WARN(1, "rc %d while trying to pin vcpu\n", ret);
  209. disable_pinning = true;
  210. }
  211. }
  212. #ifdef CONFIG_HOTPLUG_CPU
  213. void xen_arch_register_cpu(int num)
  214. {
  215. arch_register_cpu(num);
  216. }
  217. EXPORT_SYMBOL(xen_arch_register_cpu);
  218. void xen_arch_unregister_cpu(int num)
  219. {
  220. arch_unregister_cpu(num);
  221. }
  222. EXPORT_SYMBOL(xen_arch_unregister_cpu);
  223. #endif