1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) Paul Mackerras 1997.
5 * Adapted for 64 bit LE PowerPC by Andrew Tauferner
11 RELACOUNT = 0x6ffffff9
14 /* A procedure descriptor used when booting this as a COFF file.
15 * When making COFF, this comes first in the link and we're
18 .globl _zimage_start_opd
20 .long 0x500000, 0, 0, 0
26 p_start: .8byte _start
27 p_etext: .8byte _etext
28 p_bss_start: .8byte __bss_start
31 p_toc: .8byte __toc_start + 0x8000 - p_base
32 p_dyn: .8byte __dynamic_start - p_base
33 p_rela: .8byte __rela_dyn_start - p_base
35 .weak _platform_stack_top
36 p_pstack: .8byte _platform_stack_top
40 p_bss_start: .long __bss_start
43 .weak _platform_stack_top
44 p_pstack: .long _platform_stack_top
48 /* Clang appears to require the .weak directive to be after the symbol
49 * is defined. See https://bugs.llvm.org/show_bug.cgi?id=38921 */
52 .globl _zimage_start_lib
54 /* Work out the offset between the address we were linked at
55 and the address where we're running. */
57 p_base: mflr r10 /* r10 now points to runtime addr of p_base */
59 /* grab the link address of the dynamic section in r11 */
60 addis r11,r10,(_GLOBAL_OFFSET_TABLE_-p_base)@ha
61 lwz r11,(_GLOBAL_OFFSET_TABLE_-p_base)@l(r11)
63 beq 3f /* if not linked -pie */
64 /* get the runtime address of the dynamic section in r12 */
66 addis r12,r10,(__dynamic_start-p_base)@ha
67 addi r12,r12,(__dynamic_start-p_base)@l
68 subf r11,r11,r12 /* runtime - linktime offset */
70 /* The dynamic section contains a series of tagged entries.
71 * We need the RELA and RELACOUNT entries. */
74 9: lwz r8,0(r12) /* get tag */
76 beq 10f /* end of list */
79 lwz r9,4(r12) /* get RELA pointer in r9 */
81 11: addis r8,r8,(-RELACOUNT)@ha
84 lwz r0,4(r12) /* get RELACOUNT value in r0 */
88 /* The relocation section contains a list of relocations.
89 * We now do the R_PPC_RELATIVE ones, which point to words
90 * which need to be initialized with addend + offset.
91 * The R_PPC_RELATIVE ones come first and there are RELACOUNT
93 10: /* skip relocation if we don't have both */
99 add r9,r9,r11 /* Relocate RELA pointer */
101 2: lbz r0,4+3(r9) /* ELF32_R_INFO(reloc->r_info) */
102 cmpwi r0,22 /* R_PPC_RELATIVE */
104 lwz r12,0(r9) /* reloc->r_offset */
105 lwz r0,8(r9) /* reloc->r_addend */
111 /* Do a cache flush for our text, in case the loader didn't */
112 3: lwz r9,p_start-p_base(r10) /* note: these are relocated now */
113 lwz r8,p_etext-p_base(r10)
123 lwz r9,p_bss_start-p_base(r10)
124 lwz r8,p_end-p_base(r10)
131 /* Possibly set up a custom stack */
132 lwz r8,p_pstack-p_base(r10)
137 stwu r0,-16(r1) /* establish a stack frame */
139 #else /* __powerpc64__ */
140 /* Save the prom pointer at p_prom. */
141 std r5,(p_prom-p_base)(r10)
143 /* Set r2 to the TOC. */
144 ld r2,(p_toc-p_base)(r10)
147 /* Grab the link address of the dynamic section in r11. */
150 beq 3f /* if not linked -pie then no dynamic section */
152 ld r11,(p_dyn-p_base)(r10)
154 ld r9,(p_rela-p_base)(r10)
159 9: ld r12,0(r11) /* get tag */
161 beq 12f /* end of list */
164 ld r13,8(r11) /* get RELA pointer in r13 */
166 10: addis r12,r12,(-RELACOUNT)@ha
167 cmpdi r12,RELACOUNT@l
169 ld r8,8(r11) /* get RELACOUNT value in r8 */
173 cmpdi r13,0 /* check we have both RELA and RELACOUNT */
178 /* Calcuate the runtime offset. */
181 /* Run through the list of relocations and process the
182 * R_PPC64_RELATIVE ones. */
184 13: ld r0,8(r9) /* ELF64_R_TYPE(reloc->r_info) */
185 cmpdi r0,22 /* R_PPC64_RELATIVE */
187 ld r12,0(r9) /* reloc->r_offset */
188 ld r0,16(r9) /* reloc->r_addend */
194 /* Do a cache flush for our text, in case the loader didn't */
195 3: ld r9,p_start-p_base(r10) /* note: these are relocated now */
196 ld r8,p_etext-p_base(r10)
206 ld r9,p_bss_start-p_base(r10)
207 ld r8,p_end-p_base(r10)
214 /* Possibly set up a custom stack */
215 ld r8,p_pstack-p_base(r10)
220 stdu r0,-112(r1) /* establish a stack frame */
222 #endif /* __powerpc64__ */
223 /* Call platform_init() */
231 #define PROM_FRAME_SIZE 512
232 #define SAVE_GPR(n, base) std n,8*(n)(base)
233 #define REST_GPR(n, base) ld n,8*(n)(base)
234 #define SAVE_2GPRS(n, base) SAVE_GPR(n, base); SAVE_GPR(n+1, base)
235 #define SAVE_4GPRS(n, base) SAVE_2GPRS(n, base); SAVE_2GPRS(n+2, base)
236 #define SAVE_8GPRS(n, base) SAVE_4GPRS(n, base); SAVE_4GPRS(n+4, base)
237 #define SAVE_10GPRS(n, base) SAVE_8GPRS(n, base); SAVE_2GPRS(n+8, base)
238 #define REST_2GPRS(n, base) REST_GPR(n, base); REST_GPR(n+1, base)
239 #define REST_4GPRS(n, base) REST_2GPRS(n, base); REST_2GPRS(n+2, base)
240 #define REST_8GPRS(n, base) REST_4GPRS(n, base); REST_4GPRS(n+4, base)
241 #define REST_10GPRS(n, base) REST_8GPRS(n, base); REST_2GPRS(n+8, base)
243 /* prom handles the jump into and return from firmware. The prom args pointer
249 stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
260 /* remove MSR_LE from msr but keep MSR_SF */
265 /* Load FW address, set LR to label 1, and jump to FW */
271 ld r10,(p_prom-0b)(r10)
276 1: /* Return from OF */
279 /* Restore registers and return. */
282 /* Restore the MSR (back to 64 bits) */
287 /* Restore other registers */
295 addi r1,r1,PROM_FRAME_SIZE