You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xengrant.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <strings.h>
  26. #include <errno.h>
  27. #include <assert.h>
  28. #include <ipxe/io.h>
  29. #include <ipxe/xen.h>
  30. #include <ipxe/xengrant.h>
  31. /** @file
  32. *
  33. * Xen grant tables
  34. *
  35. */
  36. /** Grant table version to try setting
  37. *
  38. * Using version 1 grant tables limits guests to using 16TB of
  39. * grantable RAM, and prevents the use of subpage grants. Some
  40. * versions of the Xen hypervisor refuse to allow the grant table
  41. * version to be set after the first grant references have been
  42. * created, so the loaded operating system may be stuck with whatever
  43. * choice we make here. We therefore currently use version 2 grant
  44. * tables, since they give the most flexibility to the loaded OS.
  45. *
  46. * Current versions (7.2.0) of the Windows PV drivers have no support
  47. * for version 2 grant tables, and will merrily create version 1
  48. * entries in what the hypervisor believes to be a version 2 table.
  49. * This causes some confusion.
  50. *
  51. * Avoid this problem by attempting to use version 1 tables, since
  52. * otherwise we may render Windows unable to boot.
  53. *
  54. * Play nicely with other potential bootloaders by accepting either
  55. * version 1 or version 2 grant tables (if we are unable to set our
  56. * requested version).
  57. */
  58. #define XENGRANT_TRY_VERSION 1
  59. /**
  60. * Initialise grant table
  61. *
  62. * @v xen Xen hypervisor
  63. * @ret rc Return status code
  64. */
  65. int xengrant_init ( struct xen_hypervisor *xen ) {
  66. struct gnttab_query_size size;
  67. struct gnttab_set_version set_version;
  68. struct gnttab_get_version get_version;
  69. struct grant_entry_v1 *v1;
  70. union grant_entry_v2 *v2;
  71. unsigned int version;
  72. int xenrc;
  73. int rc;
  74. /* Get grant table size */
  75. size.dom = DOMID_SELF;
  76. if ( ( xenrc = xengrant_query_size ( xen, &size ) ) != 0 ) {
  77. rc = -EXEN ( xenrc );
  78. DBGC ( xen, "XENGRANT could not get table size: %s\n",
  79. strerror ( rc ) );
  80. return rc;
  81. }
  82. xen->grant.len = ( size.nr_frames * PAGE_SIZE );
  83. /* Set grant table version, if applicable */
  84. set_version.version = XENGRANT_TRY_VERSION;
  85. if ( ( xenrc = xengrant_set_version ( xen, &set_version ) ) != 0 ) {
  86. rc = -EXEN ( xenrc );
  87. DBGC ( xen, "XENGRANT could not set version %d: %s\n",
  88. XENGRANT_TRY_VERSION, strerror ( rc ) );
  89. /* Continue; use whatever version is current */
  90. }
  91. /* Get grant table version */
  92. get_version.dom = DOMID_SELF;
  93. get_version.pad = 0;
  94. if ( ( xenrc = xengrant_get_version ( xen, &get_version ) ) == 0 ) {
  95. version = get_version.version;
  96. switch ( version ) {
  97. case 0:
  98. /* Version not yet specified: will be version 1 */
  99. version = 1;
  100. break;
  101. case 1 :
  102. /* Version 1 table: nothing special to do */
  103. break;
  104. case 2:
  105. /* Version 2 table: configure shift appropriately */
  106. xen->grant.shift = ( fls ( sizeof ( *v2 ) /
  107. sizeof ( *v1 ) ) - 1 );
  108. break;
  109. default:
  110. /* Unsupported version */
  111. DBGC ( xen, "XENGRANT detected unsupported version "
  112. "%d\n", version );
  113. return -ENOTSUP;
  114. }
  115. } else {
  116. rc = -EXEN ( xenrc );
  117. DBGC ( xen, "XENGRANT could not get version (assuming v1): "
  118. "%s\n", strerror ( rc ) );
  119. version = 1;
  120. }
  121. DBGC ( xen, "XENGRANT using v%d table with %d entries\n",
  122. version, xengrant_entries ( xen ) );
  123. return 0;
  124. }
  125. /**
  126. * Allocate grant references
  127. *
  128. * @v xen Xen hypervisor
  129. * @v refs Grant references to fill in
  130. * @v count Number of references
  131. * @ret rc Return status code
  132. */
  133. int xengrant_alloc ( struct xen_hypervisor *xen, grant_ref_t *refs,
  134. unsigned int count ) {
  135. struct grant_entry_header *hdr;
  136. unsigned int entries = xengrant_entries ( xen );
  137. unsigned int mask = ( entries - 1 );
  138. unsigned int check = 0;
  139. unsigned int avail;
  140. unsigned int ref;
  141. /* Fail unless we have enough references available */
  142. avail = ( entries - xen->grant.used - GNTTAB_NR_RESERVED_ENTRIES );
  143. if ( avail < count ) {
  144. DBGC ( xen, "XENGRANT cannot allocate %d references (only %d "
  145. "of %d available)\n", count, avail, entries );
  146. return -ENOBUFS;
  147. }
  148. DBGC ( xen, "XENGRANT allocating %d references (from %d of %d "
  149. "available)\n", count, avail, entries );
  150. /* Update number of references used */
  151. xen->grant.used += count;
  152. /* Find unused references */
  153. for ( ref = xen->grant.ref ; count ; ref = ( ( ref + 1 ) & mask ) ) {
  154. /* Sanity check */
  155. assert ( check++ < entries );
  156. /* Skip reserved references */
  157. if ( ref < GNTTAB_NR_RESERVED_ENTRIES )
  158. continue;
  159. /* Skip in-use references */
  160. hdr = xengrant_header ( xen, ref );
  161. if ( readw ( &hdr->flags ) & GTF_type_mask )
  162. continue;
  163. if ( readw ( &hdr->domid ) == DOMID_SELF )
  164. continue;
  165. /* Zero reference */
  166. xengrant_zero ( xen, hdr );
  167. /* Mark reference as in-use. We leave the flags as
  168. * empty (to avoid creating a valid grant table entry)
  169. * and set the domid to DOMID_SELF.
  170. */
  171. writew ( DOMID_SELF, &hdr->domid );
  172. DBGC2 ( xen, "XENGRANT allocated ref %d\n", ref );
  173. /* Record reference */
  174. refs[--count] = ref;
  175. }
  176. /* Update cursor */
  177. xen->grant.ref = ref;
  178. return 0;
  179. }
  180. /**
  181. * Free grant references
  182. *
  183. * @v xen Xen hypervisor
  184. * @v refs Grant references
  185. * @v count Number of references
  186. */
  187. void xengrant_free ( struct xen_hypervisor *xen, grant_ref_t *refs,
  188. unsigned int count ) {
  189. struct grant_entry_header *hdr;
  190. unsigned int ref;
  191. unsigned int i;
  192. /* Free references */
  193. for ( i = 0 ; i < count ; i++ ) {
  194. /* Sanity check */
  195. ref = refs[i];
  196. assert ( ref < xengrant_entries ( xen ) );
  197. /* Zero reference */
  198. hdr = xengrant_header ( xen, ref );
  199. xengrant_zero ( xen, hdr );
  200. DBGC2 ( xen, "XENGRANT freed ref %d\n", ref );
  201. }
  202. }