grant-table.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <asm/pgtable.h>
  22. static struct gnttab_vm_area {
  23. struct vm_struct *area;
  24. pte_t **ptes;
  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 arch_gnttab_valloc(struct gnttab_vm_area *area, unsigned nr_frames)
  76. {
  77. area->ptes = kmalloc_array(nr_frames, sizeof(*area->ptes), GFP_KERNEL);
  78. if (area->ptes == NULL)
  79. return -ENOMEM;
  80. area->area = alloc_vm_area(PAGE_SIZE * nr_frames, area->ptes);
  81. if (area->area == NULL) {
  82. kfree(area->ptes);
  83. return -ENOMEM;
  84. }
  85. return 0;
  86. }
  87. static void arch_gnttab_vfree(struct gnttab_vm_area *area)
  88. {
  89. free_vm_area(area->area);
  90. kfree(area->ptes);
  91. }
  92. int arch_gnttab_init(unsigned long nr_shared, unsigned long nr_status)
  93. {
  94. int ret;
  95. if (!xen_pv_domain())
  96. return 0;
  97. ret = arch_gnttab_valloc(&gnttab_shared_vm_area, nr_shared);
  98. if (ret < 0)
  99. return ret;
  100. /*
  101. * Always allocate the space for the status frames in case
  102. * we're migrated to a host with V2 support.
  103. */
  104. ret = arch_gnttab_valloc(&gnttab_status_vm_area, nr_status);
  105. if (ret < 0)
  106. goto err;
  107. return 0;
  108. err:
  109. arch_gnttab_vfree(&gnttab_shared_vm_area);
  110. return -ENOMEM;
  111. }
  112. #ifdef CONFIG_XEN_PVH
  113. #include <xen/events.h>
  114. #include <xen/xen-ops.h>
  115. static int __init xen_pvh_gnttab_setup(void)
  116. {
  117. if (!xen_pvh_domain())
  118. return -ENODEV;
  119. xen_auto_xlat_grant_frames.count = gnttab_max_grant_frames();
  120. return xen_xlate_map_ballooned_pages(&xen_auto_xlat_grant_frames.pfn,
  121. &xen_auto_xlat_grant_frames.vaddr,
  122. xen_auto_xlat_grant_frames.count);
  123. }
  124. /* Call it _before_ __gnttab_init as we need to initialize the
  125. * xen_auto_xlat_grant_frames first. */
  126. core_initcall(xen_pvh_gnttab_setup);
  127. #endif