2018-05-06 21:58:06 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2017-12-26 05:55:48 +00:00
|
|
|
/*
|
|
|
|
* Startup Code for RISC-V Core
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Microsemi Corporation.
|
|
|
|
* Copyright (c) 2017 Padmarao Begari <Padmarao.Begari@microsemi.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Andes Technology Corporation
|
|
|
|
* Rick Chen, Andes Technology Corporation <rick@andestech.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <asm-offsets.h>
|
|
|
|
#include <config.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <elf.h>
|
|
|
|
#include <asm/encoding.h>
|
2018-12-12 14:12:45 +00:00
|
|
|
#include <generated/asm-offsets.h>
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_32BIT
|
2018-11-22 10:26:24 +00:00
|
|
|
#define LREG lw
|
|
|
|
#define SREG sw
|
|
|
|
#define REGBYTES 4
|
2017-12-26 05:55:48 +00:00
|
|
|
#define RELOC_TYPE R_RISCV_32
|
|
|
|
#define SYM_INDEX 0x8
|
|
|
|
#define SYM_SIZE 0x10
|
|
|
|
#else
|
2018-11-22 10:26:24 +00:00
|
|
|
#define LREG ld
|
|
|
|
#define SREG sd
|
|
|
|
#define REGBYTES 8
|
2017-12-26 05:55:48 +00:00
|
|
|
#define RELOC_TYPE R_RISCV_64
|
|
|
|
#define SYM_INDEX 0x20
|
|
|
|
#define SYM_SIZE 0x18
|
|
|
|
#endif
|
|
|
|
|
2019-03-17 18:28:40 +00:00
|
|
|
.section .data
|
|
|
|
secondary_harts_relocation_error:
|
|
|
|
.ascii "Relocation of secondary harts has failed, error %d\n"
|
|
|
|
|
2018-11-22 10:26:24 +00:00
|
|
|
.section .text
|
2017-12-26 05:55:48 +00:00
|
|
|
.globl _start
|
|
|
|
_start:
|
2019-08-21 19:14:43 +00:00
|
|
|
#if CONFIG_IS_ENABLED(RISCV_MMODE)
|
2019-07-11 06:43:13 +00:00
|
|
|
csrr a0, CSR_MHARTID
|
2019-03-17 18:28:39 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-21 11:51:41 +00:00
|
|
|
/*
|
|
|
|
* Save hart id and dtb pointer. The thread pointer register is not
|
|
|
|
* modified by C code. It is used by secondary_hart_loop.
|
|
|
|
*/
|
2019-03-17 18:28:36 +00:00
|
|
|
mv tp, a0
|
2018-11-22 10:26:29 +00:00
|
|
|
mv s1, a1
|
|
|
|
|
riscv: Ensure gp is NULL or points to valid data
This ensures constructs like `if (gd & gd->...) { ... }` work when
accessing the global data pointer. Without this change, it was possible for
a very early trap to cause _exit_trap to directly or indirectly (through
printf) to read arbitrary memory. This could cause a second trap,
preventing show_regs from being printed.
printf (and specifically puts) uses gd to determine what function to print
with. These functions in turn use gd to find the serial device, etc.
However, before accessing gd, puts first checks to see if it is non-NULL.
This indicates an existing (perhaps undocumented) assumption that either gd
is NULL or it is completely valid.
Before this patch, gd either points to unexpected data (because it retains
the value it did from the prior-stage) or points to uninitialized data
(because it has not yet been initialized by board_init_f_init_reserve)
until the hart has acquired available_harts_lock. This can cause two
problems, depending on the value of gd->flags. If GD_FLG_SERIAL_READY is
unset, then some garbage data will be printed to stdout, but there will not
be a second trap. However, if GD_FLG_SERIAL_READY is set, then puts will
try to print with serial_puts, which will likely cause a second trap.
After this patch, gd is zero up until either a hart has set it in
wait_for_gd_init, or until it is set by arch_init_gd. This prevents its
usage before its data is initialized because both handle_trap and puts
ensure that gd is nonzero before using it. After gd has been set, it is OK
to access it because its data has been cleared (and so flags is valid).
XIP cannot use locks because flash is not writable. This leaves it
vulnerable to the same class of bugs regarding already-pending IPIs as
before this series. Fixing that would require finding another method of
synchronization, which is outside the scope of this series.
Fixes: 7c6ca03eae ("riscv: additional crash information")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bin.meng@windriver.com>
Reviewed-by: Rick Chen <rick@andestech.com>
2020-09-21 11:51:40 +00:00
|
|
|
/*
|
|
|
|
* Set the global data pointer to a known value in case we get a very
|
|
|
|
* early trap. The global data pointer will be set its actual value only
|
|
|
|
* after it has been initialized.
|
|
|
|
*/
|
|
|
|
mv gp, zero
|
|
|
|
|
2020-09-21 11:51:41 +00:00
|
|
|
/*
|
|
|
|
* Set the trap handler. This must happen after initializing gp because
|
|
|
|
* the handler may use it.
|
|
|
|
*/
|
2018-11-22 10:26:24 +00:00
|
|
|
la t0, trap_entry
|
2018-12-03 05:27:40 +00:00
|
|
|
csrw MODE_PREFIX(tvec), t0
|
2018-11-22 10:26:28 +00:00
|
|
|
|
2020-09-21 11:51:41 +00:00
|
|
|
/*
|
|
|
|
* Mask all interrupts. Interrupts are disabled globally (in m/sstatus)
|
|
|
|
* for U-Boot, but we will need to read m/sip to determine if we get an
|
|
|
|
* IPI
|
|
|
|
*/
|
2018-12-03 05:27:40 +00:00
|
|
|
csrw MODE_PREFIX(ie), zero
|
2017-12-26 05:55:48 +00:00
|
|
|
|
2020-04-16 15:09:30 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SMP)
|
2019-03-17 18:28:37 +00:00
|
|
|
/* check if hart is within range */
|
|
|
|
/* tp: hart id */
|
|
|
|
li t0, CONFIG_NR_CPUS
|
|
|
|
bge tp, t0, hart_out_of_bounds_loop
|
|
|
|
|
|
|
|
/* set xSIE bit to receive IPIs */
|
2019-08-21 19:14:43 +00:00
|
|
|
#if CONFIG_IS_ENABLED(RISCV_MMODE)
|
2019-03-17 18:28:37 +00:00
|
|
|
li t0, MIE_MSIE
|
|
|
|
#else
|
|
|
|
li t0, SIE_SSIE
|
|
|
|
#endif
|
|
|
|
csrs MODE_PREFIX(ie), t0
|
|
|
|
#endif
|
|
|
|
|
2017-12-26 05:55:48 +00:00
|
|
|
/*
|
|
|
|
* Set stackpointer in internal/ex RAM to call board_init_f
|
|
|
|
*/
|
|
|
|
call_board_init_f:
|
2018-11-22 10:26:24 +00:00
|
|
|
li t0, -16
|
2019-08-21 19:14:45 +00:00
|
|
|
#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
|
|
|
|
li t1, CONFIG_SPL_STACK
|
|
|
|
#else
|
2018-11-22 10:26:24 +00:00
|
|
|
li t1, CONFIG_SYS_INIT_SP_ADDR
|
2019-08-21 19:14:45 +00:00
|
|
|
#endif
|
2018-11-22 10:26:24 +00:00
|
|
|
and sp, t1, t0 /* force 16 byte alignment */
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
call_board_init_f_0:
|
|
|
|
mv a0, sp
|
|
|
|
jal board_init_f_alloc_reserve
|
2019-03-17 18:28:37 +00:00
|
|
|
|
|
|
|
/*
|
riscv: Ensure gp is NULL or points to valid data
This ensures constructs like `if (gd & gd->...) { ... }` work when
accessing the global data pointer. Without this change, it was possible for
a very early trap to cause _exit_trap to directly or indirectly (through
printf) to read arbitrary memory. This could cause a second trap,
preventing show_regs from being printed.
printf (and specifically puts) uses gd to determine what function to print
with. These functions in turn use gd to find the serial device, etc.
However, before accessing gd, puts first checks to see if it is non-NULL.
This indicates an existing (perhaps undocumented) assumption that either gd
is NULL or it is completely valid.
Before this patch, gd either points to unexpected data (because it retains
the value it did from the prior-stage) or points to uninitialized data
(because it has not yet been initialized by board_init_f_init_reserve)
until the hart has acquired available_harts_lock. This can cause two
problems, depending on the value of gd->flags. If GD_FLG_SERIAL_READY is
unset, then some garbage data will be printed to stdout, but there will not
be a second trap. However, if GD_FLG_SERIAL_READY is set, then puts will
try to print with serial_puts, which will likely cause a second trap.
After this patch, gd is zero up until either a hart has set it in
wait_for_gd_init, or until it is set by arch_init_gd. This prevents its
usage before its data is initialized because both handle_trap and puts
ensure that gd is nonzero before using it. After gd has been set, it is OK
to access it because its data has been cleared (and so flags is valid).
XIP cannot use locks because flash is not writable. This leaves it
vulnerable to the same class of bugs regarding already-pending IPIs as
before this series. Fixing that would require finding another method of
synchronization, which is outside the scope of this series.
Fixes: 7c6ca03eae ("riscv: additional crash information")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bin.meng@windriver.com>
Reviewed-by: Rick Chen <rick@andestech.com>
2020-09-21 11:51:40 +00:00
|
|
|
* Save global data pointer for later. We don't set it here because it
|
|
|
|
* is not initialized yet.
|
2019-03-17 18:28:37 +00:00
|
|
|
*/
|
riscv: Ensure gp is NULL or points to valid data
This ensures constructs like `if (gd & gd->...) { ... }` work when
accessing the global data pointer. Without this change, it was possible for
a very early trap to cause _exit_trap to directly or indirectly (through
printf) to read arbitrary memory. This could cause a second trap,
preventing show_regs from being printed.
printf (and specifically puts) uses gd to determine what function to print
with. These functions in turn use gd to find the serial device, etc.
However, before accessing gd, puts first checks to see if it is non-NULL.
This indicates an existing (perhaps undocumented) assumption that either gd
is NULL or it is completely valid.
Before this patch, gd either points to unexpected data (because it retains
the value it did from the prior-stage) or points to uninitialized data
(because it has not yet been initialized by board_init_f_init_reserve)
until the hart has acquired available_harts_lock. This can cause two
problems, depending on the value of gd->flags. If GD_FLG_SERIAL_READY is
unset, then some garbage data will be printed to stdout, but there will not
be a second trap. However, if GD_FLG_SERIAL_READY is set, then puts will
try to print with serial_puts, which will likely cause a second trap.
After this patch, gd is zero up until either a hart has set it in
wait_for_gd_init, or until it is set by arch_init_gd. This prevents its
usage before its data is initialized because both handle_trap and puts
ensure that gd is nonzero before using it. After gd has been set, it is OK
to access it because its data has been cleared (and so flags is valid).
XIP cannot use locks because flash is not writable. This leaves it
vulnerable to the same class of bugs regarding already-pending IPIs as
before this series. Fixing that would require finding another method of
synchronization, which is outside the scope of this series.
Fixes: 7c6ca03eae ("riscv: additional crash information")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bin.meng@windriver.com>
Reviewed-by: Rick Chen <rick@andestech.com>
2020-09-21 11:51:40 +00:00
|
|
|
mv s0, a0
|
2019-03-17 18:28:37 +00:00
|
|
|
|
|
|
|
/* setup stack */
|
2020-04-16 15:09:30 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SMP)
|
2019-03-17 18:28:37 +00:00
|
|
|
/* tp: hart id */
|
|
|
|
slli t0, tp, CONFIG_STACK_SIZE_SHIFT
|
|
|
|
sub sp, a0, t0
|
|
|
|
#else
|
2017-12-26 05:55:48 +00:00
|
|
|
mv sp, a0
|
2019-03-17 18:28:37 +00:00
|
|
|
#endif
|
|
|
|
|
2019-04-30 05:49:33 +00:00
|
|
|
#ifndef CONFIG_XIP
|
2019-03-17 18:28:37 +00:00
|
|
|
/*
|
|
|
|
* Pick hart to initialize global data and run U-Boot. The other harts
|
|
|
|
* wait for initialization to complete.
|
|
|
|
*/
|
|
|
|
la t0, hart_lottery
|
2020-11-13 11:47:51 +00:00
|
|
|
li t1, 1
|
2019-03-17 18:28:37 +00:00
|
|
|
amoswap.w s2, t1, 0(t0)
|
|
|
|
bnez s2, wait_for_gd_init
|
2019-04-30 05:49:33 +00:00
|
|
|
#else
|
riscv: Ensure gp is NULL or points to valid data
This ensures constructs like `if (gd & gd->...) { ... }` work when
accessing the global data pointer. Without this change, it was possible for
a very early trap to cause _exit_trap to directly or indirectly (through
printf) to read arbitrary memory. This could cause a second trap,
preventing show_regs from being printed.
printf (and specifically puts) uses gd to determine what function to print
with. These functions in turn use gd to find the serial device, etc.
However, before accessing gd, puts first checks to see if it is non-NULL.
This indicates an existing (perhaps undocumented) assumption that either gd
is NULL or it is completely valid.
Before this patch, gd either points to unexpected data (because it retains
the value it did from the prior-stage) or points to uninitialized data
(because it has not yet been initialized by board_init_f_init_reserve)
until the hart has acquired available_harts_lock. This can cause two
problems, depending on the value of gd->flags. If GD_FLG_SERIAL_READY is
unset, then some garbage data will be printed to stdout, but there will not
be a second trap. However, if GD_FLG_SERIAL_READY is set, then puts will
try to print with serial_puts, which will likely cause a second trap.
After this patch, gd is zero up until either a hart has set it in
wait_for_gd_init, or until it is set by arch_init_gd. This prevents its
usage before its data is initialized because both handle_trap and puts
ensure that gd is nonzero before using it. After gd has been set, it is OK
to access it because its data has been cleared (and so flags is valid).
XIP cannot use locks because flash is not writable. This leaves it
vulnerable to the same class of bugs regarding already-pending IPIs as
before this series. Fixing that would require finding another method of
synchronization, which is outside the scope of this series.
Fixes: 7c6ca03eae ("riscv: additional crash information")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bin.meng@windriver.com>
Reviewed-by: Rick Chen <rick@andestech.com>
2020-09-21 11:51:40 +00:00
|
|
|
/*
|
|
|
|
* FIXME: gp is set before it is initialized. If an XIP U-Boot ever
|
|
|
|
* encounters a pending IPI on boot it is liable to jump to whatever
|
|
|
|
* memory happens to be in ipi_data.addr on boot. It may also run into
|
|
|
|
* problems if it encounters an exception too early (because printf/puts
|
|
|
|
* accesses gd).
|
|
|
|
*/
|
|
|
|
mv gp, s0
|
2019-04-30 05:49:33 +00:00
|
|
|
bnez tp, secondary_hart_loop
|
|
|
|
#endif
|
2018-11-22 10:26:29 +00:00
|
|
|
|
2019-04-30 05:49:35 +00:00
|
|
|
#ifdef CONFIG_OF_PRIOR_STAGE
|
2018-11-22 10:26:29 +00:00
|
|
|
la t0, prior_stage_fdt_address
|
|
|
|
SREG s1, 0(t0)
|
2019-04-30 05:49:35 +00:00
|
|
|
#endif
|
2018-11-22 10:26:29 +00:00
|
|
|
|
2017-12-26 05:55:48 +00:00
|
|
|
jal board_init_f_init_reserve
|
|
|
|
|
2020-04-21 18:15:01 +00:00
|
|
|
SREG s1, GD_FIRMWARE_FDT_ADDR(gp)
|
2018-12-12 14:12:45 +00:00
|
|
|
/* save the boot hart id to global_data */
|
2019-03-17 18:28:36 +00:00
|
|
|
SREG tp, GD_BOOT_HART(gp)
|
2018-12-12 14:12:45 +00:00
|
|
|
|
2019-04-30 05:49:33 +00:00
|
|
|
#ifndef CONFIG_XIP
|
2019-03-17 18:28:37 +00:00
|
|
|
la t0, available_harts_lock
|
2020-09-21 11:51:39 +00:00
|
|
|
amoswap.w.rl zero, zero, 0(t0)
|
2019-03-17 18:28:37 +00:00
|
|
|
|
|
|
|
wait_for_gd_init:
|
|
|
|
la t0, available_harts_lock
|
|
|
|
li t1, 1
|
2020-09-21 11:51:39 +00:00
|
|
|
1: amoswap.w.aq t1, t1, 0(t0)
|
2019-03-17 18:28:37 +00:00
|
|
|
bnez t1, 1b
|
|
|
|
|
riscv: Ensure gp is NULL or points to valid data
This ensures constructs like `if (gd & gd->...) { ... }` work when
accessing the global data pointer. Without this change, it was possible for
a very early trap to cause _exit_trap to directly or indirectly (through
printf) to read arbitrary memory. This could cause a second trap,
preventing show_regs from being printed.
printf (and specifically puts) uses gd to determine what function to print
with. These functions in turn use gd to find the serial device, etc.
However, before accessing gd, puts first checks to see if it is non-NULL.
This indicates an existing (perhaps undocumented) assumption that either gd
is NULL or it is completely valid.
Before this patch, gd either points to unexpected data (because it retains
the value it did from the prior-stage) or points to uninitialized data
(because it has not yet been initialized by board_init_f_init_reserve)
until the hart has acquired available_harts_lock. This can cause two
problems, depending on the value of gd->flags. If GD_FLG_SERIAL_READY is
unset, then some garbage data will be printed to stdout, but there will not
be a second trap. However, if GD_FLG_SERIAL_READY is set, then puts will
try to print with serial_puts, which will likely cause a second trap.
After this patch, gd is zero up until either a hart has set it in
wait_for_gd_init, or until it is set by arch_init_gd. This prevents its
usage before its data is initialized because both handle_trap and puts
ensure that gd is nonzero before using it. After gd has been set, it is OK
to access it because its data has been cleared (and so flags is valid).
XIP cannot use locks because flash is not writable. This leaves it
vulnerable to the same class of bugs regarding already-pending IPIs as
before this series. Fixing that would require finding another method of
synchronization, which is outside the scope of this series.
Fixes: 7c6ca03eae ("riscv: additional crash information")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bin.meng@windriver.com>
Reviewed-by: Rick Chen <rick@andestech.com>
2020-09-21 11:51:40 +00:00
|
|
|
/*
|
|
|
|
* Set the global data pointer only when gd_t has been initialized.
|
|
|
|
* This was already set by arch_setup_gd on the boot hart, but all other
|
|
|
|
* harts' global data pointers gets set here.
|
|
|
|
*/
|
|
|
|
mv gp, s0
|
|
|
|
|
2019-03-17 18:28:37 +00:00
|
|
|
/* register available harts in the available_harts mask */
|
|
|
|
li t1, 1
|
|
|
|
sll t1, t1, tp
|
|
|
|
LREG t2, GD_AVAILABLE_HARTS(gp)
|
|
|
|
or t2, t2, t1
|
|
|
|
SREG t2, GD_AVAILABLE_HARTS(gp)
|
|
|
|
|
2020-09-21 11:51:39 +00:00
|
|
|
amoswap.w.rl zero, zero, 0(t0)
|
2019-03-17 18:28:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Continue on hart lottery winner, others branch to
|
|
|
|
* secondary_hart_loop.
|
|
|
|
*/
|
|
|
|
bnez s2, secondary_hart_loop
|
2019-04-30 05:49:33 +00:00
|
|
|
#endif
|
2019-03-17 18:28:37 +00:00
|
|
|
|
2019-03-17 18:28:35 +00:00
|
|
|
/* Enable cache */
|
|
|
|
jal icache_enable
|
|
|
|
jal dcache_enable
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_UART
|
|
|
|
jal debug_uart_init
|
|
|
|
#endif
|
|
|
|
|
2018-11-22 10:26:24 +00:00
|
|
|
mv a0, zero /* a0 <-- boot_flags = 0 */
|
|
|
|
la t5, board_init_f
|
2019-08-21 19:14:45 +00:00
|
|
|
jalr t5 /* jump to board_init_f() */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SPL_BUILD
|
|
|
|
spl_clear_bss:
|
|
|
|
la t0, __bss_start
|
|
|
|
la t1, __bss_end
|
2019-08-21 19:14:46 +00:00
|
|
|
beq t0, t1, spl_stack_gd_setup
|
2019-08-21 19:14:45 +00:00
|
|
|
|
|
|
|
spl_clear_bss_loop:
|
|
|
|
SREG zero, 0(t0)
|
|
|
|
addi t0, t0, REGBYTES
|
2019-11-14 05:52:27 +00:00
|
|
|
blt t0, t1, spl_clear_bss_loop
|
2019-08-21 19:14:45 +00:00
|
|
|
|
2019-08-21 19:14:46 +00:00
|
|
|
spl_stack_gd_setup:
|
|
|
|
jal spl_relocate_stack_gd
|
|
|
|
|
|
|
|
/* skip setup if we did not relocate */
|
|
|
|
beqz a0, spl_call_board_init_r
|
|
|
|
mv s0, a0
|
|
|
|
|
|
|
|
/* setup stack on main hart */
|
2020-04-16 15:09:30 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SMP)
|
2019-08-21 19:14:46 +00:00
|
|
|
/* tp: hart id */
|
|
|
|
slli t0, tp, CONFIG_STACK_SIZE_SHIFT
|
|
|
|
sub sp, s0, t0
|
|
|
|
#else
|
|
|
|
mv sp, s0
|
|
|
|
#endif
|
|
|
|
|
2020-06-29 08:27:28 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SMP)
|
2019-08-21 19:14:46 +00:00
|
|
|
/* set new stack and global data pointer on secondary harts */
|
|
|
|
spl_secondary_hart_stack_gd_setup:
|
|
|
|
la a0, secondary_hart_relocate
|
|
|
|
mv a1, s0
|
|
|
|
mv a2, s0
|
2019-12-08 22:28:51 +00:00
|
|
|
mv a3, zero
|
2019-08-21 19:14:46 +00:00
|
|
|
jal smp_call_function
|
|
|
|
|
|
|
|
/* hang if relocation of secondary harts has failed */
|
|
|
|
beqz a0, 1f
|
|
|
|
mv a1, a0
|
|
|
|
la a0, secondary_harts_relocation_error
|
|
|
|
jal printf
|
|
|
|
jal hang
|
2020-06-29 08:27:28 +00:00
|
|
|
#endif
|
2019-08-21 19:14:46 +00:00
|
|
|
|
|
|
|
/* set new global data pointer on main hart */
|
|
|
|
1: mv gp, s0
|
|
|
|
|
2019-08-21 19:14:45 +00:00
|
|
|
spl_call_board_init_r:
|
|
|
|
mv a0, zero
|
|
|
|
mv a1, zero
|
|
|
|
jal board_init_r
|
|
|
|
#endif
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
/*
|
2019-12-28 17:44:45 +00:00
|
|
|
* void relocate_code(addr_sp, gd, addr_moni)
|
2017-12-26 05:55:48 +00:00
|
|
|
*
|
|
|
|
* This "function" does not return, instead it continues in RAM
|
|
|
|
* after relocating the monitor code.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
.globl relocate_code
|
|
|
|
relocate_code:
|
2018-11-22 10:26:24 +00:00
|
|
|
mv s2, a0 /* save addr_sp */
|
|
|
|
mv s3, a1 /* save addr of gd */
|
|
|
|
mv s4, a2 /* save addr of destination */
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
*Set up the stack
|
|
|
|
*/
|
|
|
|
stack_setup:
|
2020-04-16 15:09:30 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SMP)
|
2019-03-17 18:28:37 +00:00
|
|
|
/* tp: hart id */
|
|
|
|
slli t0, tp, CONFIG_STACK_SIZE_SHIFT
|
|
|
|
sub sp, s2, t0
|
|
|
|
#else
|
2018-11-22 10:26:24 +00:00
|
|
|
mv sp, s2
|
2019-03-17 18:28:37 +00:00
|
|
|
#endif
|
|
|
|
|
2018-11-22 10:26:24 +00:00
|
|
|
la t0, _start
|
|
|
|
sub t6, s4, t0 /* t6 <- relocation offset */
|
|
|
|
beq t0, s4, clear_bss /* skip relocation */
|
2017-12-26 05:55:48 +00:00
|
|
|
|
2018-11-22 10:26:24 +00:00
|
|
|
mv t1, s4 /* t1 <- scratch for copy_loop */
|
|
|
|
la t3, __bss_start
|
|
|
|
sub t3, t3, t0 /* t3 <- __bss_start_ofs */
|
|
|
|
add t2, t0, t3 /* t2 <- source end address */
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
copy_loop:
|
2018-11-22 10:26:24 +00:00
|
|
|
LREG t5, 0(t0)
|
|
|
|
addi t0, t0, REGBYTES
|
|
|
|
SREG t5, 0(t1)
|
|
|
|
addi t1, t1, REGBYTES
|
|
|
|
blt t0, t2, copy_loop
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update dynamic relocations after board_init_f
|
|
|
|
*/
|
|
|
|
fix_rela_dyn:
|
2018-11-22 10:26:24 +00:00
|
|
|
la t1, __rel_dyn_start
|
|
|
|
la t2, __rel_dyn_end
|
|
|
|
beq t1, t2, clear_bss
|
|
|
|
add t1, t1, t6 /* t1 <- rela_dyn_start in RAM */
|
|
|
|
add t2, t2, t6 /* t2 <- rela_dyn_end in RAM */
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* skip first reserved entry: address, type, addend
|
|
|
|
*/
|
2019-08-11 12:45:29 +00:00
|
|
|
j 10f
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
6:
|
2018-11-22 10:26:24 +00:00
|
|
|
LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */
|
|
|
|
li t3, R_RISCV_RELATIVE /* reloc type R_RISCV_RELATIVE */
|
|
|
|
bne t5, t3, 8f /* skip non-RISCV_RELOC entries */
|
|
|
|
LREG t3, -(REGBYTES*3)(t1)
|
|
|
|
LREG t5, -(REGBYTES)(t1) /* t5 <-- addend */
|
|
|
|
add t5, t5, t6 /* t5 <-- location to fix up in RAM */
|
|
|
|
add t3, t3, t6 /* t3 <-- location to fix up in RAM */
|
|
|
|
SREG t5, 0(t3)
|
2019-08-11 12:45:29 +00:00
|
|
|
j 10f
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
8:
|
2018-11-22 10:26:24 +00:00
|
|
|
la t4, __dyn_sym_start
|
|
|
|
add t4, t4, t6
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
9:
|
2018-11-22 10:26:24 +00:00
|
|
|
LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */
|
|
|
|
srli t0, t5, SYM_INDEX /* t0 <--- sym table index */
|
|
|
|
andi t5, t5, 0xFF /* t5 <--- relocation type */
|
|
|
|
li t3, RELOC_TYPE
|
|
|
|
bne t5, t3, 10f /* skip non-addned entries */
|
2017-12-26 05:55:48 +00:00
|
|
|
|
2018-11-22 10:26:24 +00:00
|
|
|
LREG t3, -(REGBYTES*3)(t1)
|
|
|
|
li t5, SYM_SIZE
|
|
|
|
mul t0, t0, t5
|
2018-11-22 10:26:29 +00:00
|
|
|
add s5, t4, t0
|
2019-08-11 12:45:29 +00:00
|
|
|
LREG t0, -(REGBYTES)(t1) /* t0 <-- addend */
|
2018-11-22 10:26:29 +00:00
|
|
|
LREG t5, REGBYTES(s5)
|
2019-08-11 12:45:29 +00:00
|
|
|
add t5, t5, t0
|
2018-11-22 10:26:24 +00:00
|
|
|
add t5, t5, t6 /* t5 <-- location to fix up in RAM */
|
|
|
|
add t3, t3, t6 /* t3 <-- location to fix up in RAM */
|
|
|
|
SREG t5, 0(t3)
|
2017-12-26 05:55:48 +00:00
|
|
|
10:
|
2018-11-22 10:26:24 +00:00
|
|
|
addi t1, t1, (REGBYTES*3)
|
2019-08-11 12:45:29 +00:00
|
|
|
ble t1, t2, 6b
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* trap update
|
|
|
|
*/
|
2018-11-22 10:26:24 +00:00
|
|
|
la t0, trap_entry
|
|
|
|
add t0, t0, t6
|
2018-12-03 05:27:40 +00:00
|
|
|
csrw MODE_PREFIX(tvec), t0
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
clear_bss:
|
2018-11-22 10:26:24 +00:00
|
|
|
la t0, __bss_start /* t0 <- rel __bss_start in FLASH */
|
|
|
|
add t0, t0, t6 /* t0 <- rel __bss_start in RAM */
|
|
|
|
la t1, __bss_end /* t1 <- rel __bss_end in FLASH */
|
|
|
|
add t1, t1, t6 /* t1 <- rel __bss_end in RAM */
|
2019-03-17 18:28:37 +00:00
|
|
|
beq t0, t1, relocate_secondary_harts
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
clbss_l:
|
2018-11-22 10:26:28 +00:00
|
|
|
SREG zero, 0(t0) /* clear loop... */
|
2018-11-22 10:26:24 +00:00
|
|
|
addi t0, t0, REGBYTES
|
2019-11-14 05:52:27 +00:00
|
|
|
blt t0, t1, clbss_l
|
2017-12-26 05:55:48 +00:00
|
|
|
|
2019-03-17 18:28:37 +00:00
|
|
|
relocate_secondary_harts:
|
2020-04-16 15:09:30 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SMP)
|
2019-03-17 18:28:37 +00:00
|
|
|
/* send relocation IPI */
|
|
|
|
la t0, secondary_hart_relocate
|
|
|
|
add a0, t0, t6
|
|
|
|
|
|
|
|
/* store relocation offset */
|
|
|
|
mv s5, t6
|
|
|
|
|
|
|
|
mv a1, s2
|
|
|
|
mv a2, s3
|
2019-12-08 22:28:51 +00:00
|
|
|
mv a3, zero
|
2019-03-17 18:28:37 +00:00
|
|
|
jal smp_call_function
|
|
|
|
|
2019-03-17 18:28:40 +00:00
|
|
|
/* hang if relocation of secondary harts has failed */
|
|
|
|
beqz a0, 1f
|
|
|
|
mv a1, a0
|
|
|
|
la a0, secondary_harts_relocation_error
|
|
|
|
jal printf
|
|
|
|
jal hang
|
|
|
|
|
2019-03-17 18:28:37 +00:00
|
|
|
/* restore relocation offset */
|
2019-03-17 18:28:40 +00:00
|
|
|
1: mv t6, s5
|
2019-03-17 18:28:37 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-26 05:55:48 +00:00
|
|
|
/*
|
|
|
|
* We are done. Do not return, instead branch to second part of board
|
|
|
|
* initialization, now running from RAM.
|
|
|
|
*/
|
|
|
|
call_board_init_r:
|
2018-11-07 01:34:06 +00:00
|
|
|
jal invalidate_icache_all
|
|
|
|
jal flush_dcache_all
|
2020-01-27 21:39:44 +00:00
|
|
|
la t0, board_init_r /* offset of board_init_r() */
|
|
|
|
add t4, t0, t6 /* real address of board_init_r() */
|
2017-12-26 05:55:48 +00:00
|
|
|
/*
|
|
|
|
* setup parameters for board_init_r
|
|
|
|
*/
|
2018-11-22 10:26:24 +00:00
|
|
|
mv a0, s3 /* gd_t */
|
|
|
|
mv a1, s4 /* dest_addr */
|
2017-12-26 05:55:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* jump to it ...
|
|
|
|
*/
|
2018-11-22 10:26:24 +00:00
|
|
|
jr t4 /* jump to board_init_r() */
|
2019-03-17 18:28:37 +00:00
|
|
|
|
2020-04-16 15:09:30 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SMP)
|
2019-03-17 18:28:37 +00:00
|
|
|
hart_out_of_bounds_loop:
|
|
|
|
/* Harts in this loop are out of bounds, increase CONFIG_NR_CPUS. */
|
|
|
|
wfi
|
|
|
|
j hart_out_of_bounds_loop
|
|
|
|
|
|
|
|
/* SMP relocation entry */
|
|
|
|
secondary_hart_relocate:
|
|
|
|
/* a1: new sp */
|
|
|
|
/* a2: new gd */
|
|
|
|
/* tp: hart id */
|
|
|
|
|
|
|
|
/* setup stack */
|
|
|
|
slli t0, tp, CONFIG_STACK_SIZE_SHIFT
|
|
|
|
sub sp, a1, t0
|
|
|
|
|
|
|
|
/* update global data pointer */
|
|
|
|
mv gp, a2
|
|
|
|
#endif
|
|
|
|
|
2020-09-21 11:51:41 +00:00
|
|
|
/*
|
|
|
|
* Interrupts are disabled globally, but they can still be read from m/sip. The
|
|
|
|
* wfi function will wake us up if we get an IPI, even if we do not trap.
|
|
|
|
*/
|
2019-03-17 18:28:37 +00:00
|
|
|
secondary_hart_loop:
|
|
|
|
wfi
|
|
|
|
|
2020-04-16 15:09:30 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SMP)
|
2019-03-17 18:28:37 +00:00
|
|
|
csrr t0, MODE_PREFIX(ip)
|
2019-08-21 19:14:43 +00:00
|
|
|
#if CONFIG_IS_ENABLED(RISCV_MMODE)
|
2019-03-17 18:28:37 +00:00
|
|
|
andi t0, t0, MIE_MSIE
|
|
|
|
#else
|
|
|
|
andi t0, t0, SIE_SSIE
|
|
|
|
#endif
|
|
|
|
beqz t0, secondary_hart_loop
|
|
|
|
|
|
|
|
mv a0, tp
|
|
|
|
jal handle_ipi
|
|
|
|
#endif
|
|
|
|
|
|
|
|
j secondary_hart_loop
|