iPXE
Macros | Functions
xengrant.c File Reference

Xen grant tables. More...

#include <stdint.h>
#include <strings.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/io.h>
#include <ipxe/xen.h>
#include <ipxe/xengrant.h>

Go to the source code of this file.

Macros

#define XENGRANT_TRY_VERSION   1
 Grant table version to try setting. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int xengrant_init (struct xen_hypervisor *xen)
 Initialise grant table. More...
 
int xengrant_alloc (struct xen_hypervisor *xen, grant_ref_t *refs, unsigned int count)
 Allocate grant references. More...
 
void xengrant_free (struct xen_hypervisor *xen, grant_ref_t *refs, unsigned int count)
 Free grant references. More...
 

Detailed Description

Xen grant tables.

Definition in file xengrant.c.

Macro Definition Documentation

◆ XENGRANT_TRY_VERSION

#define XENGRANT_TRY_VERSION   1

Grant table version to try setting.

Using version 1 grant tables limits guests to using 16TB of grantable RAM, and prevents the use of subpage grants. Some versions of the Xen hypervisor refuse to allow the grant table version to be set after the first grant references have been created, so the loaded operating system may be stuck with whatever choice we make here. We therefore currently use version 2 grant tables, since they give the most flexibility to the loaded OS.

Current versions (7.2.0) of the Windows PV drivers have no support for version 2 grant tables, and will merrily create version 1 entries in what the hypervisor believes to be a version 2 table. This causes some confusion.

Avoid this problem by attempting to use version 1 tables, since otherwise we may render Windows unable to boot.

Play nicely with other potential bootloaders by accepting either version 1 or version 2 grant tables (if we are unable to set our requested version).

Definition at line 62 of file xengrant.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ xengrant_init()

int xengrant_init ( struct xen_hypervisor xen)

Initialise grant table.

Parameters
xenXen hypervisor
Return values
rcReturn status code

Definition at line 70 of file xengrant.c.

