blob: 1d83966f5ef640a95be3c429cb028b75ec2b8e87 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) Paul Mackerras 1997.
4 *
Cédric Le Goaterf16e9682014-04-24 09:23:37 +02005 * Adapted for 64 bit LE PowerPC by Andrew Tauferner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Olaf Heringdecd3002005-08-08 13:24:38 +10008#include "ppc_asm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Cédric Le Goaterf16e9682014-04-24 09:23:37 +020010RELA = 7
11RELACOUNT = 0x6ffffff9
12
Paul Mackerras55645972018-11-27 09:01:54 +110013 .data
Michael Ellerman6975a782011-04-12 20:38:55 +000014 /* A procedure descriptor used when booting this as a COFF file.
15 * When making COFF, this comes first in the link and we're
16 * linked at 0x500000.
17 */
Paul Mackerrasf40e5242007-06-07 22:21:31 +100018 .globl _zimage_start_opd
Paul Mackerras66a45dd2006-01-14 15:04:06 +110019_zimage_start_opd:
Michael Ellerman6975a782011-04-12 20:38:55 +000020 .long 0x500000, 0, 0, 0
Paul Mackerras55645972018-11-27 09:01:54 +110021 .text
22 b _zimage_start
Michael Ellerman6975a782011-04-12 20:38:55 +000023
Cédric Le Goaterf16e9682014-04-24 09:23:37 +020024#ifdef __powerpc64__
25.balign 8
Tobin C. Hardingeb039162017-03-09 16:42:12 +110026p_start: .8byte _start
27p_etext: .8byte _etext
28p_bss_start: .8byte __bss_start
29p_end: .8byte _end
Cédric Le Goaterf16e9682014-04-24 09:23:37 +020030
Tobin C. Hardingeb039162017-03-09 16:42:12 +110031p_toc: .8byte __toc_start + 0x8000 - p_base
32p_dyn: .8byte __dynamic_start - p_base
33p_rela: .8byte __rela_dyn_start - p_base
34p_prom: .8byte 0
Cédric Le Goaterf16e9682014-04-24 09:23:37 +020035 .weak _platform_stack_top
Tobin C. Hardingeb039162017-03-09 16:42:12 +110036p_pstack: .8byte _platform_stack_top
Cédric Le Goaterf16e9682014-04-24 09:23:37 +020037#else
Michael Ellerman6975a782011-04-12 20:38:55 +000038p_start: .long _start
39p_etext: .long _etext
40p_bss_start: .long __bss_start
41p_end: .long _end
42
43 .weak _platform_stack_top
44p_pstack: .long _platform_stack_top
Cédric Le Goaterf16e9682014-04-24 09:23:37 +020045#endif
Paul Mackerras66a45dd2006-01-14 15:04:06 +110046
Joel Stanleyee9d21b2018-09-14 13:36:47 +093047 .weak _zimage_start
Olaf Hering67a1b682005-10-28 17:46:49 -070048_zimage_start:
Milton Miller160cc3e2007-03-21 09:02:53 -060049 .globl _zimage_start_lib
50_zimage_start_lib:
Paul Mackerras66a45dd2006-01-14 15:04:06 +110051 /* Work out the offset between the address we were linked at
52 and the address where we're running. */
Michael Ellerman6975a782011-04-12 20:38:55 +000053 bl .+4
54p_base: mflr r10 /* r10 now points to runtime addr of p_base */
Cédric Le Goaterf16e9682014-04-24 09:23:37 +020055#ifndef __powerpc64__
Michael Ellerman6975a782011-04-12 20:38:55 +000056 /* 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)
59 cmpwi r11,0
60 beq 3f /* if not linked -pie */
61 /* get the runtime address of the dynamic section in r12 */
62 .weak __dynamic_start
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 */
Olaf Heringa4497232005-10-28 17:46:48 -070066
Michael Ellerman6975a782011-04-12 20:38:55 +000067 /* The dynamic section contains a series of tagged entries.
68 * We need the RELA and RELACOUNT entries. */
Michael Ellerman6975a782011-04-12 20:38:55 +000069 li r9,0
70 li r0,0
719: 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
7811: addis r8,r8,(-RELACOUNT)@ha
79 cmpwi r8,RELACOUNT@l
80 bne 12f
81 lwz r0,4(r12) /* get RELACOUNT value in r0 */
8212: addi r12,r12,8
83 b 9b
84
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
89 * of them. */
9010: /* skip relocation if we don't have both */
91 cmpwi r0,0
Olaf Hering68643cf2005-11-17 22:09:02 +010092 beq 3f
Michael Ellerman6975a782011-04-12 20:38:55 +000093 cmpwi r9,0
94 beq 3f
95
96 add r9,r9,r11 /* Relocate RELA pointer */
97 mtctr r0
982: lbz r0,4+3(r9) /* ELF32_R_INFO(reloc->r_info) */
99 cmpwi r0,22 /* R_PPC_RELATIVE */
100 bne 3f
101 lwz r12,0(r9) /* reloc->r_offset */
102 lwz r0,8(r9) /* reloc->r_addend */
103 add r0,r0,r11
104 stwx r0,r11,r12
105 addi r9,r9,12
Olaf Hering68643cf2005-11-17 22:09:02 +0100106 bdnz 2b
Olaf Heringa4497232005-10-28 17:46:48 -0700107
David Gibsoncd197ff2007-03-05 14:24:52 +1100108 /* Do a cache flush for our text, in case the loader didn't */
Michael Ellerman6975a782011-04-12 20:38:55 +00001093: lwz r9,p_start-p_base(r10) /* note: these are relocated now */
110 lwz r8,p_etext-p_base(r10)
Olaf Hering68643cf2005-11-17 22:09:02 +01001114: dcbf r0,r9
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 icbi r0,r9
113 addi r9,r9,0x20
Olaf Heringeacb1962006-03-04 13:15:40 +0100114 cmplw cr0,r9,r8
Olaf Hering68643cf2005-11-17 22:09:02 +0100115 blt 4b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 sync
117 isync
118
David Gibsoncd197ff2007-03-05 14:24:52 +1100119 /* Clear the BSS */
Michael Ellerman6975a782011-04-12 20:38:55 +0000120 lwz r9,p_bss_start-p_base(r10)
121 lwz r8,p_end-p_base(r10)
122 li r0,0
1235: stw r0,0(r9)
David Gibsoncd197ff2007-03-05 14:24:52 +1100124 addi r9,r9,4
125 cmplw cr0,r9,r8
126 blt 5b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
David Gibsoncd197ff2007-03-05 14:24:52 +1100128 /* Possibly set up a custom stack */
Michael Ellerman6975a782011-04-12 20:38:55 +0000129 lwz r8,p_pstack-p_base(r10)
David Gibsoncd197ff2007-03-05 14:24:52 +1100130 cmpwi r8,0
131 beq 6f
132 lwz r1,0(r8)
133 li r0,0
134 stwu r0,-16(r1) /* establish a stack frame */
1356:
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200136#else /* __powerpc64__ */
137 /* Save the prom pointer at p_prom. */
138 std r5,(p_prom-p_base)(r10)
David Gibsoncd197ff2007-03-05 14:24:52 +1100139
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200140 /* Set r2 to the TOC. */
141 ld r2,(p_toc-p_base)(r10)
142 add r2,r2,r10
143
144 /* Grab the link address of the dynamic section in r11. */
145 ld r11,-32768(r2)
146 cmpwi r11,0
147 beq 3f /* if not linked -pie then no dynamic section */
148
149 ld r11,(p_dyn-p_base)(r10)
150 add r11,r11,r10
151 ld r9,(p_rela-p_base)(r10)
152 add r9,r9,r10
153
Jeremy Kerr7f664cf2015-02-11 12:55:44 +0800154 li r13,0
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200155 li r8,0
Jeremy Kerr7f664cf2015-02-11 12:55:44 +08001569: ld r12,0(r11) /* get tag */
157 cmpdi r12,0
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200158 beq 12f /* end of list */
Jeremy Kerr7f664cf2015-02-11 12:55:44 +0800159 cmpdi r12,RELA
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200160 bne 10f
Jeremy Kerr7f664cf2015-02-11 12:55:44 +0800161 ld r13,8(r11) /* get RELA pointer in r13 */
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200162 b 11f
Jeremy Kerr7f664cf2015-02-11 12:55:44 +080016310: addis r12,r12,(-RELACOUNT)@ha
164 cmpdi r12,RELACOUNT@l
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200165 bne 11f
166 ld r8,8(r11) /* get RELACOUNT value in r8 */
16711: addi r11,r11,16
168 b 9b
16912:
Jeremy Kerr7f664cf2015-02-11 12:55:44 +0800170 cmpdi r13,0 /* check we have both RELA and RELACOUNT */
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200171 cmpdi cr1,r8,0
172 beq 3f
173 beq cr1,3f
174
175 /* Calcuate the runtime offset. */
Jeremy Kerr7f664cf2015-02-11 12:55:44 +0800176 subf r13,r13,r9
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200177
178 /* Run through the list of relocations and process the
179 * R_PPC64_RELATIVE ones. */
180 mtctr r8
18113: ld r0,8(r9) /* ELF64_R_TYPE(reloc->r_info) */
182 cmpdi r0,22 /* R_PPC64_RELATIVE */
183 bne 3f
Jeremy Kerr7f664cf2015-02-11 12:55:44 +0800184 ld r12,0(r9) /* reloc->r_offset */
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200185 ld r0,16(r9) /* reloc->r_addend */
Jeremy Kerr7f664cf2015-02-11 12:55:44 +0800186 add r0,r0,r13
187 stdx r0,r13,r12
Cédric Le Goaterf16e9682014-04-24 09:23:37 +0200188 addi r9,r9,24
189 bdnz 13b
190
191 /* Do a cache flush for our text, in case the loader didn't */
1923: ld r9,p_start-p_base(r10) /* note: these are relocated now */
193 ld r8,p_etext-p_base(r10)
1944: dcbf r0,r9
195 icbi r0,r9
196 addi r9,r9,0x20
197 cmpld cr0,r9,r8
198 blt 4b
199 sync
200 isync
201
202 /* Clear the BSS */
203 ld r9,p_bss_start-p_base(r10)
204 ld r8,p_end-p_base(r10)
205 li r0,0
2065: std r0,0(r9)
207 addi r9,r9,8
208 cmpld cr0,r9,r8
209 blt 5b
210
211 /* Possibly set up a custom stack */
212 ld r8,p_pstack-p_base(r10)
213 cmpdi r8,0
214 beq 6f
215 ld r1,0(r8)
216 li r0,0
Jeremy Kerr8c06f0d2015-02-11 12:55:44 +0800217 stdu r0,-112(r1) /* establish a stack frame */
Cédric Le Goaterf16e9682014-04-24 09:23:37 +02002186:
219#endif /* __powerpc64__ */
David Gibsoncd197ff2007-03-05 14:24:52 +1100220 /* Call platform_init() */
221 bl platform_init
222
223 /* Call start */
David Gibsoncd197ff2007-03-05 14:24:52 +1100224 b start
Cédric Le Goater93d39212014-04-24 09:23:36 +0200225
226#ifdef __powerpc64__
227
228#define PROM_FRAME_SIZE 512
229#define SAVE_GPR(n, base) std n,8*(n)(base)
230#define REST_GPR(n, base) ld n,8*(n)(base)
231#define SAVE_2GPRS(n, base) SAVE_GPR(n, base); SAVE_GPR(n+1, base)
232#define SAVE_4GPRS(n, base) SAVE_2GPRS(n, base); SAVE_2GPRS(n+2, base)
233#define SAVE_8GPRS(n, base) SAVE_4GPRS(n, base); SAVE_4GPRS(n+4, base)
234#define SAVE_10GPRS(n, base) SAVE_8GPRS(n, base); SAVE_2GPRS(n+8, base)
235#define REST_2GPRS(n, base) REST_GPR(n, base); REST_GPR(n+1, base)
236#define REST_4GPRS(n, base) REST_2GPRS(n, base); REST_2GPRS(n+2, base)
237#define REST_8GPRS(n, base) REST_4GPRS(n, base); REST_4GPRS(n+4, base)
238#define REST_10GPRS(n, base) REST_8GPRS(n, base); REST_2GPRS(n+8, base)
239
240/* prom handles the jump into and return from firmware. The prom args pointer
241 is loaded in r3. */
242.globl prom
243prom:
244 mflr r0
245 std r0,16(r1)
246 stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
247
248 SAVE_GPR(2, r1)
249 SAVE_GPR(13, r1)
250 SAVE_8GPRS(14, r1)
251 SAVE_10GPRS(22, r1)
252 mfcr r10
253 std r10,8*32(r1)
254 mfmsr r10
255 std r10,8*33(r1)
256
257 /* remove MSR_LE from msr but keep MSR_SF */
258 mfmsr r10
259 rldicr r10,r10,0,62
260 mtsrr1 r10
261
262 /* Load FW address, set LR to label 1, and jump to FW */
263 bl 0f
2640: mflr r10
265 addi r11,r10,(1f-0b)
266 mtlr r11
267
268 ld r10,(p_prom-0b)(r10)
269 mtsrr0 r10
270
271 rfid
272
2731: /* Return from OF */
Cédric Le Goater147c0512014-04-24 09:23:39 +0200274 FIXUP_ENDIAN
Cédric Le Goater93d39212014-04-24 09:23:36 +0200275
276 /* Restore registers and return. */
277 rldicl r1,r1,0,32
278
279 /* Restore the MSR (back to 64 bits) */
280 ld r10,8*(33)(r1)
281 mtmsr r10
282 isync
283
284 /* Restore other registers */
285 REST_GPR(2, r1)
286 REST_GPR(13, r1)
287 REST_8GPRS(14, r1)
288 REST_10GPRS(22, r1)
289 ld r10,8*32(r1)
290 mtcr r10
291
292 addi r1,r1,PROM_FRAME_SIZE
293 ld r0,16(r1)
294 mtlr r0
295 blr
296#endif