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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <ipxe/io.h>
  2. #include <registers.h>
  3. /*
  4. * Originally by Eric Biederman
  5. *
  6. * Heavily modified by Michael Brown
  7. *
  8. */
  9. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  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 iPXE
  29. *
  30. * @v ebp Maximum address to use for relocation
  31. * @ret esi Current physical address
  32. * @ret edi New physical address
  33. * @ret ecx Length to copy
  34. *
  35. * This finds a suitable location for iPXE near the top of 32-bit
  36. * address space, and returns the physical address of the new location
  37. * to the prefix in %edi.
  38. */
  39. __asmcall void relocate ( struct i386_all_regs *ix86 ) {
  40. struct memory_map memmap;
  41. unsigned long start, end, size, padded_size, max;
  42. unsigned long new_start, new_end;
  43. unsigned i;
  44. /* Get memory map and current location */
  45. get_memmap ( &memmap );
  46. start = virt_to_phys ( _textdata );
  47. end = virt_to_phys ( _etextdata );
  48. size = ( end - start );
  49. padded_size = ( size + max_align - 1 );
  50. DBG ( "Relocate: currently at [%lx,%lx)\n"
  51. "...need %lx bytes for %d-byte alignment\n",
  52. start, end, padded_size, max_align );
  53. /* Determine maximum usable address */
  54. max = MAX_ADDR;
  55. if ( ix86->regs.ebp < max ) {
  56. max = ix86->regs.ebp;
  57. DBG ( "Limiting relocation to [0,%lx)\n", max );
  58. }
  59. /* Walk through the memory map and find the highest address
  60. * below 4GB that iPXE will fit into.
  61. */
  62. new_end = end;
  63. for ( i = 0 ; i < memmap.count ; i++ ) {
  64. struct memory_region *region = &memmap.regions[i];
  65. unsigned long r_start, r_end;
  66. DBG ( "Considering [%llx,%llx)\n", region->start, region->end);
  67. /* Truncate block to maximum address. This will be
  68. * less than 4GB, which means that we can get away
  69. * with using just 32-bit arithmetic after this stage.
  70. */
  71. if ( region->start > max ) {
  72. DBG ( "...starts after max=%lx\n", max );
  73. continue;
  74. }
  75. r_start = region->start;
  76. if ( region->end > max ) {
  77. DBG ( "...end truncated to max=%lx\n", max );
  78. r_end = max;
  79. } else {
  80. r_end = region->end;
  81. }
  82. DBG ( "...usable portion is [%lx,%lx)\n", r_start, r_end );
  83. /* If we have rounded down r_end below r_ start, skip
  84. * this block.
  85. */
  86. if ( r_end < r_start ) {
  87. DBG ( "...truncated to negative size\n" );
  88. continue;
  89. }
  90. /* Check that there is enough space to fit in iPXE */
  91. if ( ( r_end - r_start ) < size ) {
  92. DBG ( "...too small (need %lx bytes)\n", size );
  93. continue;
  94. }
  95. /* If the start address of the iPXE we would
  96. * place in this block is higher than the end address
  97. * of the current highest block, use this block.
  98. *
  99. * Note that this avoids overlaps with the current
  100. * iPXE, as well as choosing the highest of all viable
  101. * blocks.
  102. */
  103. if ( ( r_end - size ) > new_end ) {
  104. new_end = r_end;
  105. DBG ( "...new best block found.\n" );
  106. }
  107. }
  108. /* Calculate new location of iPXE, and align it to the
  109. * required alignemnt.
  110. */
  111. new_start = new_end - padded_size;
  112. new_start += ( start - new_start ) & ( max_align - 1 );
  113. new_end = new_start + size;
  114. DBG ( "Relocating from [%lx,%lx) to [%lx,%lx)\n",
  115. start, end, new_start, new_end );
  116. /* Let prefix know what to copy */
  117. ix86->regs.esi = start;
  118. ix86->regs.edi = new_start;
  119. ix86->regs.ecx = size;
  120. }