Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / firmware / pcbios / hidemem.c
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
19 FILE_LICENCE ( GPL2_OR_LATER );
20
21 #include <assert.h>
22 #include <realmode.h>
23 #include <biosint.h>
24 #include <basemem.h>
25 #include <fakee820.h>
26 #include <ipxe/init.h>
27 #include <ipxe/io.h>
28 #include <ipxe/hidemem.h>
29
30 /** Set to true if you want to test a fake E820 map */
31 #define FAKE_E820 0
32
33 /** Alignment for hidden memory regions */
34 #define ALIGN_HIDDEN 4096   /* 4kB page alignment should be enough */
35
36 /**
37  * A hidden region of iPXE
38  *
39  * This represents a region that will be edited out of the system's
40  * memory map.
41  *
42  * This structure is accessed by assembly code, so must not be
43  * changed.
44  */
45 struct hidden_region {
46         /** Physical start address */
47         uint64_t start;
48         /** Physical end address */
49         uint64_t end;
50 };
51
52 /** Hidden base memory */
53 extern struct hidden_region __data16 ( hidemem_base );
54 #define hidemem_base __use_data16 ( hidemem_base )
55
56 /** Hidden umalloc memory */
57 extern struct hidden_region __data16 ( hidemem_umalloc );
58 #define hidemem_umalloc __use_data16 ( hidemem_umalloc )
59
60 /** Hidden text memory */
61 extern struct hidden_region __data16 ( hidemem_textdata );
62 #define hidemem_textdata __use_data16 ( hidemem_textdata )
63
64 /** Assembly routine in e820mangler.S */
65 extern void int15();
66
67 /** Vector for storing original INT 15 handler */
68 extern struct segoff __text16 ( int15_vector );
69 #define int15_vector __use_text16 ( int15_vector )
70
71 /* The linker defines these symbols for us */
72 extern char _textdata[];
73 extern char _etextdata[];
74 extern char _text16_memsz[];
75 #define _text16_memsz ( ( unsigned int ) _text16_memsz )
76 extern char _data16_memsz[];
77 #define _data16_memsz ( ( unsigned int ) _data16_memsz )
78
79 /**
80  * Hide region of memory from system memory map
81  *
82  * @v region            Hidden memory region
83  * @v start             Start of region
84  * @v end               End of region
85  */
86 static void hide_region ( struct hidden_region *region,
87                           physaddr_t start, physaddr_t end ) {
88
89         /* Some operating systems get a nasty shock if a region of the
90          * E820 map seems to start on a non-page boundary.  Make life
91          * safer by rounding out our edited region.
92          */
93         region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
94         region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
95
96         DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
97 }
98
99 /**
100  * Hide used base memory
101  *
102  */
103 void hide_basemem ( void ) {
104         /* Hide from the top of free base memory to 640kB.  Don't use
105          * hide_region(), because we don't want this rounded to the
106          * nearest page boundary.
107          */
108         hidemem_base.start = ( get_fbms() * 1024 );
109 }
110
111 /**
112  * Hide umalloc() region
113  *
114  */
115 void hide_umalloc ( physaddr_t start, physaddr_t end ) {
116         assert ( end <= virt_to_phys ( _textdata ) );
117         hide_region ( &hidemem_umalloc, start, end );
118 }
119
120 /**
121  * Hide .text and .data
122  *
123  */
124 void hide_textdata ( void ) {
125         hide_region ( &hidemem_textdata, virt_to_phys ( _textdata ),
126                       virt_to_phys ( _etextdata ) );
127 }
128
129 /**
130  * Hide Etherboot
131  *
132  * Installs an INT 15 handler to edit Etherboot out of the memory map
133  * returned by the BIOS.
134  */
135 static void hide_etherboot ( void ) {
136         struct memory_map memmap;
137         unsigned int rm_ds_top;
138         unsigned int rm_cs_top;
139         unsigned int fbms;
140
141         /* Dump memory map before mangling */
142         DBG ( "Hiding iPXE from system memory map\n" );
143         get_memmap ( &memmap );
144
145         /* Hook in fake E820 map, if we're testing one */
146         if ( FAKE_E820 ) {
147                 DBG ( "Hooking in fake E820 map\n" );
148                 fake_e820();
149                 get_memmap ( &memmap );
150         }
151
152         /* Initialise the hidden regions */
153         hide_basemem();
154         hide_umalloc ( virt_to_phys ( _textdata ), virt_to_phys ( _textdata ) );
155         hide_textdata();
156
157         /* Some really moronic BIOSes bring up the PXE stack via the
158          * UNDI loader entry point and then don't bother to unload it
159          * before overwriting the code and data segments.  If this
160          * happens, we really don't want to leave INT 15 hooked,
161          * because that will cause any loaded OS to die horribly as
162          * soon as it attempts to fetch the system memory map.
163          *
164          * We use a heuristic to guess whether or not we are being
165          * loaded sensibly.
166          */
167         rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_memsz + 1024 - 1 ) >> 10 );
168         rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_memsz + 1024 - 1 ) >> 10 );
169         fbms = get_fbms();
170         if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
171                 DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
172                       "DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
173                 DBG ( "Disabling INT 15 memory hiding\n" );
174                 return;
175         }
176
177         /* Hook INT 15 */
178         hook_bios_interrupt ( 0x15, ( unsigned int ) int15,
179                               &int15_vector );
180
181         /* Dump memory map after mangling */
182         DBG ( "Hidden iPXE from system memory map\n" );
183         get_memmap ( &memmap );
184 }
185
186 /**
187  * Unhide Etherboot
188  *
189  * Uninstalls the INT 15 handler installed by hide_etherboot(), if
190  * possible.
191  */
192 static void unhide_etherboot ( int flags __unused ) {
193         struct memory_map memmap;
194         int rc;
195
196         /* If we have more than one hooked interrupt at this point, it
197          * means that some other vector is still hooked, in which case
198          * we can't safely unhook INT 15 because we need to keep our
199          * memory protected.  (We expect there to be at least one
200          * hooked interrupt, because INT 15 itself is still hooked).
201          */
202         if ( hooked_bios_interrupts > 1 ) {
203                 DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
204                       hooked_bios_interrupts );
205                 return;
206         }
207
208         /* Try to unhook INT 15 */
209         if ( ( rc = unhook_bios_interrupt ( 0x15, ( unsigned int ) int15,
210                                             &int15_vector ) ) != 0 ) {
211                 DBG ( "Cannot unhook INT15: %s\n", strerror ( rc ) );
212                 /* Leave it hooked; there's nothing else we can do,
213                  * and it should be intrinsically safe (though
214                  * wasteful of RAM).
215                  */
216         }
217
218         /* Unhook fake E820 map, if used */
219         if ( FAKE_E820 )
220                 unfake_e820();
221
222         /* Dump memory map after unhiding */
223         DBG ( "Unhidden iPXE from system memory map\n" );
224         get_memmap ( &memmap );
225 }
226
227 /** Hide Etherboot startup function */
228 struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
229         .startup = hide_etherboot,
230         .shutdown = unhide_etherboot,
231 };