relocate.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * relocate - common relocation function for ARM U-Boot
  4. *
  5. * Copyright (c) 2013 Albert ARIBAUD <albert.u.boot@aribaud.net>
  6. */
  7. #include <asm-offsets.h>
  8. #include <config.h>
  9. #include <elf.h>
  10. #include <linux/linkage.h>
  11. #ifdef CONFIG_CPU_V7M
  12. #include <asm/armv7m.h>
  13. #endif
  14. /*
  15. * Default/weak exception vectors relocation routine
  16. *
  17. * This routine covers the standard ARM cases: normal (0x00000000),
  18. * high (0xffff0000) and VBAR. SoCs which do not comply with any of
  19. * the standard cases must provide their own, strong, version.
  20. */
  21. .section .text.relocate_vectors,"ax",%progbits
  22. .weak relocate_vectors
  23. ENTRY(relocate_vectors)
  24. #ifdef CONFIG_CPU_V7M
  25. /*
  26. * On ARMv7-M we only have to write the new vector address
  27. * to VTOR register.
  28. */
  29. ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
  30. ldr r1, =V7M_SCB_BASE
  31. str r0, [r1, V7M_SCB_VTOR]
  32. #else
  33. #ifdef CONFIG_HAS_VBAR
  34. /*
  35. * If the ARM processor has the security extensions,
  36. * use VBAR to relocate the exception vectors.
  37. */
  38. ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
  39. mcr p15, 0, r0, c12, c0, 0 /* Set VBAR */
  40. #else
  41. /*
  42. * Copy the relocated exception vectors to the
  43. * correct address
  44. * CP15 c1 V bit gives us the location of the vectors:
  45. * 0x00000000 or 0xFFFF0000.
  46. */
  47. ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
  48. mrc p15, 0, r2, c1, c0, 0 /* V bit (bit[13]) in CP15 c1 */
  49. ands r2, r2, #(1 << 13)
  50. ldreq r1, =0x00000000 /* If V=0 */
  51. ldrne r1, =0xFFFF0000 /* If V=1 */
  52. ldmia r0!, {r2-r8,r10}
  53. stmia r1!, {r2-r8,r10}
  54. ldmia r0!, {r2-r8,r10}
  55. stmia r1!, {r2-r8,r10}
  56. #endif
  57. #endif
  58. bx lr
  59. ENDPROC(relocate_vectors)
  60. /*
  61. * void relocate_code(addr_moni)
  62. *
  63. * This function relocates the monitor code.
  64. *
  65. * NOTE:
  66. * To prevent the code below from containing references with an R_ARM_ABS32
  67. * relocation record type, we never refer to linker-defined symbols directly.
  68. * Instead, we declare literals which contain their relative location with
  69. * respect to relocate_code, and at run time, add relocate_code back to them.
  70. */
  71. ENTRY(relocate_code)
  72. ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */
  73. subs r4, r0, r1 /* r4 <- relocation offset */
  74. beq relocate_done /* skip relocation */
  75. ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */
  76. copy_loop:
  77. ldmia r1!, {r10-r11} /* copy from source address [r1] */
  78. stmia r0!, {r10-r11} /* copy to target address [r0] */
  79. cmp r1, r2 /* until source end address [r2] */
  80. blo copy_loop
  81. /*
  82. * fix .rel.dyn relocations
  83. */
  84. ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */
  85. ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */
  86. fixloop:
  87. ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */
  88. and r1, r1, #0xff
  89. cmp r1, #R_ARM_RELATIVE
  90. bne fixnext
  91. /* relative fix: increase location by offset */
  92. add r0, r0, r4
  93. ldr r1, [r0]
  94. add r1, r1, r4
  95. str r1, [r0]
  96. fixnext:
  97. cmp r2, r3
  98. blo fixloop
  99. relocate_done:
  100. #ifdef __XSCALE__
  101. /*
  102. * On xscale, icache must be invalidated and write buffers drained,
  103. * even with cache disabled - 4.2.7 of xscale core developer's manual
  104. */
  105. mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */
  106. mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
  107. #endif
  108. /* ARMv4- don't know bx lr but the assembler fails to see that */
  109. #ifdef __ARM_ARCH_4__
  110. mov pc, lr
  111. #else
  112. bx lr
  113. #endif
  114. ENDPROC(relocate_code)