blob: b67772152d2740a1cd3e8bddee942e438f5908d7 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Olivier Deprez1d2b88d2020-03-06 17:17:56 +01002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
8#include <asm_macros.S>
9#include <assert_macros.S>
Olivier Deprez1d2b88d2020-03-06 17:17:56 +010010#include <lib/xlat_tables/xlat_tables_defs.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020011
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020012 .globl smc
13
14 .globl zeromem16
15 .globl memcpy16
16
17 .globl disable_mmu
18 .globl disable_mmu_icache
19
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020020func smc
21 smc #0
22endfunc smc
23
24/* -----------------------------------------------------------------------
25 * void zeromem16(void *mem, unsigned int length);
26 *
27 * Initialise a memory region to 0.
28 * The memory address must be 16-byte aligned.
29 * -----------------------------------------------------------------------
30 */
31func zeromem16
32#if ENABLE_ASSERTIONS
33 tst x0, #0xf
34 ASM_ASSERT(eq)
35#endif
36 add x2, x0, x1
37/* zero 16 bytes at a time */
38z_loop16:
39 sub x3, x2, x0
40 cmp x3, #16
41 b.lt z_loop1
42 stp xzr, xzr, [x0], #16
43 b z_loop16
44/* zero byte per byte */
45z_loop1:
46 cmp x0, x2
47 b.eq z_end
48 strb wzr, [x0], #1
49 b z_loop1
50z_end:
51 ret
52endfunc zeromem16
53
54
55/* --------------------------------------------------------------------------
56 * void memcpy16(void *dest, const void *src, unsigned int length)
57 *
58 * Copy length bytes from memory area src to memory area dest.
59 * The memory areas should not overlap.
60 * Destination and source addresses must be 16-byte aligned.
61 * --------------------------------------------------------------------------
62 */
63func memcpy16
64#if ENABLE_ASSERTIONS
65 orr x3, x0, x1
66 tst x3, #0xf
67 ASM_ASSERT(eq)
68#endif
69/* copy 16 bytes at a time */
70m_loop16:
71 cmp x2, #16
72 b.lt m_loop1
73 ldp x3, x4, [x1], #16
74 stp x3, x4, [x0], #16
75 sub x2, x2, #16
76 b m_loop16
77/* copy byte per byte */
78m_loop1:
79 cbz x2, m_end
80 ldrb w3, [x1], #1
81 strb w3, [x0], #1
82 subs x2, x2, #1
83 b.ne m_loop1
84m_end:
85 ret
86endfunc memcpy16
87
88/* ---------------------------------------------------------------------------
89 * Disable the MMU at the current exception level (NS-EL1 or EL2)
90 * This is implemented in assembler to ensure that the data cache is cleaned
91 * and invalidated after the MMU is disabled without any intervening cacheable
92 * data accesses
93 * ---------------------------------------------------------------------------
94 */
95func disable_mmu
96 mov x1, #(SCTLR_M_BIT | SCTLR_C_BIT)
97do_disable_mmu:
98 asm_read_sctlr_el1_or_el2
99 bic x0, x0, x1
100 asm_write_sctlr_el1_or_el2 x1
101 isb /* ensure MMU is off */
102 mov x0, #DCCISW /* DCache clean and invalidate */
103 b dcsw_op_all
104endfunc disable_mmu
105
106func disable_mmu_icache
107 mov x1, #(SCTLR_M_BIT | SCTLR_C_BIT | SCTLR_I_BIT)
108 b do_disable_mmu
109endfunc disable_mmu_icache
110
111/* Need this label for asm_read/write_sctlr_el1_or_el2 */
112dead:
113 b dead
Olivier Deprez1d2b88d2020-03-06 17:17:56 +0100114
115/* ---------------------------------------------------------------------------
116 * Helper to fixup Global Offset table (GOT) and dynamic relocations
117 * (.rela.dyn) at runtime.
118 *
119 * This function is meant to be used when the firmware is compiled with -fpie
120 * and linked with -pie options. We rely on the linker script exporting
121 * appropriate markers for start and end of the section. For GOT, we
122 * expect __GOT_START__ and __GOT_END__. Similarly for .rela.dyn, we expect
123 * __RELA_START__ and __RELA_END__.
124 *
125 * The function takes the limits of the memory to apply fixups to as
126 * arguments (which is usually the limits of the relocable BL image).
127 * x0 - the start of the fixup region
128 * x1 - the limit of the fixup region
129 * These addresses have to be page (4KB aligned).
130 * ---------------------------------------------------------------------------
131 */
132.globl fixup_gdt_reloc
133func fixup_gdt_reloc
134 mov x6, x0
135 mov x7, x1
136
137 /* Test if the limits are 4K aligned */
138#if ENABLE_ASSERTIONS
139 orr x0, x0, x1
140 tst x0, #(PAGE_SIZE - 1)
141 ASM_ASSERT(eq)
142#endif
143 /*
144 * Calculate the offset based on return address in x30.
145 * Assume that this function is called within a page at the start of
146 * fixup region.
147 */
148 and x2, x30, #~(PAGE_SIZE - 1)
149 sub x0, x2, x6 /* Diff(S) = Current Address - Compiled Address */
150
151 adrp x1, __GOT_START__
152 add x1, x1, :lo12:__GOT_START__
153 adrp x2, __GOT_END__
154 add x2, x2, :lo12:__GOT_END__
155
156 /*
157 * GOT is an array of 64_bit addresses which must be fixed up as
158 * new_addr = old_addr + Diff(S).
159 * The new_addr is the address currently the binary is executing from
160 * and old_addr is the address at compile time.
161 */
1621:
163 ldr x3, [x1]
164 /* Skip adding offset if address is < lower limit */
165 cmp x3, x6
166 b.lo 2f
167 /* Skip adding offset if address is >= upper limit */
168 cmp x3, x7
169 b.ge 2f
170 add x3, x3, x0
171 str x3, [x1]
1722:
173 add x1, x1, #8
174 cmp x1, x2
175 b.lo 1b
176
177 /* Starting dynamic relocations. Use adrp/adr to get RELA_START and END */
178 adrp x1, __RELA_START__
179 add x1, x1, :lo12:__RELA_START__
180 adrp x2, __RELA_END__
181 add x2, x2, :lo12:__RELA_END__
182 /*
183 * According to ELF-64 specification, the RELA data structure is as
184 * follows:
185 * typedef struct
186 * {
187 * Elf64_Addr r_offset;
188 * Elf64_Xword r_info;
189 * Elf64_Sxword r_addend;
190 * } Elf64_Rela;
191 *
192 * r_offset is address of reference
193 * r_info is symbol index and type of relocation (in this case
194 * 0x403 which corresponds to R_AARCH64_RELATIVE).
195 * r_addend is constant part of expression.
196 *
197 * Size of Elf64_Rela structure is 24 bytes.
198 */
1991:
200 /* Assert that the relocation type is R_AARCH64_RELATIVE */
201#if ENABLE_ASSERTIONS
202 ldr x3, [x1, #8]
203 cmp x3, #0x403
204 ASM_ASSERT(eq)
205#endif
206 ldr x3, [x1] /* r_offset */
207 add x3, x0, x3
208 ldr x4, [x1, #16] /* r_addend */
209
210 /* Skip adding offset if r_addend is < lower limit */
211 cmp x4, x6
212 b.lo 2f
213 /* Skip adding offset if r_addend entry is >= upper limit */
214 cmp x4, x7
215 b.ge 2f
216
217 add x4, x0, x4 /* Diff(S) + r_addend */
218 str x4, [x3]
219
2202: add x1, x1, #24
221 cmp x1, x2
222 b.lo 1b
223
224 ret
225endfunc fixup_gdt_reloc