70  {
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;
76  unsigned int version;
77  int xenrc;
78  int rc;
79 
80  /* Get grant table size */
81  size.dom = DOMID_SELF;
82  if ( ( xenrc = xengrant_query_size ( xen, &size ) ) != 0 ) {
83  rc = -EXEN ( xenrc );
84  DBGC ( xen, "XENGRANT could not get table size: %s\n",
85  strerror ( rc ) );
86  return rc;
87  }
88  xen->grant.len = ( size.nr_frames * PAGE_SIZE );
89 
90  /* Set grant table version, if applicable */
91  set_version.version = XENGRANT_TRY_VERSION;
92  if ( ( xenrc = xengrant_set_version ( xen, &set_version ) ) != 0 ) {
93  rc = -EXEN ( xenrc );
94  DBGC ( xen, "XENGRANT could not set version %d: %s\n",
96  /* Continue; use whatever version is current */
97  }
98 
99  /* Get grant table version */
100  get_version.dom = DOMID_SELF;
101  get_version.pad = 0;
102  if ( ( xenrc = xengrant_get_version ( xen, &get_version ) ) == 0 ) {
103  version = get_version.version;
104  switch ( version ) {
105 
106  case 0:
107  /* Version not yet specified: will be version 1 */
108  version = 1;
109  break;
110 
111  case 1 :
112  /* Version 1 table: nothing special to do */
113  break;
114 
115  case 2:
116  /* Version 2 table: configure shift appropriately */
117  xen->grant.shift = ( fls ( sizeof ( *v2 ) /
118  sizeof ( *v1 ) ) - 1 );
119  break;
120 
121  default:
122  /* Unsupported version */
123  DBGC ( xen, "XENGRANT detected unsupported version "
124  "%d\n", version );
125  return -ENOTSUP;
126 
127  }
128  } else {
129  rc = -EXEN ( xenrc );
130  DBGC ( xen, "XENGRANT could not get version (assuming v1): "
131  "%s\n", strerror ( rc ) );
132  version = 1;
133  }
134 
135  DBGC ( xen, "XENGRANT using v%d table with %d entries\n",
136  version, xengrant_entries ( xen ) );
137  return 0;
138 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
v1
Definition: xengrant.h:93
#define PAGE_SIZE
Page size.
Definition: io.h:27
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
#define EXEN(xenrc)
Convert a Xen status code to an iPXE status code.
Definition: xen.h:87
u32 version
Driver version.
Definition: ath9k_hw.c:1983
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
Definition: grant_table.h:117
union grant_entry_v2 * v2
Definition: xengrant.h:178
#define XENGRANT_TRY_VERSION
Grant table version to try setting.
Definition: xengrant.c:62
unsigned int shift
Entry size shift (for later version tables)
Definition: xen.h:35
struct xen_grant grant
Grant table.
Definition: xen.h:57
#define DOMID_SELF
Definition: xen.h:567
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
size_t len
Total grant table length.
Definition: xen.h:33
#define fls(x)
Find last (i.e.
Definition: strings.h:166

References DBGC, DOMID_SELF, ENOTSUP, EXEN, fls, xen_hypervisor::grant, xen_grant::len, PAGE_SIZE, rc, xen_grant::shift, size, strerror(), v1, v2, version, and XENGRANT_TRY_VERSION.

Referenced by hvm_map_grant().

◆ xengrant_alloc()

int xengrant_alloc ( struct xen_hypervisor xen,
grant_ref_t refs,
unsigned int  count 
)

Allocate grant references.

Parameters
xenXen hypervisor
refsGrant references to fill in
countNumber of references
Return values
rcReturn status code

Definition at line 148 of file xengrant.c.

149  {
150  struct grant_entry_header *hdr;
151  unsigned int entries = xengrant_entries ( xen );
152  unsigned int mask = ( entries - 1 );
153  unsigned int check = 0;
154  unsigned int avail;
155  unsigned int ref;
156 
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 );
162  return -ENOBUFS;
163  }
164  DBGC ( xen, "XENGRANT allocating %d references (from %d of %d "
165  "available)\n", count, avail, entries );
166 
167  /* Update number of references used */
168  xen->grant.used += count;
169 
170  /* Find unused references */
171  for ( ref = xen->grant.ref ; count ; ref = ( ( ref + 1 ) & mask ) ) {
172 
173  /* Sanity check */
174  assert ( check++ < entries );
175 
176  /* Skip reserved references */
178  continue;
179 
180  /* Skip in-use references */
181  hdr = xengrant_header ( xen, ref );
182  if ( readw ( &hdr->flags ) & GTF_type_mask )
183  continue;
184  if ( readw ( &hdr->domid ) == DOMID_SELF )
185  continue;
186 
187  /* Zero reference */
188  xengrant_zero ( xen, hdr );
189 
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.
193  */
194  writew ( DOMID_SELF, &hdr->domid );
195  DBGC2 ( xen, "XENGRANT allocated ref %d\n", ref );
196 
197  /* Record reference */
198  refs[--count] = ref;
199  }
200 
201  /* Update cursor */
202  xen->grant.ref = ref;
203 
204  return 0;
205 }
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28
uint16_t readw(volatile uint16_t *io_addr)
Read 16-bit word from memory-mapped device.
#define DBGC(...)
Definition: compiler.h:505
#define GNTTAB_NR_RESERVED_ENTRIES
Definition: grant_table.h:135
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint16_t count
Number of entries.
Definition: ena.h:22
#define GTF_type_mask
Definition: grant_table.h:152
static const char grant_ref_t unsigned int struct io_buffer grant_ref_t * refs
Definition: netfront.h:93
unsigned int ref
Most recently used grant reference.
Definition: xen.h:39
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
#define DBGC2(...)
Definition: compiler.h:522
struct xen_grant grant
Grant table.
Definition: xen.h:57
#define writew
Definition: w89c840.c:159
#define DOMID_SELF
Definition: xen.h:567
static const char grant_ref_t ref
Definition: netfront.h:91
static void xengrant_zero(struct xen_hypervisor *xen, struct grant_entry_header *hdr)
Zero grant table entry.
Definition: xengrant.h:127
unsigned int used
Number of grant table entries in use.
Definition: xen.h:37

References assert(), count, DBGC, DBGC2, DOMID_SELF, ENOBUFS, GNTTAB_NR_RESERVED_ENTRIES, xen_hypervisor::grant, GTF_type_mask, hdr, readw(), xen_grant::ref, ref, refs, xen_grant::used, writew, and xengrant_zero().

Referenced by netfront_probe().

◆ xengrant_free()

void xengrant_free ( struct xen_hypervisor xen,
grant_ref_t refs,
unsigned int  count 
)

Free grant references.

Parameters
xenXen hypervisor
refsGrant references
countNumber of references

Definition at line 214 of file xengrant.c.

215  {
216  struct grant_entry_header *hdr;
217  unsigned int ref;
218  unsigned int i;
219 
220  /* Free references */
221  for ( i = 0 ; i < count ; i++ ) {
222 
223  /* Sanity check */
224  ref = refs[i];
225  assert ( ref < xengrant_entries ( xen ) );
226 
227  /* Zero reference */
228  hdr = xengrant_header ( xen, ref );
229  xengrant_zero ( xen, hdr );
230  DBGC2 ( xen, "XENGRANT freed ref %d\n", ref );
231  }
232 }
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint16_t count
Number of entries.
Definition: ena.h:22
static const char grant_ref_t unsigned int struct io_buffer grant_ref_t * refs
Definition: netfront.h:93
#define DBGC2(...)
Definition: compiler.h:522
static const char grant_ref_t ref
Definition: netfront.h:91
static void xengrant_zero(struct xen_hypervisor *xen, struct grant_entry_header *hdr)
Zero grant table entry.
Definition: xengrant.h:127

References assert(), count, DBGC2, hdr, ref, refs, and xengrant_zero().

Referenced by netfront_probe(), and netfront_remove().