2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 #include <ipxe/xengrant.h>
40 /** Grant table version to try setting
42 * Using version 1 grant tables limits guests to using 16TB of
43 * grantable RAM, and prevents the use of subpage grants. Some
44 * versions of the Xen hypervisor refuse to allow the grant table
45 * version to be set after the first grant references have been
46 * created, so the loaded operating system may be stuck with whatever
47 * choice we make here. We therefore currently use version 2 grant
48 * tables, since they give the most flexibility to the loaded OS.
50 * Current versions (7.2.0) of the Windows PV drivers have no support
51 * for version 2 grant tables, and will merrily create version 1
52 * entries in what the hypervisor believes to be a version 2 table.
53 * This causes some confusion.
55 * Avoid this problem by attempting to use version 1 tables, since
56 * otherwise we may render Windows unable to boot.
58 * Play nicely with other potential bootloaders by accepting either
59 * version 1 or version 2 grant tables (if we are unable to set our
62 #define XENGRANT_TRY_VERSION 1
65 * Initialise grant table
67 * @v xen Xen hypervisor
68 * @ret rc Return status code
70 int xengrant_init ( struct xen_hypervisor *xen ) {
71 struct gnttab_query_size size;
72 struct gnttab_set_version set_version;
73 struct gnttab_get_version get_version;
74 struct grant_entry_v1 *v1;
75 union grant_entry_v2 *v2;
80 /* Get grant table size */
81 size.dom = DOMID_SELF;
82 if ( ( xenrc = xengrant_query_size ( xen, &size ) ) != 0 ) {
84 DBGC ( xen, "XENGRANT could not get table size: %s\n",
88 xen->grant.len = ( size.nr_frames * PAGE_SIZE );
90 /* Set grant table version, if applicable */
91 set_version.version = XENGRANT_TRY_VERSION;
92 if ( ( xenrc = xengrant_set_version ( xen, &set_version ) ) != 0 ) {
94 DBGC ( xen, "XENGRANT could not set version %d: %s\n",
95 XENGRANT_TRY_VERSION, strerror ( rc ) );
96 /* Continue; use whatever version is current */
99 /* Get grant table version */
100 get_version.dom = DOMID_SELF;
102 if ( ( xenrc = xengrant_get_version ( xen, &get_version ) ) == 0 ) {
103 version = get_version.version;
107 /* Version not yet specified: will be version 1 */
112 /* Version 1 table: nothing special to do */
116 /* Version 2 table: configure shift appropriately */
117 xen->grant.shift = ( fls ( sizeof ( *v2 ) /
118 sizeof ( *v1 ) ) - 1 );
122 /* Unsupported version */
123 DBGC ( xen, "XENGRANT detected unsupported version "
129 rc = -EXEN ( xenrc );
130 DBGC ( xen, "XENGRANT could not get version (assuming v1): "
131 "%s\n", strerror ( rc ) );
135 DBGC ( xen, "XENGRANT using v%d table with %d entries\n",
136 version, xengrant_entries ( xen ) );
141 * Allocate grant references
143 * @v xen Xen hypervisor
144 * @v refs Grant references to fill in
145 * @v count Number of references
146 * @ret rc Return status code
148 int xengrant_alloc ( struct xen_hypervisor *xen, grant_ref_t *refs,
149 unsigned int count ) {
150 struct grant_entry_header *hdr;
151 unsigned int entries = xengrant_entries ( xen );
152 unsigned int mask = ( entries - 1 );
153 unsigned int check = 0;
157 /* Fail unless we have enough references available */
158 avail = ( entries - xen->grant.used - GNTTAB_NR_RESERVED_ENTRIES );
159 if ( avail < count ) {
160 DBGC ( xen, "XENGRANT cannot allocate %d references (only %d "
161 "of %d available)\n", count, avail, entries );
164 DBGC ( xen, "XENGRANT allocating %d references (from %d of %d "
165 "available)\n", count, avail, entries );
167 /* Update number of references used */
168 xen->grant.used += count;
170 /* Find unused references */
171 for ( ref = xen->grant.ref ; count ; ref = ( ( ref + 1 ) & mask ) ) {
174 assert ( check++ < entries );
176 /* Skip reserved references */
177 if ( ref < GNTTAB_NR_RESERVED_ENTRIES )
180 /* Skip in-use references */
181 hdr = xengrant_header ( xen, ref );
182 if ( readw ( &hdr->flags ) & GTF_type_mask )
184 if ( readw ( &hdr->domid ) == DOMID_SELF )
188 xengrant_zero ( xen, hdr );
190 /* Mark reference as in-use. We leave the flags as
191 * empty (to avoid creating a valid grant table entry)
192 * and set the domid to DOMID_SELF.
194 writew ( DOMID_SELF, &hdr->domid );
195 DBGC2 ( xen, "XENGRANT allocated ref %d\n", ref );
197 /* Record reference */
202 xen->grant.ref = ref;
208 * Free grant references
210 * @v xen Xen hypervisor
211 * @v refs Grant references
212 * @v count Number of references
214 void xengrant_free ( struct xen_hypervisor *xen, grant_ref_t *refs,
215 unsigned int count ) {
216 struct grant_entry_header *hdr;
220 /* Free references */
221 for ( i = 0 ; i < count ; i++ ) {
225 assert ( ref < xengrant_entries ( xen ) );
228 hdr = xengrant_header ( xen, ref );
229 xengrant_zero ( xen, hdr );
230 DBGC2 ( xen, "XENGRANT freed ref %d\n", ref );