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.

relocate.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "virtaddr.h"
  2. #include "memsizes.h"
  3. #include "osdep.h"
  4. #include "etherboot.h"
  5. #include "relocate.h"
  6. /* by Eric Biederman */
  7. /* On some platforms etherboot is compiled as a shared library, and we use
  8. * the ELF pic support to make it relocateable. This works very nicely
  9. * for code, but since no one has implemented PIC data yet pointer
  10. * values in variables are a a problem. Global variables are a
  11. * pain but the return addresses on the stack are the worst. On these
  12. * platforms relocate_to will restart etherboot, to ensure the stack
  13. * is reinitialize and hopefully get the global variables
  14. * appropriately reinitialized as well.
  15. *
  16. */
  17. /*
  18. * relocate() must be called without any hardware resources pointing
  19. * at the current copy of Etherboot. The easiest way to achieve this
  20. * is to call relocate() from within arch_initialise(), before the NIC
  21. * gets touched in any way.
  22. *
  23. */
  24. /*
  25. * The linker passes in the symbol _max_align, which is the alignment
  26. * that we must preserve, in bytes.
  27. *
  28. */
  29. extern char _max_align[];
  30. #define max_align ( ( unsigned int ) _max_align )
  31. /* Linker symbols */
  32. extern char _text[];
  33. extern char _end[];
  34. #undef DBG
  35. #ifdef DEBUG_RELOCATE
  36. #define DBG(...) printf ( __VA_ARGS__ )
  37. #else
  38. #define DBG(...)
  39. #endif
  40. void relocate ( void ) {
  41. unsigned long addr, eaddr, size;
  42. unsigned i;
  43. /* Walk through the memory map and find the highest address
  44. * below 4GB that etherboot will fit into. Ensure etherboot
  45. * lies entirely within a range with A20=0. This means that
  46. * even if something screws up the state of the A20 line, the
  47. * etherboot code is still visible and we have a chance to
  48. * diagnose the problem.
  49. */
  50. /* First find the size of etherboot, including enough space to
  51. * pad it to the required alignment
  52. */
  53. size = _end - _text + max_align - 1;
  54. /* Current end address of Etherboot. If the current etherboot
  55. * is beyond MAX_ADDR pretend it is at the lowest possible
  56. * address.
  57. */
  58. eaddr = virt_to_phys(_end);
  59. if ( eaddr > MAX_ADDR ) {
  60. eaddr = 0;
  61. }
  62. DBG ( "Relocate: currently at [%x,%x)\n"
  63. "...need %x bytes for %d-byte alignment\n",
  64. virt_to_phys ( _text ), eaddr, size, max_align );
  65. for ( i = 0; i < meminfo.map_count; i++ ) {
  66. unsigned long r_start, r_end;
  67. DBG ( "Considering [%x%x,%x%x)\n",
  68. ( unsigned long ) ( meminfo.map[i].addr >> 32 ),
  69. ( unsigned long ) meminfo.map[i].addr,
  70. ( unsigned long )
  71. ( ( meminfo.map[i].addr + meminfo.map[i].size ) >> 32 ),
  72. ( unsigned long )
  73. ( meminfo.map[i].addr + meminfo.map[i].size ) );
  74. /* Check block is usable memory */
  75. if (meminfo.map[i].type != E820_RAM) {
  76. DBG ( "...not RAM\n" );
  77. continue;
  78. }
  79. /* Truncate block to MAX_ADDR. This will be less than
  80. * 4GB, which means that we can get away with using
  81. * just 32-bit arithmetic after this stage.
  82. */
  83. if ( meminfo.map[i].addr > MAX_ADDR ) {
  84. DBG ( "...starts after MAX_ADDR=%x\n", MAX_ADDR );
  85. continue;
  86. }
  87. r_start = meminfo.map[i].addr;
  88. if ( meminfo.map[i].addr + meminfo.map[i].size > MAX_ADDR ) {
  89. r_end = MAX_ADDR;
  90. DBG ( "...end truncated to MAX_ADDR=%x\n", MAX_ADDR );
  91. } else {
  92. r_end = meminfo.map[i].addr + meminfo.map[i].size;
  93. }
  94. /* Shrink the range down to use only even megabytes
  95. * (i.e. A20=0).
  96. */
  97. if ( ( r_end - 1 ) & 0x100000 ) {
  98. /* If last byte that might be used (r_end-1)
  99. * is in an odd megabyte, round down r_end to
  100. * the top of the next even megabyte.
  101. */
  102. r_end = ( r_end - 1 ) & ~0xfffff;
  103. DBG ( "...end truncated to %x "
  104. "(avoid ending in odd megabyte)\n",
  105. r_end );
  106. } else if ( ( r_end - size ) & 0x100000 ) {
  107. /* If the last byte that might be used
  108. * (r_end-1) is in an even megabyte, but the
  109. * first byte that might be used (r_end-size)
  110. * is an odd megabyte, round down to the top
  111. * of the next even megabyte.
  112. *
  113. * Make sure that we don't accidentally wrap
  114. * r_end below 0.
  115. */
  116. if ( r_end > 0x100000 ) {
  117. r_end = ( r_end - 0x100000 ) & ~0xfffff;
  118. DBG ( "...end truncated to %x "
  119. "(avoid starting in odd megabyte)\n",
  120. r_end );
  121. }
  122. }
  123. DBG ( "...usable portion is [%x,%x)\n", r_start, r_end );
  124. /* If we have rounded down r_end below r_ start, skip
  125. * this block.
  126. */
  127. if ( r_end < r_start ) {
  128. DBG ( "...truncated to negative size\n" );
  129. continue;
  130. }
  131. /* Check that there is enough space to fit in Etherboot */
  132. if ( r_end - r_start < size ) {
  133. DBG ( "...too small (need %x bytes)\n", size );
  134. continue;
  135. }
  136. /* If the start address of the Etherboot we would
  137. * place in this block is higher than the end address
  138. * of the current highest block, use this block.
  139. *
  140. * Note that this avoids overlaps with the current
  141. * Etherboot, as well as choosing the highest of all
  142. * viable blocks.
  143. */
  144. if ( r_end - size > eaddr ) {
  145. eaddr = r_end;
  146. DBG ( "...new best block found.\n" );
  147. }
  148. }
  149. DBG ( "New location will be in [%x,%x)\n", eaddr - size, eaddr );
  150. /* Calculate new location of Etherboot, and align it to the
  151. * required alignemnt.
  152. */
  153. addr = eaddr - size;
  154. addr += ( virt_to_phys ( _text ) - addr ) & ( max_align - 1 );
  155. DBG ( "After alignment, new location is [%x,%x)\n",
  156. addr, addr + _end - _text );
  157. if ( addr != virt_to_phys ( _text ) ) {
  158. DBG ( "Relocating _text from: [%lx,%lx) to [%lx,%lx)\n",
  159. virt_to_phys ( _text ), virt_to_phys ( _end ),
  160. addr, addr + _end - _text );
  161. relocate_to ( addr );
  162. /* Note that we cannot make real-mode calls
  163. * (e.g. printf) at this point, because the pointer
  164. * installed_librm uses a virtual address (in order
  165. * that it can have a valid initialiser) and so is
  166. * currently invalid.
  167. */
  168. }
  169. }