iPXE
Data Structures | Macros | Functions
int15.c File Reference

Memory mapping. More...

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <realmode.h>
#include <bios.h>
#include <memsizes.h>
#include <ipxe/io.h>
#include <ipxe/memmap.h>

Go to the source code of this file.

Data Structures

struct  e820_entry
 An INT 15,e820 memory map entry. More...
 

Macros

#define SMAP   ( 0x534d4150 )
 Magic value for INT 15,e820 calls. More...
 
#define E820_TYPE_RAM   1
 Normal memory. More...
 
#define E820_TYPE_RESERVED   2
 Reserved and unavailable. More...
 
#define E820_TYPE_ACPI   3
 ACPI reclaim memory. More...
 
#define E820_TYPE_NVS   4
 ACPI NVS memory. More...
 
#define E820_ATTR_ENABLED   0x00000001UL
 
#define E820_ATTR_NONVOLATILE   0x00000002UL
 
#define E820_ATTR_UNKNOWN   0xfffffffcUL
 
#define E820_MIN_SIZE   20
 
#define e820buf   __use_data16 ( e820buf )
 
#define memmap_post   __use_data16 ( memmap_post )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static struct e820_entry __bss16 (e820buf)
 Buffer for INT 15,e820 calls. More...
 
uint8_t __bss16 (memmap_post)
 We are running during POST; inhibit INT 15,e820 and INT 15,e801. More...
 
static unsigned int extmemsize_e801 (void)
 Get size of extended memory via INT 15,e801. More...
 
static unsigned int extmemsize_88 (void)
 Get size of extended memory via INT 15,88. More...
 
unsigned int extmemsize (void)
 Get size of extended memory. More...
 
static int meme820 (struct memmap_region *region)
 Get e820 memory map. More...
 
static void int15_describe (uint64_t min, int hide, struct memmap_region *region)
 Describe memory region from system memory map. More...
 
 PROVIDE_MEMMAP (int15, memmap_describe, int15_describe)
 

Detailed Description

Memory mapping.

Definition in file int15.c.

Macro Definition Documentation

◆ SMAP

#define SMAP   ( 0x534d4150 )

Magic value for INT 15,e820 calls.

Definition at line 44 of file int15.c.

◆ E820_TYPE_RAM

#define E820_TYPE_RAM   1

Normal memory.

Definition at line 58 of file int15.c.

◆ E820_TYPE_RESERVED

#define E820_TYPE_RESERVED   2

Reserved and unavailable.

Definition at line 59 of file int15.c.

◆ E820_TYPE_ACPI

#define E820_TYPE_ACPI   3

ACPI reclaim memory.

Definition at line 60 of file int15.c.

◆ E820_TYPE_NVS

#define E820_TYPE_NVS   4

ACPI NVS memory.

Definition at line 61 of file int15.c.

◆ E820_ATTR_ENABLED

#define E820_ATTR_ENABLED   0x00000001UL

Definition at line 63 of file int15.c.

◆ E820_ATTR_NONVOLATILE

#define E820_ATTR_NONVOLATILE   0x00000002UL

Definition at line 64 of file int15.c.

◆ E820_ATTR_UNKNOWN

#define E820_ATTR_UNKNOWN   0xfffffffcUL

Definition at line 65 of file int15.c.

◆ E820_MIN_SIZE

#define E820_MIN_SIZE   20

Definition at line 67 of file int15.c.

◆ e820buf

#define e820buf   __use_data16 ( e820buf )

Definition at line 71 of file int15.c.

◆ memmap_post

#define memmap_post   __use_data16 ( memmap_post )

Definition at line 75 of file int15.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __bss16() [1/2]

static struct e820_entry __bss16 ( e820buf  )
static

Buffer for INT 15,e820 calls.

◆ __bss16() [2/2]

uint8_t __bss16 ( memmap_post  )

We are running during POST; inhibit INT 15,e820 and INT 15,e801.

◆ extmemsize_e801()

static unsigned int extmemsize_e801 ( void  )
static

Get size of extended memory via INT 15,e801.

Return values
extmemExtended memory size, in kB, or 0

Definition at line 82 of file int15.c.

