iPXE
Data Structures | Macros | Functions | Variables
hidemem.c File Reference
#include <string.h>
#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/uheap.h>
#include <ipxe/memmap.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 int15_intercept_flag   __use_text16 ( int15_intercept_flag )
 
#define _text16_memsz   ABS_VALUE ( _text16_memsz )
 
#define _data16_memsz   ABS_VALUE ( _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...
 
uint8_t __text16 (int15_intercept_flag)
 INT 15 interception flag. More...
 
size_t ABS_SYMBOL (_text16_memsz)
 
size_t ABS_SYMBOL (_data16_memsz)
 
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_textdata (void)
 Hide .text and .data. More...
 
static void int15_sync (void)
 Synchronise in-use regions with the externally visible system memory map. More...
 
void int15_intercept (int intercept)
 Set INT 15 interception flag. 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...
 
 PROVIDE_MEMMAP (int15, memmap_sync, int15_sync)
 

Variables

struct hidden_region __attribute__
 
char _textdata []
 
char _etextdata []
 

Macro Definition Documentation

◆ FAKE_E820

#define FAKE_E820   0

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

Definition at line 37 of file hidemem.c.

◆ ALIGN_HIDDEN

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

Alignment for hidden memory regions.

Definition at line 40 of file hidemem.c.

◆ hidemem_base

#define hidemem_base   __use_data16 ( hidemem_base )

Definition at line 60 of file hidemem.c.

◆ hidemem_umalloc

#define hidemem_umalloc   __use_data16 ( hidemem_umalloc )

Definition at line 64 of file hidemem.c.

◆ hidemem_textdata

#define hidemem_textdata   __use_data16 ( hidemem_textdata )

Definition at line 68 of file hidemem.c.

◆ int15_vector

#define int15_vector   __use_text16 ( int15_vector )

Definition at line 75 of file hidemem.c.

◆ int15_intercept_flag

#define int15_intercept_flag   __use_text16 ( int15_intercept_flag )

Definition at line 79 of file hidemem.c.

◆ _text16_memsz

#define _text16_memsz   ABS_VALUE ( _text16_memsz )

Definition at line 85 of file hidemem.c.

◆ _data16_memsz

#define _data16_memsz   ABS_VALUE ( _data16_memsz )

Definition at line 87 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() [1/2]

struct segoff __text16 ( int15_vector  )

Vector for storing original INT 15 handler.

◆ __text16() [2/2]

uint8_t __text16 ( int15_intercept_flag  )

INT 15 interception flag.

◆ ABS_SYMBOL() [1/2]

size_t ABS_SYMBOL ( _text16_memsz  )

◆ ABS_SYMBOL() [2/2]

size_t ABS_SYMBOL ( _data16_memsz  )

◆ 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 96 of file hidemem.c.

97  {
98 
99  /* Some operating systems get a nasty shock if a region of the
100  * E820 map seems to start on a non-page boundary. Make life
101  * safer by rounding out our edited region.
102  */
103  region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
104  region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
105 
106  DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
107 }
uint32_t start
Starting offset.
Definition: netvsc.h:12
uint64_t start
Physical start address.
Definition: hidemem.c:53
#define ALIGN_HIDDEN
Alignment for hidden memory regions.
Definition: hidemem.c:40
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint64_t end
Physical end address.
Definition: hidemem.c:55
#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 int15_sync().

◆ hide_basemem()

void hide_basemem ( void  )

Hide used base memory.

Definition at line 113 of file hidemem.c.

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

References get_fbms(), and hidemem_base.

Referenced by hide_etherboot(), and set_fbms().

◆ hide_textdata()

void hide_textdata ( void  )

Hide .text and .data.

Definition at line 125 of file hidemem.c.

125  {
126  hide_region ( &hidemem_textdata, virt_to_phys ( _textdata ),
127  virt_to_phys ( _etextdata ) );
128 }
#define hidemem_textdata
Definition: hidemem.c:68
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:96
char _textdata[]
char _etextdata[]

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

Referenced by hide_etherboot().

◆ int15_sync()

static void int15_sync ( void  )
static

Synchronise in-use regions with the externally visible system memory map.

Definition at line 134 of file hidemem.c.

134  {
136  physaddr_t end;
137 
138  /* Besides our fixed base memory and textdata regions, we
139  * support hiding only a single in-use memory region (the
140  * umalloc region), which must be placed before the hidden
141  * textdata region (even if zero-length).
142  */
143  start = uheap_start;
144  end = uheap_end;
145  if ( start == end )
146  start = end = virt_to_phys ( _textdata );
148 }
physaddr_t uheap_start
Start of external heap.
Definition: uheap.c:57
uint32_t start
Starting offset.
Definition: netvsc.h:12
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:96
unsigned long physaddr_t
Definition: stdint.h:20
char _textdata[]
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define hidemem_umalloc
Definition: hidemem.c:64
physaddr_t uheap_end
End of external heap.
Definition: uheap.c:60

References _textdata, end, hide_region(), hidemem_umalloc, start, uheap_end, and uheap_start.

Referenced by hide_etherboot().

◆ int15_intercept()

void int15_intercept ( int  intercept)

Set INT 15 interception flag.

Parameters
interceptIntercept INT 15 calls to modify memory map

