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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 );
  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 ix86 x86 register dump from prefix
  31. * @ret ix86 x86 registers to return to prefix
  32. *
  33. * This finds a suitable location for iPXE 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. __asmcall 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 iPXE will fit into.
  53. */
  54. new_end = end;
  55. for ( i = 0 ; i < memmap.count ; i++ ) {
  56. struct memory_region *region = &memmap.regions[i];
  57. unsigned long r_start, r_end;
  58. DBG ( "Considering [%llx,%llx)\n", region->start, region->end);
  59. /* Truncate block to MAX_ADDR. This will be less than
  60. * 4GB, which means that we can get away with using
  61. * just 32-bit arithmetic after this stage.
  62. */
  63. if ( region->start > MAX_ADDR ) {
  64. DBG ( "...starts after MAX_ADDR=%lx\n", MAX_ADDR );
  65. continue;
  66. }
  67. r_start = region->start;
  68. if ( region->end > MAX_ADDR ) {
  69. DBG ( "...end truncated to MAX_ADDR=%lx\n", MAX_ADDR );
  70. r_end = MAX_ADDR;
  71. } else {
  72. r_end = region->end;
  73. }
  74. DBG ( "...usable portion is [%lx,%lx)\n", r_start, r_end );
  75. /* If we have rounded down r_end below r_ start, skip
  76. * this block.
  77. */
  78. if ( r_end < r_start ) {
  79. DBG ( "...truncated to negative size\n" );
  80. continue;
  81. }
  82. /* Check that there is enough space to fit in iPXE */
  83. if ( ( r_end - r_start ) < size ) {
  84. DBG ( "...too small (need %lx bytes)\n", size );
  85. continue;
  86. }
  87. /* If the start address of the iPXE we would
  88. * place in this block is higher than the end address
  89. * of the current highest block, use this block.
  90. *
  91. * Note that this avoids overlaps with the current
  92. * iPXE, as well as choosing the highest of all viable
  93. * blocks.
  94. */
  95. if ( ( r_end - size ) > new_end ) {
  96. new_end = r_end;
  97. DBG ( "...new best block found.\n" );
  98. }
  99. }
  100. /* Calculate new location of iPXE, and align it to the
  101. * required alignemnt.
  102. */
  103. new_start = new_end - padded_size;
  104. new_start += ( start - new_start ) & ( max_align - 1 );
  105. new_end = new_start + size;
  106. DBG ( "Relocating from [%lx,%lx) to [%lx,%lx)\n",
  107. start, end, new_start, new_end );
  108. /* Let prefix know what to copy */
  109. ix86->regs.esi = start;
  110. ix86->regs.edi = new_start;
  111. ix86->regs.ecx = size;
  112. }