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

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