blob: a06727354fad7209bd9dd925f790e75faf257195 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Catalin Marinas9703d9d2012-03-05 11:49:27 +00002/*
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 Marinas9703d9d2012-03-05 11:49:27 +000010 */
11
12#include <linux/linkage.h>
13#include <linux/init.h>
Marc Zyngier021f6532014-06-30 16:01:31 +010014#include <linux/irqchip/arm-gic-v3.h>
Catalin Marinas9703d9d2012-03-05 11:49:27 +000015
16#include <asm/assembler.h>
Ard Biesheuvel08cdac62016-04-18 17:09:47 +020017#include <asm/boot.h>
Catalin Marinas9703d9d2012-03-05 11:49:27 +000018#include <asm/ptrace.h>
19#include <asm/asm-offsets.h>
Catalin Marinasc218bca2014-03-26 18:25:55 +000020#include <asm/cache.h>
Javi Merino0359b0e2012-08-29 18:32:18 +010021#include <asm/cputype.h>
Ard Biesheuvel1e48ef72016-01-26 09:13:44 +010022#include <asm/elf.h>
AKASHI Takahirof56063c52018-11-15 14:52:46 +090023#include <asm/image.h>
Suzuki K. Poulose87d15872015-10-19 14:19:27 +010024#include <asm/kernel-pgtable.h>
Marc Zyngier1f364c82014-02-19 09:33:14 +000025#include <asm/kvm_arm.h>
Catalin Marinas9703d9d2012-03-05 11:49:27 +000026#include <asm/memory.h>
Catalin Marinas9703d9d2012-03-05 11:49:27 +000027#include <asm/pgtable-hwdef.h>
28#include <asm/pgtable.h>
29#include <asm/page.h>
Suzuki K Poulosebb905272016-02-23 10:31:42 +000030#include <asm/smp.h>
Suzuki K. Poulose4bf8b962015-10-19 14:19:35 +010031#include <asm/sysreg.h>
32#include <asm/thread_info.h>
Marc Zyngierf35a9202012-10-26 15:40:05 +010033#include <asm/virt.h>
Catalin Marinas9703d9d2012-03-05 11:49:27 +000034
Ard Biesheuvelb5f4a212017-03-23 19:00:46 +000035#include "efi-header.S"
36
Ard Biesheuvel6f4d57f2015-03-17 09:14:29 +010037#define __PHYS_OFFSET (KERNEL_START - TEXT_OFFSET)
Catalin Marinas9703d9d2012-03-05 11:49:27 +000038
Ard Biesheuvel41903122014-08-13 18:53:03 +010039#if (TEXT_OFFSET & 0xfff) != 0
40#error TEXT_OFFSET must be at least 4KB aligned
41#elif (PAGE_OFFSET & 0x1fffff) != 0
Mark Rutlandda57a362014-06-24 16:51:37 +010042#error PAGE_OFFSET must be at least 2MB aligned
Ard Biesheuvel41903122014-08-13 18:53:03 +010043#elif TEXT_OFFSET > 0x1fffff
Mark Rutlandda57a362014-06-24 16:51:37 +010044#error TEXT_OFFSET must be less than 2MB
Catalin Marinas9703d9d2012-03-05 11:49:27 +000045#endif
46
Catalin Marinas9703d9d2012-03-05 11:49:27 +000047/*
Catalin Marinas9703d9d2012-03-05 11:49:27 +000048 * 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 Biesheuvel2bf31a42015-12-26 12:46:40 +010063_head:
Catalin Marinas9703d9d2012-03-05 11:49:27 +000064 /*
65 * DO NOT MODIFY. Image header expected by Linux boot-loaders.
66 */
Mark Salter3c7f2552014-04-15 22:47:52 -040067#ifdef CONFIG_EFI
Mark Salter3c7f2552014-04-15 22:47:52 -040068 /*
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 Marinas9703d9d2012-03-05 11:49:27 +000075 b stext // branch to kernel start, magic
76 .long 0 // reserved
Mark Salter3c7f2552014-04-15 22:47:52 -040077#endif
Ard Biesheuvel6ad1fe52015-12-26 13:48:02 +010078 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 Franz4370eec2013-08-15 00:10:00 +010081 .quad 0 // reserved
82 .quad 0 // reserved
83 .quad 0 // reserved
AKASHI Takahirof56063c52018-11-15 14:52:46 +090084 .ascii ARM64_IMAGE_MAGIC // Magic number
Mark Salter3c7f2552014-04-15 22:47:52 -040085#ifdef CONFIG_EFI
Ard Biesheuvel2bf31a42015-12-26 12:46:40 +010086 .long pe_header - _head // Offset to the PE header.
Mark Salter3c7f2552014-04-15 22:47:52 -040087
Mark Salter3c7f2552014-04-15 22:47:52 -040088pe_header:
Ard Biesheuvelb5f4a212017-03-23 19:00:46 +000089 __EFI_PE_HEADER
Ard Biesheuvel99922252017-03-23 19:00:47 +000090#else
91 .long 0 // reserved
Mark Salter3c7f2552014-04-15 22:47:52 -040092#endif
Catalin Marinas9703d9d2012-03-05 11:49:27 +000093
Ard Biesheuvel546c8c42016-03-30 17:43:07 +020094 __INIT
95
Ard Biesheuvela9be2ee2016-08-31 12:05:17 +010096 /*
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 Collingbourne5cf896f2019-07-31 18:18:42 -0700105 * x24 __primary_switch() .. relocate_kernel()
106 * current RELR displacement
Ard Biesheuvela9be2ee2016-08-31 12:05:17 +0100107 */
Mark Brownebdf44a2020-02-18 19:58:34 +0000108SYM_CODE_START(stext)
Ard Biesheuvelda9c1772015-03-17 10:55:12 +0100109 bl preserve_boot_args
Ard Biesheuvel23c8a502016-08-31 12:05:12 +0100110 bl el2_setup // Drop to EL1, w0=cpu_boot_mode
Ard Biesheuvelb929fe32016-08-31 12:05:15 +0100111 adrp x23, __PHYS_OFFSET
112 and x23, x23, MIN_KIMG_ALIGN - 1 // KASLR offset, defaults to 0
Matthew Leach828e9832013-10-11 14:52:16 +0100113 bl set_cpu_boot_mode_flag
Ard Biesheuvelaea73ab2016-08-16 21:02:32 +0200114 bl __create_page_tables
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000115 /*
Marc Zyngiera591ede2015-03-18 14:55:20 +0000116 * The following calls CPU setup code, see arch/arm64/mm/proc.S for
117 * details.
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000118 * On return, the CPU will be ready for the MMU to be turned on and
119 * the TCR will have been set.
120 */
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200121 bl __cpu_setup // initialise processor
Ard Biesheuvel3c5e9f22016-08-31 12:05:13 +0100122 b __primary_switch
Mark Brownebdf44a2020-02-18 19:58:34 +0000123SYM_CODE_END(stext)
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000124
125/*
Ard Biesheuvelda9c1772015-03-17 10:55:12 +0100126 * Preserve the arguments passed by the bootloader in x0 .. x3
127 */
Mark Brownebdf44a2020-02-18 19:58:34 +0000128SYM_CODE_START_LOCAL(preserve_boot_args)
Ard Biesheuvelda9c1772015-03-17 10:55:12 +0100129 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 Murphyd46befe2017-07-25 11:55:39 +0100138 mov x1, #0x20 // 4 x 8 bytes
139 b __inval_dcache_area // tail call
Mark Brownebdf44a2020-02-18 19:58:34 +0000140SYM_CODE_END(preserve_boot_args)
Ard Biesheuvelda9c1772015-03-17 10:55:12 +0100141
142/*
Laura Abbott034edab2014-11-21 13:50:41 -0800143 * 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 Martsenkofa2a8442017-12-13 17:07:24 +0000151 * Corrupts: ptrs, tmp1, tmp2
Laura Abbott034edab2014-11-21 13:50:41 -0800152 * Returns: tbl -> next level table page address
153 */
154 .macro create_table_entry, tbl, virt, shift, ptrs, tmp1, tmp2
Kristina Martsenkoe6d588a2017-12-13 17:07:19 +0000155 add \tmp1, \tbl, #PAGE_SIZE
Will Deacon79ddab32018-01-29 11:59:59 +0000156 phys_to_pte \tmp2, \tmp1
Kristina Martsenkoe6d588a2017-12-13 17:07:19 +0000157 orr \tmp2, \tmp2, #PMD_TYPE_TABLE // address of next table and entry type
Laura Abbott034edab2014-11-21 13:50:41 -0800158 lsr \tmp1, \virt, #\shift
Kristina Martsenkofa2a8442017-12-13 17:07:24 +0000159 sub \ptrs, \ptrs, #1
160 and \tmp1, \tmp1, \ptrs // table index
Laura Abbott034edab2014-11-21 13:50:41 -0800161 str \tmp2, [\tbl, \tmp1, lsl #3]
162 add \tbl, \tbl, #PAGE_SIZE // next level table page
163 .endm
164
165/*
Steve Capper0370b312018-01-11 10:11:59 +0000166 * 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 Abbott034edab2014-11-21 13:50:41 -0800168 *
Steve Capper0370b312018-01-11 10:11:59 +0000169 * 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 Abbott034edab2014-11-21 13:50:41 -0800180 */
Steve Capper0370b312018-01-11 10:11:59 +0000181 .macro populate_entries, tbl, rtbl, index, eindex, flags, inc, tmp1
Will Deacon79ddab32018-01-29 11:59:59 +0000182.Lpe\@: phys_to_pte \tmp1, \rtbl
Steve Capper0370b312018-01-11 10:11:59 +0000183 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 Abbott034edab2014-11-21 13:50:41 -0800189 .endm
190
191/*
Steve Capper0370b312018-01-11 10:11:59 +0000192 * 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 Abbott034edab2014-11-21 13:50:41 -0800195 *
Steve Capper0370b312018-01-11 10:11:59 +0000196 * 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 Abbott034edab2014-11-21 13:50:41 -0800208 */
Steve Capper0370b312018-01-11 10:11:59 +0000209 .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 Abbott034edab2014-11-21 13:50:41 -0800269 .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 Biesheuvel61bd93c2015-06-01 13:40:32 +0200276 * been enabled
Laura Abbott034edab2014-11-21 13:50:41 -0800277 */
Mark Brownc63d9f82020-02-18 19:58:33 +0000278SYM_FUNC_START_LOCAL(__create_page_tables)
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100279 mov x28, lr
Laura Abbott034edab2014-11-21 13:50:41 -0800280
281 /*
Jun Yao8eb7e282018-09-24 17:56:18 +0100282 * 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 Abbott034edab2014-11-21 13:50:41 -0800286 */
Jun Yao8eb7e282018-09-24 17:56:18 +0100287 adrp x0, init_pg_dir
Jun Yao2b5548b2018-09-24 15:47:49 +0100288 adrp x1, init_pg_end
Steve Capper0370b312018-01-11 10:11:59 +0000289 sub x1, x1, x0
Robin Murphyd46befe2017-07-25 11:55:39 +0100290 bl __inval_dcache_area
Laura Abbott034edab2014-11-21 13:50:41 -0800291
292 /*
Jun Yao8eb7e282018-09-24 17:56:18 +0100293 * Clear the init page tables.
Laura Abbott034edab2014-11-21 13:50:41 -0800294 */
Jun Yao8eb7e282018-09-24 17:56:18 +0100295 adrp x0, init_pg_dir
Jun Yao2b5548b2018-09-24 15:47:49 +0100296 adrp x1, init_pg_end
Steve Capper0370b312018-01-11 10:11:59 +0000297 sub x1, x1, x0
Laura Abbott034edab2014-11-21 13:50:41 -08002981: stp xzr, xzr, [x0], #16
299 stp xzr, xzr, [x0], #16
300 stp xzr, xzr, [x0], #16
301 stp xzr, xzr, [x0], #16
Robin Murphyd46befe2017-07-25 11:55:39 +0100302 subs x1, x1, #64
303 b.ne 1b
Laura Abbott034edab2014-11-21 13:50:41 -0800304
Ard Biesheuvelb03cc882016-04-18 17:09:45 +0200305 mov x7, SWAPPER_MM_MMUFLAGS
Laura Abbott034edab2014-11-21 13:50:41 -0800306
307 /*
308 * Create the identity mapping.
309 */
Ard Biesheuvelaea73ab2016-08-16 21:02:32 +0200310 adrp x0, idmap_pg_dir
Ard Biesheuvel5dfe9d72015-06-01 13:40:33 +0200311 adrp x3, __idmap_text_start // __pa(__idmap_text_start)
Ard Biesheuveldd006da2015-03-19 16:42:27 +0000312
Steve Capperb6d00d42019-08-07 16:55:22 +0100313#ifdef CONFIG_ARM64_VA_BITS_52
Steve Capper67e7fdf2018-12-06 22:50:41 +0000314 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 Capper90ec95c2019-08-07 16:55:17 +0100319 mov x5, #VA_BITS_MIN
Steve Capper67e7fdf2018-12-06 22:50:41 +00003201:
Steve Capper5383cc62019-08-07 16:55:18 +0100321 adr_l x6, vabits_actual
Steve Capper67e7fdf2018-12-06 22:50:41 +0000322 str x5, [x6]
323 dmb sy
324 dc ivac, x6 // Invalidate potentially stale cache line
325
Kristina Martsenkofa2a8442017-12-13 17:07:24 +0000326 /*
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 Biesheuveldd006da2015-03-19 16:42:27 +0000349#define EXTRA_SHIFT (PGDIR_SHIFT + PAGE_SHIFT - 3)
Kristina Martsenkofa2a8442017-12-13 17:07:24 +0000350#define EXTRA_PTRS (1 << (PHYS_MASK_SHIFT - EXTRA_SHIFT))
Ard Biesheuveldd006da2015-03-19 16:42:27 +0000351
352 /*
Kristina Martsenkofa2a8442017-12-13 17:07:24 +0000353 * If VA_BITS < 48, we have to configure an additional table level.
Ard Biesheuveldd006da2015-03-19 16:42:27 +0000354 * 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 Martsenkofa2a8442017-12-13 17:07:24 +0000363 mov x4, EXTRA_PTRS
364 create_table_entry x0, x3, EXTRA_SHIFT, x4, x5, x6
365#else
Ard Biesheuveldd006da2015-03-19 16:42:27 +0000366 /*
Kristina Martsenkofa2a8442017-12-13 17:07:24 +0000367 * If VA_BITS == 48, we don't have to configure an additional
368 * translation level, but the top-level table has more entries.
Ard Biesheuveldd006da2015-03-19 16:42:27 +0000369 */
Kristina Martsenkofa2a8442017-12-13 17:07:24 +0000370 mov x4, #1 << (PHYS_MASK_SHIFT - PGDIR_SHIFT)
371 str_l x4, idmap_ptrs_per_pgd, x5
Ard Biesheuveldd006da2015-03-19 16:42:27 +0000372#endif
Kristina Martsenkofa2a8442017-12-13 17:07:24 +00003731:
374 ldr_l x4, idmap_ptrs_per_pgd
Ard Biesheuvel5dfe9d72015-06-01 13:40:33 +0200375 mov x5, x3 // __pa(__idmap_text_start)
376 adr_l x6, __idmap_text_end // __pa(__idmap_text_end)
Steve Capper0370b312018-01-11 10:11:59 +0000377
378 map_memory x0, x1, x3, x6, x7, x3, x4, x10, x11, x12, x13, x14
Laura Abbott034edab2014-11-21 13:50:41 -0800379
380 /*
381 * Map the kernel image (starting with PHYS_OFFSET).
382 */
Jun Yao2b5548b2018-09-24 15:47:49 +0100383 adrp x0, init_pg_dir
Ard Biesheuvel18b9c0d2016-04-18 17:09:46 +0200384 mov_q x5, KIMAGE_VADDR + TEXT_OFFSET // compile time __va(_text)
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100385 add x5, x5, x23 // add KASLR displacement
Kristina Martsenkofa2a8442017-12-13 17:07:24 +0000386 mov x4, PTRS_PER_PGD
Ard Biesheuvel18b9c0d2016-04-18 17:09:46 +0200387 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 Capper0370b312018-01-11 10:11:59 +0000391
392 map_memory x0, x1, x5, x6, x7, x3, x4, x10, x11, x12, x13, x14
Laura Abbott034edab2014-11-21 13:50:41 -0800393
394 /*
Laura Abbott034edab2014-11-21 13:50:41 -0800395 * 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 Biesheuvelaea73ab2016-08-16 21:02:32 +0200399 adrp x0, idmap_pg_dir
Jun Yao2b5548b2018-09-24 15:47:49 +0100400 adrp x1, init_pg_end
Steve Capper0370b312018-01-11 10:11:59 +0000401 sub x1, x1, x0
Mark Rutland91d57152015-03-24 13:50:27 +0000402 dmb sy
Robin Murphyd46befe2017-07-25 11:55:39 +0100403 bl __inval_dcache_area
Laura Abbott034edab2014-11-21 13:50:41 -0800404
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100405 ret x28
Mark Brownc63d9f82020-02-18 19:58:33 +0000406SYM_FUNC_END(__create_page_tables)
Laura Abbott034edab2014-11-21 13:50:41 -0800407 .ltorg
408
Laura Abbott034edab2014-11-21 13:50:41 -0800409/*
Ard Biesheuvela871d352015-03-04 11:51:48 +0100410 * The following fragment of code is executed with the MMU enabled.
Ard Biesheuvelb929fe32016-08-31 12:05:15 +0100411 *
412 * x0 = __PHYS_OFFSET
Laura Abbott034edab2014-11-21 13:50:41 -0800413 */
Mark Brownc63d9f82020-02-18 19:58:33 +0000414SYM_FUNC_START_LOCAL(__primary_switched)
Ard Biesheuvel60699ba2016-08-31 12:05:16 +0100415 adrp x4, init_thread_union
416 add sp, x4, #THREAD_SIZE
Mark Rutlandc02433d2016-11-03 20:23:13 +0000417 adr_l x5, init_task
418 msr sp_el0, x5 // Save thread_info
Ard Biesheuvel60699ba2016-08-31 12:05:16 +0100419
Ard Biesheuvel2bf31a42015-12-26 12:46:40 +0100420 adr_l x8, vectors // load VBAR_EL1 with virtual
421 msr vbar_el1, x8 // vector table address
422 isb
423
Ard Biesheuvel60699ba2016-08-31 12:05:16 +0100424 stp xzr, x30, [sp, #-16]!
425 mov x29, sp
426
Ard Biesheuvelb929fe32016-08-31 12:05:15 +0100427 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 Rutland2a803c42016-01-06 11:05:27 +0000433 // 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 Rutland5227cfa2016-01-25 11:44:57 +0000439 dsb ishst // Make zero page visible to PTW
Laura Abbott034edab2014-11-21 13:50:41 -0800440
Andrey Ryabinin39d114d2015-10-12 18:52:58 +0300441#ifdef CONFIG_KASAN
442 bl kasan_early_init
443#endif
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100444#ifdef CONFIG_RANDOMIZE_BASE
Ard Biesheuvel08cdac62016-04-18 17:09:47 +0200445 tst x23, ~(MIN_KIMG_ALIGN - 1) // already running randomized?
446 b.ne 0f
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100447 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 Biesheuvel08cdac62016-04-18 17:09:47 +0200450 orr x23, x23, x0 // record KASLR offset
Ard Biesheuvel60699ba2016-08-31 12:05:16 +0100451 ldp x29, x30, [sp], #16 // we must enable KASLR, return
452 ret // to __primary_switch()
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +01004530:
454#endif
Ard Biesheuvel73267492017-07-22 18:45:33 +0100455 add sp, sp, #16
456 mov x29, #0
457 mov x30, #0
Laura Abbott034edab2014-11-21 13:50:41 -0800458 b start_kernel
Mark Brownc63d9f82020-02-18 19:58:33 +0000459SYM_FUNC_END(__primary_switched)
Laura Abbott034edab2014-11-21 13:50:41 -0800460
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 Deacon439e70e2018-01-29 12:00:00 +0000465 .section ".idmap.text","awx"
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100466
Mark Browna5d44202020-02-18 19:58:35 +0000467SYM_DATA_START(kimage_vaddr)
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100468 .quad _text - TEXT_OFFSET
Mark Browna5d44202020-02-18 19:58:35 +0000469SYM_DATA_END(kimage_vaddr)
Will Deaconb89d82e2019-01-08 16:19:01 +0000470EXPORT_SYMBOL(kimage_vaddr)
Ard Biesheuvelf80fb3a2016-01-26 14:12:01 +0100471
Laura Abbott034edab2014-11-21 13:50:41 -0800472/*
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000473 * If we're fortunate enough to boot at EL2, ensure that the world is
474 * sane before dropping to EL1.
Matthew Leach828e9832013-10-11 14:52:16 +0100475 *
Mark Rutland510224c2017-01-09 14:31:55 +0000476 * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in w0 if
Matthew Leach828e9832013-10-11 14:52:16 +0100477 * booted in EL1 or EL2 respectively.
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000478 */
Mark Brownc63d9f82020-02-18 19:58:33 +0000479SYM_FUNC_START(el2_setup)
Marc Zyngier53715132017-09-26 15:57:16 +0100480 msr SPsel, #1 // We want to use SP_EL{1,2}
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000481 mrs x0, CurrentEL
Marc Zyngier974c8e42014-06-06 14:16:21 +0100482 cmp x0, #CurrentEL_EL2
Mark Rutland3ad47d02017-02-15 14:54:16 +0000483 b.eq 1f
James Morse7a00d682018-01-15 19:38:55 +0000484 mov_q x0, (SCTLR_EL1_RES1 | ENDIAN_SET_EL1)
Matthew Leach9cf71722013-10-11 14:52:17 +0100485 msr sctlr_el1, x0
Ard Biesheuvel23c8a502016-08-31 12:05:12 +0100486 mov w0, #BOOT_CPU_MODE_EL1 // This cpu booted in EL1
Matthew Leach9cf71722013-10-11 14:52:17 +0100487 isb
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000488 ret
489
James Morse7a00d682018-01-15 19:38:55 +00004901: mov_q x0, (SCTLR_EL2_RES1 | ENDIAN_SET_EL2)
Mark Rutland3ad47d02017-02-15 14:54:16 +0000491 msr sctlr_el2, x0
492
Marc Zyngier1f364c82014-02-19 09:33:14 +0000493#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 Eliseif6e56432019-04-05 11:20:12 +0100500 ubfx x2, x2, #ID_AA64MMFR1_VHE_SHIFT, #4
Marc Zyngier1f364c82014-02-19 09:33:14 +0000501#else
502 mov x2, xzr
503#endif
504
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000505 /* Hyp configuration. */
Mark Rutland4eaed6a2018-12-07 18:39:21 +0000506 mov_q x0, HCR_HOST_NVHE_FLAGS
Marc Zyngier1f364c82014-02-19 09:33:14 +0000507 cbz x2, set_hcr
Mark Rutland4eaed6a2018-12-07 18:39:21 +0000508 mov_q x0, HCR_HOST_VHE_FLAGS
Marc Zyngier1f364c82014-02-19 09:33:14 +0000509set_hcr:
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000510 msr hcr_el2, x0
Marc Zyngier1f364c82014-02-19 09:33:14 +0000511 isb
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000512
Jintack1650ac42016-11-28 21:13:02 -0500513 /*
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 Marinas9703d9d2012-03-05 11:49:27 +0000524 mrs x0, cnthctl_el2
525 orr x0, x0, #3 // Enable EL1 physical timers
526 msr cnthctl_el2, x0
Jintack1650ac42016-11-28 21:13:02 -05005271:
Will Deacon1f75ff02012-11-29 22:48:31 +0000528 msr cntvoff_el2, xzr // Clear virtual offset
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000529
Marc Zyngier021f6532014-06-30 16:01:31 +0100530#ifdef CONFIG_ARM_GIC_V3
531 /* GICv3 system register access */
532 mrs x0, id_aa64pfr0_el1
Alexandru Eliseif6e56432019-04-05 11:20:12 +0100533 ubfx x0, x0, #ID_AA64PFR0_GIC_SHIFT, #4
Vladimir Murzin74698f62019-02-20 11:43:05 +0000534 cbz x0, 3f
Marc Zyngier021f6532014-06-30 16:01:31 +0100535
Mark Rutland0e9884f2017-01-19 17:57:43 +0000536 mrs_s x0, SYS_ICC_SRE_EL2
Marc Zyngier021f6532014-06-30 16:01:31 +0100537 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 Rutland0e9884f2017-01-19 17:57:43 +0000539 msr_s SYS_ICC_SRE_EL2, x0
Marc Zyngier021f6532014-06-30 16:01:31 +0100540 isb // Make sure SRE is now set
Mark Rutland0e9884f2017-01-19 17:57:43 +0000541 mrs_s x0, SYS_ICC_SRE_EL2 // Read SRE back,
Marc Zyngierd2719762015-09-30 11:39:59 +0100542 tbz x0, #0, 3f // and check that it sticks
Mark Rutland0e9884f2017-01-19 17:57:43 +0000543 msr_s SYS_ICH_HCR_EL2, xzr // Reset ICC_HCR_EL2 to defaults
Marc Zyngier021f6532014-06-30 16:01:31 +0100544
5453:
546#endif
547
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000548 /* 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 Marinas9703d9d2012-03-05 11:49:27 +0000554#ifdef CONFIG_COMPAT
555 msr hstr_el2, xzr // Disable CP15 traps to EL2
556#endif
557
Will Deacond10bcd42015-09-02 18:49:28 +0100558 /* EL2 debug */
Alexandru Eliseif6e56432019-04-05 11:20:12 +0100559 mrs x1, id_aa64dfr0_el1
560 sbfx x0, x1, #ID_AA64DFR0_PMUVER_SHIFT, #4
Lorenzo Pieralisif436b2a2016-01-13 14:50:03 +0000561 cmp x0, #1
562 b.lt 4f // Skip if no PMU present
Will Deacond10bcd42015-09-02 18:49:28 +0100563 mrs x0, pmcr_el0 // Disable debug access traps
564 ubfx x0, x0, #11, #5 // to EL2 and allow access to
Lorenzo Pieralisif436b2a2016-01-13 14:50:03 +00005654:
Will Deacon2bf47e12016-09-22 11:25:25 +0100566 csel x3, xzr, x0, lt // all PMU counters from EL1
567
568 /* Statistical profiling */
Alexandru Eliseif6e56432019-04-05 11:20:12 +0100569 ubfx x0, x1, #ID_AA64DFR0_PMSVER_SHIFT, #4
Will Deaconb0c57e12017-07-07 13:47:02 +0100570 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
5785:
Will Deacon2bf47e12016-09-22 11:25:25 +0100579 mov x1, #(MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT)
580 orr x3, x3, x1 // If we don't have VHE, then
Will Deaconb0c57e12017-07-07 13:47:02 +0100581 b 7f // use EL1&0 translation.
5826: // For VHE, use EL2 translation
Will Deacon2bf47e12016-09-22 11:25:25 +0100583 orr x3, x3, #MDCR_EL2_TPMS // and disable access from EL1
Will Deaconb0c57e12017-07-07 13:47:02 +01005847:
Will Deacon2bf47e12016-09-22 11:25:25 +0100585 msr mdcr_el2, x3 // Configure debug traps
Will Deacond10bcd42015-09-02 18:49:28 +0100586
Mark Rutlandcc33c4e2018-02-13 13:39:23 +0000587 /* 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
5921:
593
Marc Zyngier7dbfbe52012-11-06 19:27:59 +0000594 /* Stage-2 translation */
595 msr vttbr_el2, xzr
596
Marc Zyngier1f364c82014-02-19 09:33:14 +0000597 cbz x2, install_el2_stub
598
Ard Biesheuvel23c8a502016-08-31 12:05:12 +0100599 mov w0, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
Marc Zyngier1f364c82014-02-19 09:33:14 +0000600 isb
601 ret
602
603install_el2_stub:
Mark Rutlandd61c97a2017-02-15 14:54:17 +0000604 /*
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 Morse7a00d682018-01-15 19:38:55 +0000611 mov_q x0, (SCTLR_EL1_RES1 | ENDIAN_SET_EL1)
Mark Rutlandd61c97a2017-02-15 14:54:17 +0000612 msr sctlr_el1, x0
613
614 /* Coprocessor traps. */
615 mov x0, #0x33ff
616 msr cptr_el2, x0 // Disable copro. traps to EL2
617
Dave Martin22043a32017-10-31 15:51:04 +0000618 /* 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 Zyngier712c6ff2012-10-19 17:46:27 +0100629 /* Hypervisor stub */
Dave Martin22043a32017-10-31 15:51:04 +00006307: adr_l x0, __hyp_stub_vectors
Marc Zyngier712c6ff2012-10-19 17:46:27 +0100631 msr vbar_el2, x0
632
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000633 /* 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 Biesheuvel23c8a502016-08-31 12:05:12 +0100638 mov w0, #BOOT_CPU_MODE_EL2 // This CPU booted in EL2
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000639 eret
Mark Brownc63d9f82020-02-18 19:58:33 +0000640SYM_FUNC_END(el2_setup)
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000641
Marc Zyngierf35a9202012-10-26 15:40:05 +0100642/*
Matthew Leach828e9832013-10-11 14:52:16 +0100643 * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
Mark Rutland510224c2017-01-09 14:31:55 +0000644 * in w0. See arch/arm64/include/asm/virt.h for more info.
Matthew Leach828e9832013-10-11 14:52:16 +0100645 */
Mark Brownc63d9f82020-02-18 19:58:33 +0000646SYM_FUNC_START_LOCAL(set_cpu_boot_mode_flag)
Ard Biesheuvel6f4d57f2015-03-17 09:14:29 +0100647 adr_l x1, __boot_cpu_mode
Ard Biesheuvel23c8a502016-08-31 12:05:12 +0100648 cmp w0, #BOOT_CPU_MODE_EL2
Matthew Leach828e9832013-10-11 14:52:16 +0100649 b.ne 1f
650 add x1, x1, #4
Ard Biesheuvel23c8a502016-08-31 12:05:12 +01006511: str w0, [x1] // This CPU has booted in EL1
Will Deacond0488592014-05-02 16:24:13 +0100652 dmb sy
653 dc ivac, x1 // Invalidate potentially stale cache line
Matthew Leach828e9832013-10-11 14:52:16 +0100654 ret
Mark Brownc63d9f82020-02-18 19:58:33 +0000655SYM_FUNC_END(set_cpu_boot_mode_flag)
Matthew Leach828e9832013-10-11 14:52:16 +0100656
657/*
James Morseb6113032016-08-24 18:27:29 +0100658 * 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 Zyngierf35a9202012-10-26 15:40:05 +0100665 * 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 Browna5d44202020-02-18 19:58:35 +0000671SYM_DATA_START(__boot_cpu_mode)
Marc Zyngierf35a9202012-10-26 15:40:05 +0100672 .long BOOT_CPU_MODE_EL2
Mark Rutland424a3832015-03-13 16:14:36 +0000673 .long BOOT_CPU_MODE_EL1
Mark Browna5d44202020-02-18 19:58:35 +0000674SYM_DATA_END(__boot_cpu_mode)
James Morseb6113032016-08-24 18:27:29 +0100675/*
676 * The booting CPU updates the failed status @__early_cpu_boot_status,
677 * with MMU turned off.
678 */
Mark Browna5d44202020-02-18 19:58:35 +0000679SYM_DATA_START(__early_cpu_boot_status)
Arun KS61cf61d2019-04-30 16:05:04 +0530680 .quad 0
Mark Browna5d44202020-02-18 19:58:35 +0000681SYM_DATA_END(__early_cpu_boot_status)
James Morseb6113032016-08-24 18:27:29 +0100682
Marc Zyngierf35a9202012-10-26 15:40:05 +0100683 .popsection
684
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000685 /*
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 Brownc63d9f82020-02-18 19:58:33 +0000689SYM_FUNC_START(secondary_holding_pen)
Ard Biesheuvel23c8a502016-08-31 12:05:12 +0100690 bl el2_setup // Drop to EL1, w0=cpu_boot_mode
Matthew Leach828e9832013-10-11 14:52:16 +0100691 bl set_cpu_boot_mode_flag
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000692 mrs x0, mpidr_el1
Ard Biesheuvelb03cc882016-04-18 17:09:45 +0200693 mov_q x1, MPIDR_HWID_BITMASK
Javi Merino0359b0e2012-08-29 18:32:18 +0100694 and x0, x0, x1
Ard Biesheuvelb1c98292015-03-10 15:00:03 +0100695 adr_l x3, secondary_holding_pen_release
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000696pen: ldr x4, [x3]
697 cmp x4, x0
698 b.eq secondary_startup
699 wfe
700 b pen
Mark Brownc63d9f82020-02-18 19:58:33 +0000701SYM_FUNC_END(secondary_holding_pen)
Mark Rutland652af892013-10-24 20:30:16 +0100702
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 Brownc63d9f82020-02-18 19:58:33 +0000707SYM_FUNC_START(secondary_entry)
Mark Rutland652af892013-10-24 20:30:16 +0100708 bl el2_setup // Drop to EL1
Lorenzo Pieralisi85cc00e2013-11-18 18:56:42 +0000709 bl set_cpu_boot_mode_flag
Mark Rutland652af892013-10-24 20:30:16 +0100710 b secondary_startup
Mark Brownc63d9f82020-02-18 19:58:33 +0000711SYM_FUNC_END(secondary_entry)
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000712
Mark Brownc63d9f82020-02-18 19:58:33 +0000713SYM_FUNC_START_LOCAL(secondary_startup)
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000714 /*
715 * Common entry point for secondary CPUs.
716 */
Steve Cappera96a33b2018-12-06 22:50:40 +0000717 bl __cpu_secondary_check52bitva
Marc Zyngiera591ede2015-03-18 14:55:20 +0000718 bl __cpu_setup // initialise processor
Jun Yao693d5632018-09-24 14:51:13 +0100719 adrp x1, swapper_pg_dir
Ard Biesheuvel9dcf7912016-08-31 12:05:14 +0100720 bl __enable_mmu
721 ldr x8, =__secondary_switched
722 br x8
Mark Brownc63d9f82020-02-18 19:58:33 +0000723SYM_FUNC_END(secondary_startup)
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000724
Mark Brownc63d9f82020-02-18 19:58:33 +0000725SYM_FUNC_START_LOCAL(__secondary_switched)
Ard Biesheuvel2bf31a42015-12-26 12:46:40 +0100726 adr_l x5, vectors
727 msr vbar_el1, x5
728 isb
729
Suzuki K Poulosebb905272016-02-23 10:31:42 +0000730 adr_l x0, secondary_data
Mark Rutlandc02433d2016-11-03 20:23:13 +0000731 ldr x1, [x0, #CPU_BOOT_STACK] // get secondary_data.stack
Will Deacon5b1cfe32019-08-27 14:36:38 +0100732 cbz x1, __secondary_too_slow
Mark Rutlandc02433d2016-11-03 20:23:13 +0000733 mov sp, x1
734 ldr x2, [x0, #CPU_BOOT_TASK]
Will Deacon5b1cfe32019-08-27 14:36:38 +0100735 cbz x2, __secondary_too_slow
Mark Rutlandc02433d2016-11-03 20:23:13 +0000736 msr sp_el0, x2
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000737 mov x29, #0
Ard Biesheuvel73267492017-07-22 18:45:33 +0100738 mov x30, #0
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000739 b secondary_start_kernel
Mark Brownc63d9f82020-02-18 19:58:33 +0000740SYM_FUNC_END(__secondary_switched)
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000741
Mark Brownc63d9f82020-02-18 19:58:33 +0000742SYM_FUNC_START_LOCAL(__secondary_too_slow)
Will Deacon5b1cfe32019-08-27 14:36:38 +0100743 wfe
744 wfi
745 b __secondary_too_slow
Mark Brownc63d9f82020-02-18 19:58:33 +0000746SYM_FUNC_END(__secondary_too_slow)
Will Deacon5b1cfe32019-08-27 14:36:38 +0100747
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000748/*
Suzuki K Poulosebb905272016-02-23 10:31:42 +0000749 * 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 Biesheuveladb49072016-04-15 12:11:21 +0200760 adr_l \tmp1, __early_cpu_boot_status
761 str \tmp2, [\tmp1]
Suzuki K Poulosebb905272016-02-23 10:31:42 +0000762 dmb sy
763 dc ivac, \tmp1 // Invalidate potentially stale cache line
764 .endm
765
Suzuki K Poulosebb905272016-02-23 10:31:42 +0000766/*
Ard Biesheuvel8b0a9572015-03-17 08:59:53 +0100767 * Enable the MMU.
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000768 *
Ard Biesheuvel8b0a9572015-03-17 08:59:53 +0100769 * x0 = SCTLR_EL1 value for turning on the MMU.
Jun Yao693d5632018-09-24 14:51:13 +0100770 * x1 = TTBR1_EL1 value
Ard Biesheuvel8b0a9572015-03-17 08:59:53 +0100771 *
Ard Biesheuvel9dcf7912016-08-31 12:05:14 +0100772 * Returns to the caller via x30/lr. This requires the caller to be covered
773 * by the .idmap.text section.
Suzuki K. Poulose4bf8b962015-10-19 14:19:35 +0100774 *
775 * Checks if the selected granule size is supported by the CPU.
776 * If it isn't, park the CPU
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000777 */
Mark Brownc63d9f82020-02-18 19:58:33 +0000778SYM_FUNC_START(__enable_mmu)
Jun Yao693d5632018-09-24 14:51:13 +0100779 mrs x2, ID_AA64MMFR0_EL1
780 ubfx x2, x2, #ID_AA64MMFR0_TGRAN_SHIFT, 4
Suzuki K. Poulose4bf8b962015-10-19 14:19:35 +0100781 cmp x2, #ID_AA64MMFR0_TGRAN_SUPPORTED
782 b.ne __no_granule_support
Jun Yao693d5632018-09-24 14:51:13 +0100783 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 Capperc8120262019-08-07 16:55:19 +0100788 offset_ttbr1 x1, x3
Jun Yao693d5632018-09-24 14:51:13 +0100789 msr ttbr1_el1, x1 // load TTBR1
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000790 isb
Catalin Marinas9703d9d2012-03-05 11:49:27 +0000791 msr sctlr_el1, x0
792 isb
Will Deacon8ec41982015-08-04 17:49:36 +0100793 /*
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 Biesheuvel9dcf7912016-08-31 12:05:14 +0100801 ret
Mark Brownc63d9f82020-02-18 19:58:33 +0000802SYM_FUNC_END(__enable_mmu)
Suzuki K. Poulose4bf8b962015-10-19 14:19:35 +0100803
Mark Brownc63d9f82020-02-18 19:58:33 +0000804SYM_FUNC_START(__cpu_secondary_check52bitva)
Steve Capperb6d00d42019-08-07 16:55:22 +0100805#ifdef CONFIG_ARM64_VA_BITS_52
Steve Capper2c624fe2019-08-07 16:55:23 +0100806 ldr_l x0, vabits_actual
Steve Cappera96a33b2018-12-06 22:50:40 +0000807 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 Deacon66f16a22018-12-10 14:21:13 +0000814 update_early_cpu_boot_status \
815 CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_52_BIT_VA, x0, x1
Steve Cappera96a33b2018-12-06 22:50:40 +00008161: wfe
817 wfi
818 b 1b
819
820#endif
8212: ret
Mark Brownc63d9f82020-02-18 19:58:33 +0000822SYM_FUNC_END(__cpu_secondary_check52bitva)
Steve Cappera96a33b2018-12-06 22:50:40 +0000823
Mark Brownc63d9f82020-02-18 19:58:33 +0000824SYM_FUNC_START_LOCAL(__no_granule_support)
Suzuki K Poulosebb905272016-02-23 10:31:42 +0000825 /* Indicate that this CPU can't boot and is stuck in the kernel */
Will Deacon66f16a22018-12-10 14:21:13 +0000826 update_early_cpu_boot_status \
827 CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_NO_GRAN, x1, x2
Suzuki K Poulosebb905272016-02-23 10:31:42 +00008281:
Suzuki K. Poulose4bf8b962015-10-19 14:19:35 +0100829 wfe
Suzuki K Poulosebb905272016-02-23 10:31:42 +0000830 wfi
Ard Biesheuvel3c5e9f22016-08-31 12:05:13 +0100831 b 1b
Mark Brownc63d9f82020-02-18 19:58:33 +0000832SYM_FUNC_END(__no_granule_support)
Ard Biesheuvele5ebeec2016-04-18 17:09:42 +0200833
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200834#ifdef CONFIG_RELOCATABLE
Mark Brownc63d9f82020-02-18 19:58:33 +0000835SYM_FUNC_START_LOCAL(__relocate_kernel)
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200836 /*
837 * Iterate over each entry in the relocation table, and apply the
838 * relocations in place.
839 */
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200840 ldr w9, =__rela_offset // offset to reloc table
841 ldr w10, =__rela_size // size of reloc table
842
Ard Biesheuvelb03cc882016-04-18 17:09:45 +0200843 mov_q x11, KIMAGE_VADDR // default virtual offset
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200844 add x11, x11, x23 // actual virtual offset
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200845 add x9, x9, x11 // __va(.rela)
846 add x10, x9, x10 // __va(.rela) + sizeof(.rela)
847
8480: cmp x9, x10
Ard Biesheuvel08cc55b2016-07-24 14:00:13 +0200849 b.hs 1f
Peter Collingbourne5cf896f2019-07-31 18:18:42 -0700850 ldp x12, x13, [x9], #24
851 ldr x14, [x9, #-8]
852 cmp w13, #R_AARCH64_RELATIVE
Ard Biesheuvel08cc55b2016-07-24 14:00:13 +0200853 b.ne 0b
Peter Collingbourne5cf896f2019-07-31 18:18:42 -0700854 add x14, x14, x23 // relocate
855 str x14, [x12, x23]
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200856 b 0b
Peter Collingbourne5cf896f2019-07-31 18:18:42 -0700857
8581:
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
9052: 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
9153: mov x14, x13
9164: 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
9235: add x14, x14, #8 // move to next bit's address
924 b 4b
925
9266: /*
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
9337:
934#endif
935 ret
936
Mark Brownc63d9f82020-02-18 19:58:33 +0000937SYM_FUNC_END(__relocate_kernel)
Ard Biesheuvel3c5e9f22016-08-31 12:05:13 +0100938#endif
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200939
Mark Brownc63d9f82020-02-18 19:58:33 +0000940SYM_FUNC_START_LOCAL(__primary_switch)
Ard Biesheuvel3c5e9f22016-08-31 12:05:13 +0100941#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 Yao2b5548b2018-09-24 15:47:49 +0100946 adrp x1, init_pg_dir
Ard Biesheuvel9dcf7912016-08-31 12:05:14 +0100947 bl __enable_mmu
Ard Biesheuvel3c5e9f22016-08-31 12:05:13 +0100948#ifdef CONFIG_RELOCATABLE
Peter Collingbourne5cf896f2019-07-31 18:18:42 -0700949#ifdef CONFIG_RELR
950 mov x24, #0 // no RELR displacement yet
951#endif
Ard Biesheuvel3c5e9f22016-08-31 12:05:13 +0100952 bl __relocate_kernel
953#ifdef CONFIG_RANDOMIZE_BASE
954 ldr x8, =__primary_switched
Ard Biesheuvelb929fe32016-08-31 12:05:15 +0100955 adrp x0, __PHYS_OFFSET
Ard Biesheuvel3c5e9f22016-08-31 12:05:13 +0100956 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 Donthineni3060e9f2018-01-29 11:59:52 +0000963 pre_disable_mmu_workaround
Ard Biesheuvel3c5e9f22016-08-31 12:05:13 +0100964 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 Biesheuvel0cd3def2016-04-18 17:09:43 +0200979#endif
980 ldr x8, =__primary_switched
Ard Biesheuvelb929fe32016-08-31 12:05:15 +0100981 adrp x0, __PHYS_OFFSET
Ard Biesheuvel0cd3def2016-04-18 17:09:43 +0200982 br x8
Mark Brownc63d9f82020-02-18 19:58:33 +0000983SYM_FUNC_END(__primary_switch)