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

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