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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <gpxe/init.h>
  22. #include <gpxe/memmap.h>
  23. #include <gpxe/hidemem.h>
  24. /** Alignment for hidden memory regions */
  25. #define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
  26. /**
  27. * A hidden region of gPXE
  28. *
  29. * This represents a region that will be edited out of the system's
  30. * memory map.
  31. *
  32. * This structure is accessed by assembly code, so must not be
  33. * changed.
  34. */
  35. struct hidden_region {
  36. /** Physical start address */
  37. uint64_t start;
  38. /** Physical end address */
  39. uint64_t end;
  40. };
  41. /** Hidden base memory */
  42. extern struct hidden_region __data16 ( hidemem_base );
  43. #define hidemem_base __use_data16 ( hidemem_base )
  44. /** Hidden umalloc memory */
  45. extern struct hidden_region __data16 ( hidemem_umalloc );
  46. #define hidemem_umalloc __use_data16 ( hidemem_umalloc )
  47. /** Hidden text memory */
  48. extern struct hidden_region __data16 ( hidemem_text );
  49. #define hidemem_text __use_data16 ( hidemem_text )
  50. /** Assembly routine in e820mangler.S */
  51. extern void int15();
  52. /** Vector for storing original INT 15 handler */
  53. extern struct segoff __text16 ( int15_vector );
  54. #define int15_vector __use_text16 ( int15_vector )
  55. /* The linker defines these symbols for us */
  56. extern char _text[];
  57. extern char _end[];
  58. /**
  59. * Hide region of memory from system memory map
  60. *
  61. * @v region Hidden memory region
  62. * @v start Start of region
  63. * @v end End of region
  64. */
  65. static void hide_region ( struct hidden_region *region,
  66. physaddr_t start, physaddr_t end ) {
  67. /* Some operating systems get a nasty shock if a region of the
  68. * E820 map seems to start on a non-page boundary. Make life
  69. * safer by rounding out our edited region.
  70. */
  71. region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
  72. region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
  73. DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
  74. }
  75. /**
  76. * Hide used base memory
  77. *
  78. */
  79. void hide_basemem ( void ) {
  80. /* Hide from the top of free base memory to 640kB. Don't use
  81. * hide_region(), because we don't want this rounded to the
  82. * nearest page boundary.
  83. */
  84. hidemem_base.start = ( get_fbms() * 1024 );
  85. }
  86. /**
  87. * Hide umalloc() region
  88. *
  89. */
  90. void hide_umalloc ( physaddr_t start, physaddr_t end ) {
  91. assert ( end <= virt_to_phys ( _text ) );
  92. hide_region ( &hidemem_umalloc, start, end );
  93. }
  94. /**
  95. * Hide .text and .data
  96. *
  97. */
  98. void hide_text ( void ) {
  99. hide_region ( &hidemem_text, virt_to_phys ( _text ),
  100. virt_to_phys ( _end ) );
  101. }
  102. /**
  103. * Hide Etherboot
  104. *
  105. * Installs an INT 15 handler to edit Etherboot out of the memory map
  106. * returned by the BIOS.
  107. */
  108. static void hide_etherboot ( void ) {
  109. struct memory_map memmap;
  110. /* Dump memory map before mangling */
  111. DBG ( "Hiding gPXE from system memory map\n" );
  112. get_memmap ( &memmap );
  113. /* Initialise the hidden regions */
  114. hide_basemem();
  115. hide_umalloc ( virt_to_phys ( _text ), virt_to_phys ( _text ) );
  116. hide_text();
  117. /* Hook INT 15 */
  118. hook_bios_interrupt ( 0x15, ( unsigned int ) int15,
  119. &int15_vector );
  120. /* Dump memory map after mangling */
  121. DBG ( "Hidden gPXE from system memory map\n" );
  122. get_memmap ( &memmap );
  123. }
  124. /**
  125. * Unhide Etherboot
  126. *
  127. * Uninstalls the INT 15 handler installed by hide_etherboot(), if
  128. * possible.
  129. */
  130. static void unhide_etherboot ( int flags __unused ) {
  131. /* If we have more than one hooked interrupt at this point, it
  132. * means that some other vector is still hooked, in which case
  133. * we can't safely unhook INT 15 because we need to keep our
  134. * memory protected. (We expect there to be at least one
  135. * hooked interrupt, because INT 15 itself is still hooked).
  136. */
  137. if ( hooked_bios_interrupts > 1 ) {
  138. DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
  139. hooked_bios_interrupts );
  140. return;
  141. }
  142. /* Try to unhook INT 15. If it fails, then just leave it
  143. * hooked; it takes care of protecting itself. :)
  144. */
  145. unhook_bios_interrupt ( 0x15, ( unsigned int ) int15,
  146. &int15_vector );
  147. }
  148. /** Hide Etherboot startup function */
  149. struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
  150. .startup = hide_etherboot,
  151. .shutdown = unhide_etherboot,
  152. };