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 <assert.h>
26 #include <realmode.h>
27 #include <biosint.h>
28 #include <basemem.h>
29 #include <fakee820.h>
30 #include <ipxe/init.h>
31 #include <ipxe/io.h>
32 #include <ipxe/hidemem.h>
33 
34 /** Set to true if you want to test a fake E820 map */
35 #define FAKE_E820 0
36 
37 /** Alignment for hidden memory regions */
38 #define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
39 
40 /**
41  * A hidden region of iPXE
42  *
43  * This represents a region that will be edited out of the system's
44  * memory map.
45  *
46  * This structure is accessed by assembly code, so must not be
47  * changed.
48  */
49 struct hidden_region {
50  /** Physical start address */
52  /** Physical end address */
54 };
55 
56 /** Hidden base memory */
57 extern struct hidden_region __data16 ( hidemem_base );
58 #define hidemem_base __use_data16 ( hidemem_base )
59 
60 /** Hidden umalloc memory */
61 extern struct hidden_region __data16 ( hidemem_umalloc );
62 #define hidemem_umalloc __use_data16 ( hidemem_umalloc )
63 
64 /** Hidden text memory */
65 extern struct hidden_region __data16 ( hidemem_textdata );
66 #define hidemem_textdata __use_data16 ( hidemem_textdata )
67 
68 /** Assembly routine in e820mangler.S */
69 extern void int15();
70 
71 /** Vector for storing original INT 15 handler */
72 extern struct segoff __text16 ( int15_vector );
73 #define int15_vector __use_text16 ( int15_vector )
74 
75 /* The linker defines these symbols for us */
76 extern char _textdata[];
77 extern char _etextdata[];
78 extern char _text16_memsz[];
79 #define _text16_memsz ( ( size_t ) _text16_memsz )
80 extern char _data16_memsz[];
81 #define _data16_memsz ( ( size_t ) _data16_memsz )
82 
83 /**
84  * Hide region of memory from system memory map
85  *
86  * @v region Hidden memory region
87  * @v start Start of region
88  * @v end End of region
89  */
90 static void hide_region ( struct hidden_region *region,
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 }
102 
103 /**
104  * Hide used base memory
105  *
106  */
107 void hide_basemem ( void ) {
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 }
114 
115 /**
116  * Hide umalloc() region
117  *
118  */
120  assert ( end <= virt_to_phys ( _textdata ) );
122 }
123 
124 /**
125  * Hide .text and .data
126  *
127  */
128 void hide_textdata ( void ) {
130  virt_to_phys ( _etextdata ) );
131 }
132 
133 /**
134  * Hide Etherboot
135  *
136  * Installs an INT 15 handler to edit Etherboot out of the memory map
137  * returned by the BIOS.
138  */
139 static void hide_etherboot ( void ) {
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 }
188 
189 /**
190  * Unhide Etherboot
191  *
192  * Uninstalls the INT 15 handler installed by hide_etherboot(), if
193  * possible.
194  */
195 static void unhide_etherboot ( int flags __unused ) {
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 }
229 
230 /** Hide Etherboot startup function */
231 struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
232  .name = "hidemem",
233  .startup = hide_etherboot,
234  .shutdown = unhide_etherboot,
235 };
#define _text16_memsz
Definition: hidemem.c:79
iPXE I/O API
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void get_memmap(struct memory_map *memmap)
Get memory map.
static void hide_etherboot(void)
Hide Etherboot.
Definition: hidemem.c:139
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 STARTUP_EARLY
Early startup.
Definition: init.h:62
Base memory allocation.
A hidden region of iPXE.
Definition: hidemem.c:49
unsigned long long uint64_t
Definition: stdint.h:13
struct hidden_region __data16(hidemem_base)
Hidden base memory.
#define hidemem_base
Definition: hidemem.c:58
#define rm_ds
Definition: libkir.h:39
A memory map.
Definition: io.h:499
unsigned long intptr_t
Definition: stdint.h:21
const char * name
Definition: init.h:42
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
uint32_t start
Starting offset.
Definition: netvsc.h:12
A startup/shutdown function.
Definition: init.h:41
int unhook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Unhook INT vector.
Definition: biosint.c:69
#define hidemem_textdata
Definition: hidemem.c:66
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void unhide_etherboot(int flags __unused)
Unhide Etherboot.
Definition: hidemem.c:195
uint64_t start
Physical start address.
Definition: hidemem.c:51
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
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)
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
void fake_e820(void)
Definition: fakee820.c:64
Hidden memory regions.
unsigned long physaddr_t
Definition: stdint.h:20
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define _data16_memsz
Definition: hidemem.c:81
char _textdata[]
#define int15_vector
Definition: hidemem.c:73
void unfake_e820(void)
Definition: fakee820.c:95
#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 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 hidemem_umalloc
Definition: hidemem.c:62
struct startup_fn hide_etherboot_startup_fn __startup_fn(STARTUP_EARLY)
Hide Etherboot startup function.
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
char _etextdata[]
uint8_t flags
Flags.
Definition: ena.h:18