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

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