enlighten.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include <xen/xen.h>
  2. #include <xen/events.h>
  3. #include <xen/grant_table.h>
  4. #include <xen/hvm.h>
  5. #include <xen/interface/vcpu.h>
  6. #include <xen/interface/xen.h>
  7. #include <xen/interface/memory.h>
  8. #include <xen/interface/hvm/params.h>
  9. #include <xen/features.h>
  10. #include <xen/platform_pci.h>
  11. #include <xen/xenbus.h>
  12. #include <xen/page.h>
  13. #include <xen/interface/sched.h>
  14. #include <xen/xen-ops.h>
  15. #include <asm/xen/hypervisor.h>
  16. #include <asm/xen/hypercall.h>
  17. #include <asm/system_misc.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irqreturn.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_address.h>
  24. #include <linux/cpuidle.h>
  25. #include <linux/cpufreq.h>
  26. #include <linux/cpu.h>
  27. #include <linux/mm.h>
  28. struct start_info _xen_start_info;
  29. struct start_info *xen_start_info = &_xen_start_info;
  30. EXPORT_SYMBOL_GPL(xen_start_info);
  31. enum xen_domain_type xen_domain_type = XEN_NATIVE;
  32. EXPORT_SYMBOL_GPL(xen_domain_type);
  33. struct shared_info xen_dummy_shared_info;
  34. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  35. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  36. static struct vcpu_info __percpu *xen_vcpu_info;
  37. /* These are unused until we support booting "pre-ballooned" */
  38. unsigned long xen_released_pages;
  39. struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
  40. /* TODO: to be removed */
  41. __read_mostly int xen_have_vector_callback;
  42. EXPORT_SYMBOL_GPL(xen_have_vector_callback);
  43. int xen_platform_pci_unplug = XEN_UNPLUG_ALL;
  44. EXPORT_SYMBOL_GPL(xen_platform_pci_unplug);
  45. static __read_mostly int xen_events_irq = -1;
  46. /* map fgmfn of domid to lpfn in the current domain */
  47. static int map_foreign_page(unsigned long lpfn, unsigned long fgmfn,
  48. unsigned int domid)
  49. {
  50. int rc;
  51. struct xen_add_to_physmap_range xatp = {
  52. .domid = DOMID_SELF,
  53. .foreign_domid = domid,
  54. .size = 1,
  55. .space = XENMAPSPACE_gmfn_foreign,
  56. };
  57. xen_ulong_t idx = fgmfn;
  58. xen_pfn_t gpfn = lpfn;
  59. int err = 0;
  60. set_xen_guest_handle(xatp.idxs, &idx);
  61. set_xen_guest_handle(xatp.gpfns, &gpfn);
  62. set_xen_guest_handle(xatp.errs, &err);
  63. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
  64. if (rc || err) {
  65. pr_warn("Failed to map pfn to mfn rc:%d:%d pfn:%lx mfn:%lx\n",
  66. rc, err, lpfn, fgmfn);
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. struct remap_data {
  72. xen_pfn_t fgmfn; /* foreign domain's gmfn */
  73. pgprot_t prot;
  74. domid_t domid;
  75. struct vm_area_struct *vma;
  76. int index;
  77. struct page **pages;
  78. struct xen_remap_mfn_info *info;
  79. };
  80. static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
  81. void *data)
  82. {
  83. struct remap_data *info = data;
  84. struct page *page = info->pages[info->index++];
  85. unsigned long pfn = page_to_pfn(page);
  86. pte_t pte = pte_mkspecial(pfn_pte(pfn, info->prot));
  87. if (map_foreign_page(pfn, info->fgmfn, info->domid))
  88. return -EFAULT;
  89. set_pte_at(info->vma->vm_mm, addr, ptep, pte);
  90. return 0;
  91. }
  92. int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
  93. unsigned long addr,
  94. xen_pfn_t mfn, int nr,
  95. pgprot_t prot, unsigned domid,
  96. struct page **pages)
  97. {
  98. int err;
  99. struct remap_data data;
  100. /* TBD: Batching, current sole caller only does page at a time */
  101. if (nr > 1)
  102. return -EINVAL;
  103. data.fgmfn = mfn;
  104. data.prot = prot;
  105. data.domid = domid;
  106. data.vma = vma;
  107. data.index = 0;
  108. data.pages = pages;
  109. err = apply_to_page_range(vma->vm_mm, addr, nr << PAGE_SHIFT,
  110. remap_pte_fn, &data);
  111. return err;
  112. }
  113. EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_range);
  114. int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
  115. int nr, struct page **pages)
  116. {
  117. int i;
  118. for (i = 0; i < nr; i++) {
  119. struct xen_remove_from_physmap xrp;
  120. unsigned long rc, pfn;
  121. pfn = page_to_pfn(pages[i]);
  122. xrp.domid = DOMID_SELF;
  123. xrp.gpfn = pfn;
  124. rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
  125. if (rc) {
  126. pr_warn("Failed to unmap pfn:%lx rc:%ld\n",
  127. pfn, rc);
  128. return rc;
  129. }
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(xen_unmap_domain_mfn_range);
  134. static void xen_percpu_init(void)
  135. {
  136. struct vcpu_register_vcpu_info info;
  137. struct vcpu_info *vcpup;
  138. int err;
  139. int cpu = get_cpu();
  140. pr_info("Xen: initializing cpu%d\n", cpu);
  141. vcpup = per_cpu_ptr(xen_vcpu_info, cpu);
  142. info.mfn = __pa(vcpup) >> PAGE_SHIFT;
  143. info.offset = offset_in_page(vcpup);
  144. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  145. BUG_ON(err);
  146. per_cpu(xen_vcpu, cpu) = vcpup;
  147. enable_percpu_irq(xen_events_irq, 0);
  148. put_cpu();
  149. }
  150. static void xen_restart(enum reboot_mode reboot_mode, const char *cmd)
  151. {
  152. struct sched_shutdown r = { .reason = SHUTDOWN_reboot };
  153. int rc;
  154. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  155. if (rc)
  156. BUG();
  157. }
  158. static void xen_power_off(void)
  159. {
  160. struct sched_shutdown r = { .reason = SHUTDOWN_poweroff };
  161. int rc;
  162. rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  163. if (rc)
  164. BUG();
  165. }
  166. static int xen_cpu_notification(struct notifier_block *self,
  167. unsigned long action,
  168. void *hcpu)
  169. {
  170. switch (action) {
  171. case CPU_STARTING:
  172. xen_percpu_init();
  173. break;
  174. default:
  175. break;
  176. }
  177. return NOTIFY_OK;
  178. }
  179. static struct notifier_block xen_cpu_notifier = {
  180. .notifier_call = xen_cpu_notification,
  181. };
  182. static irqreturn_t xen_arm_callback(int irq, void *arg)
  183. {
  184. xen_hvm_evtchn_do_upcall();
  185. return IRQ_HANDLED;
  186. }
  187. /*
  188. * see Documentation/devicetree/bindings/arm/xen.txt for the
  189. * documentation of the Xen Device Tree format.
  190. */
  191. #define GRANT_TABLE_PHYSADDR 0
  192. static int __init xen_guest_init(void)
  193. {
  194. struct xen_add_to_physmap xatp;
  195. static struct shared_info *shared_info_page = 0;
  196. struct device_node *node;
  197. int len;
  198. const char *s = NULL;
  199. const char *version = NULL;
  200. const char *xen_prefix = "xen,xen-";
  201. struct resource res;
  202. phys_addr_t grant_frames;
  203. node = of_find_compatible_node(NULL, NULL, "xen,xen");
  204. if (!node) {
  205. pr_debug("No Xen support\n");
  206. return 0;
  207. }
  208. s = of_get_property(node, "compatible", &len);
  209. if (strlen(xen_prefix) + 3 < len &&
  210. !strncmp(xen_prefix, s, strlen(xen_prefix)))
  211. version = s + strlen(xen_prefix);
  212. if (version == NULL) {
  213. pr_debug("Xen version not found\n");
  214. return 0;
  215. }
  216. if (of_address_to_resource(node, GRANT_TABLE_PHYSADDR, &res))
  217. return 0;
  218. grant_frames = res.start;
  219. xen_events_irq = irq_of_parse_and_map(node, 0);
  220. pr_info("Xen %s support found, events_irq=%d gnttab_frame=%pa\n",
  221. version, xen_events_irq, &grant_frames);
  222. if (xen_events_irq < 0)
  223. return -ENODEV;
  224. xen_domain_type = XEN_HVM_DOMAIN;
  225. xen_setup_features();
  226. if (xen_feature(XENFEAT_dom0))
  227. xen_start_info->flags |= SIF_INITDOMAIN|SIF_PRIVILEGED;
  228. else
  229. xen_start_info->flags &= ~(SIF_INITDOMAIN|SIF_PRIVILEGED);
  230. if (!shared_info_page)
  231. shared_info_page = (struct shared_info *)
  232. get_zeroed_page(GFP_KERNEL);
  233. if (!shared_info_page) {
  234. pr_err("not enough memory\n");
  235. return -ENOMEM;
  236. }
  237. xatp.domid = DOMID_SELF;
  238. xatp.idx = 0;
  239. xatp.space = XENMAPSPACE_shared_info;
  240. xatp.gpfn = __pa(shared_info_page) >> PAGE_SHIFT;
  241. if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
  242. BUG();
  243. HYPERVISOR_shared_info = (struct shared_info *)shared_info_page;
  244. /* xen_vcpu is a pointer to the vcpu_info struct in the shared_info
  245. * page, we use it in the event channel upcall and in some pvclock
  246. * related functions.
  247. * The shared info contains exactly 1 CPU (the boot CPU). The guest
  248. * is required to use VCPUOP_register_vcpu_info to place vcpu info
  249. * for secondary CPUs as they are brought up.
  250. * For uniformity we use VCPUOP_register_vcpu_info even on cpu0.
  251. */
  252. xen_vcpu_info = __alloc_percpu(sizeof(struct vcpu_info),
  253. sizeof(struct vcpu_info));
  254. if (xen_vcpu_info == NULL)
  255. return -ENOMEM;
  256. if (gnttab_setup_auto_xlat_frames(grant_frames)) {
  257. free_percpu(xen_vcpu_info);
  258. return -ENOMEM;
  259. }
  260. gnttab_init();
  261. if (!xen_initial_domain())
  262. xenbus_probe(NULL);
  263. /*
  264. * Making sure board specific code will not set up ops for
  265. * cpu idle and cpu freq.
  266. */
  267. disable_cpuidle();
  268. disable_cpufreq();
  269. xen_init_IRQ();
  270. if (request_percpu_irq(xen_events_irq, xen_arm_callback,
  271. "events", &xen_vcpu)) {
  272. pr_err("Error request IRQ %d\n", xen_events_irq);
  273. return -EINVAL;
  274. }
  275. xen_percpu_init();
  276. register_cpu_notifier(&xen_cpu_notifier);
  277. return 0;
  278. }
  279. early_initcall(xen_guest_init);
  280. static int __init xen_pm_init(void)
  281. {
  282. if (!xen_domain())
  283. return -ENODEV;
  284. pm_power_off = xen_power_off;
  285. arm_pm_restart = xen_restart;
  286. return 0;
  287. }
  288. late_initcall(xen_pm_init);
  289. /* empty stubs */
  290. void xen_arch_pre_suspend(void) { }
  291. void xen_arch_post_suspend(int suspend_cancelled) { }
  292. void xen_timer_resume(void) { }
  293. void xen_arch_resume(void) { }
  294. /* In the hypervisor.S file. */
  295. EXPORT_SYMBOL_GPL(HYPERVISOR_event_channel_op);
  296. EXPORT_SYMBOL_GPL(HYPERVISOR_grant_table_op);
  297. EXPORT_SYMBOL_GPL(HYPERVISOR_xen_version);
  298. EXPORT_SYMBOL_GPL(HYPERVISOR_console_io);
  299. EXPORT_SYMBOL_GPL(HYPERVISOR_sched_op);
  300. EXPORT_SYMBOL_GPL(HYPERVISOR_hvm_op);
  301. EXPORT_SYMBOL_GPL(HYPERVISOR_memory_op);
  302. EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
  303. EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
  304. EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
  305. EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
  306. EXPORT_SYMBOL_GPL(privcmd_call);