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
49 .globl _zimage_start_lib
51 /* Work out the offset between the address we were linked at
52 and the address where we're running. */
54 p_base: mflr r10 /* r10 now points to runtime addr of p_base */
56 /* grab the link address of the dynamic section in r11 */
57 addis r11,r10,(_GLOBAL_OFFSET_TABLE_-p_base)@ha
58 lwz r11,(_GLOBAL_OFFSET_TABLE_-p_base)@l(r11)
60 beq 3f /* if not linked -pie */
61 /* get the runtime address of the dynamic section in r12 */
63 addis r12,r10,(__dynamic_start-p_base)@ha
64 addi r12,r12,(__dynamic_start-p_base)@l
65 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. */
71 9: lwz r8,0(r12) /* get tag */
73 beq 10f /* end of list */
76 lwz r9,4(r12) /* get RELA pointer in r9 */
78 11: addis r8,r8,(-RELACOUNT)@ha
81 lwz r0,4(r12) /* get RELACOUNT value in r0 */
85 /* The relocation section contains a list of relocations.
86 * We now do the R_PPC_RELATIVE ones, which point to words
87 * which need to be initialized with addend + offset.
88 * The R_PPC_RELATIVE ones come first and there are RELACOUNT
90 10: /* skip relocation if we don't have both */
96 add r9,r9,r11 /* Relocate RELA pointer */
98 2: lbz r0,4+3(r9) /* ELF32_R_INFO(reloc->r_info) */
99 cmpwi r0,22 /* R_PPC_RELATIVE */
101 lwz r12,0(r9) /* reloc->r_offset */
102 lwz r0,8(r9) /* reloc->r_addend */
108 /* Do a cache flush for our text, in case the loader didn't */
109 3: lwz r9,p_start-p_base(r10) /* note: these are relocated now */
110 lwz r8,p_etext-p_base(r10)
120 lwz r9,p_bss_start-p_base(r10)
121 lwz r8,p_end-p_base(r10)
128 /* Possibly set up a custom stack */
129 lwz r8,p_pstack-p_base(r10)
134 stwu r0,-16(r1) /* establish a stack frame */
136 #else /* __powerpc64__ */
137 /* Save the prom pointer at p_prom. */
138 std r5,(p_prom-p_base)(r10)
140 /* Set r2 to the TOC. */
141 ld r2,(p_toc-p_base)(r10)
144 /* Grab the link address of the dynamic section in r11. */
147 beq 3f /* if not linked -pie then no dynamic section */
149 ld r11,(p_dyn-p_base)(r10)
151 ld r9,(p_rela-p_base)(r10)
156 9: ld r12,0(r11) /* get tag */
158 beq 12f /* end of list */
161 ld r13,8(r11) /* get RELA pointer in r13 */
163 10: addis r12,r12,(-RELACOUNT)@ha
164 cmpdi r12,RELACOUNT@l
166 ld r8,8(r11) /* get RELACOUNT value in r8 */
170 cmpdi r13,0 /* check we have both RELA and RELACOUNT */
175 /* Calcuate the runtime offset. */
178 /* Run through the list of relocations and process the
179 * R_PPC64_RELATIVE ones. */
181 13: ld r0,8(r9) /* ELF64_R_TYPE(reloc->r_info) */
182 cmpdi r0,22 /* R_PPC64_RELATIVE */
184 ld r12,0(r9) /* reloc->r_offset */
185 ld r0,16(r9) /* reloc->r_addend */
191 /* Do a cache flush for our text, in case the loader didn't */
192 3: ld r9,p_start-p_base(r10) /* note: these are relocated now */
193 ld r8,p_etext-p_base(r10)
203 ld r9,p_bss_start-p_base(r10)
204 ld r8,p_end-p_base(r10)
211 /* Possibly set up a custom stack */
212 ld r8,p_pstack-p_base(r10)
217 stdu r0,-112(r1) /* establish a stack frame */
219 #endif /* __powerpc64__ */
220 /* Call platform_init() */
228 #define PROM_FRAME_SIZE 512
230 .macro OP_REGS op, width, start, end, base, offset
232 .rept (\end - \start + 1)
233 \op .Lreg,\offset+\width*.Lreg(\base)
238 #define SAVE_GPRS(start, end, base) OP_REGS std, 8, start, end, base, 0
239 #define REST_GPRS(start, end, base) OP_REGS ld, 8, start, end, base, 0
240 #define SAVE_GPR(n, base) SAVE_GPRS(n, n, base)
241 #define REST_GPR(n, base) REST_GPRS(n, n, 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 */
252 SAVE_GPRS(13, 31, r1)
258 /* remove MSR_LE from msr but keep MSR_SF */
263 /* Load FW address, set LR to label 1, and jump to FW */
269 ld r10,(p_prom-0b)(r10)
274 1: /* Return from OF */
277 /* Restore registers and return. */
280 /* Restore the MSR (back to 64 bits) */
285 /* Restore other registers */
287 REST_GPRS(13, 31, r1)
291 addi r1,r1,PROM_FRAME_SIZE