crt0.S 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) Paul Mackerras 1997.
  4. *
  5. * Adapted for 64 bit LE PowerPC by Andrew Tauferner
  6. */
  7. #include "ppc_asm.h"
  8. RELA = 7
  9. RELASZ = 8
  10. RELAENT = 9
  11. .data
  12. /* A procedure descriptor used when booting this as a COFF file.
  13. * When making COFF, this comes first in the link and we're
  14. * linked at 0x500000.
  15. */
  16. .globl _zimage_start_opd
  17. _zimage_start_opd:
  18. .long 0x500000, 0, 0, 0
  19. .text
  20. b _zimage_start
  21. #ifdef __powerpc64__
  22. .balign 8
  23. p_start: .8byte _start
  24. p_etext: .8byte _etext
  25. p_bss_start: .8byte __bss_start
  26. p_end: .8byte _end
  27. p_toc: .8byte .TOC. - p_base
  28. p_dyn: .8byte __dynamic_start - p_base
  29. p_rela: .8byte __rela_dyn_start - p_base
  30. p_prom: .8byte 0
  31. .weak _platform_stack_top
  32. p_pstack: .8byte _platform_stack_top
  33. #else
  34. p_start: .long _start
  35. p_etext: .long _etext
  36. p_bss_start: .long __bss_start
  37. p_end: .long _end
  38. .weak _platform_stack_top
  39. p_pstack: .long _platform_stack_top
  40. #endif
  41. .weak _zimage_start
  42. _zimage_start:
  43. .globl _zimage_start_lib
  44. _zimage_start_lib:
  45. /* Work out the offset between the address we were linked at
  46. and the address where we're running. */
  47. bl .+4
  48. p_base: mflr r10 /* r10 now points to runtime addr of p_base */
  49. #ifndef __powerpc64__
  50. /* grab the link address of the dynamic section in r11 */
  51. addis r11,r10,(_GLOBAL_OFFSET_TABLE_-p_base)@ha
  52. lwz r11,(_GLOBAL_OFFSET_TABLE_-p_base)@l(r11)
  53. cmpwi r11,0
  54. beq 3f /* if not linked -pie */
  55. /* get the runtime address of the dynamic section in r12 */
  56. .weak __dynamic_start
  57. addis r12,r10,(__dynamic_start-p_base)@ha
  58. addi r12,r12,(__dynamic_start-p_base)@l
  59. subf r11,r11,r12 /* runtime - linktime offset */
  60. /* The dynamic section contains a series of tagged entries.
  61. * We need the RELA and RELACOUNT entries. */
  62. li r9,0
  63. li r0,0
  64. 9: lwz r8,0(r12) /* get tag */
  65. cmpwi r8,0
  66. beq 10f /* end of list */
  67. cmpwi r8,RELA
  68. bne 11f
  69. lwz r9,4(r12) /* get RELA pointer in r9 */
  70. b 12f
  71. 11: cmpwi r8,RELASZ
  72. bne .Lcheck_for_relaent
  73. lwz r0,4(r12) /* get RELASZ value in r0 */
  74. b 12f
  75. .Lcheck_for_relaent:
  76. cmpwi r8,RELAENT
  77. bne 12f
  78. lwz r14,4(r12) /* get RELAENT value in r14 */
  79. 12: addi r12,r12,8
  80. b 9b
  81. /* The relocation section contains a list of relocations.
  82. * We now do the R_PPC_RELATIVE ones, which point to words
  83. * which need to be initialized with addend + offset */
  84. 10: /* skip relocation if we don't have both */
  85. cmpwi r0,0
  86. beq 3f
  87. cmpwi r9,0
  88. beq 3f
  89. cmpwi r14,0
  90. beq 3f
  91. add r9,r9,r11 /* Relocate RELA pointer */
  92. divwu r0,r0,r14 /* RELASZ / RELAENT */
  93. mtctr r0
  94. 2: lbz r0,4+3(r9) /* ELF32_R_INFO(reloc->r_info) */
  95. cmpwi r0,22 /* R_PPC_RELATIVE */
  96. bne .Lnext
  97. lwz r12,0(r9) /* reloc->r_offset */
  98. lwz r0,8(r9) /* reloc->r_addend */
  99. add r0,r0,r11
  100. stwx r0,r11,r12
  101. .Lnext: add r9,r9,r14
  102. bdnz 2b
  103. /* Do a cache flush for our text, in case the loader didn't */
  104. 3: lwz r9,p_start-p_base(r10) /* note: these are relocated now */
  105. lwz r8,p_etext-p_base(r10)
  106. 4: dcbf r0,r9
  107. icbi r0,r9
  108. addi r9,r9,0x20
  109. cmplw cr0,r9,r8
  110. blt 4b
  111. sync
  112. isync
  113. /* Clear the BSS */
  114. lwz r9,p_bss_start-p_base(r10)
  115. lwz r8,p_end-p_base(r10)
  116. li r0,0
  117. 5: stw r0,0(r9)
  118. addi r9,r9,4
  119. cmplw cr0,r9,r8
  120. blt 5b
  121. /* Possibly set up a custom stack */
  122. lwz r8,p_pstack-p_base(r10)
  123. cmpwi r8,0
  124. beq 6f
  125. lwz r1,0(r8)
  126. li r0,0
  127. stwu r0,-16(r1) /* establish a stack frame */
  128. 6:
  129. #else /* __powerpc64__ */
  130. /* Save the prom pointer at p_prom. */
  131. std r5,(p_prom-p_base)(r10)
  132. /* Set r2 to the TOC. */
  133. ld r2,(p_toc-p_base)(r10)
  134. add r2,r2,r10
  135. /* Grab the link address of the dynamic section in r11. */
  136. ld r11,-32768(r2)
  137. cmpwi r11,0
  138. beq 3f /* if not linked -pie then no dynamic section */
  139. ld r11,(p_dyn-p_base)(r10)
  140. add r11,r11,r10
  141. ld r9,(p_rela-p_base)(r10)
  142. add r9,r9,r10
  143. li r13,0
  144. li r8,0
  145. 9: ld r12,0(r11) /* get tag */
  146. cmpdi r12,0
  147. beq 12f /* end of list */
  148. cmpdi r12,RELA
  149. bne 10f
  150. ld r13,8(r11) /* get RELA pointer in r13 */
  151. b 11f
  152. 10: cmpwi r12,RELASZ
  153. bne .Lcheck_for_relaent
  154. lwz r8,8(r11) /* get RELASZ pointer in r8 */
  155. b 11f
  156. .Lcheck_for_relaent:
  157. cmpwi r12,RELAENT
  158. bne 11f
  159. lwz r14,8(r11) /* get RELAENT pointer in r14 */
  160. 11: addi r11,r11,16
  161. b 9b
  162. 12:
  163. cmpdi r13,0 /* check we have both RELA, RELASZ, RELAENT*/
  164. cmpdi cr1,r8,0
  165. beq 3f
  166. beq cr1,3f
  167. cmpdi r14,0
  168. beq 3f
  169. /* Calcuate the runtime offset. */
  170. subf r13,r13,r9
  171. /* Run through the list of relocations and process the
  172. * R_PPC64_RELATIVE ones. */
  173. divdu r8,r8,r14 /* RELASZ / RELAENT */
  174. mtctr r8
  175. 13: ld r0,8(r9) /* ELF64_R_TYPE(reloc->r_info) */
  176. cmpdi r0,22 /* R_PPC64_RELATIVE */
  177. bne .Lnext
  178. ld r12,0(r9) /* reloc->r_offset */
  179. ld r0,16(r9) /* reloc->r_addend */
  180. add r0,r0,r13
  181. stdx r0,r13,r12
  182. .Lnext: add r9,r9,r14
  183. bdnz 13b
  184. /* Do a cache flush for our text, in case the loader didn't */
  185. 3: ld r9,p_start-p_base(r10) /* note: these are relocated now */
  186. ld r8,p_etext-p_base(r10)
  187. 4: dcbf r0,r9
  188. icbi r0,r9
  189. addi r9,r9,0x20
  190. cmpld cr0,r9,r8
  191. blt 4b
  192. sync
  193. isync
  194. /* Clear the BSS */
  195. ld r9,p_bss_start-p_base(r10)
  196. ld r8,p_end-p_base(r10)
  197. li r0,0
  198. 5: std r0,0(r9)
  199. addi r9,r9,8
  200. cmpld cr0,r9,r8
  201. blt 5b
  202. /* Possibly set up a custom stack */
  203. ld r8,p_pstack-p_base(r10)
  204. cmpdi r8,0
  205. beq 6f
  206. ld r1,0(r8)
  207. li r0,0
  208. stdu r0,-112(r1) /* establish a stack frame */
  209. 6:
  210. #endif /* __powerpc64__ */
  211. /* Call platform_init() */
  212. bl platform_init
  213. /* Call start */
  214. b start
  215. #ifdef __powerpc64__
  216. #define PROM_FRAME_SIZE 512
  217. .macro OP_REGS op, width, start, end, base, offset
  218. .Lreg=\start
  219. .rept (\end - \start + 1)
  220. \op .Lreg,\offset+\width*.Lreg(\base)
  221. .Lreg=.Lreg+1
  222. .endr
  223. .endm
  224. #define SAVE_GPRS(start, end, base) OP_REGS std, 8, start, end, base, 0
  225. #define REST_GPRS(start, end, base) OP_REGS ld, 8, start, end, base, 0
  226. #define SAVE_GPR(n, base) SAVE_GPRS(n, n, base)
  227. #define REST_GPR(n, base) REST_GPRS(n, n, base)
  228. /* prom handles the jump into and return from firmware. The prom args pointer
  229. is loaded in r3. */
  230. .globl prom
  231. prom:
  232. mflr r0
  233. std r0,16(r1)
  234. stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
  235. SAVE_GPR(2, r1)
  236. SAVE_GPRS(13, 31, r1)
  237. mfcr r10
  238. std r10,8*32(r1)
  239. mfmsr r10
  240. std r10,8*33(r1)
  241. /* remove MSR_LE from msr but keep MSR_SF */
  242. mfmsr r10
  243. rldicr r10,r10,0,62
  244. mtsrr1 r10
  245. /* Load FW address, set LR to label 1, and jump to FW */
  246. bl 0f
  247. 0: mflr r10
  248. addi r11,r10,(1f-0b)
  249. mtlr r11
  250. ld r10,(p_prom-0b)(r10)
  251. mtsrr0 r10
  252. rfid
  253. 1: /* Return from OF */
  254. FIXUP_ENDIAN
  255. /* Restore registers and return. */
  256. rldicl r1,r1,0,32
  257. /* Restore the MSR (back to 64 bits) */
  258. ld r10,8*(33)(r1)
  259. mtmsr r10
  260. isync
  261. /* Restore other registers */
  262. REST_GPR(2, r1)
  263. REST_GPRS(13, 31, r1)
  264. ld r10,8*32(r1)
  265. mtcr r10
  266. addi r1,r1,PROM_FRAME_SIZE
  267. ld r0,16(r1)
  268. mtlr r0
  269. blr
  270. #endif