Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Low-level CPU initialisation |
| 4 | * Based on arch/arm/kernel/head.S |
| 5 | * |
| 6 | * Copyright (C) 1994-2002 Russell King |
| 7 | * Copyright (C) 2003-2012 ARM Ltd. |
| 8 | * Authors: Catalin Marinas <catalin.marinas@arm.com> |
| 9 | * Will Deacon <will.deacon@arm.com> |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/linkage.h> |
| 13 | #include <linux/init.h> |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 14 | #include <linux/irqchip/arm-gic-v3.h> |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 15 | |
| 16 | #include <asm/assembler.h> |
Ard Biesheuvel | 08cdac6 | 2016-04-18 17:09:47 +0200 | [diff] [blame] | 17 | #include <asm/boot.h> |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 18 | #include <asm/ptrace.h> |
| 19 | #include <asm/asm-offsets.h> |
Catalin Marinas | c218bca | 2014-03-26 18:25:55 +0000 | [diff] [blame] | 20 | #include <asm/cache.h> |
Javi Merino | 0359b0e | 2012-08-29 18:32:18 +0100 | [diff] [blame] | 21 | #include <asm/cputype.h> |
Ard Biesheuvel | 1e48ef7 | 2016-01-26 09:13:44 +0100 | [diff] [blame] | 22 | #include <asm/elf.h> |
AKASHI Takahiro | f56063c5 | 2018-11-15 14:52:46 +0900 | [diff] [blame] | 23 | #include <asm/image.h> |
Suzuki K. Poulose | 87d1587 | 2015-10-19 14:19:27 +0100 | [diff] [blame] | 24 | #include <asm/kernel-pgtable.h> |
Marc Zyngier | 1f364c8 | 2014-02-19 09:33:14 +0000 | [diff] [blame] | 25 | #include <asm/kvm_arm.h> |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 26 | #include <asm/memory.h> |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 27 | #include <asm/pgtable-hwdef.h> |
| 28 | #include <asm/pgtable.h> |
| 29 | #include <asm/page.h> |
Suzuki K Poulose | bb90527 | 2016-02-23 10:31:42 +0000 | [diff] [blame] | 30 | #include <asm/smp.h> |
Suzuki K. Poulose | 4bf8b96 | 2015-10-19 14:19:35 +0100 | [diff] [blame] | 31 | #include <asm/sysreg.h> |
| 32 | #include <asm/thread_info.h> |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 33 | #include <asm/virt.h> |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 34 | |
Ard Biesheuvel | b5f4a21 | 2017-03-23 19:00:46 +0000 | [diff] [blame] | 35 | #include "efi-header.S" |
| 36 | |
Ard Biesheuvel | 6f4d57f | 2015-03-17 09:14:29 +0100 | [diff] [blame] | 37 | #define __PHYS_OFFSET (KERNEL_START - TEXT_OFFSET) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 38 | |
Ard Biesheuvel | 4190312 | 2014-08-13 18:53:03 +0100 | [diff] [blame] | 39 | #if (TEXT_OFFSET & 0xfff) != 0 |
| 40 | #error TEXT_OFFSET must be at least 4KB aligned |
| 41 | #elif (PAGE_OFFSET & 0x1fffff) != 0 |
Mark Rutland | da57a36 | 2014-06-24 16:51:37 +0100 | [diff] [blame] | 42 | #error PAGE_OFFSET must be at least 2MB aligned |
Ard Biesheuvel | 4190312 | 2014-08-13 18:53:03 +0100 | [diff] [blame] | 43 | #elif TEXT_OFFSET > 0x1fffff |
Mark Rutland | da57a36 | 2014-06-24 16:51:37 +0100 | [diff] [blame] | 44 | #error TEXT_OFFSET must be less than 2MB |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 47 | /* |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 48 | * Kernel startup entry point. |
| 49 | * --------------------------- |
| 50 | * |
| 51 | * The requirements are: |
| 52 | * MMU = off, D-cache = off, I-cache = on or off, |
| 53 | * x0 = physical address to the FDT blob. |
| 54 | * |
| 55 | * This code is mostly position independent so you call this at |
| 56 | * __pa(PAGE_OFFSET + TEXT_OFFSET). |
| 57 | * |
| 58 | * Note that the callee-saved registers are used for storing variables |
| 59 | * that are useful before the MMU is enabled. The allocations are described |
| 60 | * in the entry routines. |
| 61 | */ |
| 62 | __HEAD |
Ard Biesheuvel | 2bf31a4 | 2015-12-26 12:46:40 +0100 | [diff] [blame] | 63 | _head: |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 64 | /* |
| 65 | * DO NOT MODIFY. Image header expected by Linux boot-loaders. |
| 66 | */ |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 67 | #ifdef CONFIG_EFI |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 68 | /* |
| 69 | * This add instruction has no meaningful effect except that |
| 70 | * its opcode forms the magic "MZ" signature required by UEFI. |
| 71 | */ |
| 72 | add x13, x18, #0x16 |
| 73 | b stext |
| 74 | #else |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 75 | b stext // branch to kernel start, magic |
| 76 | .long 0 // reserved |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 77 | #endif |
Ard Biesheuvel | 6ad1fe5 | 2015-12-26 13:48:02 +0100 | [diff] [blame] | 78 | le64sym _kernel_offset_le // Image load offset from start of RAM, little-endian |
| 79 | le64sym _kernel_size_le // Effective size of kernel image, little-endian |
| 80 | le64sym _kernel_flags_le // Informative flags, little-endian |
Roy Franz | 4370eec | 2013-08-15 00:10:00 +0100 | [diff] [blame] | 81 | .quad 0 // reserved |
| 82 | .quad 0 // reserved |
| 83 | .quad 0 // reserved |
AKASHI Takahiro | f56063c5 | 2018-11-15 14:52:46 +0900 | [diff] [blame] | 84 | .ascii ARM64_IMAGE_MAGIC // Magic number |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 85 | #ifdef CONFIG_EFI |
Ard Biesheuvel | 2bf31a4 | 2015-12-26 12:46:40 +0100 | [diff] [blame] | 86 | .long pe_header - _head // Offset to the PE header. |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 87 | |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 88 | pe_header: |
Ard Biesheuvel | b5f4a21 | 2017-03-23 19:00:46 +0000 | [diff] [blame] | 89 | __EFI_PE_HEADER |
Ard Biesheuvel | 9992225 | 2017-03-23 19:00:47 +0000 | [diff] [blame] | 90 | #else |
| 91 | .long 0 // reserved |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 92 | #endif |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 93 | |
Ard Biesheuvel | 546c8c4 | 2016-03-30 17:43:07 +0200 | [diff] [blame] | 94 | __INIT |
| 95 | |
Ard Biesheuvel | a9be2ee | 2016-08-31 12:05:17 +0100 | [diff] [blame] | 96 | /* |
| 97 | * The following callee saved general purpose registers are used on the |
| 98 | * primary lowlevel boot path: |
| 99 | * |
| 100 | * Register Scope Purpose |
| 101 | * x21 stext() .. start_kernel() FDT pointer passed at boot in x0 |
| 102 | * x23 stext() .. start_kernel() physical misalignment/KASLR offset |
| 103 | * x28 __create_page_tables() callee preserved temp register |
| 104 | * x19/x20 __primary_switch() callee preserved temp registers |
Peter Collingbourne | 5cf896f | 2019-07-31 18:18:42 -0700 | [diff] [blame] | 105 | * x24 __primary_switch() .. relocate_kernel() |
| 106 | * current RELR displacement |
Ard Biesheuvel | a9be2ee | 2016-08-31 12:05:17 +0100 | [diff] [blame] | 107 | */ |
Mark Brown | ebdf44a | 2020-02-18 19:58:34 +0000 | [diff] [blame] | 108 | SYM_CODE_START(stext) |
Ard Biesheuvel | da9c177 | 2015-03-17 10:55:12 +0100 | [diff] [blame] | 109 | bl preserve_boot_args |
Ard Biesheuvel | 23c8a50 | 2016-08-31 12:05:12 +0100 | [diff] [blame] | 110 | bl el2_setup // Drop to EL1, w0=cpu_boot_mode |
Ard Biesheuvel | b929fe3 | 2016-08-31 12:05:15 +0100 | [diff] [blame] | 111 | adrp x23, __PHYS_OFFSET |
| 112 | and x23, x23, MIN_KIMG_ALIGN - 1 // KASLR offset, defaults to 0 |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 113 | bl set_cpu_boot_mode_flag |
Ard Biesheuvel | aea73ab | 2016-08-16 21:02:32 +0200 | [diff] [blame] | 114 | bl __create_page_tables |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 115 | /* |
Marc Zyngier | a591ede | 2015-03-18 14:55:20 +0000 | [diff] [blame] | 116 | * The following calls CPU setup code, see arch/arm64/mm/proc.S for |
| 117 | * details. |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 118 | * On return, the CPU will be ready for the MMU to be turned on and |
| 119 | * the TCR will have been set. |
| 120 | */ |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 121 | bl __cpu_setup // initialise processor |
Ard Biesheuvel | 3c5e9f2 | 2016-08-31 12:05:13 +0100 | [diff] [blame] | 122 | b __primary_switch |
Mark Brown | ebdf44a | 2020-02-18 19:58:34 +0000 | [diff] [blame] | 123 | SYM_CODE_END(stext) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 124 | |
| 125 | /* |
Ard Biesheuvel | da9c177 | 2015-03-17 10:55:12 +0100 | [diff] [blame] | 126 | * Preserve the arguments passed by the bootloader in x0 .. x3 |
| 127 | */ |
Mark Brown | ebdf44a | 2020-02-18 19:58:34 +0000 | [diff] [blame] | 128 | SYM_CODE_START_LOCAL(preserve_boot_args) |
Ard Biesheuvel | da9c177 | 2015-03-17 10:55:12 +0100 | [diff] [blame] | 129 | mov x21, x0 // x21=FDT |
| 130 | |
| 131 | adr_l x0, boot_args // record the contents of |
| 132 | stp x21, x1, [x0] // x0 .. x3 at kernel entry |
| 133 | stp x2, x3, [x0, #16] |
| 134 | |
| 135 | dmb sy // needed before dc ivac with |
| 136 | // MMU off |
| 137 | |
Robin Murphy | d46befe | 2017-07-25 11:55:39 +0100 | [diff] [blame] | 138 | mov x1, #0x20 // 4 x 8 bytes |
| 139 | b __inval_dcache_area // tail call |
Mark Brown | ebdf44a | 2020-02-18 19:58:34 +0000 | [diff] [blame] | 140 | SYM_CODE_END(preserve_boot_args) |
Ard Biesheuvel | da9c177 | 2015-03-17 10:55:12 +0100 | [diff] [blame] | 141 | |
| 142 | /* |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 143 | * Macro to create a table entry to the next page. |
| 144 | * |
| 145 | * tbl: page table address |
| 146 | * virt: virtual address |
| 147 | * shift: #imm page table shift |
| 148 | * ptrs: #imm pointers per table page |
| 149 | * |
| 150 | * Preserves: virt |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 151 | * Corrupts: ptrs, tmp1, tmp2 |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 152 | * Returns: tbl -> next level table page address |
| 153 | */ |
| 154 | .macro create_table_entry, tbl, virt, shift, ptrs, tmp1, tmp2 |
Kristina Martsenko | e6d588a | 2017-12-13 17:07:19 +0000 | [diff] [blame] | 155 | add \tmp1, \tbl, #PAGE_SIZE |
Will Deacon | 79ddab3 | 2018-01-29 11:59:59 +0000 | [diff] [blame] | 156 | phys_to_pte \tmp2, \tmp1 |
Kristina Martsenko | e6d588a | 2017-12-13 17:07:19 +0000 | [diff] [blame] | 157 | orr \tmp2, \tmp2, #PMD_TYPE_TABLE // address of next table and entry type |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 158 | lsr \tmp1, \virt, #\shift |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 159 | sub \ptrs, \ptrs, #1 |
| 160 | and \tmp1, \tmp1, \ptrs // table index |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 161 | str \tmp2, [\tbl, \tmp1, lsl #3] |
| 162 | add \tbl, \tbl, #PAGE_SIZE // next level table page |
| 163 | .endm |
| 164 | |
| 165 | /* |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 166 | * Macro to populate page table entries, these entries can be pointers to the next level |
| 167 | * or last level entries pointing to physical memory. |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 168 | * |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 169 | * tbl: page table address |
| 170 | * rtbl: pointer to page table or physical memory |
| 171 | * index: start index to write |
| 172 | * eindex: end index to write - [index, eindex] written to |
| 173 | * flags: flags for pagetable entry to or in |
| 174 | * inc: increment to rtbl between each entry |
| 175 | * tmp1: temporary variable |
| 176 | * |
| 177 | * Preserves: tbl, eindex, flags, inc |
| 178 | * Corrupts: index, tmp1 |
| 179 | * Returns: rtbl |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 180 | */ |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 181 | .macro populate_entries, tbl, rtbl, index, eindex, flags, inc, tmp1 |
Will Deacon | 79ddab3 | 2018-01-29 11:59:59 +0000 | [diff] [blame] | 182 | .Lpe\@: phys_to_pte \tmp1, \rtbl |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 183 | orr \tmp1, \tmp1, \flags // tmp1 = table entry |
| 184 | str \tmp1, [\tbl, \index, lsl #3] |
| 185 | add \rtbl, \rtbl, \inc // rtbl = pa next level |
| 186 | add \index, \index, #1 |
| 187 | cmp \index, \eindex |
| 188 | b.ls .Lpe\@ |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 189 | .endm |
| 190 | |
| 191 | /* |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 192 | * Compute indices of table entries from virtual address range. If multiple entries |
| 193 | * were needed in the previous page table level then the next page table level is assumed |
| 194 | * to be composed of multiple pages. (This effectively scales the end index). |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 195 | * |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 196 | * vstart: virtual address of start of range |
| 197 | * vend: virtual address of end of range |
| 198 | * shift: shift used to transform virtual address into index |
| 199 | * ptrs: number of entries in page table |
| 200 | * istart: index in table corresponding to vstart |
| 201 | * iend: index in table corresponding to vend |
| 202 | * count: On entry: how many extra entries were required in previous level, scales |
| 203 | * our end index. |
| 204 | * On exit: returns how many extra entries required for next page table level |
| 205 | * |
| 206 | * Preserves: vstart, vend, shift, ptrs |
| 207 | * Returns: istart, iend, count |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 208 | */ |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 209 | .macro compute_indices, vstart, vend, shift, ptrs, istart, iend, count |
| 210 | lsr \iend, \vend, \shift |
| 211 | mov \istart, \ptrs |
| 212 | sub \istart, \istart, #1 |
| 213 | and \iend, \iend, \istart // iend = (vend >> shift) & (ptrs - 1) |
| 214 | mov \istart, \ptrs |
| 215 | mul \istart, \istart, \count |
| 216 | add \iend, \iend, \istart // iend += (count - 1) * ptrs |
| 217 | // our entries span multiple tables |
| 218 | |
| 219 | lsr \istart, \vstart, \shift |
| 220 | mov \count, \ptrs |
| 221 | sub \count, \count, #1 |
| 222 | and \istart, \istart, \count |
| 223 | |
| 224 | sub \count, \iend, \istart |
| 225 | .endm |
| 226 | |
| 227 | /* |
| 228 | * Map memory for specified virtual address range. Each level of page table needed supports |
| 229 | * multiple entries. If a level requires n entries the next page table level is assumed to be |
| 230 | * formed from n pages. |
| 231 | * |
| 232 | * tbl: location of page table |
| 233 | * rtbl: address to be used for first level page table entry (typically tbl + PAGE_SIZE) |
| 234 | * vstart: start address to map |
| 235 | * vend: end address to map - we map [vstart, vend] |
| 236 | * flags: flags to use to map last level entries |
| 237 | * phys: physical address corresponding to vstart - physical memory is contiguous |
| 238 | * pgds: the number of pgd entries |
| 239 | * |
| 240 | * Temporaries: istart, iend, tmp, count, sv - these need to be different registers |
| 241 | * Preserves: vstart, vend, flags |
| 242 | * Corrupts: tbl, rtbl, istart, iend, tmp, count, sv |
| 243 | */ |
| 244 | .macro map_memory, tbl, rtbl, vstart, vend, flags, phys, pgds, istart, iend, tmp, count, sv |
| 245 | add \rtbl, \tbl, #PAGE_SIZE |
| 246 | mov \sv, \rtbl |
| 247 | mov \count, #0 |
| 248 | compute_indices \vstart, \vend, #PGDIR_SHIFT, \pgds, \istart, \iend, \count |
| 249 | populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp |
| 250 | mov \tbl, \sv |
| 251 | mov \sv, \rtbl |
| 252 | |
| 253 | #if SWAPPER_PGTABLE_LEVELS > 3 |
| 254 | compute_indices \vstart, \vend, #PUD_SHIFT, #PTRS_PER_PUD, \istart, \iend, \count |
| 255 | populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp |
| 256 | mov \tbl, \sv |
| 257 | mov \sv, \rtbl |
| 258 | #endif |
| 259 | |
| 260 | #if SWAPPER_PGTABLE_LEVELS > 2 |
| 261 | compute_indices \vstart, \vend, #SWAPPER_TABLE_SHIFT, #PTRS_PER_PMD, \istart, \iend, \count |
| 262 | populate_entries \tbl, \rtbl, \istart, \iend, #PMD_TYPE_TABLE, #PAGE_SIZE, \tmp |
| 263 | mov \tbl, \sv |
| 264 | #endif |
| 265 | |
| 266 | compute_indices \vstart, \vend, #SWAPPER_BLOCK_SHIFT, #PTRS_PER_PTE, \istart, \iend, \count |
| 267 | bic \count, \phys, #SWAPPER_BLOCK_SIZE - 1 |
| 268 | populate_entries \tbl, \count, \istart, \iend, \flags, #SWAPPER_BLOCK_SIZE, \tmp |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 269 | .endm |
| 270 | |
| 271 | /* |
| 272 | * Setup the initial page tables. We only setup the barest amount which is |
| 273 | * required to get the kernel running. The following sections are required: |
| 274 | * - identity mapping to enable the MMU (low address, TTBR0) |
| 275 | * - first few MB of the kernel linear mapping to jump to once the MMU has |
Ard Biesheuvel | 61bd93c | 2015-06-01 13:40:32 +0200 | [diff] [blame] | 276 | * been enabled |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 277 | */ |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 278 | SYM_FUNC_START_LOCAL(__create_page_tables) |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 279 | mov x28, lr |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 280 | |
| 281 | /* |
Jun Yao | 8eb7e28 | 2018-09-24 17:56:18 +0100 | [diff] [blame] | 282 | * Invalidate the init page tables to avoid potential dirty cache lines |
| 283 | * being evicted. Other page tables are allocated in rodata as part of |
| 284 | * the kernel image, and thus are clean to the PoC per the boot |
| 285 | * protocol. |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 286 | */ |
Jun Yao | 8eb7e28 | 2018-09-24 17:56:18 +0100 | [diff] [blame] | 287 | adrp x0, init_pg_dir |
Jun Yao | 2b5548b | 2018-09-24 15:47:49 +0100 | [diff] [blame] | 288 | adrp x1, init_pg_end |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 289 | sub x1, x1, x0 |
Robin Murphy | d46befe | 2017-07-25 11:55:39 +0100 | [diff] [blame] | 290 | bl __inval_dcache_area |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 291 | |
| 292 | /* |
Jun Yao | 8eb7e28 | 2018-09-24 17:56:18 +0100 | [diff] [blame] | 293 | * Clear the init page tables. |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 294 | */ |
Jun Yao | 8eb7e28 | 2018-09-24 17:56:18 +0100 | [diff] [blame] | 295 | adrp x0, init_pg_dir |
Jun Yao | 2b5548b | 2018-09-24 15:47:49 +0100 | [diff] [blame] | 296 | adrp x1, init_pg_end |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 297 | sub x1, x1, x0 |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 298 | 1: stp xzr, xzr, [x0], #16 |
| 299 | stp xzr, xzr, [x0], #16 |
| 300 | stp xzr, xzr, [x0], #16 |
| 301 | stp xzr, xzr, [x0], #16 |
Robin Murphy | d46befe | 2017-07-25 11:55:39 +0100 | [diff] [blame] | 302 | subs x1, x1, #64 |
| 303 | b.ne 1b |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 304 | |
Ard Biesheuvel | b03cc88 | 2016-04-18 17:09:45 +0200 | [diff] [blame] | 305 | mov x7, SWAPPER_MM_MMUFLAGS |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * Create the identity mapping. |
| 309 | */ |
Ard Biesheuvel | aea73ab | 2016-08-16 21:02:32 +0200 | [diff] [blame] | 310 | adrp x0, idmap_pg_dir |
Ard Biesheuvel | 5dfe9d7 | 2015-06-01 13:40:33 +0200 | [diff] [blame] | 311 | adrp x3, __idmap_text_start // __pa(__idmap_text_start) |
Ard Biesheuvel | dd006da | 2015-03-19 16:42:27 +0000 | [diff] [blame] | 312 | |
Steve Capper | b6d00d4 | 2019-08-07 16:55:22 +0100 | [diff] [blame] | 313 | #ifdef CONFIG_ARM64_VA_BITS_52 |
Steve Capper | 67e7fdf | 2018-12-06 22:50:41 +0000 | [diff] [blame] | 314 | mrs_s x6, SYS_ID_AA64MMFR2_EL1 |
| 315 | and x6, x6, #(0xf << ID_AA64MMFR2_LVA_SHIFT) |
| 316 | mov x5, #52 |
| 317 | cbnz x6, 1f |
| 318 | #endif |
Steve Capper | 90ec95c | 2019-08-07 16:55:17 +0100 | [diff] [blame] | 319 | mov x5, #VA_BITS_MIN |
Steve Capper | 67e7fdf | 2018-12-06 22:50:41 +0000 | [diff] [blame] | 320 | 1: |
Steve Capper | 5383cc6 | 2019-08-07 16:55:18 +0100 | [diff] [blame] | 321 | adr_l x6, vabits_actual |
Steve Capper | 67e7fdf | 2018-12-06 22:50:41 +0000 | [diff] [blame] | 322 | str x5, [x6] |
| 323 | dmb sy |
| 324 | dc ivac, x6 // Invalidate potentially stale cache line |
| 325 | |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 326 | /* |
| 327 | * VA_BITS may be too small to allow for an ID mapping to be created |
| 328 | * that covers system RAM if that is located sufficiently high in the |
| 329 | * physical address space. So for the ID map, use an extended virtual |
| 330 | * range in that case, and configure an additional translation level |
| 331 | * if needed. |
| 332 | * |
| 333 | * Calculate the maximum allowed value for TCR_EL1.T0SZ so that the |
| 334 | * entire ID map region can be mapped. As T0SZ == (64 - #bits used), |
| 335 | * this number conveniently equals the number of leading zeroes in |
| 336 | * the physical address of __idmap_text_end. |
| 337 | */ |
| 338 | adrp x5, __idmap_text_end |
| 339 | clz x5, x5 |
| 340 | cmp x5, TCR_T0SZ(VA_BITS) // default T0SZ small enough? |
| 341 | b.ge 1f // .. then skip VA range extension |
| 342 | |
| 343 | adr_l x6, idmap_t0sz |
| 344 | str x5, [x6] |
| 345 | dmb sy |
| 346 | dc ivac, x6 // Invalidate potentially stale cache line |
| 347 | |
| 348 | #if (VA_BITS < 48) |
Ard Biesheuvel | dd006da | 2015-03-19 16:42:27 +0000 | [diff] [blame] | 349 | #define EXTRA_SHIFT (PGDIR_SHIFT + PAGE_SHIFT - 3) |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 350 | #define EXTRA_PTRS (1 << (PHYS_MASK_SHIFT - EXTRA_SHIFT)) |
Ard Biesheuvel | dd006da | 2015-03-19 16:42:27 +0000 | [diff] [blame] | 351 | |
| 352 | /* |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 353 | * If VA_BITS < 48, we have to configure an additional table level. |
Ard Biesheuvel | dd006da | 2015-03-19 16:42:27 +0000 | [diff] [blame] | 354 | * First, we have to verify our assumption that the current value of |
| 355 | * VA_BITS was chosen such that all translation levels are fully |
| 356 | * utilised, and that lowering T0SZ will always result in an additional |
| 357 | * translation level to be configured. |
| 358 | */ |
| 359 | #if VA_BITS != EXTRA_SHIFT |
| 360 | #error "Mismatch between VA_BITS and page size/number of translation levels" |
| 361 | #endif |
| 362 | |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 363 | mov x4, EXTRA_PTRS |
| 364 | create_table_entry x0, x3, EXTRA_SHIFT, x4, x5, x6 |
| 365 | #else |
Ard Biesheuvel | dd006da | 2015-03-19 16:42:27 +0000 | [diff] [blame] | 366 | /* |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 367 | * If VA_BITS == 48, we don't have to configure an additional |
| 368 | * translation level, but the top-level table has more entries. |
Ard Biesheuvel | dd006da | 2015-03-19 16:42:27 +0000 | [diff] [blame] | 369 | */ |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 370 | mov x4, #1 << (PHYS_MASK_SHIFT - PGDIR_SHIFT) |
| 371 | str_l x4, idmap_ptrs_per_pgd, x5 |
Ard Biesheuvel | dd006da | 2015-03-19 16:42:27 +0000 | [diff] [blame] | 372 | #endif |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 373 | 1: |
| 374 | ldr_l x4, idmap_ptrs_per_pgd |
Ard Biesheuvel | 5dfe9d7 | 2015-06-01 13:40:33 +0200 | [diff] [blame] | 375 | mov x5, x3 // __pa(__idmap_text_start) |
| 376 | adr_l x6, __idmap_text_end // __pa(__idmap_text_end) |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 377 | |
| 378 | map_memory x0, x1, x3, x6, x7, x3, x4, x10, x11, x12, x13, x14 |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * Map the kernel image (starting with PHYS_OFFSET). |
| 382 | */ |
Jun Yao | 2b5548b | 2018-09-24 15:47:49 +0100 | [diff] [blame] | 383 | adrp x0, init_pg_dir |
Ard Biesheuvel | 18b9c0d | 2016-04-18 17:09:46 +0200 | [diff] [blame] | 384 | mov_q x5, KIMAGE_VADDR + TEXT_OFFSET // compile time __va(_text) |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 385 | add x5, x5, x23 // add KASLR displacement |
Kristina Martsenko | fa2a844 | 2017-12-13 17:07:24 +0000 | [diff] [blame] | 386 | mov x4, PTRS_PER_PGD |
Ard Biesheuvel | 18b9c0d | 2016-04-18 17:09:46 +0200 | [diff] [blame] | 387 | adrp x6, _end // runtime __pa(_end) |
| 388 | adrp x3, _text // runtime __pa(_text) |
| 389 | sub x6, x6, x3 // _end - _text |
| 390 | add x6, x6, x5 // runtime __va(_end) |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 391 | |
| 392 | map_memory x0, x1, x5, x6, x7, x3, x4, x10, x11, x12, x13, x14 |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 393 | |
| 394 | /* |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 395 | * Since the page tables have been populated with non-cacheable |
| 396 | * accesses (MMU disabled), invalidate the idmap and swapper page |
| 397 | * tables again to remove any speculatively loaded cache lines. |
| 398 | */ |
Ard Biesheuvel | aea73ab | 2016-08-16 21:02:32 +0200 | [diff] [blame] | 399 | adrp x0, idmap_pg_dir |
Jun Yao | 2b5548b | 2018-09-24 15:47:49 +0100 | [diff] [blame] | 400 | adrp x1, init_pg_end |
Steve Capper | 0370b31 | 2018-01-11 10:11:59 +0000 | [diff] [blame] | 401 | sub x1, x1, x0 |
Mark Rutland | 91d5715 | 2015-03-24 13:50:27 +0000 | [diff] [blame] | 402 | dmb sy |
Robin Murphy | d46befe | 2017-07-25 11:55:39 +0100 | [diff] [blame] | 403 | bl __inval_dcache_area |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 404 | |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 405 | ret x28 |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 406 | SYM_FUNC_END(__create_page_tables) |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 407 | .ltorg |
| 408 | |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 409 | /* |
Ard Biesheuvel | a871d35 | 2015-03-04 11:51:48 +0100 | [diff] [blame] | 410 | * The following fragment of code is executed with the MMU enabled. |
Ard Biesheuvel | b929fe3 | 2016-08-31 12:05:15 +0100 | [diff] [blame] | 411 | * |
| 412 | * x0 = __PHYS_OFFSET |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 413 | */ |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 414 | SYM_FUNC_START_LOCAL(__primary_switched) |
Ard Biesheuvel | 60699ba | 2016-08-31 12:05:16 +0100 | [diff] [blame] | 415 | adrp x4, init_thread_union |
| 416 | add sp, x4, #THREAD_SIZE |
Mark Rutland | c02433d | 2016-11-03 20:23:13 +0000 | [diff] [blame] | 417 | adr_l x5, init_task |
| 418 | msr sp_el0, x5 // Save thread_info |
Ard Biesheuvel | 60699ba | 2016-08-31 12:05:16 +0100 | [diff] [blame] | 419 | |
Ard Biesheuvel | 2bf31a4 | 2015-12-26 12:46:40 +0100 | [diff] [blame] | 420 | adr_l x8, vectors // load VBAR_EL1 with virtual |
| 421 | msr vbar_el1, x8 // vector table address |
| 422 | isb |
| 423 | |
Ard Biesheuvel | 60699ba | 2016-08-31 12:05:16 +0100 | [diff] [blame] | 424 | stp xzr, x30, [sp, #-16]! |
| 425 | mov x29, sp |
| 426 | |
Ard Biesheuvel | b929fe3 | 2016-08-31 12:05:15 +0100 | [diff] [blame] | 427 | str_l x21, __fdt_pointer, x5 // Save FDT pointer |
| 428 | |
| 429 | ldr_l x4, kimage_vaddr // Save the offset between |
| 430 | sub x4, x4, x0 // the kernel virtual and |
| 431 | str_l x4, kimage_voffset, x5 // physical mappings |
| 432 | |
Mark Rutland | 2a803c4 | 2016-01-06 11:05:27 +0000 | [diff] [blame] | 433 | // Clear BSS |
| 434 | adr_l x0, __bss_start |
| 435 | mov x1, xzr |
| 436 | adr_l x2, __bss_stop |
| 437 | sub x2, x2, x0 |
| 438 | bl __pi_memset |
Mark Rutland | 5227cfa | 2016-01-25 11:44:57 +0000 | [diff] [blame] | 439 | dsb ishst // Make zero page visible to PTW |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 440 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 441 | #ifdef CONFIG_KASAN |
| 442 | bl kasan_early_init |
| 443 | #endif |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 444 | #ifdef CONFIG_RANDOMIZE_BASE |
Ard Biesheuvel | 08cdac6 | 2016-04-18 17:09:47 +0200 | [diff] [blame] | 445 | tst x23, ~(MIN_KIMG_ALIGN - 1) // already running randomized? |
| 446 | b.ne 0f |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 447 | mov x0, x21 // pass FDT address in x0 |
| 448 | bl kaslr_early_init // parse FDT for KASLR options |
| 449 | cbz x0, 0f // KASLR disabled? just proceed |
Ard Biesheuvel | 08cdac6 | 2016-04-18 17:09:47 +0200 | [diff] [blame] | 450 | orr x23, x23, x0 // record KASLR offset |
Ard Biesheuvel | 60699ba | 2016-08-31 12:05:16 +0100 | [diff] [blame] | 451 | ldp x29, x30, [sp], #16 // we must enable KASLR, return |
| 452 | ret // to __primary_switch() |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 453 | 0: |
| 454 | #endif |
Ard Biesheuvel | 7326749 | 2017-07-22 18:45:33 +0100 | [diff] [blame] | 455 | add sp, sp, #16 |
| 456 | mov x29, #0 |
| 457 | mov x30, #0 |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 458 | b start_kernel |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 459 | SYM_FUNC_END(__primary_switched) |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 460 | |
| 461 | /* |
| 462 | * end early head section, begin head code that is also used for |
| 463 | * hotplug and needs to have the same protections as the text region |
| 464 | */ |
Will Deacon | 439e70e | 2018-01-29 12:00:00 +0000 | [diff] [blame] | 465 | .section ".idmap.text","awx" |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 466 | |
Mark Brown | a5d4420 | 2020-02-18 19:58:35 +0000 | [diff] [blame^] | 467 | SYM_DATA_START(kimage_vaddr) |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 468 | .quad _text - TEXT_OFFSET |
Mark Brown | a5d4420 | 2020-02-18 19:58:35 +0000 | [diff] [blame^] | 469 | SYM_DATA_END(kimage_vaddr) |
Will Deacon | b89d82e | 2019-01-08 16:19:01 +0000 | [diff] [blame] | 470 | EXPORT_SYMBOL(kimage_vaddr) |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame] | 471 | |
Laura Abbott | 034edab | 2014-11-21 13:50:41 -0800 | [diff] [blame] | 472 | /* |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 473 | * If we're fortunate enough to boot at EL2, ensure that the world is |
| 474 | * sane before dropping to EL1. |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 475 | * |
Mark Rutland | 510224c | 2017-01-09 14:31:55 +0000 | [diff] [blame] | 476 | * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in w0 if |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 477 | * booted in EL1 or EL2 respectively. |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 478 | */ |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 479 | SYM_FUNC_START(el2_setup) |
Marc Zyngier | 5371513 | 2017-09-26 15:57:16 +0100 | [diff] [blame] | 480 | msr SPsel, #1 // We want to use SP_EL{1,2} |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 481 | mrs x0, CurrentEL |
Marc Zyngier | 974c8e4 | 2014-06-06 14:16:21 +0100 | [diff] [blame] | 482 | cmp x0, #CurrentEL_EL2 |
Mark Rutland | 3ad47d0 | 2017-02-15 14:54:16 +0000 | [diff] [blame] | 483 | b.eq 1f |
James Morse | 7a00d68 | 2018-01-15 19:38:55 +0000 | [diff] [blame] | 484 | mov_q x0, (SCTLR_EL1_RES1 | ENDIAN_SET_EL1) |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 485 | msr sctlr_el1, x0 |
Ard Biesheuvel | 23c8a50 | 2016-08-31 12:05:12 +0100 | [diff] [blame] | 486 | mov w0, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1 |
Matthew Leach | 9cf7172 | 2013-10-11 14:52:17 +0100 | [diff] [blame] | 487 | isb |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 488 | ret |
| 489 | |
James Morse | 7a00d68 | 2018-01-15 19:38:55 +0000 | [diff] [blame] | 490 | 1: mov_q x0, (SCTLR_EL2_RES1 | ENDIAN_SET_EL2) |
Mark Rutland | 3ad47d0 | 2017-02-15 14:54:16 +0000 | [diff] [blame] | 491 | msr sctlr_el2, x0 |
| 492 | |
Marc Zyngier | 1f364c8 | 2014-02-19 09:33:14 +0000 | [diff] [blame] | 493 | #ifdef CONFIG_ARM64_VHE |
| 494 | /* |
| 495 | * Check for VHE being present. For the rest of the EL2 setup, |
| 496 | * x2 being non-zero indicates that we do have VHE, and that the |
| 497 | * kernel is intended to run at EL2. |
| 498 | */ |
| 499 | mrs x2, id_aa64mmfr1_el1 |
Alexandru Elisei | f6e5643 | 2019-04-05 11:20:12 +0100 | [diff] [blame] | 500 | ubfx x2, x2, #ID_AA64MMFR1_VHE_SHIFT, #4 |
Marc Zyngier | 1f364c8 | 2014-02-19 09:33:14 +0000 | [diff] [blame] | 501 | #else |
| 502 | mov x2, xzr |
| 503 | #endif |
| 504 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 505 | /* Hyp configuration. */ |
Mark Rutland | 4eaed6a | 2018-12-07 18:39:21 +0000 | [diff] [blame] | 506 | mov_q x0, HCR_HOST_NVHE_FLAGS |
Marc Zyngier | 1f364c8 | 2014-02-19 09:33:14 +0000 | [diff] [blame] | 507 | cbz x2, set_hcr |
Mark Rutland | 4eaed6a | 2018-12-07 18:39:21 +0000 | [diff] [blame] | 508 | mov_q x0, HCR_HOST_VHE_FLAGS |
Marc Zyngier | 1f364c8 | 2014-02-19 09:33:14 +0000 | [diff] [blame] | 509 | set_hcr: |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 510 | msr hcr_el2, x0 |
Marc Zyngier | 1f364c8 | 2014-02-19 09:33:14 +0000 | [diff] [blame] | 511 | isb |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 512 | |
Jintack | 1650ac4 | 2016-11-28 21:13:02 -0500 | [diff] [blame] | 513 | /* |
| 514 | * Allow Non-secure EL1 and EL0 to access physical timer and counter. |
| 515 | * This is not necessary for VHE, since the host kernel runs in EL2, |
| 516 | * and EL0 accesses are configured in the later stage of boot process. |
| 517 | * Note that when HCR_EL2.E2H == 1, CNTHCTL_EL2 has the same bit layout |
| 518 | * as CNTKCTL_EL1, and CNTKCTL_EL1 accessing instructions are redefined |
| 519 | * to access CNTHCTL_EL2. This allows the kernel designed to run at EL1 |
| 520 | * to transparently mess with the EL0 bits via CNTKCTL_EL1 access in |
| 521 | * EL2. |
| 522 | */ |
| 523 | cbnz x2, 1f |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 524 | mrs x0, cnthctl_el2 |
| 525 | orr x0, x0, #3 // Enable EL1 physical timers |
| 526 | msr cnthctl_el2, x0 |
Jintack | 1650ac4 | 2016-11-28 21:13:02 -0500 | [diff] [blame] | 527 | 1: |
Will Deacon | 1f75ff0 | 2012-11-29 22:48:31 +0000 | [diff] [blame] | 528 | msr cntvoff_el2, xzr // Clear virtual offset |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 529 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 530 | #ifdef CONFIG_ARM_GIC_V3 |
| 531 | /* GICv3 system register access */ |
| 532 | mrs x0, id_aa64pfr0_el1 |
Alexandru Elisei | f6e5643 | 2019-04-05 11:20:12 +0100 | [diff] [blame] | 533 | ubfx x0, x0, #ID_AA64PFR0_GIC_SHIFT, #4 |
Vladimir Murzin | 74698f6 | 2019-02-20 11:43:05 +0000 | [diff] [blame] | 534 | cbz x0, 3f |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 535 | |
Mark Rutland | 0e9884f | 2017-01-19 17:57:43 +0000 | [diff] [blame] | 536 | mrs_s x0, SYS_ICC_SRE_EL2 |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 537 | orr x0, x0, #ICC_SRE_EL2_SRE // Set ICC_SRE_EL2.SRE==1 |
| 538 | orr x0, x0, #ICC_SRE_EL2_ENABLE // Set ICC_SRE_EL2.Enable==1 |
Mark Rutland | 0e9884f | 2017-01-19 17:57:43 +0000 | [diff] [blame] | 539 | msr_s SYS_ICC_SRE_EL2, x0 |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 540 | isb // Make sure SRE is now set |
Mark Rutland | 0e9884f | 2017-01-19 17:57:43 +0000 | [diff] [blame] | 541 | mrs_s x0, SYS_ICC_SRE_EL2 // Read SRE back, |
Marc Zyngier | d271976 | 2015-09-30 11:39:59 +0100 | [diff] [blame] | 542 | tbz x0, #0, 3f // and check that it sticks |
Mark Rutland | 0e9884f | 2017-01-19 17:57:43 +0000 | [diff] [blame] | 543 | msr_s SYS_ICH_HCR_EL2, xzr // Reset ICC_HCR_EL2 to defaults |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 544 | |
| 545 | 3: |
| 546 | #endif |
| 547 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 548 | /* Populate ID registers. */ |
| 549 | mrs x0, midr_el1 |
| 550 | mrs x1, mpidr_el1 |
| 551 | msr vpidr_el2, x0 |
| 552 | msr vmpidr_el2, x1 |
| 553 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 554 | #ifdef CONFIG_COMPAT |
| 555 | msr hstr_el2, xzr // Disable CP15 traps to EL2 |
| 556 | #endif |
| 557 | |
Will Deacon | d10bcd4 | 2015-09-02 18:49:28 +0100 | [diff] [blame] | 558 | /* EL2 debug */ |
Alexandru Elisei | f6e5643 | 2019-04-05 11:20:12 +0100 | [diff] [blame] | 559 | mrs x1, id_aa64dfr0_el1 |
| 560 | sbfx x0, x1, #ID_AA64DFR0_PMUVER_SHIFT, #4 |
Lorenzo Pieralisi | f436b2a | 2016-01-13 14:50:03 +0000 | [diff] [blame] | 561 | cmp x0, #1 |
| 562 | b.lt 4f // Skip if no PMU present |
Will Deacon | d10bcd4 | 2015-09-02 18:49:28 +0100 | [diff] [blame] | 563 | mrs x0, pmcr_el0 // Disable debug access traps |
| 564 | ubfx x0, x0, #11, #5 // to EL2 and allow access to |
Lorenzo Pieralisi | f436b2a | 2016-01-13 14:50:03 +0000 | [diff] [blame] | 565 | 4: |
Will Deacon | 2bf47e1 | 2016-09-22 11:25:25 +0100 | [diff] [blame] | 566 | csel x3, xzr, x0, lt // all PMU counters from EL1 |
| 567 | |
| 568 | /* Statistical profiling */ |
Alexandru Elisei | f6e5643 | 2019-04-05 11:20:12 +0100 | [diff] [blame] | 569 | ubfx x0, x1, #ID_AA64DFR0_PMSVER_SHIFT, #4 |
Will Deacon | b0c57e1 | 2017-07-07 13:47:02 +0100 | [diff] [blame] | 570 | cbz x0, 7f // Skip if SPE not present |
| 571 | cbnz x2, 6f // VHE? |
| 572 | mrs_s x4, SYS_PMBIDR_EL1 // If SPE available at EL2, |
| 573 | and x4, x4, #(1 << SYS_PMBIDR_EL1_P_SHIFT) |
| 574 | cbnz x4, 5f // then permit sampling of physical |
| 575 | mov x4, #(1 << SYS_PMSCR_EL2_PCT_SHIFT | \ |
| 576 | 1 << SYS_PMSCR_EL2_PA_SHIFT) |
| 577 | msr_s SYS_PMSCR_EL2, x4 // addresses and physical counter |
| 578 | 5: |
Will Deacon | 2bf47e1 | 2016-09-22 11:25:25 +0100 | [diff] [blame] | 579 | mov x1, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT) |
| 580 | orr x3, x3, x1 // If we don't have VHE, then |
Will Deacon | b0c57e1 | 2017-07-07 13:47:02 +0100 | [diff] [blame] | 581 | b 7f // use EL1&0 translation. |
| 582 | 6: // For VHE, use EL2 translation |
Will Deacon | 2bf47e1 | 2016-09-22 11:25:25 +0100 | [diff] [blame] | 583 | orr x3, x3, #MDCR_EL2_TPMS // and disable access from EL1 |
Will Deacon | b0c57e1 | 2017-07-07 13:47:02 +0100 | [diff] [blame] | 584 | 7: |
Will Deacon | 2bf47e1 | 2016-09-22 11:25:25 +0100 | [diff] [blame] | 585 | msr mdcr_el2, x3 // Configure debug traps |
Will Deacon | d10bcd4 | 2015-09-02 18:49:28 +0100 | [diff] [blame] | 586 | |
Mark Rutland | cc33c4e | 2018-02-13 13:39:23 +0000 | [diff] [blame] | 587 | /* LORegions */ |
| 588 | mrs x1, id_aa64mmfr1_el1 |
| 589 | ubfx x0, x1, #ID_AA64MMFR1_LOR_SHIFT, 4 |
| 590 | cbz x0, 1f |
| 591 | msr_s SYS_LORC_EL1, xzr |
| 592 | 1: |
| 593 | |
Marc Zyngier | 7dbfbe5 | 2012-11-06 19:27:59 +0000 | [diff] [blame] | 594 | /* Stage-2 translation */ |
| 595 | msr vttbr_el2, xzr |
| 596 | |
Marc Zyngier | 1f364c8 | 2014-02-19 09:33:14 +0000 | [diff] [blame] | 597 | cbz x2, install_el2_stub |
| 598 | |
Ard Biesheuvel | 23c8a50 | 2016-08-31 12:05:12 +0100 | [diff] [blame] | 599 | mov w0, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2 |
Marc Zyngier | 1f364c8 | 2014-02-19 09:33:14 +0000 | [diff] [blame] | 600 | isb |
| 601 | ret |
| 602 | |
| 603 | install_el2_stub: |
Mark Rutland | d61c97a | 2017-02-15 14:54:17 +0000 | [diff] [blame] | 604 | /* |
| 605 | * When VHE is not in use, early init of EL2 and EL1 needs to be |
| 606 | * done here. |
| 607 | * When VHE _is_ in use, EL1 will not be used in the host and |
| 608 | * requires no configuration, and all non-hyp-specific EL2 setup |
| 609 | * will be done via the _EL1 system register aliases in __cpu_setup. |
| 610 | */ |
James Morse | 7a00d68 | 2018-01-15 19:38:55 +0000 | [diff] [blame] | 611 | mov_q x0, (SCTLR_EL1_RES1 | ENDIAN_SET_EL1) |
Mark Rutland | d61c97a | 2017-02-15 14:54:17 +0000 | [diff] [blame] | 612 | msr sctlr_el1, x0 |
| 613 | |
| 614 | /* Coprocessor traps. */ |
| 615 | mov x0, #0x33ff |
| 616 | msr cptr_el2, x0 // Disable copro. traps to EL2 |
| 617 | |
Dave Martin | 22043a3 | 2017-10-31 15:51:04 +0000 | [diff] [blame] | 618 | /* SVE register access */ |
| 619 | mrs x1, id_aa64pfr0_el1 |
| 620 | ubfx x1, x1, #ID_AA64PFR0_SVE_SHIFT, #4 |
| 621 | cbz x1, 7f |
| 622 | |
| 623 | bic x0, x0, #CPTR_EL2_TZ // Also disable SVE traps |
| 624 | msr cptr_el2, x0 // Disable copro. traps to EL2 |
| 625 | isb |
| 626 | mov x1, #ZCR_ELx_LEN_MASK // SVE: Enable full vector |
| 627 | msr_s SYS_ZCR_EL2, x1 // length for EL1. |
| 628 | |
Marc Zyngier | 712c6ff | 2012-10-19 17:46:27 +0100 | [diff] [blame] | 629 | /* Hypervisor stub */ |
Dave Martin | 22043a3 | 2017-10-31 15:51:04 +0000 | [diff] [blame] | 630 | 7: adr_l x0, __hyp_stub_vectors |
Marc Zyngier | 712c6ff | 2012-10-19 17:46:27 +0100 | [diff] [blame] | 631 | msr vbar_el2, x0 |
| 632 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 633 | /* spsr */ |
| 634 | mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\ |
| 635 | PSR_MODE_EL1h) |
| 636 | msr spsr_el2, x0 |
| 637 | msr elr_el2, lr |
Ard Biesheuvel | 23c8a50 | 2016-08-31 12:05:12 +0100 | [diff] [blame] | 638 | mov w0, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 639 | eret |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 640 | SYM_FUNC_END(el2_setup) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 641 | |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 642 | /* |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 643 | * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed |
Mark Rutland | 510224c | 2017-01-09 14:31:55 +0000 | [diff] [blame] | 644 | * in w0. See arch/arm64/include/asm/virt.h for more info. |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 645 | */ |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 646 | SYM_FUNC_START_LOCAL(set_cpu_boot_mode_flag) |
Ard Biesheuvel | 6f4d57f | 2015-03-17 09:14:29 +0100 | [diff] [blame] | 647 | adr_l x1, __boot_cpu_mode |
Ard Biesheuvel | 23c8a50 | 2016-08-31 12:05:12 +0100 | [diff] [blame] | 648 | cmp w0, #BOOT_CPU_MODE_EL2 |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 649 | b.ne 1f |
| 650 | add x1, x1, #4 |
Ard Biesheuvel | 23c8a50 | 2016-08-31 12:05:12 +0100 | [diff] [blame] | 651 | 1: str w0, [x1] // This CPU has booted in EL1 |
Will Deacon | d048859 | 2014-05-02 16:24:13 +0100 | [diff] [blame] | 652 | dmb sy |
| 653 | dc ivac, x1 // Invalidate potentially stale cache line |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 654 | ret |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 655 | SYM_FUNC_END(set_cpu_boot_mode_flag) |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 656 | |
| 657 | /* |
James Morse | b611303 | 2016-08-24 18:27:29 +0100 | [diff] [blame] | 658 | * These values are written with the MMU off, but read with the MMU on. |
| 659 | * Writers will invalidate the corresponding address, discarding up to a |
| 660 | * 'Cache Writeback Granule' (CWG) worth of data. The linker script ensures |
| 661 | * sufficient alignment that the CWG doesn't overlap another section. |
| 662 | */ |
| 663 | .pushsection ".mmuoff.data.write", "aw" |
| 664 | /* |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 665 | * We need to find out the CPU boot mode long after boot, so we need to |
| 666 | * store it in a writable variable. |
| 667 | * |
| 668 | * This is not in .bss, because we set it sufficiently early that the boot-time |
| 669 | * zeroing of .bss would clobber it. |
| 670 | */ |
Mark Brown | a5d4420 | 2020-02-18 19:58:35 +0000 | [diff] [blame^] | 671 | SYM_DATA_START(__boot_cpu_mode) |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 672 | .long BOOT_CPU_MODE_EL2 |
Mark Rutland | 424a383 | 2015-03-13 16:14:36 +0000 | [diff] [blame] | 673 | .long BOOT_CPU_MODE_EL1 |
Mark Brown | a5d4420 | 2020-02-18 19:58:35 +0000 | [diff] [blame^] | 674 | SYM_DATA_END(__boot_cpu_mode) |
James Morse | b611303 | 2016-08-24 18:27:29 +0100 | [diff] [blame] | 675 | /* |
| 676 | * The booting CPU updates the failed status @__early_cpu_boot_status, |
| 677 | * with MMU turned off. |
| 678 | */ |
Mark Brown | a5d4420 | 2020-02-18 19:58:35 +0000 | [diff] [blame^] | 679 | SYM_DATA_START(__early_cpu_boot_status) |
Arun KS | 61cf61d | 2019-04-30 16:05:04 +0530 | [diff] [blame] | 680 | .quad 0 |
Mark Brown | a5d4420 | 2020-02-18 19:58:35 +0000 | [diff] [blame^] | 681 | SYM_DATA_END(__early_cpu_boot_status) |
James Morse | b611303 | 2016-08-24 18:27:29 +0100 | [diff] [blame] | 682 | |
Marc Zyngier | f35a920 | 2012-10-26 15:40:05 +0100 | [diff] [blame] | 683 | .popsection |
| 684 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 685 | /* |
| 686 | * This provides a "holding pen" for platforms to hold all secondary |
| 687 | * cores are held until we're ready for them to initialise. |
| 688 | */ |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 689 | SYM_FUNC_START(secondary_holding_pen) |
Ard Biesheuvel | 23c8a50 | 2016-08-31 12:05:12 +0100 | [diff] [blame] | 690 | bl el2_setup // Drop to EL1, w0=cpu_boot_mode |
Matthew Leach | 828e983 | 2013-10-11 14:52:16 +0100 | [diff] [blame] | 691 | bl set_cpu_boot_mode_flag |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 692 | mrs x0, mpidr_el1 |
Ard Biesheuvel | b03cc88 | 2016-04-18 17:09:45 +0200 | [diff] [blame] | 693 | mov_q x1, MPIDR_HWID_BITMASK |
Javi Merino | 0359b0e | 2012-08-29 18:32:18 +0100 | [diff] [blame] | 694 | and x0, x0, x1 |
Ard Biesheuvel | b1c9829 | 2015-03-10 15:00:03 +0100 | [diff] [blame] | 695 | adr_l x3, secondary_holding_pen_release |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 696 | pen: ldr x4, [x3] |
| 697 | cmp x4, x0 |
| 698 | b.eq secondary_startup |
| 699 | wfe |
| 700 | b pen |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 701 | SYM_FUNC_END(secondary_holding_pen) |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 702 | |
| 703 | /* |
| 704 | * Secondary entry point that jumps straight into the kernel. Only to |
| 705 | * be used where CPUs are brought online dynamically by the kernel. |
| 706 | */ |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 707 | SYM_FUNC_START(secondary_entry) |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 708 | bl el2_setup // Drop to EL1 |
Lorenzo Pieralisi | 85cc00e | 2013-11-18 18:56:42 +0000 | [diff] [blame] | 709 | bl set_cpu_boot_mode_flag |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 710 | b secondary_startup |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 711 | SYM_FUNC_END(secondary_entry) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 712 | |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 713 | SYM_FUNC_START_LOCAL(secondary_startup) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 714 | /* |
| 715 | * Common entry point for secondary CPUs. |
| 716 | */ |
Steve Capper | a96a33b | 2018-12-06 22:50:40 +0000 | [diff] [blame] | 717 | bl __cpu_secondary_check52bitva |
Marc Zyngier | a591ede | 2015-03-18 14:55:20 +0000 | [diff] [blame] | 718 | bl __cpu_setup // initialise processor |
Jun Yao | 693d563 | 2018-09-24 14:51:13 +0100 | [diff] [blame] | 719 | adrp x1, swapper_pg_dir |
Ard Biesheuvel | 9dcf791 | 2016-08-31 12:05:14 +0100 | [diff] [blame] | 720 | bl __enable_mmu |
| 721 | ldr x8, =__secondary_switched |
| 722 | br x8 |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 723 | SYM_FUNC_END(secondary_startup) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 724 | |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 725 | SYM_FUNC_START_LOCAL(__secondary_switched) |
Ard Biesheuvel | 2bf31a4 | 2015-12-26 12:46:40 +0100 | [diff] [blame] | 726 | adr_l x5, vectors |
| 727 | msr vbar_el1, x5 |
| 728 | isb |
| 729 | |
Suzuki K Poulose | bb90527 | 2016-02-23 10:31:42 +0000 | [diff] [blame] | 730 | adr_l x0, secondary_data |
Mark Rutland | c02433d | 2016-11-03 20:23:13 +0000 | [diff] [blame] | 731 | ldr x1, [x0, #CPU_BOOT_STACK] // get secondary_data.stack |
Will Deacon | 5b1cfe3 | 2019-08-27 14:36:38 +0100 | [diff] [blame] | 732 | cbz x1, __secondary_too_slow |
Mark Rutland | c02433d | 2016-11-03 20:23:13 +0000 | [diff] [blame] | 733 | mov sp, x1 |
| 734 | ldr x2, [x0, #CPU_BOOT_TASK] |
Will Deacon | 5b1cfe3 | 2019-08-27 14:36:38 +0100 | [diff] [blame] | 735 | cbz x2, __secondary_too_slow |
Mark Rutland | c02433d | 2016-11-03 20:23:13 +0000 | [diff] [blame] | 736 | msr sp_el0, x2 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 737 | mov x29, #0 |
Ard Biesheuvel | 7326749 | 2017-07-22 18:45:33 +0100 | [diff] [blame] | 738 | mov x30, #0 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 739 | b secondary_start_kernel |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 740 | SYM_FUNC_END(__secondary_switched) |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 741 | |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 742 | SYM_FUNC_START_LOCAL(__secondary_too_slow) |
Will Deacon | 5b1cfe3 | 2019-08-27 14:36:38 +0100 | [diff] [blame] | 743 | wfe |
| 744 | wfi |
| 745 | b __secondary_too_slow |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 746 | SYM_FUNC_END(__secondary_too_slow) |
Will Deacon | 5b1cfe3 | 2019-08-27 14:36:38 +0100 | [diff] [blame] | 747 | |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 748 | /* |
Suzuki K Poulose | bb90527 | 2016-02-23 10:31:42 +0000 | [diff] [blame] | 749 | * The booting CPU updates the failed status @__early_cpu_boot_status, |
| 750 | * with MMU turned off. |
| 751 | * |
| 752 | * update_early_cpu_boot_status tmp, status |
| 753 | * - Corrupts tmp1, tmp2 |
| 754 | * - Writes 'status' to __early_cpu_boot_status and makes sure |
| 755 | * it is committed to memory. |
| 756 | */ |
| 757 | |
| 758 | .macro update_early_cpu_boot_status status, tmp1, tmp2 |
| 759 | mov \tmp2, #\status |
Ard Biesheuvel | adb4907 | 2016-04-15 12:11:21 +0200 | [diff] [blame] | 760 | adr_l \tmp1, __early_cpu_boot_status |
| 761 | str \tmp2, [\tmp1] |
Suzuki K Poulose | bb90527 | 2016-02-23 10:31:42 +0000 | [diff] [blame] | 762 | dmb sy |
| 763 | dc ivac, \tmp1 // Invalidate potentially stale cache line |
| 764 | .endm |
| 765 | |
Suzuki K Poulose | bb90527 | 2016-02-23 10:31:42 +0000 | [diff] [blame] | 766 | /* |
Ard Biesheuvel | 8b0a957 | 2015-03-17 08:59:53 +0100 | [diff] [blame] | 767 | * Enable the MMU. |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 768 | * |
Ard Biesheuvel | 8b0a957 | 2015-03-17 08:59:53 +0100 | [diff] [blame] | 769 | * x0 = SCTLR_EL1 value for turning on the MMU. |
Jun Yao | 693d563 | 2018-09-24 14:51:13 +0100 | [diff] [blame] | 770 | * x1 = TTBR1_EL1 value |
Ard Biesheuvel | 8b0a957 | 2015-03-17 08:59:53 +0100 | [diff] [blame] | 771 | * |
Ard Biesheuvel | 9dcf791 | 2016-08-31 12:05:14 +0100 | [diff] [blame] | 772 | * Returns to the caller via x30/lr. This requires the caller to be covered |
| 773 | * by the .idmap.text section. |
Suzuki K. Poulose | 4bf8b96 | 2015-10-19 14:19:35 +0100 | [diff] [blame] | 774 | * |
| 775 | * Checks if the selected granule size is supported by the CPU. |
| 776 | * If it isn't, park the CPU |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 777 | */ |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 778 | SYM_FUNC_START(__enable_mmu) |
Jun Yao | 693d563 | 2018-09-24 14:51:13 +0100 | [diff] [blame] | 779 | mrs x2, ID_AA64MMFR0_EL1 |
| 780 | ubfx x2, x2, #ID_AA64MMFR0_TGRAN_SHIFT, 4 |
Suzuki K. Poulose | 4bf8b96 | 2015-10-19 14:19:35 +0100 | [diff] [blame] | 781 | cmp x2, #ID_AA64MMFR0_TGRAN_SUPPORTED |
| 782 | b.ne __no_granule_support |
Jun Yao | 693d563 | 2018-09-24 14:51:13 +0100 | [diff] [blame] | 783 | update_early_cpu_boot_status 0, x2, x3 |
| 784 | adrp x2, idmap_pg_dir |
| 785 | phys_to_ttbr x1, x1 |
| 786 | phys_to_ttbr x2, x2 |
| 787 | msr ttbr0_el1, x2 // load TTBR0 |
Steve Capper | c812026 | 2019-08-07 16:55:19 +0100 | [diff] [blame] | 788 | offset_ttbr1 x1, x3 |
Jun Yao | 693d563 | 2018-09-24 14:51:13 +0100 | [diff] [blame] | 789 | msr ttbr1_el1, x1 // load TTBR1 |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 790 | isb |
Catalin Marinas | 9703d9d | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 791 | msr sctlr_el1, x0 |
| 792 | isb |
Will Deacon | 8ec4198 | 2015-08-04 17:49:36 +0100 | [diff] [blame] | 793 | /* |
| 794 | * Invalidate the local I-cache so that any instructions fetched |
| 795 | * speculatively from the PoC are discarded, since they may have |
| 796 | * been dynamically patched at the PoU. |
| 797 | */ |
| 798 | ic iallu |
| 799 | dsb nsh |
| 800 | isb |
Ard Biesheuvel | 9dcf791 | 2016-08-31 12:05:14 +0100 | [diff] [blame] | 801 | ret |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 802 | SYM_FUNC_END(__enable_mmu) |
Suzuki K. Poulose | 4bf8b96 | 2015-10-19 14:19:35 +0100 | [diff] [blame] | 803 | |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 804 | SYM_FUNC_START(__cpu_secondary_check52bitva) |
Steve Capper | b6d00d4 | 2019-08-07 16:55:22 +0100 | [diff] [blame] | 805 | #ifdef CONFIG_ARM64_VA_BITS_52 |
Steve Capper | 2c624fe | 2019-08-07 16:55:23 +0100 | [diff] [blame] | 806 | ldr_l x0, vabits_actual |
Steve Capper | a96a33b | 2018-12-06 22:50:40 +0000 | [diff] [blame] | 807 | cmp x0, #52 |
| 808 | b.ne 2f |
| 809 | |
| 810 | mrs_s x0, SYS_ID_AA64MMFR2_EL1 |
| 811 | and x0, x0, #(0xf << ID_AA64MMFR2_LVA_SHIFT) |
| 812 | cbnz x0, 2f |
| 813 | |
Will Deacon | 66f16a2 | 2018-12-10 14:21:13 +0000 | [diff] [blame] | 814 | update_early_cpu_boot_status \ |
| 815 | CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_52_BIT_VA, x0, x1 |
Steve Capper | a96a33b | 2018-12-06 22:50:40 +0000 | [diff] [blame] | 816 | 1: wfe |
| 817 | wfi |
| 818 | b 1b |
| 819 | |
| 820 | #endif |
| 821 | 2: ret |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 822 | SYM_FUNC_END(__cpu_secondary_check52bitva) |
Steve Capper | a96a33b | 2018-12-06 22:50:40 +0000 | [diff] [blame] | 823 | |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 824 | SYM_FUNC_START_LOCAL(__no_granule_support) |
Suzuki K Poulose | bb90527 | 2016-02-23 10:31:42 +0000 | [diff] [blame] | 825 | /* Indicate that this CPU can't boot and is stuck in the kernel */ |
Will Deacon | 66f16a2 | 2018-12-10 14:21:13 +0000 | [diff] [blame] | 826 | update_early_cpu_boot_status \ |
| 827 | CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_NO_GRAN, x1, x2 |
Suzuki K Poulose | bb90527 | 2016-02-23 10:31:42 +0000 | [diff] [blame] | 828 | 1: |
Suzuki K. Poulose | 4bf8b96 | 2015-10-19 14:19:35 +0100 | [diff] [blame] | 829 | wfe |
Suzuki K Poulose | bb90527 | 2016-02-23 10:31:42 +0000 | [diff] [blame] | 830 | wfi |
Ard Biesheuvel | 3c5e9f2 | 2016-08-31 12:05:13 +0100 | [diff] [blame] | 831 | b 1b |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 832 | SYM_FUNC_END(__no_granule_support) |
Ard Biesheuvel | e5ebeec | 2016-04-18 17:09:42 +0200 | [diff] [blame] | 833 | |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 834 | #ifdef CONFIG_RELOCATABLE |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 835 | SYM_FUNC_START_LOCAL(__relocate_kernel) |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 836 | /* |
| 837 | * Iterate over each entry in the relocation table, and apply the |
| 838 | * relocations in place. |
| 839 | */ |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 840 | ldr w9, =__rela_offset // offset to reloc table |
| 841 | ldr w10, =__rela_size // size of reloc table |
| 842 | |
Ard Biesheuvel | b03cc88 | 2016-04-18 17:09:45 +0200 | [diff] [blame] | 843 | mov_q x11, KIMAGE_VADDR // default virtual offset |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 844 | add x11, x11, x23 // actual virtual offset |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 845 | add x9, x9, x11 // __va(.rela) |
| 846 | add x10, x9, x10 // __va(.rela) + sizeof(.rela) |
| 847 | |
| 848 | 0: cmp x9, x10 |
Ard Biesheuvel | 08cc55b | 2016-07-24 14:00:13 +0200 | [diff] [blame] | 849 | b.hs 1f |
Peter Collingbourne | 5cf896f | 2019-07-31 18:18:42 -0700 | [diff] [blame] | 850 | ldp x12, x13, [x9], #24 |
| 851 | ldr x14, [x9, #-8] |
| 852 | cmp w13, #R_AARCH64_RELATIVE |
Ard Biesheuvel | 08cc55b | 2016-07-24 14:00:13 +0200 | [diff] [blame] | 853 | b.ne 0b |
Peter Collingbourne | 5cf896f | 2019-07-31 18:18:42 -0700 | [diff] [blame] | 854 | add x14, x14, x23 // relocate |
| 855 | str x14, [x12, x23] |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 856 | b 0b |
Peter Collingbourne | 5cf896f | 2019-07-31 18:18:42 -0700 | [diff] [blame] | 857 | |
| 858 | 1: |
| 859 | #ifdef CONFIG_RELR |
| 860 | /* |
| 861 | * Apply RELR relocations. |
| 862 | * |
| 863 | * RELR is a compressed format for storing relative relocations. The |
| 864 | * encoded sequence of entries looks like: |
| 865 | * [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ] |
| 866 | * |
| 867 | * i.e. start with an address, followed by any number of bitmaps. The |
| 868 | * address entry encodes 1 relocation. The subsequent bitmap entries |
| 869 | * encode up to 63 relocations each, at subsequent offsets following |
| 870 | * the last address entry. |
| 871 | * |
| 872 | * The bitmap entries must have 1 in the least significant bit. The |
| 873 | * assumption here is that an address cannot have 1 in lsb. Odd |
| 874 | * addresses are not supported. Any odd addresses are stored in the RELA |
| 875 | * section, which is handled above. |
| 876 | * |
| 877 | * Excluding the least significant bit in the bitmap, each non-zero |
| 878 | * bit in the bitmap represents a relocation to be applied to |
| 879 | * a corresponding machine word that follows the base address |
| 880 | * word. The second least significant bit represents the machine |
| 881 | * word immediately following the initial address, and each bit |
| 882 | * that follows represents the next word, in linear order. As such, |
| 883 | * a single bitmap can encode up to 63 relocations in a 64-bit object. |
| 884 | * |
| 885 | * In this implementation we store the address of the next RELR table |
| 886 | * entry in x9, the address being relocated by the current address or |
| 887 | * bitmap entry in x13 and the address being relocated by the current |
| 888 | * bit in x14. |
| 889 | * |
| 890 | * Because addends are stored in place in the binary, RELR relocations |
| 891 | * cannot be applied idempotently. We use x24 to keep track of the |
| 892 | * currently applied displacement so that we can correctly relocate if |
| 893 | * __relocate_kernel is called twice with non-zero displacements (i.e. |
| 894 | * if there is both a physical misalignment and a KASLR displacement). |
| 895 | */ |
| 896 | ldr w9, =__relr_offset // offset to reloc table |
| 897 | ldr w10, =__relr_size // size of reloc table |
| 898 | add x9, x9, x11 // __va(.relr) |
| 899 | add x10, x9, x10 // __va(.relr) + sizeof(.relr) |
| 900 | |
| 901 | sub x15, x23, x24 // delta from previous offset |
| 902 | cbz x15, 7f // nothing to do if unchanged |
| 903 | mov x24, x23 // save new offset |
| 904 | |
| 905 | 2: cmp x9, x10 |
| 906 | b.hs 7f |
| 907 | ldr x11, [x9], #8 |
| 908 | tbnz x11, #0, 3f // branch to handle bitmaps |
| 909 | add x13, x11, x23 |
| 910 | ldr x12, [x13] // relocate address entry |
| 911 | add x12, x12, x15 |
| 912 | str x12, [x13], #8 // adjust to start of bitmap |
| 913 | b 2b |
| 914 | |
| 915 | 3: mov x14, x13 |
| 916 | 4: lsr x11, x11, #1 |
| 917 | cbz x11, 6f |
| 918 | tbz x11, #0, 5f // skip bit if not set |
| 919 | ldr x12, [x14] // relocate bit |
| 920 | add x12, x12, x15 |
| 921 | str x12, [x14] |
| 922 | |
| 923 | 5: add x14, x14, #8 // move to next bit's address |
| 924 | b 4b |
| 925 | |
| 926 | 6: /* |
| 927 | * Move to the next bitmap's address. 8 is the word size, and 63 is the |
| 928 | * number of significant bits in a bitmap entry. |
| 929 | */ |
| 930 | add x13, x13, #(8 * 63) |
| 931 | b 2b |
| 932 | |
| 933 | 7: |
| 934 | #endif |
| 935 | ret |
| 936 | |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 937 | SYM_FUNC_END(__relocate_kernel) |
Ard Biesheuvel | 3c5e9f2 | 2016-08-31 12:05:13 +0100 | [diff] [blame] | 938 | #endif |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 939 | |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 940 | SYM_FUNC_START_LOCAL(__primary_switch) |
Ard Biesheuvel | 3c5e9f2 | 2016-08-31 12:05:13 +0100 | [diff] [blame] | 941 | #ifdef CONFIG_RANDOMIZE_BASE |
| 942 | mov x19, x0 // preserve new SCTLR_EL1 value |
| 943 | mrs x20, sctlr_el1 // preserve old SCTLR_EL1 value |
| 944 | #endif |
| 945 | |
Jun Yao | 2b5548b | 2018-09-24 15:47:49 +0100 | [diff] [blame] | 946 | adrp x1, init_pg_dir |
Ard Biesheuvel | 9dcf791 | 2016-08-31 12:05:14 +0100 | [diff] [blame] | 947 | bl __enable_mmu |
Ard Biesheuvel | 3c5e9f2 | 2016-08-31 12:05:13 +0100 | [diff] [blame] | 948 | #ifdef CONFIG_RELOCATABLE |
Peter Collingbourne | 5cf896f | 2019-07-31 18:18:42 -0700 | [diff] [blame] | 949 | #ifdef CONFIG_RELR |
| 950 | mov x24, #0 // no RELR displacement yet |
| 951 | #endif |
Ard Biesheuvel | 3c5e9f2 | 2016-08-31 12:05:13 +0100 | [diff] [blame] | 952 | bl __relocate_kernel |
| 953 | #ifdef CONFIG_RANDOMIZE_BASE |
| 954 | ldr x8, =__primary_switched |
Ard Biesheuvel | b929fe3 | 2016-08-31 12:05:15 +0100 | [diff] [blame] | 955 | adrp x0, __PHYS_OFFSET |
Ard Biesheuvel | 3c5e9f2 | 2016-08-31 12:05:13 +0100 | [diff] [blame] | 956 | blr x8 |
| 957 | |
| 958 | /* |
| 959 | * If we return here, we have a KASLR displacement in x23 which we need |
| 960 | * to take into account by discarding the current kernel mapping and |
| 961 | * creating a new one. |
| 962 | */ |
Shanker Donthineni | 3060e9f | 2018-01-29 11:59:52 +0000 | [diff] [blame] | 963 | pre_disable_mmu_workaround |
Ard Biesheuvel | 3c5e9f2 | 2016-08-31 12:05:13 +0100 | [diff] [blame] | 964 | msr sctlr_el1, x20 // disable the MMU |
| 965 | isb |
| 966 | bl __create_page_tables // recreate kernel mapping |
| 967 | |
| 968 | tlbi vmalle1 // Remove any stale TLB entries |
| 969 | dsb nsh |
| 970 | |
| 971 | msr sctlr_el1, x19 // re-enable the MMU |
| 972 | isb |
| 973 | ic iallu // flush instructions fetched |
| 974 | dsb nsh // via old mapping |
| 975 | isb |
| 976 | |
| 977 | bl __relocate_kernel |
| 978 | #endif |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 979 | #endif |
| 980 | ldr x8, =__primary_switched |
Ard Biesheuvel | b929fe3 | 2016-08-31 12:05:15 +0100 | [diff] [blame] | 981 | adrp x0, __PHYS_OFFSET |
Ard Biesheuvel | 0cd3def | 2016-04-18 17:09:43 +0200 | [diff] [blame] | 982 | br x8 |
Mark Brown | c63d9f8 | 2020-02-18 19:58:33 +0000 | [diff] [blame] | 983 | SYM_FUNC_END(__primary_switch) |