82  {
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 }
unsigned short uint16_t
Definition: stdint.h:11
#define CF
Definition: registers.h:181
unsigned long long uint64_t
Definition: stdint.h:13
uint8_t flags
Flags.
Definition: ena.h:18
__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))
__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 memmap_post
Definition: int15.c:75
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226

References __asm__(), __volatile__(), CF, DBG, flags, memmap_post, and REAL_CODE.

Referenced by extmemsize().

◆ extmemsize_88()

static unsigned int extmemsize_88 ( void  )
static

Get size of extended memory via INT 15,88.

Return values
extmemExtended memory size, in kB

Definition at line 139 of file int15.c.

139  {
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 }
unsigned short uint16_t
Definition: stdint.h:11
__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))
__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 DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226

References __asm__(), __volatile__(), DBG, and REAL_CODE.

Referenced by extmemsize().

◆ extmemsize()

unsigned int extmemsize ( void  )

Get size of extended memory.

Return values
extmemExtended memory size, in kB

Note that this is only an approximation; for an accurate picture, use the E820 memory map obtained via memmap_describe();

Definition at line 159 of file int15.c.

159  {
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 }
static unsigned int extmemsize_e801(void)
Get size of extended memory via INT 15,e801.
Definition: int15.c:82
static unsigned int extmemsize_88(void)
Get size of extended memory via INT 15,88.
Definition: int15.c:139

References extmemsize_88(), and extmemsize_e801().

Referenced by int15_describe(), and nbi_process_segments().

◆ meme820()

static int meme820 ( struct memmap_region region)
static

Get e820 memory map.

Parameters
regionMemory region of interest to be updated
Return values
rcReturn status code

Definition at line 175 of file int15.c.

175  {
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 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
#define CF
Definition: registers.h:181
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
uint16_t size
Buffer size.
Definition: dwmac.h:14
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
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
uint32_t start
Starting offset.
Definition: netvsc.h:12
ring len
Length.
Definition: dwmac.h:231
static unsigned int count
Number of entries.
Definition: dwmac.h:225
#define e820buf
Definition: int15.c:71
uint8_t flags
Flags.
Definition: ena.h:18
__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
void * discard_D
Definition: bigint.h:31
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
__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
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:47
#define SMAP
Magic value for INT 15,e820 calls.
Definition: int15.c:44
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226
void * memset(void *dest, int character, size_t len) __nonnull

References __asm__(), __from_data16, __volatile__(), e820_entry::attrs, CF, count, DBG, discard_D, E820_ATTR_ENABLED, E820_ATTR_NONVOLATILE, E820_ATTR_UNKNOWN, E820_MIN_SIZE, E820_TYPE_RAM, e820buf, EINVAL, ENOTSUP, ENOTTY, flags, len, MEMMAP_FL_MEMORY, memmap_post, memmap_update(), memset(), next, offsetof, REAL_CODE, size, SMAP, start, and typeof().

Referenced by int15_describe().

◆ int15_describe()

static void int15_describe ( uint64_t  min,
int  hide,
struct memmap_region region 
)
static

Describe memory region from system memory map.

Parameters
minMinimum address
hideHide in-use regions from the memory map
regionRegion descriptor to fill in

Definition at line 312 of file int15.c.

313  {
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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
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
static unsigned int basememsize(void)
Get size of base memory from BIOS free base memory counter.
Definition: memsizes.h:13
#define min(x, y)
Definition: ath.h:35
unsigned int extmemsize(void)
Get size of extended memory.
Definition: int15.c:159
unsigned long long uint64_t
Definition: stdint.h:13
static int meme820(struct memmap_region *region)
Get e820 memory map.
Definition: int15.c:175
void int15_intercept(int intercept)
Set INT 15 interception flag.
Definition: hidemem.c:155
#define MEMMAP_FL_MEMORY
Contains memory.
Definition: memmap.h:59
#define MEMMAP_FL_INACCESSIBLE
Outside of addressable range.
Definition: memmap.h:62
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
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References basememsize(), DBG, extmemsize(), int15_intercept(), meme820(), MEMMAP_FL_INACCESSIBLE, MEMMAP_FL_MEMORY, memmap_init(), memmap_update(), min, NULL, and rc.

◆ PROVIDE_MEMMAP()

PROVIDE_MEMMAP ( int15  ,
memmap_describe  ,
int15_describe   
)