Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

relocate.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "virtaddr.h"
  2. #include "memsizes.h"
  3. #include "osdep.h"
  4. #include "etherboot.h"
  5. #include "init.h"
  6. #include "relocate.h"
  7. #ifndef KEEP_IT_REAL
  8. /* by Eric Biederman */
  9. /* On some platforms etherboot is compiled as a shared library, and we use
  10. * the ELF pic support to make it relocateable. This works very nicely
  11. * for code, but since no one has implemented PIC data yet pointer
  12. * values in variables are a a problem. Global variables are a
  13. * pain but the return addresses on the stack are the worst. On these
  14. * platforms relocate_to will restart etherboot, to ensure the stack
  15. * is reinitialize and hopefully get the global variables
  16. * appropriately reinitialized as well.
  17. *
  18. */
  19. /*
  20. * relocate() must be called without any hardware resources pointing
  21. * at the current copy of Etherboot. The easiest way to achieve this
  22. * is to call relocate() from within arch_initialise(), before the NIC
  23. * gets touched in any way.
  24. *
  25. */
  26. /*
  27. * The linker passes in the symbol _max_align, which is the alignment
  28. * that we must preserve, in bytes.
  29. *
  30. */
  31. extern char _max_align[];
  32. #define max_align ( ( unsigned int ) _max_align )
  33. /* Linker symbols */
  34. extern char _text[];
  35. extern char _end[];
  36. extern struct post_reloc_fn post_reloc_fns[];
  37. extern struct post_reloc_fn post_reloc_fns_end[];
  38. static void relocate ( void ) {
  39. unsigned long addr, eaddr, size;
  40. unsigned i;
  41. struct post_reloc_fn *post_reloc_fn;
  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. /* Note that we cannot make real-mode calls
  162. * (e.g. printf) at this point, because librm has just
  163. * been moved to high memory.
  164. */
  165. /* Call any registered post-relocation functions.
  166. * librm has a post-relocation function to install a
  167. * new librm into base memory.
  168. */
  169. for ( post_reloc_fn = post_reloc_fns;
  170. post_reloc_fn < post_reloc_fns_end ; post_reloc_fn++ ) {
  171. if ( post_reloc_fn->post_reloc )
  172. post_reloc_fn->post_reloc ();
  173. }
  174. }
  175. }
  176. INIT_FN ( INIT_RELOCATE, relocate, NULL, NULL );
  177. #endif /* ! KEEP_IT_REAL */