1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * Copyright (C) 2012 Regents of the University of California 4 */ 5 6#include <asm/asm-offsets.h> 7#include <asm/asm.h> 8#include <linux/init.h> 9#include <linux/linkage.h> 10#include <asm/thread_info.h> 11#include <asm/page.h> 12#include <asm/pgtable.h> 13#include <asm/csr.h> 14#include <asm/hwcap.h> 15#include <asm/image.h> 16#include <asm/scs.h> 17#include <asm/usercfi.h> 18#include <asm/alternative.h> 19#include <asm/errata_list.h> 20#include "efi-header.S" 21 22__HEAD 23SYM_CODE_START(_start) 24 /* 25 * Image header expected by Linux boot-loaders. The image header data 26 * structure is described in asm/image.h. 27 * Do not modify it without modifying the structure and all bootloaders 28 * that expects this header format!! 29 */ 30#ifdef CONFIG_EFI 31 /* 32 * This instruction decodes to "MZ" ASCII required by UEFI. 33 */ 34 c.li s4,-13 35 j _start_kernel 36#else 37 /* jump to start kernel */ 38 j _start_kernel 39 /* reserved */ 40 .word 0 41#endif 42 .balign 8 43#ifdef CONFIG_RISCV_M_MODE 44 /* Image load offset (0MB) from start of RAM for M-mode */ 45 .dword 0 46#else 47#if __riscv_xlen == 64 48 /* Image load offset(2MB) from start of RAM */ 49 .dword 0x200000 50#else 51 /* Image load offset(4MB) from start of RAM */ 52 .dword 0x400000 53#endif 54#endif 55 /* Effective size of kernel image */ 56 .dword _end - _start 57 .dword __HEAD_FLAGS 58 .word RISCV_HEADER_VERSION 59 .word 0 60 .dword 0 61 .ascii RISCV_IMAGE_MAGIC 62 .balign 4 63 .ascii RISCV_IMAGE_MAGIC2 64#ifdef CONFIG_EFI 65 .word pe_head_start - _start 66pe_head_start: 67 68 __EFI_PE_HEADER 69#else 70 .word 0 71#endif 72 73.align 2 74#ifdef CONFIG_MMU 75 .global relocate_enable_mmu 76relocate_enable_mmu: 77 /* Relocate return address */ 78 la a1, kernel_map 79 REG_L a1, KERNEL_MAP_VIRT_ADDR(a1) 80 la a2, _start 81 sub a1, a1, a2 82 add ra, ra, a1 83 84 /* Point stvec to virtual address of instruction after satp write */ 85 la a2, 1f 86 add a2, a2, a1 87 csrw CSR_TVEC, a2 88 89 /* Compute satp for kernel page tables, but don't load it yet */ 90 srl a2, a0, PAGE_SHIFT 91 la a1, satp_mode 92 REG_L a1, 0(a1) 93 or a2, a2, a1 94 95 /* 96 * Load trampoline page directory, which will cause us to trap to 97 * stvec if VA != PA, or simply fall through if VA == PA. We need a 98 * full fence here because setup_vm() just wrote these PTEs and we need 99 * to ensure the new translations are in use. 100 */ 101 la a0, trampoline_pg_dir 102 srl a0, a0, PAGE_SHIFT 103 or a0, a0, a1 104 sfence.vma 105 csrw CSR_SATP, a0 106.align 2 1071: 108 /* Set trap vector to spin forever to help debug */ 109 la a0, .Lsecondary_park 110 csrw CSR_TVEC, a0 111 112 /* Reload the global pointer */ 113 load_global_pointer 114 115 /* 116 * Switch to kernel page tables. A full fence is necessary in order to 117 * avoid using the trampoline translations, which are only correct for 118 * the first superpage. Fetching the fence is guaranteed to work 119 * because that first superpage is translated the same way. 120 */ 121 csrw CSR_SATP, a2 122 sfence.vma 123 124 ret 125#endif /* CONFIG_MMU */ 126#ifdef CONFIG_SMP 127 .global secondary_start_sbi 128secondary_start_sbi: 129 /* Mask all interrupts */ 130 csrw CSR_IE, zero 131 csrw CSR_IP, zero 132 133#ifndef CONFIG_RISCV_M_MODE 134 /* Enable time CSR */ 135 li t0, 0x2 136 csrw CSR_SCOUNTEREN, t0 137#endif 138 139 /* Load the global pointer */ 140 load_global_pointer 141 142 /* 143 * Disable FPU & VECTOR to detect illegal usage of 144 * floating point or vector in kernel space 145 */ 146 li t0, SR_FS_VS 147 csrc CSR_STATUS, t0 148 149 /* Set trap vector to spin forever to help debug */ 150 la a3, .Lsecondary_park 151 csrw CSR_TVEC, a3 152 153 /* a0 contains the hartid & a1 contains boot data */ 154 li a2, SBI_HART_BOOT_TASK_PTR_OFFSET 155 add a2, a2, a1 156 REG_L tp, (a2) 157 li a3, SBI_HART_BOOT_STACK_PTR_OFFSET 158 add a3, a3, a1 159 REG_L sp, (a3) 160 161.Lsecondary_start_common: 162 163#ifdef CONFIG_MMU 164 /* Enable virtual memory and relocate to virtual address */ 165 la a0, swapper_pg_dir 166 call relocate_enable_mmu 167#endif 168 call .Lsetup_trap_vector 169#if defined(CONFIG_RISCV_SBI) && defined(CONFIG_RISCV_USER_CFI) 170 li a7, SBI_EXT_FWFT 171 li a6, SBI_EXT_FWFT_SET 172 li a0, SBI_FWFT_SHADOW_STACK 173 li a1, 1 /* enable supervisor to access shadow stack access */ 174 li a2, SBI_FWFT_SET_FLAG_LOCK 175 ecall 176 beqz a0, 1f 177 la a1, riscv_nousercfi 178 li a0, CMDLINE_DISABLE_RISCV_USERCFI_BCFI 179 REG_S a0, (a1) 1801: 181#endif 182 scs_load_current 183 call smp_callin 184#endif /* CONFIG_SMP */ 185 186.align 2 187.Lsecondary_park: 188 /* 189 * Park this hart if we: 190 * - have too many harts on CONFIG_RISCV_BOOT_SPINWAIT 191 * - receive an early trap, before setup_trap_vector finished 192 * - fail in smp_callin(), as a successful one wouldn't return 193 */ 194 ALT_WFI 195 j .Lsecondary_park 196 197.align 2 198.Lsetup_trap_vector: 199 /* Set trap vector to exception handler */ 200 la a0, handle_exception 201 csrw CSR_TVEC, a0 202 203 /* 204 * Set sup0 scratch register to 0, indicating to exception vector that 205 * we are presently executing in kernel. 206 */ 207 csrw CSR_SCRATCH, zero 208 ret 209 210SYM_CODE_END(_start) 211 212SYM_CODE_START(_start_kernel) 213 /* Mask all interrupts */ 214 csrw CSR_IE, zero 215 csrw CSR_IP, zero 216 217#ifdef CONFIG_RISCV_M_MODE 218 /* flush the instruction cache */ 219 fence.i 220 221 /* Reset all registers except ra, a0, a1 */ 222 call reset_regs 223 224 /* 225 * Setup a PMP to permit access to all of memory. Some machines may 226 * not implement PMPs, so we set up a quick trap handler to just skip 227 * touching the PMPs on any trap. 228 */ 229 la a0, .Lpmp_done 230 csrw CSR_TVEC, a0 231 232 li a0, -1 233 csrw CSR_PMPADDR0, a0 234 li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X) 235 csrw CSR_PMPCFG0, a0 236.align 2 237.Lpmp_done: 238 239 /* 240 * The hartid in a0 is expected later on, and we have no firmware 241 * to hand it to us. 242 */ 243 csrr a0, CSR_MHARTID 244#else 245 /* Enable time CSR */ 246 li t0, 0x2 247 csrw CSR_SCOUNTEREN, t0 248#endif /* CONFIG_RISCV_M_MODE */ 249 250 /* Load the global pointer */ 251 load_global_pointer 252 253 /* 254 * Disable FPU & VECTOR to detect illegal usage of 255 * floating point or vector in kernel space 256 */ 257 li t0, SR_FS_VS 258 csrc CSR_STATUS, t0 259 260#ifdef CONFIG_RISCV_BOOT_SPINWAIT 261 li t0, CONFIG_NR_CPUS 262 blt a0, t0, .Lgood_cores 263 tail .Lsecondary_park 264.Lgood_cores: 265 266 /* The lottery system is only required for spinwait booting method */ 267 /* Pick one hart to run the main boot sequence */ 268 la a3, hart_lottery 269 li a2, 1 270 amoadd.w a3, a2, (a3) 271 bnez a3, .Lsecondary_start 272#endif /* CONFIG_RISCV_BOOT_SPINWAIT */ 273 274 /* Clear BSS for flat non-ELF images */ 275 la a3, __bss_start 276 la a4, __bss_stop 277 ble a4, a3, .Lclear_bss_done 278.Lclear_bss: 279 REG_S zero, (a3) 280 add a3, a3, RISCV_SZPTR 281 blt a3, a4, .Lclear_bss 282.Lclear_bss_done: 283 la a2, boot_cpu_hartid 284 REG_S a0, (a2) 285 286 /* Initialize page tables and relocate to virtual addresses */ 287 la tp, init_task 288 la sp, init_thread_union + THREAD_SIZE 289 addi sp, sp, -PT_SIZE_ON_STACK 290 scs_load_init_stack 291#ifdef CONFIG_BUILTIN_DTB 292 la a0, __dtb_start 293#else 294 mv a0, a1 295#endif /* CONFIG_BUILTIN_DTB */ 296 /* Set trap vector to spin forever to help debug */ 297 la a3, .Lsecondary_park 298 csrw CSR_TVEC, a3 299 call setup_vm 300#ifdef CONFIG_MMU 301 la a0, early_pg_dir 302 call relocate_enable_mmu 303#endif /* CONFIG_MMU */ 304 305 call .Lsetup_trap_vector 306 /* Restore C environment */ 307 la tp, init_task 308 la sp, init_thread_union + THREAD_SIZE 309 addi sp, sp, -PT_SIZE_ON_STACK 310#if defined(CONFIG_RISCV_SBI) && defined(CONFIG_RISCV_USER_CFI) 311 li a7, SBI_EXT_FWFT 312 li a6, SBI_EXT_FWFT_SET 313 li a0, SBI_FWFT_SHADOW_STACK 314 li a1, 1 /* enable supervisor to access shadow stack access */ 315 li a2, SBI_FWFT_SET_FLAG_LOCK 316 ecall 317 beqz a0, 1f 318 la a1, riscv_nousercfi 319 li a0, CMDLINE_DISABLE_RISCV_USERCFI_BCFI 320 REG_S a0, (a1) 3211: 322#endif 323 scs_load_current 324 325#ifdef CONFIG_KASAN 326 call kasan_early_init 327#endif 328 /* Start the kernel */ 329 call soc_early_init 330 tail start_kernel 331 332#ifdef CONFIG_RISCV_BOOT_SPINWAIT 333.Lsecondary_start: 334 /* Set trap vector to spin forever to help debug */ 335 la a3, .Lsecondary_park 336 csrw CSR_TVEC, a3 337 338 slli a3, a0, LGREG 339 la a1, __cpu_spinwait_stack_pointer 340 la a2, __cpu_spinwait_task_pointer 341 add a1, a3, a1 342 add a2, a3, a2 343 344 /* 345 * This hart didn't win the lottery, so we wait for the winning hart to 346 * get far enough along the boot process that it should continue. 347 */ 348.Lwait_for_cpu_up: 349 /* FIXME: We should WFI to save some energy here. */ 350 REG_L sp, (a1) 351 REG_L tp, (a2) 352 beqz sp, .Lwait_for_cpu_up 353 beqz tp, .Lwait_for_cpu_up 354 fence 355 356 tail .Lsecondary_start_common 357#endif /* CONFIG_RISCV_BOOT_SPINWAIT */ 358 359SYM_CODE_END(_start_kernel) 360 361#ifdef CONFIG_RISCV_M_MODE 362SYM_CODE_START_LOCAL(reset_regs) 363 li sp, 0 364 li gp, 0 365 li tp, 0 366 li t0, 0 367 li t1, 0 368 li t2, 0 369 li s0, 0 370 li s1, 0 371 li a2, 0 372 li a3, 0 373 li a4, 0 374 li a5, 0 375 li a6, 0 376 li a7, 0 377 li s2, 0 378 li s3, 0 379 li s4, 0 380 li s5, 0 381 li s6, 0 382 li s7, 0 383 li s8, 0 384 li s9, 0 385 li s10, 0 386 li s11, 0 387 li t3, 0 388 li t4, 0 389 li t5, 0 390 li t6, 0 391 csrw CSR_SCRATCH, 0 392 393#ifdef CONFIG_FPU 394 csrr t0, CSR_MISA 395 andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D) 396 beqz t0, .Lreset_regs_done_fpu 397 398 li t1, SR_FS 399 csrs CSR_STATUS, t1 400 fmv.s.x f0, zero 401 fmv.s.x f1, zero 402 fmv.s.x f2, zero 403 fmv.s.x f3, zero 404 fmv.s.x f4, zero 405 fmv.s.x f5, zero 406 fmv.s.x f6, zero 407 fmv.s.x f7, zero 408 fmv.s.x f8, zero 409 fmv.s.x f9, zero 410 fmv.s.x f10, zero 411 fmv.s.x f11, zero 412 fmv.s.x f12, zero 413 fmv.s.x f13, zero 414 fmv.s.x f14, zero 415 fmv.s.x f15, zero 416 fmv.s.x f16, zero 417 fmv.s.x f17, zero 418 fmv.s.x f18, zero 419 fmv.s.x f19, zero 420 fmv.s.x f20, zero 421 fmv.s.x f21, zero 422 fmv.s.x f22, zero 423 fmv.s.x f23, zero 424 fmv.s.x f24, zero 425 fmv.s.x f25, zero 426 fmv.s.x f26, zero 427 fmv.s.x f27, zero 428 fmv.s.x f28, zero 429 fmv.s.x f29, zero 430 fmv.s.x f30, zero 431 fmv.s.x f31, zero 432 csrw fcsr, 0 433 /* note that the caller must clear SR_FS */ 434.Lreset_regs_done_fpu: 435#endif /* CONFIG_FPU */ 436 437#ifdef CONFIG_RISCV_ISA_V 438 csrr t0, CSR_MISA 439 li t1, COMPAT_HWCAP_ISA_V 440 and t0, t0, t1 441 beqz t0, .Lreset_regs_done_vector 442 443 /* 444 * Clear vector registers and reset vcsr 445 * VLMAX has a defined value, VLEN is a constant, 446 * and this form of vsetvli is defined to set vl to VLMAX. 447 */ 448 li t1, SR_VS 449 csrs CSR_STATUS, t1 450 csrs CSR_VCSR, x0 451 vsetvli t1, x0, e8, m8, ta, ma 452 vmv.v.i v0, 0 453 vmv.v.i v8, 0 454 vmv.v.i v16, 0 455 vmv.v.i v24, 0 456 /* note that the caller must clear SR_VS */ 457.Lreset_regs_done_vector: 458#endif /* CONFIG_RISCV_ISA_V */ 459 ret 460SYM_CODE_END(reset_regs) 461#endif /* CONFIG_RISCV_M_MODE */ 462