iPXE
Data Structures | Macros | Functions | Variables
hidemem.c File Reference
#include <assert.h>
#include <realmode.h>
#include <biosint.h>
#include <basemem.h>
#include <fakee820.h>
#include <ipxe/init.h>
#include <ipxe/io.h>
#include <ipxe/hidemem.h>

Go to the source code of this file.

Data Structures

struct  hidden_region
 A hidden region of iPXE. More...
 

Macros

#define FAKE_E820   0
 Set to true if you want to test a fake E820 map. More...
 
#define ALIGN_HIDDEN   4096 /* 4kB page alignment should be enough */
 Alignment for hidden memory regions. More...
 
#define hidemem_base   __use_data16 ( hidemem_base )
 
#define hidemem_umalloc   __use_data16 ( hidemem_umalloc )
 
#define hidemem_textdata   __use_data16 ( hidemem_textdata )
 
#define int15_vector   __use_text16 ( int15_vector )
 
#define _text16_memsz   ( ( size_t ) _text16_memsz )
 
#define _data16_memsz   ( ( size_t ) _data16_memsz )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct hidden_region __data16 (hidemem_base)
 Hidden base memory. More...
 
struct hidden_region __data16 (hidemem_umalloc)
 Hidden umalloc memory. More...
 
struct hidden_region __data16 (hidemem_textdata)
 Hidden text memory. More...
 
void int15 ()
 Assembly routine in e820mangler.S. More...
 
struct segoff __text16 (int15_vector)
 Vector for storing original INT 15 handler. More...
 
static void hide_region (struct hidden_region *region, physaddr_t start, physaddr_t end)
 Hide region of memory from system memory map. More...
 
void hide_basemem (void)
 Hide used base memory. More...
 
void hide_umalloc (physaddr_t start, physaddr_t end)
 Hide umalloc() region. More...
 
void hide_textdata (void)
 Hide .text and .data. More...
 
static void hide_etherboot (void)
 Hide Etherboot. More...
 
static void unhide_etherboot (int flags __unused)
 Unhide Etherboot. More...
 
struct startup_fn hide_etherboot_startup_fn __startup_fn (STARTUP_EARLY)
 Hide Etherboot startup function. More...
 

Variables

struct hidden_region __attribute__
 
char _textdata []
 
char _etextdata []
 
char _text16_memsz []
 
char _data16_memsz []
 

Macro Definition Documentation

◆ FAKE_E820

#define FAKE_E820   0

Set to true if you want to test a fake E820 map.

Definition at line 35 of file hidemem.c.

◆ ALIGN_HIDDEN

#define ALIGN_HIDDEN   4096 /* 4kB page alignment should be enough */

Alignment for hidden memory regions.

Definition at line 38 of file hidemem.c.

◆ hidemem_base

#define hidemem_base   __use_data16 ( hidemem_base )

Definition at line 58 of file hidemem.c.

◆ hidemem_umalloc

#define hidemem_umalloc   __use_data16 ( hidemem_umalloc )

Definition at line 62 of file hidemem.c.

◆ hidemem_textdata

#define hidemem_textdata   __use_data16 ( hidemem_textdata )

Definition at line 66 of file hidemem.c.

◆ int15_vector

#define int15_vector   __use_text16 ( int15_vector )

Definition at line 73 of file hidemem.c.

◆ _text16_memsz

#define _text16_memsz   ( ( size_t ) _text16_memsz )

Definition at line 79 of file hidemem.c.

◆ _data16_memsz

#define _data16_memsz   ( ( size_t ) _data16_memsz )

Definition at line 81 of file hidemem.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __data16() [1/3]

struct hidden_region __data16 ( hidemem_base  )

Hidden base memory.

◆ __data16() [2/3]

struct hidden_region __data16 ( hidemem_umalloc  )

Hidden umalloc memory.

◆ __data16() [3/3]

struct hidden_region __data16 ( hidemem_textdata  )

Hidden text memory.

◆ int15()

void int15 ( )

Assembly routine in e820mangler.S.

Referenced by hide_etherboot(), and unhide_etherboot().

◆ __text16()

struct segoff __text16 ( int15_vector  )

Vector for storing original INT 15 handler.

◆ hide_region()

static void hide_region ( struct hidden_region region,
physaddr_t  start,
physaddr_t  end 
)
static

