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.

relocate.c 5.4KB

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