Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Pierre-Clément Tosi | e328f4a | 2022-10-27 11:43:10 +0100 | [diff] [blame] | 17 | #include <common.h> |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 18 | |
| 19 | .set .L_MAIR_DEV_nGnRE, 0x04 |
| 20 | .set .L_MAIR_MEM_WBWA, 0xff |
| 21 | .set .Lmairval, .L_MAIR_DEV_nGnRE | (.L_MAIR_MEM_WBWA << 8) |
| 22 | |
| 23 | /* 4 KiB granule size for TTBR0_EL1. */ |
| 24 | .set .L_TCR_TG0_4KB, 0x0 << 14 |
| 25 | /* 4 KiB granule size for TTBR1_EL1. */ |
| 26 | .set .L_TCR_TG1_4KB, 0x2 << 30 |
| 27 | /* Disable translation table walk for TTBR1_EL1, generating a translation fault instead. */ |
| 28 | .set .L_TCR_EPD1, 0x1 << 23 |
| 29 | /* Translation table walks for TTBR0_EL1 are inner sharable. */ |
| 30 | .set .L_TCR_SH_INNER, 0x3 << 12 |
| 31 | /* |
| 32 | * Translation table walks for TTBR0_EL1 are outer write-back read-allocate write-allocate |
| 33 | * cacheable. |
| 34 | */ |
| 35 | .set .L_TCR_RGN_OWB, 0x1 << 10 |
| 36 | /* |
| 37 | * Translation table walks for TTBR0_EL1 are inner write-back read-allocate write-allocate |
| 38 | * cacheable. |
| 39 | */ |
| 40 | .set .L_TCR_RGN_IWB, 0x1 << 8 |
| 41 | /* Size offset for TTBR0_EL1 is 2**39 bytes (512 GiB). */ |
| 42 | .set .L_TCR_T0SZ_512, 64 - 39 |
| 43 | .set .Ltcrval, .L_TCR_TG0_4KB | .L_TCR_TG1_4KB | .L_TCR_EPD1 | .L_TCR_RGN_OWB |
| 44 | .set .Ltcrval, .Ltcrval | .L_TCR_RGN_IWB | .L_TCR_SH_INNER | .L_TCR_T0SZ_512 |
| 45 | |
| 46 | /* Stage 1 instruction access cacheability is unaffected. */ |
| 47 | .set .L_SCTLR_ELx_I, 0x1 << 12 |
| 48 | /* SP alignment fault if SP is not aligned to a 16 byte boundary. */ |
| 49 | .set .L_SCTLR_ELx_SA, 0x1 << 3 |
| 50 | /* Stage 1 data access cacheability is unaffected. */ |
| 51 | .set .L_SCTLR_ELx_C, 0x1 << 2 |
| 52 | /* EL0 and EL1 stage 1 MMU enabled. */ |
| 53 | .set .L_SCTLR_ELx_M, 0x1 << 0 |
| 54 | /* Privileged Access Never is unchanged on taking an exception to EL1. */ |
| 55 | .set .L_SCTLR_EL1_SPAN, 0x1 << 23 |
Andrew Walbran | a0ecabd | 2022-04-11 14:26:18 +0000 | [diff] [blame] | 56 | /* All writable memory regions are treated as XN. */ |
| 57 | .set .L_SCTLR_EL1_WXN, 0x1 << 19 |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 58 | /* SETEND instruction disabled at EL0 in aarch32 mode. */ |
| 59 | .set .L_SCTLR_EL1_SED, 0x1 << 8 |
| 60 | /* Various IT instructions are disabled at EL0 in aarch32 mode. */ |
| 61 | .set .L_SCTLR_EL1_ITD, 0x1 << 7 |
| 62 | .set .L_SCTLR_EL1_RES1, (0x1 << 11) | (0x1 << 20) | (0x1 << 22) | (0x1 << 28) | (0x1 << 29) |
| 63 | .set .Lsctlrval, .L_SCTLR_ELx_M | .L_SCTLR_ELx_C | .L_SCTLR_ELx_SA | .L_SCTLR_EL1_ITD | .L_SCTLR_EL1_SED |
Andrew Walbran | a0ecabd | 2022-04-11 14:26:18 +0000 | [diff] [blame] | 64 | .set .Lsctlrval, .Lsctlrval | .L_SCTLR_ELx_I | .L_SCTLR_EL1_SPAN | .L_SCTLR_EL1_RES1 | .L_SCTLR_EL1_WXN |
| 65 | |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 66 | /* Bionic-compatible stack protector */ |
| 67 | .section .data.stack_protector, "aw" |
| 68 | __bionic_tls: |
| 69 | .zero 40 |
| 70 | .global __stack_chk_guard |
| 71 | __stack_chk_guard: |
| 72 | .quad 0x23d6d3f3c3b84098 /* TODO: randomize */ |
| 73 | |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 74 | /** |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 75 | * This is a generic entry point for an image. It carries out the operations required to prepare the |
| 76 | * loaded image to be run. Specifically, it zeroes the bss section using registers x25 and above, |
| 77 | * prepares the stack, enables floating point, and sets up the exception vector. It preserves x0-x3 |
| 78 | * for the Rust entry point, as these may contain boot parameters. |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 79 | */ |
| 80 | .section .init.entry, "ax" |
| 81 | .global entry |
| 82 | entry: |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 83 | /* Load and apply the memory management configuration, ready to enable MMU and caches. */ |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 84 | |
Pierre-Clément Tosi | d40ff91 | 2022-06-30 16:11:33 +0100 | [diff] [blame] | 85 | adr x30, vector_table_panic |
| 86 | msr vbar_el1, x30 |
| 87 | |
Pierre-Clément Tosi | 2cba973 | 2022-10-26 20:38:51 +0100 | [diff] [blame] | 88 | /* |
| 89 | * Our load address is set by the host so validate it before proceeding. |
| 90 | */ |
| 91 | adr x30, entry |
| 92 | mov_i x29, entry |
| 93 | cmp x29, x30 |
| 94 | b.eq 1f |
| 95 | reset_or_hang |
| 96 | 1: |
| 97 | |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 98 | adrp x30, idmap |
| 99 | msr ttbr0_el1, x30 |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 100 | |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 101 | mov_i x30, .Lmairval |
| 102 | msr mair_el1, x30 |
| 103 | |
| 104 | mov_i x30, .Ltcrval |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 105 | /* Copy the supported PA range into TCR_EL1.IPS. */ |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 106 | mrs x29, id_aa64mmfr0_el1 |
| 107 | bfi x30, x29, #32, #4 |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 108 | |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 109 | msr tcr_el1, x30 |
| 110 | |
| 111 | mov_i x30, .Lsctlrval |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * Ensure everything before this point has completed, then invalidate any potentially stale |
| 115 | * local TLB entries before they start being used. |
| 116 | */ |
| 117 | isb |
| 118 | tlbi vmalle1 |
| 119 | ic iallu |
| 120 | dsb nsh |
| 121 | isb |
| 122 | |
| 123 | /* |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 124 | * Configure sctlr_el1 to enable MMU and cache and don't proceed until this has completed. |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 125 | */ |
Andrew Walbran | e03395a | 2022-04-29 15:15:49 +0000 | [diff] [blame] | 126 | msr sctlr_el1, x30 |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 127 | isb |
| 128 | |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 129 | /* Disable trapping floating point access in EL1. */ |
| 130 | mrs x30, cpacr_el1 |
| 131 | orr x30, x30, #(0x3 << 20) |
| 132 | msr cpacr_el1, x30 |
| 133 | isb |
| 134 | |
| 135 | /* Zero out the bss section. */ |
| 136 | adr_l x29, bss_begin |
| 137 | adr_l x30, bss_end |
| 138 | 0: cmp x29, x30 |
| 139 | b.hs 1f |
| 140 | stp xzr, xzr, [x29], #16 |
| 141 | b 0b |
| 142 | |
Andrew Walbran | a0ecabd | 2022-04-11 14:26:18 +0000 | [diff] [blame] | 143 | 1: /* Copy the data section. */ |
| 144 | adr_l x28, data_begin |
| 145 | adr_l x29, data_end |
| 146 | adr_l x30, data_lma |
| 147 | 2: cmp x28, x29 |
| 148 | b.ge 3f |
| 149 | ldp q0, q1, [x30], #32 |
| 150 | stp q0, q1, [x28], #32 |
| 151 | b 2b |
| 152 | |
| 153 | 3: /* Prepare the stack. */ |
| 154 | adr_l x30, boot_stack_end |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 155 | mov sp, x30 |
| 156 | |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 157 | /* Set up exception vector. */ |
| 158 | adr x30, vector_table_el1 |
| 159 | msr vbar_el1, x30 |
| 160 | |
David Brazdil | a51c6f0 | 2022-10-12 09:51:48 +0000 | [diff] [blame] | 161 | /* Set up Bionic-compatible thread-local storage. */ |
| 162 | adr_l x30, __bionic_tls |
| 163 | msr tpidr_el0, x30 |
| 164 | |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 165 | /* Call into Rust code. */ |
Andrew Walbran | b996b4a | 2022-04-22 15:15:41 +0000 | [diff] [blame] | 166 | bl rust_entry |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 167 | |
| 168 | /* Loop forever waiting for interrupts. */ |
Andrew Walbran | a0ecabd | 2022-04-11 14:26:18 +0000 | [diff] [blame] | 169 | 4: wfi |
| 170 | b 4b |