Hide region of memory from system memory map.

Parameters
regionHidden memory region
startStart of region
endEnd of region

Definition at line 90 of file hidemem.c.

91  {
92 
93  /* Some operating systems get a nasty shock if a region of the
94  * E820 map seems to start on a non-page boundary. Make life
95  * safer by rounding out our edited region.
96  */
97  region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
98  region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
99 
100  DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
101 }
uint32_t start
Starting offset.
Definition: netvsc.h:12
uint64_t start
Physical start address.
Definition: hidemem.c:51
#define ALIGN_HIDDEN
Alignment for hidden memory regions.
Definition: hidemem.c:38
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint64_t end
Physical end address.
Definition: hidemem.c:53
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498

References ALIGN_HIDDEN, DBG, end, hidden_region::end, start, and hidden_region::start.

Referenced by hide_textdata(), and hide_umalloc().

◆ hide_basemem()

void hide_basemem ( void  )

Hide used base memory.

Definition at line 107 of file hidemem.c.

107  {
108  /* Hide from the top of free base memory to 640kB. Don't use
109  * hide_region(), because we don't want this rounded to the
110  * nearest page boundary.
111  */
112  hidemem_base.start = ( get_fbms() * 1024 );
113 }
static unsigned int get_fbms(void)
Read the BIOS free base memory counter.
Definition: basemem.h:21
#define hidemem_base
Definition: hidemem.c:58

References get_fbms(), and hidemem_base.

Referenced by hide_etherboot(), and set_fbms().

◆ hide_umalloc()

void hide_umalloc ( physaddr_t  start,
physaddr_t  end 
)

Hide umalloc() region.

Definition at line 119 of file hidemem.c.

119  {
120  assert ( end <= virt_to_phys ( _textdata ) );
122 }
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
uint32_t start
Starting offset.
Definition: netvsc.h:12
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void hide_region(struct hidden_region *region, physaddr_t start, physaddr_t end)
Hide region of memory from system memory map.
Definition: hidemem.c:90
char _textdata[]
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define hidemem_umalloc
Definition: hidemem.c:62

References _textdata, assert(), end, hide_region(), hidemem_umalloc, start, and virt_to_phys().

Referenced by hide_etherboot(), and memtop_urealloc().

◆ hide_textdata()

void hide_textdata ( void  )

Hide .text and .data.

Definition at line 128 of file hidemem.c.

128  {
130  virt_to_phys ( _etextdata ) );
131 }
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
#define hidemem_textdata
Definition: hidemem.c:66
static void hide_region(struct hidden_region *region, physaddr_t start, physaddr_t end)
Hide region of memory from system memory map.
Definition: hidemem.c:90
char _textdata[]
char _etextdata[]

References _etextdata, _textdata, hide_region(), hidemem_textdata, and virt_to_phys().

Referenced by hide_etherboot().

◆ hide_etherboot()

static void hide_etherboot ( void  )
static

Hide Etherboot.

Installs an INT 15 handler to edit Etherboot out of the memory map returned by the BIOS.

Definition at line 139 of file hidemem.c.

