gnttab.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if ((flags = nflags) & (GTF_reading | GTF_writing)) {
  86. printf("WARNING: g.e. still in use! (%x)\n", flags);
  87. return 0;
  88. }
  89. } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
  90. flags);
  91. put_free_entry(ref);
  92. return 1;
  93. }
  94. grant_ref_t gnttab_alloc_and_grant(void **map)
  95. {
  96. unsigned long mfn;
  97. grant_ref_t gref;
  98. *map = (void *)memalign(PAGE_SIZE, PAGE_SIZE);
  99. mfn = virt_to_mfn(*map);
  100. gref = gnttab_grant_access(0, mfn, 0);
  101. return gref;
  102. }
  103. static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
  104. const char *gnttabop_error(int16_t status)
  105. {
  106. status = -status;
  107. if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
  108. return "bad status";
  109. else
  110. return gnttabop_error_msgs[status];
  111. }
  112. /* Get Xen's suggested physical page assignments for the grant table. */
  113. void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz)
  114. {
  115. const void *blob = gd->fdt_blob;
  116. struct fdt_resource res;
  117. int mem;
  118. mem = fdt_node_offset_by_compatible(blob, -1, "xen,xen");
  119. if (mem < 0) {
  120. printf("No xen,xen compatible found\n");
  121. BUG();
  122. }
  123. mem = fdt_get_resource(blob, mem, "reg", 0, &res);
  124. if (mem == -FDT_ERR_NOTFOUND) {
  125. printf("No grant table base in the device tree\n");
  126. BUG();
  127. }
  128. *gnttab_base = (phys_addr_t)res.start;
  129. if (gnttab_sz)
  130. *gnttab_sz = (phys_size_t)(res.end - res.start + 1);
  131. debug("FDT suggests grant table base at %llx\n",
  132. *gnttab_base);
  133. }
  134. void init_gnttab(void)
  135. {
  136. struct xen_add_to_physmap xatp;
  137. struct gnttab_setup_table setup;
  138. xen_pfn_t frames[NR_GRANT_FRAMES];
  139. int i, rc;
  140. debug("%s\n", __func__);
  141. for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
  142. put_free_entry(i);
  143. get_gnttab_base((phys_addr_t *)&gnttab_table, NULL);
  144. for (i = 0; i < NR_GRANT_FRAMES; i++) {
  145. xatp.domid = DOMID_SELF;
  146. xatp.size = 0;
  147. xatp.space = XENMAPSPACE_grant_table;
  148. xatp.idx = i;
  149. xatp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
  150. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
  151. if (rc)
  152. printf("XENMEM_add_to_physmap failed; status = %d\n",
  153. rc);
  154. BUG_ON(rc != 0);
  155. }
  156. setup.dom = DOMID_SELF;
  157. setup.nr_frames = NR_GRANT_FRAMES;
  158. set_xen_guest_handle(setup.frame_list, frames);
  159. }
  160. void fini_gnttab(void)
  161. {
  162. struct xen_remove_from_physmap xrtp;
  163. struct gnttab_setup_table setup;
  164. int i, rc;
  165. debug("%s\n", __func__);
  166. for (i = 0; i < NR_GRANT_FRAMES; i++) {
  167. xrtp.domid = DOMID_SELF;
  168. xrtp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
  169. rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrtp);
  170. if (rc)
  171. printf("XENMEM_remove_from_physmap failed; status = %d\n",
  172. rc);
  173. BUG_ON(rc != 0);
  174. }
  175. setup.dom = DOMID_SELF;
  176. setup.nr_frames = 0;
  177. }