Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

xengrant.c 6.2KB

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