Definition at line 155 of file hidemem.c.

155  {
156 
157  /* Set flag for INT 15 handler */
158  int15_intercept_flag = intercept;
159 }
#define int15_intercept_flag
Definition: hidemem.c:79

References int15_intercept_flag.

Referenced by int15_describe().

◆ 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 167 of file hidemem.c.

167  {
168  unsigned int rm_ds_top;
169  unsigned int rm_cs_top;
170  unsigned int fbms;
171 
172  /* Dump memory map before mangling */
173  DBG ( "Hiding iPXE from system memory map\n" );
174  memmap_dump_all ( 1 );
175 
176  /* Hook in fake E820 map, if we're testing one */
177  if ( FAKE_E820 ) {
178  DBG ( "Hooking in fake E820 map\n" );
179  fake_e820();
180  memmap_dump_all ( 1 );
181  }
182 
183  /* Initialise the hidden regions */
184  hide_basemem();
185  hide_textdata();
186  int15_sync();
187 
188  /* Some really moronic BIOSes bring up the PXE stack via the
189  * UNDI loader entry point and then don't bother to unload it
190  * before overwriting the code and data segments. If this
191  * happens, we really don't want to leave INT 15 hooked,
192  * because that will cause any loaded OS to die horribly as
193  * soon as it attempts to fetch the system memory map.
194  *
195  * We use a heuristic to guess whether or not we are being
196  * loaded sensibly.
197  */
198  rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_memsz + 1024 - 1 ) >> 10 );
199  rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_memsz + 1024 - 1 ) >> 10 );
200  fbms = get_fbms();
201  if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
202  DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
203  "DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
204  DBG ( "Disabling INT 15 memory hiding\n" );
205  return;
206  }
207 
208  /* Hook INT 15 */
210 
211  /* Dump memory map after mangling */
212  DBG ( "Hidden iPXE from system memory map\n" );
213  memmap_dump_all ( 1 );
214 }
#define _text16_memsz
Definition: hidemem.c:85
static unsigned int get_fbms(void)
Read the BIOS free base memory counter.
Definition: basemem.h:21
void int15()
Assembly routine in e820mangler.S.
#define rm_ds
Definition: libkir.h:39
unsigned long intptr_t
Definition: stdint.h:21
void hook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Hook INT vector.
Definition: biosint.c:25
#define FAKE_E820
Set to true if you want to test a fake E820 map.
Definition: hidemem.c:37
void fake_e820(void)
Definition: fakee820.c:64
#define _data16_memsz
Definition: hidemem.c:87
static void memmap_dump_all(int hide)
Dump system memory map (for debugging)
Definition: memmap.h:215
#define int15_vector
Definition: hidemem.c:75
#define rm_cs
Definition: libkir.h:38
void hide_basemem(void)
Hide used base memory.
Definition: hidemem.c:113
void hide_textdata(void)
Hide .text and .data.
Definition: hidemem.c:125
static void int15_sync(void)
Synchronise in-use regions with the externally visible system memory map.
Definition: hidemem.c:134
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498

References _data16_memsz, _text16_memsz, DBG, FAKE_E820, fake_e820(), get_fbms(), hide_basemem(), hide_textdata(), hook_bios_interrupt(), int15(), int15_sync(), int15_vector, memmap_dump_all(), rm_cs, and rm_ds.

◆ 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 222 of file hidemem.c.

222  {
223  int rc;
224 
225  /* If we have more than one hooked interrupt at this point, it
226  * means that some other vector is still hooked, in which case
227  * we can't safely unhook INT 15 because we need to keep our
228  * memory protected. (We expect there to be at least one
229  * hooked interrupt, because INT 15 itself is still hooked).
230  */
231  if ( hooked_bios_interrupts > 1 ) {
232  DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
234  return;
235  }
236 
237  /* Try to unhook INT 15 */
238  if ( ( rc = unhook_bios_interrupt ( 0x15, ( intptr_t ) int15,
239  &int15_vector ) ) != 0 ) {
240  DBG ( "Cannot unhook INT15: %s\n", strerror ( rc ) );
241  /* Leave it hooked; there's nothing else we can do,
242  * and it should be intrinsically safe (though
243  * wasteful of RAM).
244  */
245  }
246 
247  /* Unhook fake E820 map, if used */
248  if ( FAKE_E820 )
249  unfake_e820();
250 
251  /* Dump memory map after unhiding */
252  DBG ( "Unhidden iPXE from system memory map\n" );
253  memmap_dump_all ( 1 );
254 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void int15()
Assembly routine in e820mangler.S.
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:70
#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:37
static void memmap_dump_all(int hide)
Dump system memory map (for debugging)
Definition: memmap.h:215
#define int15_vector
Definition: hidemem.c:75
void unfake_e820(void)
Definition: fakee820.c:95
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498

References DBG, FAKE_E820, hooked_bios_interrupts, int15(), int15_vector, memmap_dump_all(), 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.

◆ PROVIDE_MEMMAP()

PROVIDE_MEMMAP ( int15  ,
memmap_sync  ,
int15_sync   
)

Variable Documentation

◆ __attribute__

◆ _textdata

char _textdata[]

Referenced by hide_textdata(), and int15_sync().

◆ _etextdata

char _etextdata[]

Referenced by hide_textdata().