139  {
140  struct memory_map memmap;
141  unsigned int rm_ds_top;
142  unsigned int rm_cs_top;
143  unsigned int fbms;
144 
145  /* Dump memory map before mangling */
146  DBG ( "Hiding iPXE from system memory map\n" );
147  get_memmap ( &memmap );
148 
149  /* Hook in fake E820 map, if we're testing one */
150  if ( FAKE_E820 ) {
151  DBG ( "Hooking in fake E820 map\n" );
152  fake_e820();
153  get_memmap ( &memmap );
154  }
155 
156  /* Initialise the hidden regions */
157  hide_basemem();
159  hide_textdata();
160 
161  /* Some really moronic BIOSes bring up the PXE stack via the
162  * UNDI loader entry point and then don't bother to unload it
163  * before overwriting the code and data segments. If this
164  * happens, we really don't want to leave INT 15 hooked,
165  * because that will cause any loaded OS to die horribly as
166  * soon as it attempts to fetch the system memory map.
167  *
168  * We use a heuristic to guess whether or not we are being
169  * loaded sensibly.
170  */
171  rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_memsz + 1024 - 1 ) >> 10 );
172  rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_memsz + 1024 - 1 ) >> 10 );
173  fbms = get_fbms();
174  if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
175  DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
176  "DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
177  DBG ( "Disabling INT 15 memory hiding\n" );
178  return;
179  }
180 
181  /* Hook INT 15 */
183 
184  /* Dump memory map after mangling */
185  DBG ( "Hidden iPXE from system memory map\n" );
186  get_memmap ( &memmap );
187 }
#define _text16_memsz
Definition: hidemem.c:79
void get_memmap(struct memory_map *memmap)
Get memory map.
static unsigned int get_fbms(void)
Read the BIOS free base memory counter.
Definition: basemem.h:21
void int15()
Assembly routine in e820mangler.S.
void hide_umalloc(physaddr_t start, physaddr_t end)
Hide umalloc() region.
Definition: hidemem.c:119
#define rm_ds
Definition: libkir.h:39
A memory map.
Definition: io.h:499
unsigned long intptr_t
Definition: stdint.h:21
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
void hook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Hook INT vector.
Definition: biosint.c:24
#define FAKE_E820
Set to true if you want to test a fake E820 map.
Definition: hidemem.c:35
void fake_e820(void)
Definition: fakee820.c:64
#define _data16_memsz
Definition: hidemem.c:81
char _textdata[]
#define int15_vector
Definition: hidemem.c:73
#define rm_cs
Definition: libkir.h:38
void hide_basemem(void)
Hide used base memory.
Definition: hidemem.c:107
void hide_textdata(void)
Hide .text and .data.
Definition: hidemem.c:128
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498

References _data16_memsz, _text16_memsz, _textdata, DBG, FAKE_E820, fake_e820(), get_fbms(), get_memmap(), hide_basemem(), hide_textdata(), hide_umalloc(), hook_bios_interrupt(), int15(), int15_vector, rm_cs, rm_ds, and virt_to_phys().

◆ unhide_etherboot()

static void unhide_etherboot ( int flags  __unused)
static

Unhide Etherboot.

Uninstalls the INT 15 handler installed by hide_etherboot(), if possible.

Definition at line 195 of file hidemem.c.

195  {
196  struct memory_map memmap;
197  int rc;
198 
199  /* If we have more than one hooked interrupt at this point, it
200  * means that some other vector is still hooked, in which case
201  * we can't safely unhook INT 15 because we need to keep our
202  * memory protected. (We expect there to be at least one
203  * hooked interrupt, because INT 15 itself is still hooked).
204  */
205  if ( hooked_bios_interrupts > 1 ) {
206  DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
208  return;
209  }
210 
211  /* Try to unhook INT 15 */
212  if ( ( rc = unhook_bios_interrupt ( 0x15, ( intptr_t ) int15,
213  &int15_vector ) ) != 0 ) {
214  DBG ( "Cannot unhook INT15: %s\n", strerror ( rc ) );
215  /* Leave it hooked; there's nothing else we can do,
216  * and it should be intrinsically safe (though
217  * wasteful of RAM).
218  */
219  }
220 
221  /* Unhook fake E820 map, if used */
222  if ( FAKE_E820 )
223  unfake_e820();
224 
225  /* Dump memory map after unhiding */
226  DBG ( "Unhidden iPXE from system memory map\n" );
227  get_memmap ( &memmap );
228 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void get_memmap(struct memory_map *memmap)
Get memory map.
void int15()
Assembly routine in e820mangler.S.
A memory map.
Definition: io.h:499
unsigned long intptr_t
Definition: stdint.h:21
int unhook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Unhook INT vector.
Definition: biosint.c:69
#define hooked_bios_interrupts
Definition: biosint.h:25
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define FAKE_E820
Set to true if you want to test a fake E820 map.
Definition: hidemem.c:35
#define int15_vector
Definition: hidemem.c:73
void unfake_e820(void)
Definition: fakee820.c:95
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498

References DBG, FAKE_E820, get_memmap(), hooked_bios_interrupts, int15(), int15_vector, rc, strerror(), unfake_e820(), and unhook_bios_interrupt().

◆ __startup_fn()

struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY  )

Hide Etherboot startup function.

Variable Documentation

◆ __attribute__

◆ _textdata

char _textdata[]

◆ _etextdata

char _etextdata[]

Referenced by hide_textdata().

◆ _text16_memsz

char _text16_memsz[]

◆ _data16_memsz

char _data16_memsz[]