Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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