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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <io.h>
  2. #include <registers.h>
  3. #include <gpxe/memmap.h>
  4. /*
  5. * Originally by Eric Biederman
  6. *
  7. * Heavily modified by Michael Brown
  8. *
  9. */
  10. /*
  11. * The linker passes in the symbol _max_align, which is the alignment
  12. * that we must preserve, in bytes.
  13. *
  14. */
  15. extern char _max_align[];
  16. #define max_align ( ( unsigned int ) _max_align )
  17. /* Linker symbols */
  18. extern char _textdata[];
  19. extern char _etextdata[];
  20. /* within 1MB of 4GB is too close.
  21. * MAX_ADDR is the maximum address we can easily do DMA to.
  22. *
  23. * Not sure where this constraint comes from, but kept it from Eric's
  24. * old code - mcb30
  25. */
  26. #define MAX_ADDR (0xfff00000UL)
  27. /**
  28. * Relocate Etherboot
  29. *
  30. * @v ix86 x86 register dump from prefix
  31. * @ret ix86 x86 registers to return to prefix
  32. *
  33. * This finds a suitable location for Etherboot near the top of 32-bit
  34. * address space, and returns the physical address of the new location
  35. * to the prefix in %edi.
  36. */
  37. __cdecl void relocate ( struct i386_all_regs *ix86 ) {
  38. struct memory_map memmap;
  39. unsigned long start, end, size, padded_size;
  40. unsigned long new_start, new_end;
  41. unsigned i;
  42. /* Get memory map and current location */
  43. get_memmap ( &memmap );
  44. start = virt_to_phys ( _textdata );
  45. end = virt_to_phys ( _etextdata );
  46. size = ( end - start );
  47. padded_size = ( size + max_align - 1 );
  48. DBG ( "Relocate: currently at [%lx,%lx)\n"
  49. "...need %lx bytes for %d-byte alignment\n",
  50. start, end, padded_size, max_align );
  51. /* Walk through the memory map and find the highest address
  52. * below 4GB that etherboot will fit into. Ensure etherboot
  53. * lies entirely within a range with A20=0. This means that
  54. * even if something screws up the state of the A20 line, the
  55. * etherboot code is still visible and we have a chance to
  56. * diagnose the problem.
  57. */
  58. new_end = end;
  59. for ( i = 0 ; i < memmap.count ; i++ ) {
  60. struct memory_region *region = &memmap.regions[i];
  61. unsigned long r_start, r_end;
  62. DBG ( "Considering [%llx,%llx)\n", region->start, region->end);
  63. /* Truncate block to MAX_ADDR. This will be less than
  64. * 4GB, which means that we can get away with using
  65. * just 32-bit arithmetic after this stage.
  66. */
  67. if ( region->start > MAX_ADDR ) {
  68. DBG ( "...starts after MAX_ADDR=%lx\n", MAX_ADDR );
  69. continue;
  70. }
  71. r_start = region->start;
  72. if ( region->end > MAX_ADDR ) {
  73. DBG ( "...end truncated to MAX_ADDR=%lx\n", MAX_ADDR );
  74. r_end = MAX_ADDR;
  75. } else {
  76. r_end = region->end;
  77. }
  78. /* Shrink the range down to use only even megabytes
  79. * (i.e. A20=0).
  80. */
  81. if ( ( r_end - 1 ) & 0x100000 ) {
  82. /* If last byte that might be used (r_end-1)
  83. * is in an odd megabyte, round down r_end to
  84. * the top of the next even megabyte.
  85. *
  86. * Make sure that we don't accidentally wrap
  87. * r_end below 0.
  88. */
  89. if ( r_end >= 1 ) {
  90. r_end = ( r_end - 1 ) & ~0xfffff;
  91. DBG ( "...end truncated to %lx "
  92. "(avoid ending in odd megabyte)\n",
  93. r_end );
  94. }
  95. } else if ( ( r_end - size ) & 0x100000 ) {
  96. /* If the last byte that might be used
  97. * (r_end-1) is in an even megabyte, but the
  98. * first byte that might be used (r_end-size)
  99. * is an odd megabyte, round down to the top
  100. * of the next even megabyte.
  101. *
  102. * Make sure that we don't accidentally wrap
  103. * r_end below 0.
  104. */
  105. if ( r_end >= 0x100000 ) {
  106. r_end = ( r_end - 0x100000 ) & ~0xfffff;
  107. DBG ( "...end truncated to %lx "
  108. "(avoid starting in odd megabyte)\n",
  109. r_end );
  110. }
  111. }
  112. DBG ( "...usable portion is [%lx,%lx)\n", r_start, r_end );
  113. /* If we have rounded down r_end below r_ start, skip
  114. * this block.
  115. */
  116. if ( r_end < r_start ) {
  117. DBG ( "...truncated to negative size\n" );
  118. continue;
  119. }
  120. /* Check that there is enough space to fit in Etherboot */
  121. if ( ( r_end - r_start ) < size ) {
  122. DBG ( "...too small (need %lx bytes)\n", size );
  123. continue;
  124. }
  125. /* If the start address of the Etherboot we would
  126. * place in this block is higher than the end address
  127. * of the current highest block, use this block.
  128. *
  129. * Note that this avoids overlaps with the current
  130. * Etherboot, as well as choosing the highest of all
  131. * viable blocks.
  132. */
  133. if ( ( r_end - size ) > new_end ) {
  134. new_end = r_end;
  135. DBG ( "...new best block found.\n" );
  136. }
  137. }
  138. /* Calculate new location of Etherboot, and align it to the
  139. * required alignemnt.
  140. */
  141. new_start = new_end - padded_size;
  142. new_start += ( start - new_start ) & ( max_align - 1 );
  143. new_end = new_start + size;
  144. DBG ( "Relocating from [%lx,%lx) to [%lx,%lx)\n",
  145. start, end, new_start, new_end );
  146. /* Let prefix know what to copy */
  147. ix86->regs.esi = start;
  148. ix86->regs.edi = new_start;
  149. ix86->regs.ecx = size;
  150. }