You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hidemem.c 6.8KB

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