gnttab.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) 2006 - Cambridge University
  4. * (C) 2020 - EPAM Systems Inc.
  5. *
  6. * File: gnttab.c [1]
  7. * Author: Steven Smith (sos22@cam.ac.uk)
  8. * Changes: Grzegorz Milos (gm281@cam.ac.uk)
  9. *
  10. * Date: July 2006
  11. *
  12. * Description: Simple grant tables implementation. About as stupid as it's
  13. * possible to be and still work.
  14. *
  15. * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
  16. */
  17. #include <common.h>
  18. #include <linux/compiler.h>
  19. #include <log.h>
  20. #include <malloc.h>
  21. #include <asm/armv8/mmu.h>
  22. #include <asm/io.h>
  23. #include <asm/xen/system.h>
  24. #include <linux/bug.h>
  25. #include <xen/gnttab.h>
  26. #include <xen/hvm.h>
  27. #include <xen/interface/memory.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #define NR_RESERVED_ENTRIES 8
  30. /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
  31. #define NR_GRANT_FRAMES 1
  32. #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(struct grant_entry_v1))
  33. static struct grant_entry_v1 *gnttab_table;
  34. static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
  35. static void put_free_entry(grant_ref_t ref)
  36. {
  37. unsigned long flags;
  38. local_irq_save(flags);
  39. gnttab_list[ref] = gnttab_list[0];
  40. gnttab_list[0] = ref;
  41. local_irq_restore(flags);
  42. }
  43. static grant_ref_t get_free_entry(void)
  44. {
  45. unsigned int ref;
  46. unsigned long flags;
  47. local_irq_save(flags);
  48. ref = gnttab_list[0];
  49. BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
  50. gnttab_list[0] = gnttab_list[ref];
  51. local_irq_restore(flags);
  52. return ref;
  53. }
  54. /**
  55. * gnttab_grant_access() - Allow access to the given frame.
  56. * The function creates an entry in the grant table according
  57. * to the specified parameters.
  58. * @domid: the id of the domain for which access is allowed
  59. * @frame: the number of the shared frame
  60. * @readonly: determines whether the frame is shared read-only or read-write
  61. *
  62. * Return: relevant grant reference
  63. */
  64. grant_ref_t gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
  65. {
  66. grant_ref_t ref;
  67. ref = get_free_entry();
  68. gnttab_table[ref].frame = frame;
  69. gnttab_table[ref].domid = domid;
  70. wmb();
  71. readonly *= GTF_readonly;
  72. gnttab_table[ref].flags = GTF_permit_access | readonly;
  73. return ref;
  74. }
  75. /**
  76. * gnttab_end_access() - End of memory sharing. The function invalidates
  77. * the entry in the grant table.
  78. */
  79. int gnttab_end_access(grant_ref_t ref)
  80. {
  81. u16 flags, nflags;
  82. BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
  83. nflags = gnttab_table[ref].flags;
  84. do {
  85. flags = nflags;
  86. if ((flags) & (GTF_reading | GTF_writing)) {
  87. printf("WARNING: g.e. still in use! (%x)\n", flags);
  88. return 0;
  89. }
  90. } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
  91. flags);
  92. put_free_entry(ref);
  93. return 1;
  94. }
  95. grant_ref_t gnttab_alloc_and_grant(void **map)
  96. {
  97. unsigned long mfn;
  98. grant_ref_t gref;
  99. *map = (void *)memalign(PAGE_SIZE, PAGE_SIZE);
  100. mfn = virt_to_mfn(*map);
  101. gref = gnttab_grant_access(0, mfn, 0);
  102. return gref;
  103. }
  104. static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
  105. const char *gnttabop_error(int16_t status)
  106. {
  107. status = -status;
  108. if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
  109. return "bad status";
  110. else
  111. return gnttabop_error_msgs[status];
  112. }
  113. /* Get Xen's suggested physical page assignments for the grant table. */
  114. void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz)
  115. {
  116. const void *blob = gd->fdt_blob;
  117. struct fdt_resource res;
  118. int mem;
  119. mem = fdt_node_offset_by_compatible(blob, -1, "xen,xen");
  120. if (mem < 0) {
  121. printf("No xen,xen compatible found\n");
  122. BUG();
  123. }
  124. mem = fdt_get_resource(blob, mem, "reg", 0, &res);
  125. if (mem == -FDT_ERR_NOTFOUND) {
  126. printf("No grant table base in the device tree\n");
  127. BUG();
  128. }
  129. *gnttab_base = (phys_addr_t)res.start;
  130. if (gnttab_sz)
  131. *gnttab_sz = (phys_size_t)(res.end - res.start + 1);
  132. debug("FDT suggests grant table base at %llx\n",
  133. *gnttab_base);
  134. }
  135. void init_gnttab(void)
  136. {
  137. struct xen_add_to_physmap xatp;
  138. struct gnttab_setup_table setup;
  139. xen_pfn_t frames[NR_GRANT_FRAMES];
  140. int i, rc;
  141. debug("%s\n", __func__);
  142. for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
  143. put_free_entry(i);
  144. get_gnttab_base((phys_addr_t *)&gnttab_table, NULL);
  145. for (i = 0; i < NR_GRANT_FRAMES; i++) {
  146. xatp.domid = DOMID_SELF;
  147. xatp.size = 0;
  148. xatp.space = XENMAPSPACE_grant_table;
  149. xatp.idx = i;
  150. xatp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
  151. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
  152. if (rc)
  153. printf("XENMEM_add_to_physmap failed; status = %d\n",
  154. rc);
  155. BUG_ON(rc != 0);
  156. }
  157. setup.dom = DOMID_SELF;
  158. setup.nr_frames = NR_GRANT_FRAMES;
  159. set_xen_guest_handle(setup.frame_list, frames);
  160. }
  161. void fini_gnttab(void)
  162. {
  163. struct xen_remove_from_physmap xrtp;
  164. struct gnttab_setup_table setup;
  165. int i, rc;
  166. debug("%s\n", __func__);
  167. for (i = 0; i < NR_GRANT_FRAMES; i++) {
  168. xrtp.domid = DOMID_SELF;
  169. xrtp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
  170. rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrtp);
  171. if (rc)
  172. printf("XENMEM_remove_from_physmap failed; status = %d\n",
  173. rc);
  174. BUG_ON(rc != 0);
  175. }
  176. setup.dom = DOMID_SELF;
  177. setup.nr_frames = 0;
  178. }