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 | |
| 17 | .macro adr_l, reg:req, sym:req |
| 18 | adrp \reg, \sym |
| 19 | add \reg, \reg, :lo12:\sym |
| 20 | .endm |
| 21 | |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 22 | .macro mov_i, reg:req, imm:req |
| 23 | movz \reg, :abs_g3:\imm |
| 24 | movk \reg, :abs_g2_nc:\imm |
| 25 | movk \reg, :abs_g1_nc:\imm |
| 26 | movk \reg, :abs_g0_nc:\imm |
| 27 | .endm |
| 28 | |
| 29 | .set .L_MAIR_DEV_nGnRE, 0x04 |
| 30 | .set .L_MAIR_MEM_WBWA, 0xff |
| 31 | .set .Lmairval, .L_MAIR_DEV_nGnRE | (.L_MAIR_MEM_WBWA << 8) |
| 32 | |
| 33 | /* 4 KiB granule size for TTBR0_EL1. */ |
| 34 | .set .L_TCR_TG0_4KB, 0x0 << 14 |
| 35 | /* 4 KiB granule size for TTBR1_EL1. */ |
| 36 | .set .L_TCR_TG1_4KB, 0x2 << 30 |
| 37 | /* Disable translation table walk for TTBR1_EL1, generating a translation fault instead. */ |
| 38 | .set .L_TCR_EPD1, 0x1 << 23 |
| 39 | /* Translation table walks for TTBR0_EL1 are inner sharable. */ |
| 40 | .set .L_TCR_SH_INNER, 0x3 << 12 |
| 41 | /* |
| 42 | * Translation table walks for TTBR0_EL1 are outer write-back read-allocate write-allocate |
| 43 | * cacheable. |
| 44 | */ |
| 45 | .set .L_TCR_RGN_OWB, 0x1 << 10 |
| 46 | /* |
| 47 | * Translation table walks for TTBR0_EL1 are inner write-back read-allocate write-allocate |
| 48 | * cacheable. |
| 49 | */ |
| 50 | .set .L_TCR_RGN_IWB, 0x1 << 8 |
| 51 | /* Size offset for TTBR0_EL1 is 2**39 bytes (512 GiB). */ |
| 52 | .set .L_TCR_T0SZ_512, 64 - 39 |
| 53 | .set .Ltcrval, .L_TCR_TG0_4KB | .L_TCR_TG1_4KB | .L_TCR_EPD1 | .L_TCR_RGN_OWB |
| 54 | .set .Ltcrval, .Ltcrval | .L_TCR_RGN_IWB | .L_TCR_SH_INNER | .L_TCR_T0SZ_512 |
| 55 | |
| 56 | /* Stage 1 instruction access cacheability is unaffected. */ |
| 57 | .set .L_SCTLR_ELx_I, 0x1 << 12 |
| 58 | /* SP alignment fault if SP is not aligned to a 16 byte boundary. */ |
| 59 | .set .L_SCTLR_ELx_SA, 0x1 << 3 |
| 60 | /* Stage 1 data access cacheability is unaffected. */ |
| 61 | .set .L_SCTLR_ELx_C, 0x1 << 2 |
| 62 | /* EL0 and EL1 stage 1 MMU enabled. */ |
| 63 | .set .L_SCTLR_ELx_M, 0x1 << 0 |
| 64 | /* Privileged Access Never is unchanged on taking an exception to EL1. */ |
| 65 | .set .L_SCTLR_EL1_SPAN, 0x1 << 23 |
Andrew Walbran | a0ecabd | 2022-04-11 14:26:18 +0000 | [diff] [blame] | 66 | /* All writable memory regions are treated as XN. */ |
| 67 | .set .L_SCTLR_EL1_WXN, 0x1 << 19 |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 68 | /* SETEND instruction disabled at EL0 in aarch32 mode. */ |
| 69 | .set .L_SCTLR_EL1_SED, 0x1 << 8 |
| 70 | /* Various IT instructions are disabled at EL0 in aarch32 mode. */ |
| 71 | .set .L_SCTLR_EL1_ITD, 0x1 << 7 |
| 72 | .set .L_SCTLR_EL1_RES1, (0x1 << 11) | (0x1 << 20) | (0x1 << 22) | (0x1 << 28) | (0x1 << 29) |
| 73 | .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] | 74 | .set .Lsctlrval, .Lsctlrval | .L_SCTLR_ELx_I | .L_SCTLR_EL1_SPAN | .L_SCTLR_EL1_RES1 | .L_SCTLR_EL1_WXN |
| 75 | |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 76 | /** |
| 77 | * This is a generic entry point for an image. It carries out the operations |
| 78 | * required to prepare the loaded image to be run. Specifically, it zeroes the |
| 79 | * bss section using registers x25 and above, prepares the stack, enables |
| 80 | * floating point, and sets up the exception vector. |
| 81 | */ |
| 82 | .section .init.entry, "ax" |
| 83 | .global entry |
| 84 | entry: |
Andrew Walbran | 267f6c1 | 2022-03-24 11:26:36 +0000 | [diff] [blame] | 85 | /* Enable MMU and caches. */ |
| 86 | |
| 87 | /* |
| 88 | * Load and apply the memory management configuration. |
| 89 | */ |
| 90 | adrp x1, idmap |
| 91 | mov_i x2, .Lmairval |
| 92 | mov_i x3, .Ltcrval |
| 93 | mov_i x4, .Lsctlrval |
| 94 | |
| 95 | /* Copy the supported PA range into TCR_EL1.IPS. */ |
| 96 | mrs x6, id_aa64mmfr0_el1 |
| 97 | bfi x3, x6, #32, #4 |
| 98 | |
| 99 | msr ttbr0_el1, x1 |
| 100 | msr mair_el1, x2 |
| 101 | msr tcr_el1, x3 |
| 102 | |
| 103 | /* |
| 104 | * Ensure everything before this point has completed, then invalidate any potentially stale |
| 105 | * local TLB entries before they start being used. |
| 106 | */ |
| 107 | isb |
| 108 | tlbi vmalle1 |
| 109 | ic iallu |
| 110 | dsb nsh |
| 111 | isb |
| 112 | |
| 113 | /* |
| 114 | * Configure sctlr_el1 to enable MMU and cache and don't proceed until |
| 115 | * this has completed. |
| 116 | */ |
| 117 | msr sctlr_el1, x4 |
| 118 | isb |
| 119 | |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 120 | /* Disable trapping floating point access in EL1. */ |
| 121 | mrs x30, cpacr_el1 |
| 122 | orr x30, x30, #(0x3 << 20) |
| 123 | msr cpacr_el1, x30 |
| 124 | isb |
| 125 | |
| 126 | /* Zero out the bss section. */ |
| 127 | adr_l x29, bss_begin |
| 128 | adr_l x30, bss_end |
| 129 | 0: cmp x29, x30 |
| 130 | b.hs 1f |
| 131 | stp xzr, xzr, [x29], #16 |
| 132 | b 0b |
| 133 | |
Andrew Walbran | a0ecabd | 2022-04-11 14:26:18 +0000 | [diff] [blame] | 134 | 1: /* Copy the data section. */ |
| 135 | adr_l x28, data_begin |
| 136 | adr_l x29, data_end |
| 137 | adr_l x30, data_lma |
| 138 | 2: cmp x28, x29 |
| 139 | b.ge 3f |
| 140 | ldp q0, q1, [x30], #32 |
| 141 | stp q0, q1, [x28], #32 |
| 142 | b 2b |
| 143 | |
| 144 | 3: /* Prepare the stack. */ |
| 145 | adr_l x30, boot_stack_end |
Andrew Walbran | 15068b0 | 2022-03-22 15:57:34 +0000 | [diff] [blame] | 146 | mov sp, x30 |
| 147 | |
| 148 | /* Call into Rust code. */ |
| 149 | bl main |
| 150 | |
| 151 | /* Loop forever waiting for interrupts. */ |
Andrew Walbran | a0ecabd | 2022-04-11 14:26:18 +0000 | [diff] [blame] | 152 | 4: wfi |
| 153 | b 4b |