iPXE
int15.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 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 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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <realmode.h>
31 #include <bios.h>
32 #include <memsizes.h>
33 #include <ipxe/io.h>
34 #include <ipxe/memmap.h>
35 
36 /**
37  * @file
38  *
39  * Memory mapping
40  *
41  */
42 
43 /** Magic value for INT 15,e820 calls */
44 #define SMAP ( 0x534d4150 )
45 
46 /** An INT 15,e820 memory map entry */
47 struct e820_entry {
48  /** Start of region */
50  /** Length of region */
51  uint64_t len;
52  /** Type of region */
53  uint32_t type;
54  /** Extended attributes (optional) */
56 } __attribute__ (( packed ));
57 
58 #define E820_TYPE_RAM 1 /**< Normal memory */
59 #define E820_TYPE_RESERVED 2 /**< Reserved and unavailable */
60 #define E820_TYPE_ACPI 3 /**< ACPI reclaim memory */
61 #define E820_TYPE_NVS 4 /**< ACPI NVS memory */
62 
63 #define E820_ATTR_ENABLED 0x00000001UL
64 #define E820_ATTR_NONVOLATILE 0x00000002UL
65 #define E820_ATTR_UNKNOWN 0xfffffffcUL
66 
67 #define E820_MIN_SIZE 20
68 
69 /** Buffer for INT 15,e820 calls */
70 static struct e820_entry __bss16 ( e820buf );
71 #define e820buf __use_data16 ( e820buf )
72 
73 /** We are running during POST; inhibit INT 15,e820 and INT 15,e801 */
75 #define memmap_post __use_data16 ( memmap_post )
76 
77 /**
78  * Get size of extended memory via INT 15,e801
79  *
80  * @ret extmem Extended memory size, in kB, or 0
81  */
82 static unsigned int extmemsize_e801 ( void ) {
83  uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
84  uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
85  unsigned int flags;
86  unsigned int extmem;
87 
88  /* Inhibit INT 15,e801 during POST */
89  if ( memmap_post ) {
90  DBG ( "INT 15,e801 not available during POST\n" );
91  return 0;
92  }
93 
94  __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
95  "int $0x15\n\t"
96  "pushfw\n\t"
97  "popw %w0\n\t" )
98  : "=R" ( flags ),
99  "=a" ( extmem_1m_to_16m_k ),
100  "=b" ( extmem_16m_plus_64k ),
101  "=c" ( confmem_1m_to_16m_k ),
102  "=d" ( confmem_16m_plus_64k )
103  : "a" ( 0xe801 ) );
104 
105  if ( flags & CF ) {
106  DBG ( "INT 15,e801 failed with CF set\n" );
107  return 0;
108  }
109 
110  if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
111  DBG ( "INT 15,e801 extmem=0, using confmem\n" );
112  extmem_1m_to_16m_k = confmem_1m_to_16m_k;
113  extmem_16m_plus_64k = confmem_16m_plus_64k;
114  }
115 
116  extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
117  DBG ( "INT 15,e801 extended memory size %d+64*%d=%d kB "
118  "[100000,%llx)\n", extmem_1m_to_16m_k, extmem_16m_plus_64k,
119  extmem, ( 0x100000 + ( ( ( uint64_t ) extmem ) * 1024 ) ) );
120 
121  /* Sanity check. Some BIOSes report the entire 4GB address
122  * space as available, which cannot be correct (since that
123  * would leave no address space available for 32-bit PCI
124  * BARs).
125  */
126  if ( extmem == ( 0x400000 - 0x400 ) ) {
127  DBG ( "INT 15,e801 reported whole 4GB; assuming insane\n" );
128  return 0;
129  }
130 
131  return extmem;
132 }
133 
134 /**
135  * Get size of extended memory via INT 15,88
136  *
137  * @ret extmem Extended memory size, in kB
138  */
139 static unsigned int extmemsize_88 ( void ) {
140  uint16_t extmem;
141 
142  /* Ignore CF; it is not reliable for this call */
143  __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
144  : "=a" ( extmem ) : "a" ( 0x8800 ) );
145 
146  DBG ( "INT 15,88 extended memory size %d kB [100000, %x)\n",
147  extmem, ( 0x100000 + ( extmem * 1024 ) ) );
148  return extmem;
149 }
150 
151 /**
152  * Get size of extended memory
153  *
154  * @ret extmem Extended memory size, in kB
155  *
156  * Note that this is only an approximation; for an accurate picture,
157  * use the E820 memory map obtained via memmap_describe();
158  */
159 unsigned int extmemsize ( void ) {
160  unsigned int extmem_e801;
161  unsigned int extmem_88;
162 
163  /* Try INT 15,e801 first, then fall back to INT 15,88 */
164  extmem_88 = extmemsize_88();
165  extmem_e801 = extmemsize_e801();
166  return ( extmem_e801 ? extmem_e801 : extmem_88 );
167 }
168 
169 /**
170  * Get e820 memory map
171  *
172  * @v region Memory region of interest to be updated
173  * @ret rc Return status code
174  */
175 static int meme820 ( struct memmap_region *region ) {
176  unsigned int count = 0;
177  uint64_t start = 0;
178  uint64_t len = 0;
179  uint32_t next = 0;
180  uint32_t smap;
181  uint32_t size;
182  unsigned int flags;
183  unsigned int discard_D;
184 
185  /* Inhibit INT 15,e820 during POST */
186  if ( memmap_post ) {
187  DBG ( "INT 15,e820 not available during POST\n" );
188  return -ENOTTY;
189  }
190 
191  /* Clear the E820 buffer. Do this once before starting,
192  * rather than on each call; some BIOSes rely on the contents
193  * being preserved between calls.
194  */
195  memset ( &e820buf, 0, sizeof ( e820buf ) );
196 
197  do {
198  /* Some BIOSes corrupt %esi for fun. Guard against
199  * this by telling gcc that all non-output registers
200  * may be corrupted.
201  */
202  __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t"
203  "stc\n\t"
204  "int $0x15\n\t"
205  "pushfw\n\t"
206  "popw %%dx\n\t"
207  "popl %%ebp\n\t" )
208  : "=a" ( smap ), "=b" ( next ),
209  "=c" ( size ), "=d" ( flags ),
210  "=D" ( discard_D )
211  : "a" ( 0xe820 ), "b" ( next ),
212  "D" ( __from_data16 ( &e820buf ) ),
213  "c" ( sizeof ( e820buf ) ),
214  "d" ( SMAP )
215  : "esi", "memory" );
216 
217  if ( smap != SMAP ) {
218  DBG ( "INT 15,e820 failed SMAP signature check\n" );
219  return -ENOTSUP;
220  }
221 
222  if ( size < E820_MIN_SIZE ) {
223  DBG ( "INT 15,e820 returned only %d bytes\n", size );
224  return -EINVAL;
225  }
226 
227  if ( flags & CF ) {
228  DBG ( "INT 15,e820 terminated on CF set\n" );
229  break;
230  }
231 
232  DBG ( "INT 15,e820 region [%llx,%llx) type %d",
233  e820buf.start, ( e820buf.start + e820buf.len ),
234  ( int ) e820buf.type );
235  if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
236  DBG ( " (%s", ( ( e820buf.attrs & E820_ATTR_ENABLED )
237  ? "enabled" : "disabled" ) );
238  if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
239  DBG ( ", non-volatile" );
240  if ( e820buf.attrs & E820_ATTR_UNKNOWN )
241  DBG ( ", other [%08x]", e820buf.attrs );
242  DBG ( ")" );
243  }
244  DBG ( "\n" );
245 
246  /* Discard non-RAM regions */
247  if ( e820buf.type != E820_TYPE_RAM )
248  continue;
249 
250  /* Check extended attributes, if present */
251  if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
252  if ( ! ( e820buf.attrs & E820_ATTR_ENABLED ) )
253  continue;
254  if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
255  continue;
256  }
257 
258  /* Check for adjacent regions and merge them */
259  if ( e820buf.start == ( start + len ) ) {
260  len += e820buf.len;
261  } else {
262  start = e820buf.start;
263  len = e820buf.len;
264  }
265 
266  /* Sanity check: first region (base memory) should
267  * start at address zero.
268  */
269  if ( ( count == 0 ) && ( start != 0 ) ) {
270  DBG ( "INT 15,e820 region 0 starts at %llx (expected "
271  "0); assuming insane\n", start );
272  return -EINVAL;
273  }
274 
275  /* Sanity check: second region (extended memory)
276  * should start at address 0x100000.
277  */
278  if ( ( count == 1 ) && ( start != 0x100000 ) ) {
279  DBG ( "INT 15,e820 region 1 starts at %llx (expected "
280  "100000); assuming insane\n", start );
281  return -EINVAL;
282  }
283 
284  /* Update region of interest */
285  memmap_update ( region, start, len, MEMMAP_FL_MEMORY, "e820" );
286  count++;
287 
288  } while ( next != 0 );
289 
290  /* Sanity checks. Some BIOSes report complete garbage via INT
291  * 15,e820 (especially at POST time), despite passing the
292  * signature checks. We currently check for a base memory
293  * region (starting at 0) and at least one high memory region
294  * (starting at 0x100000).
295  */
296  if ( count < 2 ) {
297  DBG ( "INT 15,e820 returned only %d regions; assuming "
298  "insane\n", count );
299  return -EINVAL;
300  }
301 
302  return 0;
303 }
304 
305 /**
306  * Describe memory region from system memory map
307  *
308  * @v min Minimum address
309  * @v hide Hide in-use regions from the memory map
310  * @v region Region descriptor to fill in
311  */
312 static void int15_describe ( uint64_t min, int hide,
313  struct memmap_region *region ) {
314  unsigned int basemem;
315  unsigned int extmem;
316  uint64_t inaccessible;
317  int rc;
318 
319  /* Initialise region */
320  memmap_init ( min, region );
321 
322  /* Mark addresses above 4GB as inaccessible: we have no way to
323  * access them either in a 32-bit build or in a 64-bit build
324  * (since the 64-bit build identity-maps only the 32-bit
325  * address space).
326  */
327  inaccessible = ( 1ULL << 32 );
328  memmap_update ( region, inaccessible, -inaccessible,
330 
331  /* Enable/disable INT 15 interception as applicable */
332  int15_intercept ( hide );
333 
334  /* Try INT 15,e820 first, falling back to constructing a map
335  * from basemem and extmem sizes
336  */
337  if ( ( rc = meme820 ( region ) ) == 0 ) {
338  DBG ( "Obtained system memory map via INT 15,e820\n" );
339  } else {
340  basemem = basememsize();
341  DBG ( "FBMS base memory size %d kB [0,%x)\n",
342  basemem, ( basemem * 1024 ) );
343  extmem = extmemsize();
344  memmap_update ( region, 0, ( basemem * 1024 ),
345  MEMMAP_FL_MEMORY, "basemem" );
346  memmap_update ( region, 0x100000, ( extmem * 1024 ),
347  MEMMAP_FL_MEMORY, "extmem" );
348  }
349 
350  /* Restore INT 15 interception */
351  int15_intercept ( 1 );
352 }
353 
#define __attribute__(x)
Definition: compiler.h:10
#define EINVAL
Invalid argument.
Definition: errno.h:428
iPXE I/O API
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
#define CF
Definition: registers.h:181
uint32_t attrs
Extended attributes (optional)
Definition: int15.c:55
void memmap_update(struct memmap_region *region, uint64_t start, uint64_t size, unsigned int flags, const char *name)
Update memory region descriptor.
Definition: memmap.c:47
void int15()
Assembly routine in e820mangler.S.
Error codes.
static unsigned int basememsize(void)
Get size of base memory from BIOS free base memory counter.
Definition: memsizes.h:13
uint16_t size
Buffer size.
Definition: dwmac.h:14
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define min(x, y)
Definition: ath.h:35
unsigned int extmemsize(void)
Get size of extended memory.
Definition: int15.c:159
static unsigned int extmemsize_e801(void)
Get size of extended memory via INT 15,e801.
Definition: int15.c:82
unsigned long long uint64_t
Definition: stdint.h:13
#define E820_TYPE_RAM
Normal memory.
Definition: int15.c:58
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
static unsigned int extmemsize_88(void)
Get size of extended memory via INT 15,88.
Definition: int15.c:139
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
uint32_t start
Starting offset.
Definition: netvsc.h:12
uint64_t start
Start of region.
Definition: fakee820.c:38
void memmap_describe(uint64_t min, int hide, struct memmap_region *region)
Describe memory region from system memory map.
Definition: null_memmap.h:28
Assertions.
ring len
Length.
Definition: dwmac.h:231
An INT 15,e820 memory map entry.
Definition: fakee820.c:36
static unsigned int count
Number of entries.
Definition: dwmac.h:225
#define e820buf
Definition: int15.c:71
uint64_t len
Length of region.
Definition: fakee820.c:40
static int meme820(struct memmap_region *region)
Get e820 memory map.
Definition: int15.c:175
uint8_t flags
Flags.
Definition: ena.h:18
static void int15_describe(uint64_t min, int hide, struct memmap_region *region)
Describe memory region from system memory map.
Definition: int15.c:312
__asm__ __volatile__("call *%9" :"=a"(result), "=c"(discard_ecx), "=d"(discard_edx) :"d"(0), "a"(code), "b"(0), "c"(in_phys), "D"(0), "S"(out_phys), "m"(hypercall))
#define E820_MIN_SIZE
Definition: int15.c:67
unsigned char uint8_t
Definition: stdint.h:10
static struct e820_entry __bss16(e820buf)
Buffer for INT 15,e820 calls.
void * discard_D
Definition: bigint.h:31
void int15_intercept(int intercept)
Set INT 15 interception flag.
Definition: hidemem.c:155
unsigned int uint32_t
Definition: stdint.h:12
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
#define E820_ATTR_NONVOLATILE
Definition: int15.c:64
#define E820_ATTR_ENABLED
Definition: int15.c:63
uint32_t type
Type of region.
Definition: fakee820.c:42
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
#define __from_data16(pointer)
Definition: libkir.h:22
#define MEMMAP_FL_MEMORY
Contains memory.
Definition: memmap.h:59
#define E820_ATTR_UNKNOWN
Definition: int15.c:65
#define memmap_post
Definition: int15.c:75
#define MEMMAP_FL_INACCESSIBLE
Outside of addressable range.
Definition: memmap.h:62
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:47
A memory region descriptor.
Definition: memmap.h:48
#define SMAP
Magic value for INT 15,e820 calls.
Definition: int15.c:44
static void memmap_init(uint64_t min, struct memmap_region *region)
Initialise memory region descriptor.
Definition: memmap.h:71
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
PROVIDE_MEMMAP(int15, memmap_describe, int15_describe)
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
System memory map.
String functions.
void * memset(void *dest, int character, size_t len) __nonnull