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.3KB

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