iPXE
hidemem.c
Go to the documentation of this file.
1 /* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License as
5  * published by the Free Software Foundation; either version 2 of the
6  * License, or any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA.
17  *
18  * You can also choose to distribute this program under the terms of
19  * the Unmodified Binary Distribution Licence (as given in the file
20  * COPYING.UBDL), provided that you have satisfied its requirements.
21  */
22 
23 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
24 
25 #include <string.h>
26 #include <assert.h>
27 #include <realmode.h>
28 #include <biosint.h>
29 #include <basemem.h>
30 #include <fakee820.h>
31 #include <ipxe/init.h>
32 #include <ipxe/io.h>
33 #include <ipxe/uheap.h>
34 #include <ipxe/memmap.h>
35 
36 /** Set to true if you want to test a fake E820 map */
37 #define FAKE_E820 0
38 
39 /** Alignment for hidden memory regions */
40 #define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
41 
42 /**
43  * A hidden region of iPXE
44  *
45  * This represents a region that will be edited out of the system's
46  * memory map.
47  *
48  * This structure is accessed by assembly code, so must not be
49  * changed.
50  */
51 struct hidden_region {
52  /** Physical start address */
54  /** Physical end address */
56 };
57 
58 /** Hidden base memory */
59 extern struct hidden_region __data16 ( hidemem_base );
60 #define hidemem_base __use_data16 ( hidemem_base )
61 
62 /** Hidden umalloc memory */
63 extern struct hidden_region __data16 ( hidemem_umalloc );
64 #define hidemem_umalloc __use_data16 ( hidemem_umalloc )
65 
66 /** Hidden text memory */
67 extern struct hidden_region __data16 ( hidemem_textdata );
68 #define hidemem_textdata __use_data16 ( hidemem_textdata )
69 
70 /** Assembly routine in e820mangler.S */
71 extern void int15();
72 
73 /** Vector for storing original INT 15 handler */
74 extern struct segoff __text16 ( int15_vector );
75 #define int15_vector __use_text16 ( int15_vector )
76 
77 /** INT 15 interception flag */
79 #define int15_intercept_flag __use_text16 ( int15_intercept_flag )
80 
81 /* The linker defines these symbols for us */
82 extern char _textdata[];
83 extern char _etextdata[];
84 extern size_t ABS_SYMBOL ( _text16_memsz );
85 #define _text16_memsz ABS_VALUE ( _text16_memsz )
86 extern size_t ABS_SYMBOL ( _data16_memsz );
87 #define _data16_memsz ABS_VALUE ( _data16_memsz )
88 
89 /**
90  * Hide region of memory from system memory map
91  *
92  * @v region Hidden memory region
93  * @v start Start of region
94  * @v end End of region
95  */
96 static void hide_region ( struct hidden_region *region,
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 }
108 
109 /**
110  * Hide used base memory
111  *
112  */
113 void hide_basemem ( void ) {
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 }
120 
121 /**
122  * Hide .text and .data
123  *
124  */
125 void hide_textdata ( void ) {
126  hide_region ( &hidemem_textdata, virt_to_phys ( _textdata ),
127  virt_to_phys ( _etextdata ) );
128 }
129 
130 /**
131  * Synchronise in-use regions with the externally visible system memory map
132  *
133  */
134 static void int15_sync ( void ) {
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 }
149 
150 /**
151  * Set INT 15 interception flag
152  *
153  * @v intercept Intercept INT 15 calls to modify memory map
154  */
155 void int15_intercept ( int intercept ) {
156 
157  /* Set flag for INT 15 handler */
158  int15_intercept_flag = intercept;
159 }
160 
161 /**
162  * Hide Etherboot
163  *
164  * Installs an INT 15 handler to edit Etherboot out of the memory map
165  * returned by the BIOS.
166  */
167 static void hide_etherboot ( void ) {
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 }
215 
216 /**
217  * Unhide Etherboot
218  *
219  * Uninstalls the INT 15 handler installed by hide_etherboot(), if
220  * possible.
221  */
222 static void unhide_etherboot ( int flags __unused ) {
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 }
255 
256 /** Hide Etherboot startup function */
257 struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
258  .name = "hidemem",
259  .startup = hide_etherboot,
260  .shutdown = unhide_etherboot,
261 };
262 
#define _text16_memsz
Definition: hidemem.c:85
iPXE I/O API
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void hide_etherboot(void)
Hide Etherboot.
Definition: hidemem.c:167
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 STARTUP_EARLY
Early startup.
Definition: init.h:63
Base memory allocation.
physaddr_t uheap_start
Start of external heap.
Definition: uheap.c:57
A hidden region of iPXE.
Definition: hidemem.c:51
unsigned long long uint64_t
Definition: stdint.h:13
struct hidden_region __data16(hidemem_base)
Hidden base memory.
#define hidemem_base
Definition: hidemem.c:60
#define rm_ds
Definition: libkir.h:39
unsigned long intptr_t
Definition: stdint.h:21
const char * name
Definition: init.h:43
void hook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Hook INT vector.
Definition: biosint.c:25
uint32_t start
Starting offset.
Definition: netvsc.h:12
A startup/shutdown function.
Definition: init.h:42
int unhook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Unhook INT vector.
Definition: biosint.c:70
#define int15_intercept_flag
Definition: hidemem.c:79
#define hidemem_textdata
Definition: hidemem.c:68
Assertions.
static void unhide_etherboot(int flags __unused)
Unhide Etherboot.
Definition: hidemem.c:222
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
uint64_t start
Physical start address.
Definition: hidemem.c:53
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
struct segoff __text16(int15_vector)
Vector for storing original INT 15 handler.
#define hooked_bios_interrupts
Definition: biosint.h:25
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint8_t flags
Flags.
Definition: ena.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned char uint8_t
Definition: stdint.h:10
void int15_intercept(int intercept)
Set INT 15 interception flag.
Definition: hidemem.c:155
#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
unsigned long physaddr_t
Definition: stdint.h:20
#define _data16_memsz
Definition: hidemem.c:87
char _textdata[]
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
External ("user") heap.
static memmap_sync(void)
Synchronise in-use regions with the externally visible system memory map.
Definition: fdtmem.h:25
#define ALIGN_HIDDEN
Alignment for hidden memory regions.
Definition: hidemem.c:40
size_t ABS_SYMBOL(_text16_memsz)
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint64_t end
Physical end address.
Definition: hidemem.c:55
#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
#define hidemem_umalloc
Definition: hidemem.c:64
struct startup_fn hide_etherboot_startup_fn __startup_fn(STARTUP_EARLY)
Hide Etherboot startup function.
static void int15_sync(void)
Synchronise in-use regions with the externally visible system memory map.
Definition: hidemem.c:134
PROVIDE_MEMMAP(int15, memmap_sync, int15_sync)
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
char _etextdata[]
System memory map.
String functions.
physaddr_t uheap_end
End of external heap.
Definition: uheap.c:60