crt0.S 7.1 KB

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