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

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