Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

hidemem.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <realmode.h>
  18. #include <biosint.h>
  19. #include <basemem.h>
  20. #include <gpxe/init.h>
  21. #include <gpxe/hidemem.h>
  22. /** Alignment for hidden memory regions */
  23. #define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
  24. /**
  25. * A hidden region of Etherboot
  26. *
  27. * This represents a region that will be edited out of the system's
  28. * memory map.
  29. *
  30. * This structure is accessed by assembly code, so must not be
  31. * changed.
  32. */
  33. struct hidden_region {
  34. /* Physical start address */
  35. physaddr_t start;
  36. /* Physical end address */
  37. physaddr_t end;
  38. };
  39. /**
  40. * List of hidden regions
  41. *
  42. * Must be terminated by a zero entry.
  43. */
  44. struct hidden_region __data16_array ( hidden_regions, [] ) = {
  45. [TEXT] = { 0, 0 },
  46. [BASEMEM] = { ( 640 * 1024 ), ( 640 * 1024 ) },
  47. [EXTMEM] = { 0, 0 },
  48. { 0, 0, } /* Terminator */
  49. };
  50. #define hidden_regions __use_data16 ( hidden_regions )
  51. /** Assembly routine in e820mangler.S */
  52. extern void int15();
  53. /** Vector for storing original INT 15 handler */
  54. extern struct segoff __text16 ( int15_vector );
  55. #define int15_vector __use_text16 ( int15_vector )
  56. /**
  57. * Hide region of memory from system memory map
  58. *
  59. * @v start Start of region
  60. * @v end End of region
  61. */
  62. void hide_region ( unsigned int region_id, physaddr_t start, physaddr_t end ) {
  63. struct hidden_region *region = &hidden_regions[region_id];
  64. /* Some operating systems get a nasty shock if a region of the
  65. * E820 map seems to start on a non-page boundary. Make life
  66. * safer by rounding out our edited region.
  67. */
  68. region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
  69. region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
  70. DBG ( "Hiding region %d [%lx,%lx)\n",
  71. region_id, region->start, region->end );
  72. }
  73. /**
  74. * Hide Etherboot text
  75. *
  76. */
  77. static void hide_text ( void ) {
  78. /* The linker defines these symbols for us */
  79. extern char _text[];
  80. extern char _end[];
  81. hide_region ( TEXT, virt_to_phys ( _text ), virt_to_phys ( _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. hidden_regions[BASEMEM].start = ( get_fbms() * 1024 );
  93. }
  94. /**
  95. * Hide Etherboot
  96. *
  97. * Installs an INT 15 handler to edit Etherboot out of the memory map
  98. * returned by the BIOS.
  99. */
  100. static void hide_etherboot ( void ) {
  101. /* Initialise the hidden regions */
  102. hide_text();
  103. hide_basemem();
  104. /* Hook INT 15 */
  105. hook_bios_interrupt ( 0x15, ( unsigned int ) int15,
  106. &int15_vector );
  107. }
  108. /**
  109. * Unhide Etherboot
  110. *
  111. * Uninstalls the INT 15 handler installed by hide_etherboot(), if
  112. * possible.
  113. */
  114. static void unhide_etherboot ( void ) {
  115. /* If we have more than one hooked interrupt at this point, it
  116. * means that some other vector is still hooked, in which case
  117. * we can't safely unhook INT 15 because we need to keep our
  118. * memory protected. (We expect there to be at least one
  119. * hooked interrupt, because INT 15 itself is still hooked).
  120. */
  121. if ( hooked_bios_interrupts > 1 ) {
  122. DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
  123. hooked_bios_interrupts );
  124. return;
  125. }
  126. /* Try to unhook INT 15. If it fails, then just leave it
  127. * hooked; it takes care of protecting itself. :)
  128. */
  129. unhook_bios_interrupt ( 0x15, ( unsigned int ) int15,
  130. &int15_vector );
  131. }
  132. /** Hide Etherboot startup function */
  133. struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
  134. .startup = hide_etherboot,
  135. .shutdown = unhide_etherboot,
  136. };