crt0.S 6.7 KB

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