Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
Olivier Deprez | 1d2b88d | 2020-03-06 17:17:56 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, Arm Limited. All rights reserved. |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <asm_macros.S> |
| 9 | #include <assert_macros.S> |
Olivier Deprez | 1d2b88d | 2020-03-06 17:17:56 +0100 | [diff] [blame] | 10 | #include <lib/xlat_tables/xlat_tables_defs.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 11 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 12 | .globl smc |
| 13 | |
| 14 | .globl zeromem16 |
| 15 | .globl memcpy16 |
| 16 | |
| 17 | .globl disable_mmu |
| 18 | .globl disable_mmu_icache |
| 19 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 20 | func smc |
| 21 | smc #0 |
| 22 | endfunc 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 | */ |
| 31 | func 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 */ |
| 38 | z_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 */ |
| 45 | z_loop1: |
| 46 | cmp x0, x2 |
| 47 | b.eq z_end |
| 48 | strb wzr, [x0], #1 |
| 49 | b z_loop1 |
| 50 | z_end: |
| 51 | ret |
| 52 | endfunc 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 | */ |
| 63 | func 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 */ |
| 70 | m_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 */ |
| 78 | m_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 |
| 84 | m_end: |
| 85 | ret |
| 86 | endfunc 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 | */ |
| 95 | func disable_mmu |
| 96 | mov x1, #(SCTLR_M_BIT | SCTLR_C_BIT) |
| 97 | do_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 |
| 104 | endfunc disable_mmu |
| 105 | |
| 106 | func disable_mmu_icache |
| 107 | mov x1, #(SCTLR_M_BIT | SCTLR_C_BIT | SCTLR_I_BIT) |
| 108 | b do_disable_mmu |
| 109 | endfunc disable_mmu_icache |
| 110 | |
| 111 | /* Need this label for asm_read/write_sctlr_el1_or_el2 */ |
| 112 | dead: |
| 113 | b dead |
Olivier Deprez | 1d2b88d | 2020-03-06 17:17:56 +0100 | [diff] [blame] | 114 | |
| 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 |
| 133 | func 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 | */ |
| 162 | 1: |
| 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] |
| 172 | 2: |
| 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 | */ |
| 199 | 1: |
Shruti Gupta | fe69a8c | 2023-08-21 11:49:32 +0100 | [diff] [blame] | 200 | /* Skip R_AARCH64_NONE entry with code 0 */ |
| 201 | ldr x3, [x1, #8] |
| 202 | cbz x3, 2f |
| 203 | |
Olivier Deprez | 1d2b88d | 2020-03-06 17:17:56 +0100 | [diff] [blame] | 204 | /* Assert that the relocation type is R_AARCH64_RELATIVE */ |
| 205 | #if ENABLE_ASSERTIONS |
Olivier Deprez | 1d2b88d | 2020-03-06 17:17:56 +0100 | [diff] [blame] | 206 | cmp x3, #0x403 |
| 207 | ASM_ASSERT(eq) |
| 208 | #endif |
| 209 | ldr x3, [x1] /* r_offset */ |
| 210 | add x3, x0, x3 |
| 211 | ldr x4, [x1, #16] /* r_addend */ |
| 212 | |
| 213 | /* Skip adding offset if r_addend is < lower limit */ |
| 214 | cmp x4, x6 |
| 215 | b.lo 2f |
| 216 | /* Skip adding offset if r_addend entry is >= upper limit */ |
| 217 | cmp x4, x7 |
| 218 | b.ge 2f |
| 219 | |
| 220 | add x4, x0, x4 /* Diff(S) + r_addend */ |
| 221 | str x4, [x3] |
| 222 | |
| 223 | 2: add x1, x1, #24 |
| 224 | cmp x1, x2 |
| 225 | b.lo 1b |
| 226 | |
| 227 | ret |
| 228 | endfunc fixup_gdt_reloc |