grant-table.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /******************************************************************************
  3. * grant_table.c
  4. * x86 specific part
  5. *
  6. * Granting foreign access to our memory reservation.
  7. *
  8. * Copyright (c) 2005-2006, Christopher Clark
  9. * Copyright (c) 2004-2005, K A Fraser
  10. * Copyright (c) 2008 Isaku Yamahata <yamahata at valinux co jp>
  11. * VA Linux Systems Japan. Split out x86 specific part.
  12. */
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/slab.h>
  16. #include <linux/vmalloc.h>
  17. #include <xen/interface/xen.h>
  18. #include <xen/page.h>
  19. #include <xen/grant_table.h>
  20. #include <xen/xen.h>
  21. static struct gnttab_vm_area {
  22. struct vm_struct *area;
  23. pte_t **ptes;
  24. int idx;
  25. } gnttab_shared_vm_area, gnttab_status_vm_area;
  26. int arch_gnttab_map_shared(unsigned long *frames, unsigned long nr_gframes,
  27. unsigned long max_nr_gframes,
  28. void **__shared)
  29. {
  30. void *shared = *__shared;
  31. unsigned long addr;
  32. unsigned long i;
  33. if (shared == NULL)
  34. *__shared = shared = gnttab_shared_vm_area.area->addr;
  35. addr = (unsigned long)shared;
  36. for (i = 0; i < nr_gframes; i++) {
  37. set_pte_at(&init_mm, addr, gnttab_shared_vm_area.ptes[i],
  38. mfn_pte(frames[i], PAGE_KERNEL));
  39. addr += PAGE_SIZE;
  40. }
  41. return 0;
  42. }
  43. int arch_gnttab_map_status(uint64_t *frames, unsigned long nr_gframes,
  44. unsigned long max_nr_gframes,
  45. grant_status_t **__shared)
  46. {
  47. grant_status_t *shared = *__shared;
  48. unsigned long addr;
  49. unsigned long i;
  50. if (shared == NULL)
  51. *__shared = shared = gnttab_status_vm_area.area->addr;
  52. addr = (unsigned long)shared;
  53. for (i = 0; i < nr_gframes; i++) {
  54. set_pte_at(&init_mm, addr, gnttab_status_vm_area.ptes[i],
  55. mfn_pte(frames[i], PAGE_KERNEL));
  56. addr += PAGE_SIZE;
  57. }
  58. return 0;
  59. }
  60. void arch_gnttab_unmap(void *shared, unsigned long nr_gframes)
  61. {
  62. pte_t **ptes;
  63. unsigned long addr;
  64. unsigned long i;
  65. if (shared == gnttab_status_vm_area.area->addr)
  66. ptes = gnttab_status_vm_area.ptes;
  67. else
  68. ptes = gnttab_shared_vm_area.ptes;
  69. addr = (unsigned long)shared;
  70. for (i = 0; i < nr_gframes; i++) {
  71. set_pte_at(&init_mm, addr, ptes[i], __pte(0));
  72. addr += PAGE_SIZE;
  73. }
  74. }
  75. static int gnttab_apply(pte_t *pte, unsigned long addr, void *data)
  76. {
  77. struct gnttab_vm_area *area = data;
  78. area->ptes[area->idx++] = pte;
  79. return 0;
  80. }
  81. static int arch_gnttab_valloc(struct gnttab_vm_area *area, unsigned nr_frames)
  82. {
  83. area->ptes = kmalloc_array(nr_frames, sizeof(*area->ptes), GFP_KERNEL);
  84. if (area->ptes == NULL)
  85. return -ENOMEM;
  86. area->area = get_vm_area(PAGE_SIZE * nr_frames, VM_IOREMAP);
  87. if (!area->area)
  88. goto out_free_ptes;
  89. if (apply_to_page_range(&init_mm, (unsigned long)area->area->addr,
  90. PAGE_SIZE * nr_frames, gnttab_apply, area))
  91. goto out_free_vm_area;
  92. return 0;
  93. out_free_vm_area:
  94. free_vm_area(area->area);
  95. out_free_ptes:
  96. kfree(area->ptes);
  97. return -ENOMEM;
  98. }
  99. static void arch_gnttab_vfree(struct gnttab_vm_area *area)
  100. {
  101. free_vm_area(area->area);
  102. kfree(area->ptes);
  103. }
  104. int arch_gnttab_init(unsigned long nr_shared, unsigned long nr_status)
  105. {
  106. int ret;
  107. if (!xen_pv_domain())
  108. return 0;
  109. ret = arch_gnttab_valloc(&gnttab_shared_vm_area, nr_shared);
  110. if (ret < 0)
  111. return ret;
  112. /*
  113. * Always allocate the space for the status frames in case
  114. * we're migrated to a host with V2 support.
  115. */
  116. ret = arch_gnttab_valloc(&gnttab_status_vm_area, nr_status);
  117. if (ret < 0)
  118. goto err;
  119. return 0;
  120. err:
  121. arch_gnttab_vfree(&gnttab_shared_vm_area);
  122. return -ENOMEM;
  123. }
  124. #ifdef CONFIG_XEN_PVH
  125. #include <xen/events.h>
  126. #include <xen/xen-ops.h>
  127. static int __init xen_pvh_gnttab_setup(void)
  128. {
  129. if (!xen_pvh_domain())
  130. return -ENODEV;
  131. xen_auto_xlat_grant_frames.count = gnttab_max_grant_frames();
  132. return xen_xlate_map_ballooned_pages(&xen_auto_xlat_grant_frames.pfn,
  133. &xen_auto_xlat_grant_frames.vaddr,
  134. xen_auto_xlat_grant_frames.count);
  135. }
  136. /* Call it _before_ __gnttab_init as we need to initialize the
  137. * xen_auto_xlat_grant_frames first. */
  138. core_initcall(xen_pvh_gnttab_setup);
  139. #endif