Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

relocate.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /* Linker symbols */
  11. extern char _textdata[];
  12. extern char _etextdata[];
  13. /* within 1MB of 4GB is too close.
  14. * MAX_ADDR is the maximum address we can easily do DMA to.
  15. *
  16. * Not sure where this constraint comes from, but kept it from Eric's
  17. * old code - mcb30
  18. */
  19. #define MAX_ADDR (0xfff00000UL)
  20. /* Preserve alignment to a 4kB page
  21. *
  22. * Required for x86_64, and doesn't hurt for i386.
  23. */
  24. #define ALIGN 4096
  25. /**
  26. * Relocate iPXE
  27. *
  28. * @v ebp Maximum address to use for relocation
  29. * @ret esi Current physical address
  30. * @ret edi New physical address
  31. * @ret ecx Length to copy
  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. uint32_t start, end, size, padded_size, max;
  40. uint32_t 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 + ALIGN - 1 );
  48. DBG ( "Relocate: currently at [%x,%x)\n"
  49. "...need %x bytes for %d-byte alignment\n",
  50. start, end, padded_size, ALIGN );
  51. /* Determine maximum usable address */
  52. max = MAX_ADDR;
  53. if ( ix86->regs.ebp < max ) {
  54. max = ix86->regs.ebp;
  55. DBG ( "Limiting relocation to [0,%x)\n", max );
  56. }
  57. /* Walk through the memory map and find the highest address
  58. * below 4GB that iPXE will fit into.
  59. */
  60. new_end = end;
  61. for ( i = 0 ; i < memmap.count ; i++ ) {
  62. struct memory_region *region = &memmap.regions[i];
  63. uint32_t r_start, r_end;
  64. DBG ( "Considering [%llx,%llx)\n", region->start, region->end);
  65. /* Truncate block to maximum address. This will be
  66. * less than 4GB, which means that we can get away
  67. * with using just 32-bit arithmetic after this stage.
  68. */
  69. if ( region->start > max ) {
  70. DBG ( "...starts after max=%x\n", max );
  71. continue;
  72. }
  73. r_start = region->start;
  74. if ( region->end > max ) {
  75. DBG ( "...end truncated to max=%x\n", max );
  76. r_end = max;
  77. } else {
  78. r_end = region->end;
  79. }
  80. DBG ( "...usable portion is [%x,%x)\n", r_start, r_end );
  81. /* If we have rounded down r_end below r_ start, skip
  82. * this block.
  83. */
  84. if ( r_end < r_start ) {
  85. DBG ( "...truncated to negative size\n" );
  86. continue;
  87. }
  88. /* Check that there is enough space to fit in iPXE */
  89. if ( ( r_end - r_start ) < size ) {
  90. DBG ( "...too small (need %x bytes)\n", size );
  91. continue;
  92. }
  93. /* If the start address of the iPXE we would
  94. * place in this block is higher than the end address
  95. * of the current highest block, use this block.
  96. *
  97. * Note that this avoids overlaps with the current
  98. * iPXE, as well as choosing the highest of all viable
  99. * blocks.
  100. */
  101. if ( ( r_end - size ) > new_end ) {
  102. new_end = r_end;
  103. DBG ( "...new best block found.\n" );
  104. }
  105. }
  106. /* Calculate new location of iPXE, and align it to the
  107. * required alignemnt.
  108. */
  109. new_start = new_end - padded_size;
  110. new_start += ( ( start - new_start ) & ( ALIGN - 1 ) );
  111. new_end = new_start + size;
  112. DBG ( "Relocating from [%x,%x) to [%x,%x)\n",
  113. start, end, new_start, new_end );
  114. /* Let prefix know what to copy */
  115. ix86->regs.esi = start;
  116. ix86->regs.edi = new_start;
  117. ix86->regs.ecx = size;
  118. }