選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

relocate.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #undef DBG
  39. #ifdef DEBUG_RELOCATE
  40. #define DBG(...) printf ( __VA_ARGS__ )
  41. #else
  42. #define DBG(...)
  43. #endif
  44. static void relocate ( void ) {
  45. unsigned long addr, eaddr, size;
  46. unsigned i;
  47. struct post_reloc_fn *post_reloc_fn;
  48. /* Walk through the memory map and find the highest address
  49. * below 4GB that etherboot will fit into. Ensure etherboot
  50. * lies entirely within a range with A20=0. This means that
  51. * even if something screws up the state of the A20 line, the
  52. * etherboot code is still visible and we have a chance to
  53. * diagnose the problem.
  54. */
  55. /* First find the size of etherboot, including enough space to
  56. * pad it to the required alignment
  57. */
  58. size = _end - _text + max_align - 1;
  59. /* Current end address of Etherboot. If the current etherboot
  60. * is beyond MAX_ADDR pretend it is at the lowest possible
  61. * address.
  62. */
  63. eaddr = virt_to_phys(_end);
  64. if ( eaddr > MAX_ADDR ) {
  65. eaddr = 0;
  66. }
  67. DBG ( "Relocate: currently at [%x,%x)\n"
  68. "...need %x bytes for %d-byte alignment\n",
  69. virt_to_phys ( _text ), eaddr, size, max_align );
  70. for ( i = 0; i < meminfo.map_count; i++ ) {
  71. unsigned long r_start, r_end;
  72. DBG ( "Considering [%x%x,%x%x)\n",
  73. ( unsigned long ) ( meminfo.map[i].addr >> 32 ),
  74. ( unsigned long ) meminfo.map[i].addr,
  75. ( unsigned long )
  76. ( ( meminfo.map[i].addr + meminfo.map[i].size ) >> 32 ),
  77. ( unsigned long )
  78. ( meminfo.map[i].addr + meminfo.map[i].size ) );
  79. /* Check block is usable memory */
  80. if (meminfo.map[i].type != E820_RAM) {
  81. DBG ( "...not RAM\n" );
  82. continue;
  83. }
  84. /* Truncate block to MAX_ADDR. This will be less than
  85. * 4GB, which means that we can get away with using
  86. * just 32-bit arithmetic after this stage.
  87. */
  88. if ( meminfo.map[i].addr > MAX_ADDR ) {
  89. DBG ( "...starts after MAX_ADDR=%x\n", MAX_ADDR );
  90. continue;
  91. }
  92. r_start = meminfo.map[i].addr;
  93. if ( meminfo.map[i].addr + meminfo.map[i].size > MAX_ADDR ) {
  94. r_end = MAX_ADDR;
  95. DBG ( "...end truncated to MAX_ADDR=%x\n", MAX_ADDR );
  96. } else {
  97. r_end = meminfo.map[i].addr + meminfo.map[i].size;
  98. }
  99. /* Shrink the range down to use only even megabytes
  100. * (i.e. A20=0).
  101. */
  102. if ( ( r_end - 1 ) & 0x100000 ) {
  103. /* If last byte that might be used (r_end-1)
  104. * is in an odd megabyte, round down r_end to
  105. * the top of the next even megabyte.
  106. */
  107. r_end = ( r_end - 1 ) & ~0xfffff;
  108. DBG ( "...end truncated to %x "
  109. "(avoid ending in odd megabyte)\n",
  110. r_end );
  111. } else if ( ( r_end - size ) & 0x100000 ) {
  112. /* If the last byte that might be used
  113. * (r_end-1) is in an even megabyte, but the
  114. * first byte that might be used (r_end-size)
  115. * is an odd megabyte, round down to the top
  116. * of the next even megabyte.
  117. *
  118. * Make sure that we don't accidentally wrap
  119. * r_end below 0.
  120. */
  121. if ( r_end > 0x100000 ) {
  122. r_end = ( r_end - 0x100000 ) & ~0xfffff;
  123. DBG ( "...end truncated to %x "
  124. "(avoid starting in odd megabyte)\n",
  125. r_end );
  126. }
  127. }
  128. DBG ( "...usable portion is [%x,%x)\n", r_start, r_end );
  129. /* If we have rounded down r_end below r_ start, skip
  130. * this block.
  131. */
  132. if ( r_end < r_start ) {
  133. DBG ( "...truncated to negative size\n" );
  134. continue;
  135. }
  136. /* Check that there is enough space to fit in Etherboot */
  137. if ( r_end - r_start < size ) {
  138. DBG ( "...too small (need %x bytes)\n", size );
  139. continue;
  140. }
  141. /* If the start address of the Etherboot we would
  142. * place in this block is higher than the end address
  143. * of the current highest block, use this block.
  144. *
  145. * Note that this avoids overlaps with the current
  146. * Etherboot, as well as choosing the highest of all
  147. * viable blocks.
  148. */
  149. if ( r_end - size > eaddr ) {
  150. eaddr = r_end;
  151. DBG ( "...new best block found.\n" );
  152. }
  153. }
  154. DBG ( "New location will be in [%x,%x)\n", eaddr - size, eaddr );
  155. /* Calculate new location of Etherboot, and align it to the
  156. * required alignemnt.
  157. */
  158. addr = eaddr - size;
  159. addr += ( virt_to_phys ( _text ) - addr ) & ( max_align - 1 );
  160. DBG ( "After alignment, new location is [%x,%x)\n",
  161. addr, addr + _end - _text );
  162. if ( addr != virt_to_phys ( _text ) ) {
  163. DBG ( "Relocating _text from: [%lx,%lx) to [%lx,%lx)\n",
  164. virt_to_phys ( _text ), virt_to_phys ( _end ),
  165. addr, addr + _end - _text );
  166. relocate_to ( addr );
  167. /* Note that we cannot make real-mode calls
  168. * (e.g. printf) at this point, because librm has just
  169. * been moved to high memory.
  170. */
  171. /* Call any registered post-relocation functions.
  172. * librm has a post-relocation function to install a
  173. * new librm into base memory.
  174. */
  175. for ( post_reloc_fn = post_reloc_fns;
  176. post_reloc_fn < post_reloc_fns_end ; post_reloc_fn++ ) {
  177. if ( post_reloc_fn->post_reloc )
  178. post_reloc_fn->post_reloc ();
  179. }
  180. }
  181. }
  182. INIT_FN ( INIT_RELOCATE, relocate, NULL, NULL );
  183. #endif /* ! KEEP_IT_REAL */