arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Jean-Paul Etienne <fractalclone@gmail.com>
|
2018-11-01 22:25:13 -06:00
|
|
|
* Copyright (c) 2018 Foundries.io Ltd
|
2020-07-21 16:00:39 +02:00
|
|
|
* Copyright (c) 2020 BayLibre, SAS
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
*/
|
|
|
|
|
2022-05-09 13:56:13 +02:00
|
|
|
#include <zephyr/toolchain.h>
|
|
|
|
#include <zephyr/linker/sections.h>
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
#include <offsets_short.h>
|
2022-05-09 13:56:13 +02:00
|
|
|
#include <zephyr/arch/cpu.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/syscall.h>
|
|
|
|
#include <zephyr/arch/riscv/csr.h>
|
|
|
|
#include <zephyr/arch/riscv/syscall.h>
|
2022-02-22 14:09:55 -05:00
|
|
|
#include "asm_macros.inc"
|
2020-03-11 18:15:29 -07:00
|
|
|
|
2022-12-27 20:14:51 +01:00
|
|
|
#ifdef CONFIG_RISCV_SOC_HAS_ISR_STACKING
|
|
|
|
#include <soc_isr_stacking.h>
|
|
|
|
#endif
|
|
|
|
|
2023-01-17 23:32:21 -05:00
|
|
|
/* Convenience macro for loading/storing register states. */
|
2022-06-07 09:37:59 -04:00
|
|
|
#define DO_CALLER_SAVED(op) \
|
2022-06-15 18:41:36 -04:00
|
|
|
RV_E( op t0, __z_arch_esf_t_t0_OFFSET(sp) );\
|
2022-06-07 09:37:59 -04:00
|
|
|
RV_E( op t1, __z_arch_esf_t_t1_OFFSET(sp) );\
|
2022-06-15 18:41:36 -04:00
|
|
|
RV_E( op t2, __z_arch_esf_t_t2_OFFSET(sp) );\
|
|
|
|
RV_I( op t3, __z_arch_esf_t_t3_OFFSET(sp) );\
|
|
|
|
RV_I( op t4, __z_arch_esf_t_t4_OFFSET(sp) );\
|
|
|
|
RV_I( op t5, __z_arch_esf_t_t5_OFFSET(sp) );\
|
|
|
|
RV_I( op t6, __z_arch_esf_t_t6_OFFSET(sp) );\
|
|
|
|
RV_E( op a0, __z_arch_esf_t_a0_OFFSET(sp) );\
|
|
|
|
RV_E( op a1, __z_arch_esf_t_a1_OFFSET(sp) );\
|
|
|
|
RV_E( op a2, __z_arch_esf_t_a2_OFFSET(sp) );\
|
|
|
|
RV_E( op a3, __z_arch_esf_t_a3_OFFSET(sp) );\
|
|
|
|
RV_E( op a4, __z_arch_esf_t_a4_OFFSET(sp) );\
|
|
|
|
RV_E( op a5, __z_arch_esf_t_a5_OFFSET(sp) );\
|
|
|
|
RV_I( op a6, __z_arch_esf_t_a6_OFFSET(sp) );\
|
|
|
|
RV_I( op a7, __z_arch_esf_t_a7_OFFSET(sp) );\
|
|
|
|
RV_E( op ra, __z_arch_esf_t_ra_OFFSET(sp) )
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2023-01-06 17:46:21 -05:00
|
|
|
.macro get_current_cpu dst
|
|
|
|
#if defined(CONFIG_SMP) || defined(CONFIG_USERSPACE)
|
|
|
|
csrr \dst, mscratch
|
2022-03-09 23:47:38 -05:00
|
|
|
#else
|
2023-01-06 17:46:21 -05:00
|
|
|
la \dst, _kernel + ___kernel_t_cpus_OFFSET
|
2022-03-09 23:47:38 -05:00
|
|
|
#endif
|
2023-01-06 17:46:21 -05:00
|
|
|
.endm
|
2022-03-09 23:47:38 -05:00
|
|
|
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
/* imports */
|
|
|
|
GDATA(_sw_isr_table)
|
|
|
|
GTEXT(__soc_is_irq)
|
|
|
|
GTEXT(__soc_handle_irq)
|
|
|
|
GTEXT(_Fault)
|
2018-11-01 22:25:13 -06:00
|
|
|
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE
|
|
|
|
GTEXT(__soc_save_context)
|
|
|
|
GTEXT(__soc_restore_context)
|
|
|
|
#endif /* CONFIG_RISCV_SOC_CONTEXT_SAVE */
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
GTEXT(z_riscv_fatal_error)
|
2022-03-07 17:01:36 -05:00
|
|
|
GTEXT(z_get_next_switch_handle)
|
|
|
|
GTEXT(z_riscv_switch)
|
|
|
|
GTEXT(z_riscv_thread_start)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2018-04-06 07:48:53 -04:00
|
|
|
#ifdef CONFIG_TRACING
|
2019-09-19 09:25:19 +02:00
|
|
|
GTEXT(sys_trace_isr_enter)
|
2022-05-11 12:29:46 +02:00
|
|
|
GTEXT(sys_trace_isr_exit)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
#endif
|
|
|
|
|
2020-07-21 16:00:39 +02:00
|
|
|
#ifdef CONFIG_USERSPACE
|
2022-02-24 22:30:03 -05:00
|
|
|
GDATA(_k_syscall_table)
|
2020-07-21 16:00:39 +02:00
|
|
|
#endif
|
|
|
|
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
/* exports */
|
2022-06-21 10:43:40 +02:00
|
|
|
GTEXT(_isr_wrapper)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
|
|
|
/* use ABI name of registers for the sake of simplicity */
|
|
|
|
|
|
|
|
/*
|
2018-11-01 22:25:13 -06:00
|
|
|
* Generic architecture-level IRQ handling, along with callouts to
|
|
|
|
* SoC-specific routines.
|
|
|
|
*
|
|
|
|
* Architecture level IRQ handling includes basic context save/restore
|
|
|
|
* of standard registers and calling ISRs registered at Zephyr's driver
|
|
|
|
* level.
|
|
|
|
*
|
|
|
|
* Since RISC-V does not completely prescribe IRQ handling behavior,
|
|
|
|
* implementations vary (some implementations also deviate from
|
|
|
|
* what standard behavior is defined). Hence, the arch level code expects
|
|
|
|
* the following functions to be provided at the SOC level:
|
|
|
|
*
|
|
|
|
* - __soc_is_irq: decide if we're handling an interrupt or an exception
|
|
|
|
* - __soc_handle_irq: handle SoC-specific details for a pending IRQ
|
|
|
|
* (e.g. clear a pending bit in a SoC-specific register)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
*
|
2019-01-31 04:40:07 -06:00
|
|
|
* If CONFIG_RISCV_SOC_CONTEXT_SAVE=y, calls to SoC-level context save/restore
|
2018-11-01 22:25:13 -06:00
|
|
|
* routines are also made here. For details, see the Kconfig help text.
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler called upon each exception/interrupt/fault
|
|
|
|
*/
|
2022-06-21 10:43:40 +02:00
|
|
|
SECTION_FUNC(exception.entry, _isr_wrapper)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
#ifdef CONFIG_USERSPACE
|
2023-01-06 17:46:21 -05:00
|
|
|
/* retrieve address of _current_cpu preserving s0 */
|
|
|
|
csrrw s0, mscratch, s0
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2023-01-06 17:46:21 -05:00
|
|
|
/* preserve t0 and t1 temporarily */
|
|
|
|
sr t0, _curr_cpu_arch_user_exc_tmp0(s0)
|
|
|
|
sr t1, _curr_cpu_arch_user_exc_tmp1(s0)
|
2022-03-09 17:46:30 -05:00
|
|
|
|
2023-01-06 17:46:21 -05:00
|
|
|
/* determine if we come from user space */
|
|
|
|
csrr t0, mstatus
|
|
|
|
li t1, MSTATUS_MPP
|
|
|
|
and t0, t0, t1
|
2022-02-24 22:30:03 -05:00
|
|
|
bnez t0, 1f
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2023-01-06 17:46:21 -05:00
|
|
|
/* in user space we were: switch to our privileged stack */
|
|
|
|
mv t0, sp
|
|
|
|
lr sp, _curr_cpu_arch_user_exc_sp(s0)
|
|
|
|
|
|
|
|
/* Save user stack value. Coming from user space, we know this
|
|
|
|
* can't overflow the privileged stack. The esf will be allocated
|
|
|
|
* later but it is safe to store our saved user sp here. */
|
|
|
|
sr t0, (-__z_arch_esf_t_SIZEOF + __z_arch_esf_t_sp_OFFSET)(sp)
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-06-07 08:04:55 -04:00
|
|
|
/* Make sure tls pointer is sane */
|
|
|
|
lr t0, ___cpu_t_current_OFFSET(s0)
|
|
|
|
lr tp, _thread_offset_to_tls(t0)
|
|
|
|
|
2022-06-07 09:37:59 -04:00
|
|
|
/* Clear our per-thread usermode flag */
|
|
|
|
lui t0, %tprel_hi(is_user_mode)
|
|
|
|
add t0, t0, tp, %tprel_add(is_user_mode)
|
|
|
|
sb zero, %tprel_lo(is_user_mode)(t0)
|
2023-01-06 17:46:21 -05:00
|
|
|
1:
|
|
|
|
/* retrieve original t0/t1 values */
|
|
|
|
lr t0, _curr_cpu_arch_user_exc_tmp0(s0)
|
|
|
|
lr t1, _curr_cpu_arch_user_exc_tmp1(s0)
|
|
|
|
|
|
|
|
/* retrieve original s0 and restore _current_cpu in mscratch */
|
|
|
|
csrrw s0, mscratch, s0
|
2022-02-24 22:30:03 -05:00
|
|
|
#endif
|
|
|
|
|
2023-01-06 17:46:21 -05:00
|
|
|
#ifdef CONFIG_RISCV_SOC_HAS_ISR_STACKING
|
|
|
|
SOC_ISR_SW_STACKING
|
|
|
|
#else
|
|
|
|
/* Save caller-saved registers on current thread stack. */
|
|
|
|
addi sp, sp, -__z_arch_esf_t_SIZEOF
|
|
|
|
DO_CALLER_SAVED(sr) ;
|
|
|
|
#endif /* CONFIG_RISCV_SOC_HAS_ISR_STACKING */
|
|
|
|
|
|
|
|
/* Save s0 in the esf and load it with &_current_cpu. */
|
|
|
|
sr s0, __z_arch_esf_t_s0_OFFSET(sp)
|
|
|
|
get_current_cpu s0
|
|
|
|
|
2022-03-07 17:31:13 -05:00
|
|
|
/* Save MEPC register */
|
|
|
|
csrr t0, mepc
|
|
|
|
sr t0, __z_arch_esf_t_mepc_OFFSET(sp)
|
|
|
|
|
|
|
|
/* Save MSTATUS register */
|
2022-05-18 16:48:17 +02:00
|
|
|
csrr t2, mstatus
|
|
|
|
sr t2, __z_arch_esf_t_mstatus_OFFSET(sp)
|
2022-03-07 17:31:13 -05:00
|
|
|
|
2023-01-17 23:32:21 -05:00
|
|
|
#if defined(CONFIG_FPU_SHARING)
|
|
|
|
/* determine if this is an Illegal Instruction exception */
|
|
|
|
csrr t0, mcause
|
|
|
|
li t1, 2 /* 2 = illegal instruction */
|
|
|
|
bne t0, t1, no_fp
|
|
|
|
/* determine if FPU access was disabled */
|
|
|
|
csrr t0, mstatus
|
|
|
|
li t1, MSTATUS_FS
|
|
|
|
and t0, t0, t1
|
|
|
|
bnez t0, no_fp
|
|
|
|
/* determine if we trapped on an FP instruction. */
|
|
|
|
csrr t2, mtval /* get faulting instruction */
|
|
|
|
andi t0, t2, 0x7f /* keep only the opcode bits */
|
|
|
|
xori t1, t0, 0b1010011 /* OP-FP */
|
|
|
|
beqz t1, is_fp
|
|
|
|
ori t0, t0, 0b0100000
|
|
|
|
xori t1, t0, 0b0100111 /* LOAD-FP / STORE-FP */
|
|
|
|
#if !defined(CONFIG_RISCV_ISA_EXT_C)
|
|
|
|
bnez t1, no_fp
|
|
|
|
#else
|
|
|
|
beqz t1, is_fp
|
|
|
|
/* remaining non RVC (0b11) and RVC with 0b01 are not FP instructions */
|
|
|
|
andi t1, t0, 1
|
|
|
|
bnez t1, no_fp
|
|
|
|
/*
|
|
|
|
* 001...........00 = C.FLD RV32/64 (RV128 = C.LQ)
|
|
|
|
* 001...........10 = C.FLDSP RV32/64 (RV128 = C.LQSP)
|
|
|
|
* 011...........00 = C.FLW RV32 (RV64/128 = C.LD)
|
|
|
|
* 011...........10 = C.FLWSPP RV32 (RV64/128 = C.LDSP)
|
|
|
|
* 101...........00 = C.FSD RV32/64 (RV128 = C.SQ)
|
|
|
|
* 101...........10 = C.FSDSP RV32/64 (RV128 = C.SQSP)
|
|
|
|
* 111...........00 = C.FSW RV32 (RV64/128 = C.SD)
|
|
|
|
* 111...........10 = C.FSWSP RV32 (RV64/128 = C.SDSP)
|
|
|
|
*
|
|
|
|
* so must be .01............. on RV64 and ..1............. on RV32.
|
|
|
|
*/
|
|
|
|
srli t0, t2, 8
|
|
|
|
#if defined(CONFIG_64BIT)
|
|
|
|
andi t1, t0, 0b01100000
|
|
|
|
xori t1, t1, 0b00100000
|
|
|
|
bnez t1, no_fp
|
|
|
|
#else
|
|
|
|
andi t1, t0, 0b00100000
|
|
|
|
beqz t1, no_fp
|
|
|
|
#endif
|
|
|
|
#endif /* CONFIG_RISCV_ISA_EXT_C */
|
|
|
|
|
|
|
|
is_fp: /* Process the FP trap and quickly return from exception */
|
|
|
|
la ra, fp_trap_exit
|
|
|
|
mv a0, sp
|
|
|
|
tail z_riscv_fpu_trap
|
|
|
|
|
|
|
|
no_fp: /* increment _current->arch.exception_depth */
|
|
|
|
lr t0, ___cpu_t_current_OFFSET(s0)
|
|
|
|
lb t1, _thread_offset_to_exception_depth(t0)
|
|
|
|
add t1, t1, 1
|
|
|
|
sb t1, _thread_offset_to_exception_depth(t0)
|
|
|
|
|
|
|
|
/* configure the FPU for exception mode */
|
|
|
|
call z_riscv_fpu_enter_exc
|
|
|
|
#endif
|
2022-02-24 22:30:03 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE
|
|
|
|
/* Handle context saving at SOC level. */
|
|
|
|
addi a0, sp, __z_arch_esf_t_soc_context_OFFSET
|
|
|
|
jal ra, __soc_save_context
|
|
|
|
#endif /* CONFIG_RISCV_SOC_CONTEXT_SAVE */
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-03-09 17:46:30 -05:00
|
|
|
/*
|
|
|
|
* Check if exception is the result of an interrupt or not.
|
|
|
|
* (SOC dependent). Following the RISC-V architecture spec, the MSB
|
|
|
|
* of the mcause register is used to indicate whether an exception
|
|
|
|
* is the result of an interrupt or an exception/fault. But for some
|
|
|
|
* SOCs (like pulpino or riscv-qemu), the MSB is never set to indicate
|
|
|
|
* interrupt. Hence, check for interrupt/exception via the __soc_is_irq
|
|
|
|
* function (that needs to be implemented by each SOC). The result is
|
|
|
|
* returned via register a0 (1: interrupt, 0 exception)
|
|
|
|
*/
|
|
|
|
jal ra, __soc_is_irq
|
|
|
|
|
|
|
|
/* If a0 != 0, jump to is_interrupt */
|
|
|
|
bnez a0, is_interrupt
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2022-03-09 17:46:30 -05:00
|
|
|
/*
|
|
|
|
* If the exception is the result of an ECALL, check whether to
|
|
|
|
* perform a context-switch or an IRQ offload. Otherwise call _Fault
|
|
|
|
* to report the exception.
|
|
|
|
*/
|
|
|
|
csrr t0, mcause
|
|
|
|
li t2, SOC_MCAUSE_EXP_MASK
|
|
|
|
and t0, t0, t2
|
2021-12-21 16:44:57 -08:00
|
|
|
|
2022-03-09 17:46:30 -05:00
|
|
|
/*
|
|
|
|
* If mcause == SOC_MCAUSE_ECALL_EXP, handle system call from
|
|
|
|
* kernel thread.
|
2020-07-21 16:00:39 +02:00
|
|
|
*/
|
2022-02-24 22:30:03 -05:00
|
|
|
li t1, SOC_MCAUSE_ECALL_EXP
|
2022-03-09 17:46:30 -05:00
|
|
|
beq t0, t1, is_kernel_syscall
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
2022-03-09 17:46:30 -05:00
|
|
|
/*
|
2022-02-24 22:30:03 -05:00
|
|
|
* If mcause == SOC_MCAUSE_USER_ECALL_EXP, handle system call
|
|
|
|
* for user mode thread.
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
*/
|
2022-02-24 22:30:03 -05:00
|
|
|
li t1, SOC_MCAUSE_USER_ECALL_EXP
|
2022-03-09 17:46:30 -05:00
|
|
|
beq t0, t1, is_user_syscall
|
|
|
|
#endif /* CONFIG_USERSPACE */
|
2018-08-02 10:38:57 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Call _Fault to handle exception.
|
2019-07-16 15:21:19 -07:00
|
|
|
* Stack pointer is pointing to a z_arch_esf_t structure, pass it
|
2018-08-02 10:38:57 -07:00
|
|
|
* to _Fault (via register a0).
|
2020-07-21 16:00:39 +02:00
|
|
|
* If _Fault shall return, set return address to
|
|
|
|
* no_reschedule to restore stack.
|
2018-08-02 10:38:57 -07:00
|
|
|
*/
|
2022-02-21 21:36:55 -05:00
|
|
|
mv a0, sp
|
2021-07-18 02:07:48 +08:00
|
|
|
la ra, no_reschedule
|
2018-08-02 10:38:57 -07:00
|
|
|
tail _Fault
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2022-03-09 17:46:30 -05:00
|
|
|
is_kernel_syscall:
|
|
|
|
/*
|
|
|
|
* A syscall is the result of an ecall instruction, in which case the
|
|
|
|
* MEPC will contain the address of the ecall instruction.
|
|
|
|
* Increment saved MEPC by 4 to prevent triggering the same ecall
|
|
|
|
* again upon exiting the ISR.
|
|
|
|
*
|
|
|
|
* It's safe to always increment by 4, even with compressed
|
|
|
|
* instructions, because the ecall instruction is always 4 bytes.
|
|
|
|
*/
|
2022-02-22 14:09:55 -05:00
|
|
|
lr t0, __z_arch_esf_t_mepc_OFFSET(sp)
|
2022-03-09 17:46:30 -05:00
|
|
|
addi t0, t0, 4
|
2022-02-22 14:09:55 -05:00
|
|
|
sr t0, __z_arch_esf_t_mepc_OFFSET(sp)
|
2022-03-09 17:46:30 -05:00
|
|
|
|
2022-04-06 22:03:54 -04:00
|
|
|
#ifdef CONFIG_PMP_STACK_GUARD
|
|
|
|
/* Re-activate PMP for m-mode */
|
|
|
|
li t1, MSTATUS_MPP
|
|
|
|
csrc mstatus, t1
|
|
|
|
li t1, MSTATUS_MPRV
|
|
|
|
csrs mstatus, t1
|
|
|
|
#endif
|
|
|
|
|
2022-06-06 17:04:25 +02:00
|
|
|
/* Determine what to do. Operation code is in t0. */
|
|
|
|
lr t0, __z_arch_esf_t_t0_OFFSET(sp)
|
2022-03-09 17:46:30 -05:00
|
|
|
|
2022-06-30 13:02:39 -04:00
|
|
|
.if RV_ECALL_RUNTIME_EXCEPT != 0; .err; .endif
|
2022-06-06 17:04:25 +02:00
|
|
|
beqz t0, do_fault
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
#if defined(CONFIG_IRQ_OFFLOAD)
|
2022-06-30 13:02:39 -04:00
|
|
|
li t1, RV_ECALL_IRQ_OFFLOAD
|
|
|
|
beq t0, t1, do_irq_offload
|
2022-02-24 22:30:03 -05:00
|
|
|
#endif
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-06-30 14:40:52 +02:00
|
|
|
#ifdef CONFIG_RISCV_ALWAYS_SWITCH_THROUGH_ECALL
|
|
|
|
li t1, RV_ECALL_SCHEDULE
|
|
|
|
bne t0, t1, skip_schedule
|
|
|
|
lr a0, __z_arch_esf_t_a0_OFFSET(sp)
|
|
|
|
lr a1, __z_arch_esf_t_a1_OFFSET(sp)
|
|
|
|
j reschedule
|
|
|
|
skip_schedule:
|
|
|
|
#endif
|
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
/* default fault code is K_ERR_KERNEL_OOPS */
|
|
|
|
li a0, 3
|
|
|
|
j 1f
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
do_fault:
|
|
|
|
/* Handle RV_ECALL_RUNTIME_EXCEPT. Retrieve reason in a0, esf in A1. */
|
2022-02-22 14:09:55 -05:00
|
|
|
lr a0, __z_arch_esf_t_a0_OFFSET(sp)
|
2022-02-24 22:30:03 -05:00
|
|
|
1: mv a1, sp
|
|
|
|
tail z_riscv_fatal_error
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
#if defined(CONFIG_IRQ_OFFLOAD)
|
|
|
|
do_irq_offload:
|
2022-03-07 13:24:38 -05:00
|
|
|
/*
|
|
|
|
* Retrieve provided routine and argument from the stack.
|
|
|
|
* Routine pointer is in saved a0, argument in saved a1
|
|
|
|
* so we load them with a1/a0 (reversed).
|
|
|
|
*/
|
|
|
|
lr a1, __z_arch_esf_t_a0_OFFSET(sp)
|
|
|
|
lr a0, __z_arch_esf_t_a1_OFFSET(sp)
|
|
|
|
|
2022-03-09 17:21:27 -05:00
|
|
|
/* Increment _current_cpu->nested */
|
2022-05-18 16:48:17 +02:00
|
|
|
lw t1, ___cpu_t_nested_OFFSET(s0)
|
|
|
|
addi t2, t1, 1
|
|
|
|
sw t2, ___cpu_t_nested_OFFSET(s0)
|
|
|
|
bnez t1, 1f
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
/* Switch to interrupt stack */
|
|
|
|
mv t0, sp
|
2022-03-09 17:21:27 -05:00
|
|
|
lr sp, ___cpu_t_irq_stack_OFFSET(s0)
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-03-14 14:51:04 -04:00
|
|
|
/* Save thread stack pointer on interrupt stack */
|
2022-02-24 22:30:03 -05:00
|
|
|
addi sp, sp, -16
|
|
|
|
sr t0, 0(sp)
|
2022-03-14 14:51:04 -04:00
|
|
|
1:
|
2022-03-07 13:24:38 -05:00
|
|
|
/* Execute provided routine (argument is in a0 already). */
|
|
|
|
jalr ra, a1, 0
|
2022-02-24 22:30:03 -05:00
|
|
|
|
2022-03-07 13:24:38 -05:00
|
|
|
/* Leave through the regular IRQ exit path */
|
|
|
|
j irq_done
|
2022-02-24 22:30:03 -05:00
|
|
|
#endif /* CONFIG_IRQ_OFFLOAD */
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2020-07-21 16:00:39 +02:00
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
is_user_syscall:
|
2022-04-06 22:03:54 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_PMP_STACK_GUARD
|
|
|
|
/*
|
|
|
|
* We came from userspace and need to reconfigure the
|
|
|
|
* PMP for kernel mode stack guard.
|
|
|
|
*/
|
|
|
|
lr a0, ___cpu_t_current_OFFSET(s0)
|
|
|
|
call z_riscv_pmp_stackguard_enable
|
|
|
|
#endif
|
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
/* It is safe to re-enable IRQs now */
|
|
|
|
csrs mstatus, MSTATUS_IEN
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-03-09 17:46:30 -05:00
|
|
|
/*
|
2022-02-24 22:30:03 -05:00
|
|
|
* Same as for is_kernel_syscall: increment saved MEPC by 4 to
|
|
|
|
* prevent triggering the same ecall again upon exiting the ISR.
|
2022-03-09 17:46:30 -05:00
|
|
|
*/
|
2022-02-22 14:09:55 -05:00
|
|
|
lr t1, __z_arch_esf_t_mepc_OFFSET(sp)
|
2022-03-09 17:46:30 -05:00
|
|
|
addi t1, t1, 4
|
2022-02-22 14:09:55 -05:00
|
|
|
sr t1, __z_arch_esf_t_mepc_OFFSET(sp)
|
2022-02-24 22:30:03 -05:00
|
|
|
|
2020-07-21 16:00:39 +02:00
|
|
|
/* Restore argument registers from user stack */
|
2022-02-22 14:09:55 -05:00
|
|
|
lr a0, __z_arch_esf_t_a0_OFFSET(sp)
|
|
|
|
lr a1, __z_arch_esf_t_a1_OFFSET(sp)
|
|
|
|
lr a2, __z_arch_esf_t_a2_OFFSET(sp)
|
|
|
|
lr a3, __z_arch_esf_t_a3_OFFSET(sp)
|
|
|
|
lr a4, __z_arch_esf_t_a4_OFFSET(sp)
|
|
|
|
lr a5, __z_arch_esf_t_a5_OFFSET(sp)
|
2022-06-06 17:04:25 +02:00
|
|
|
lr t0, __z_arch_esf_t_t0_OFFSET(sp)
|
2022-06-07 14:18:11 +02:00
|
|
|
#if defined(CONFIG_RISCV_ISA_RV32E)
|
|
|
|
/* Stack alignment for RV32E is 4 bytes */
|
|
|
|
addi sp, sp, -4
|
|
|
|
mv t1, sp
|
|
|
|
sw t1, 0(sp)
|
|
|
|
#else
|
2020-07-21 16:00:39 +02:00
|
|
|
mv a6, sp
|
2022-06-07 14:18:11 +02:00
|
|
|
#endif /* CONFIG_RISCV_ISA_RV32E */
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
/* validate syscall limit */
|
2022-06-06 17:04:25 +02:00
|
|
|
li t1, K_SYSCALL_LIMIT
|
|
|
|
bltu t0, t1, valid_syscall_id
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
/* bad syscall id. Set arg1 to bad id and set call_id to SYSCALL_BAD */
|
2022-06-06 17:04:25 +02:00
|
|
|
mv a0, t0
|
|
|
|
li t0, K_SYSCALL_BAD
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
valid_syscall_id:
|
|
|
|
|
2022-06-06 17:04:25 +02:00
|
|
|
la t2, _k_syscall_table
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-06-06 17:04:25 +02:00
|
|
|
slli t1, t0, RV_REGSHIFT # Determine offset from indice value
|
|
|
|
add t2, t2, t1 # Table addr + offset = function addr
|
|
|
|
lr t2, 0(t2) # Load function address
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
/* Execute syscall function */
|
2022-05-18 16:48:17 +02:00
|
|
|
jalr ra, t2, 0
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-06-07 14:18:11 +02:00
|
|
|
#if defined(CONFIG_RISCV_ISA_RV32E)
|
|
|
|
addi sp, sp, 4
|
|
|
|
#endif /* CONFIG_RISCV_ISA_RV32E */
|
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
/* Update a0 (return value) on the stack */
|
2022-02-22 14:09:55 -05:00
|
|
|
sr a0, __z_arch_esf_t_a0_OFFSET(sp)
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
/* Disable IRQs again before leaving */
|
|
|
|
csrc mstatus, MSTATUS_IEN
|
2022-03-09 17:21:27 -05:00
|
|
|
j might_have_rescheduled
|
2020-07-21 16:00:39 +02:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
|
|
|
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
is_interrupt:
|
2022-04-06 22:03:54 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_PMP_STACK_GUARD
|
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
/*
|
|
|
|
* If we came from userspace then we need to reconfigure the
|
|
|
|
* PMP for kernel mode stack guard.
|
|
|
|
*/
|
|
|
|
lr t0, __z_arch_esf_t_mstatus_OFFSET(sp)
|
|
|
|
li t1, MSTATUS_MPP
|
|
|
|
and t0, t0, t1
|
|
|
|
bnez t0, 1f
|
|
|
|
lr a0, ___cpu_t_current_OFFSET(s0)
|
|
|
|
call z_riscv_pmp_stackguard_enable
|
|
|
|
j 2f
|
|
|
|
#endif /* CONFIG_USERSPACE */
|
|
|
|
1: /* Re-activate PMP for m-mode */
|
|
|
|
li t1, MSTATUS_MPP
|
|
|
|
csrc mstatus, t1
|
|
|
|
li t1, MSTATUS_MPRV
|
|
|
|
csrs mstatus, t1
|
|
|
|
2:
|
|
|
|
#endif
|
|
|
|
|
2022-03-09 17:21:27 -05:00
|
|
|
/* Increment _current_cpu->nested */
|
2022-05-18 16:48:17 +02:00
|
|
|
lw t1, ___cpu_t_nested_OFFSET(s0)
|
|
|
|
addi t2, t1, 1
|
|
|
|
sw t2, ___cpu_t_nested_OFFSET(s0)
|
|
|
|
bnez t1, on_irq_stack
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
|
|
|
/* Switch to interrupt stack */
|
2022-02-24 22:30:03 -05:00
|
|
|
mv t0, sp
|
2022-03-09 17:21:27 -05:00
|
|
|
lr sp, ___cpu_t_irq_stack_OFFSET(s0)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Save thread stack pointer on interrupt stack
|
|
|
|
* In RISC-V, stack pointer needs to be 16-byte aligned
|
|
|
|
*/
|
|
|
|
addi sp, sp, -16
|
2022-02-22 14:09:55 -05:00
|
|
|
sr t0, 0(sp)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
|
|
|
on_irq_stack:
|
|
|
|
|
2020-01-28 16:44:59 +08:00
|
|
|
#ifdef CONFIG_TRACING_ISR
|
2019-09-19 09:25:19 +02:00
|
|
|
call sys_trace_isr_enter
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get IRQ causing interrupt */
|
|
|
|
csrr a0, mcause
|
2017-02-13 01:17:01 +01:00
|
|
|
li t0, SOC_MCAUSE_EXP_MASK
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
and a0, a0, t0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear pending IRQ generating the interrupt at SOC level
|
|
|
|
* Pass IRQ number to __soc_handle_irq via register a0
|
|
|
|
*/
|
|
|
|
jal ra, __soc_handle_irq
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call corresponding registered function in _sw_isr_table.
|
2019-07-24 16:21:58 -04:00
|
|
|
* (table is 2-word wide, we should shift index accordingly)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
*/
|
|
|
|
la t0, _sw_isr_table
|
2019-07-24 16:21:58 -04:00
|
|
|
slli a0, a0, (RV_REGSHIFT + 1)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
add t0, t0, a0
|
|
|
|
|
|
|
|
/* Load argument in a0 register */
|
2022-02-22 14:09:55 -05:00
|
|
|
lr a0, 0(t0)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
|
|
|
/* Load ISR function address in register t1 */
|
2022-02-22 14:09:55 -05:00
|
|
|
lr t1, RV_REGSIZE(t0)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
|
|
|
/* Call ISR function */
|
2021-12-14 22:46:42 +08:00
|
|
|
jalr ra, t1, 0
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2022-05-11 12:29:46 +02:00
|
|
|
#ifdef CONFIG_TRACING_ISR
|
|
|
|
call sys_trace_isr_exit
|
|
|
|
#endif
|
|
|
|
|
2022-03-07 13:24:38 -05:00
|
|
|
irq_done:
|
2022-03-09 17:21:27 -05:00
|
|
|
/* Decrement _current_cpu->nested */
|
|
|
|
lw t2, ___cpu_t_nested_OFFSET(s0)
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
addi t2, t2, -1
|
2022-03-09 17:21:27 -05:00
|
|
|
sw t2, ___cpu_t_nested_OFFSET(s0)
|
2022-02-24 22:30:03 -05:00
|
|
|
bnez t2, no_reschedule
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
/* nested count is back to 0: Return to thread stack */
|
|
|
|
lr sp, 0(sp)
|
2017-06-07 09:33:16 -07:00
|
|
|
|
2017-05-11 13:29:15 -07:00
|
|
|
#ifdef CONFIG_STACK_SENTINEL
|
2019-03-08 14:19:05 -07:00
|
|
|
call z_check_stack_sentinel
|
2017-05-11 13:29:15 -07:00
|
|
|
#endif
|
|
|
|
|
2022-06-30 14:40:52 +02:00
|
|
|
check_reschedule:
|
2022-03-09 17:46:30 -05:00
|
|
|
|
2022-03-09 17:21:27 -05:00
|
|
|
/* Get pointer to current thread on this CPU */
|
|
|
|
lr a1, ___cpu_t_current_OFFSET(s0)
|
2020-07-21 16:00:39 +02:00
|
|
|
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
/*
|
2022-03-07 17:01:36 -05:00
|
|
|
* Get next thread to schedule with z_get_next_switch_handle().
|
|
|
|
* We pass it a NULL as we didn't save the whole thread context yet.
|
|
|
|
* If no scheduling is necessary then NULL will be returned.
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
*/
|
2022-03-07 17:01:36 -05:00
|
|
|
addi sp, sp, -16
|
|
|
|
sr a1, 0(sp)
|
|
|
|
mv a0, zero
|
|
|
|
call z_get_next_switch_handle
|
|
|
|
lr a1, 0(sp)
|
|
|
|
addi sp, sp, 16
|
|
|
|
beqz a0, no_reschedule
|
2020-03-11 18:15:29 -07:00
|
|
|
|
2022-06-30 14:40:52 +02:00
|
|
|
reschedule:
|
|
|
|
|
2020-03-11 18:15:29 -07:00
|
|
|
/*
|
2022-03-07 17:01:36 -05:00
|
|
|
* Perform context switch:
|
|
|
|
* a0 = new thread
|
|
|
|
* a1 = old thread
|
2020-03-11 18:15:29 -07:00
|
|
|
*/
|
2022-03-07 17:01:36 -05:00
|
|
|
call z_riscv_switch
|
2018-07-23 15:54:22 +05:30
|
|
|
|
2022-03-07 17:01:36 -05:00
|
|
|
z_riscv_thread_start:
|
2022-03-09 17:21:27 -05:00
|
|
|
might_have_rescheduled:
|
2023-01-17 23:32:21 -05:00
|
|
|
/* reload s0 with &_current_cpu as it might have changed or be unset */
|
2023-01-06 17:46:21 -05:00
|
|
|
get_current_cpu s0
|
2022-03-09 17:21:27 -05:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
no_reschedule:
|
|
|
|
|
2020-07-21 16:00:39 +02:00
|
|
|
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE
|
|
|
|
/* Restore context at SOC level */
|
|
|
|
addi a0, sp, __z_arch_esf_t_soc_context_OFFSET
|
|
|
|
jal ra, __soc_restore_context
|
|
|
|
#endif /* CONFIG_RISCV_SOC_CONTEXT_SAVE */
|
|
|
|
|
2023-01-17 23:32:21 -05:00
|
|
|
#if defined(CONFIG_FPU_SHARING)
|
|
|
|
/* FPU handling upon exception mode exit */
|
|
|
|
mv a0, sp
|
|
|
|
call z_riscv_fpu_exit_exc
|
2022-03-07 17:31:13 -05:00
|
|
|
|
2023-01-17 23:32:21 -05:00
|
|
|
/* decrement _current->arch.exception_depth */
|
|
|
|
lr t0, ___cpu_t_current_OFFSET(s0)
|
|
|
|
lb t1, _thread_offset_to_exception_depth(t0)
|
|
|
|
add t1, t1, -1
|
|
|
|
sb t1, _thread_offset_to_exception_depth(t0)
|
|
|
|
fp_trap_exit:
|
|
|
|
#endif
|
2022-03-07 17:31:13 -05:00
|
|
|
|
2023-01-17 23:32:21 -05:00
|
|
|
/* Restore MEPC and MSTATUS registers */
|
|
|
|
lr t0, __z_arch_esf_t_mepc_OFFSET(sp)
|
|
|
|
lr t2, __z_arch_esf_t_mstatus_OFFSET(sp)
|
|
|
|
csrw mepc, t0
|
|
|
|
csrw mstatus, t2
|
2017-06-29 22:45:55 +02:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
#ifdef CONFIG_USERSPACE
|
2020-04-14 14:56:12 -07:00
|
|
|
/*
|
2022-02-24 22:30:03 -05:00
|
|
|
* Check if we are returning to user mode. If so then we must
|
2023-01-06 17:46:21 -05:00
|
|
|
* set is_user_mode to true and preserve our kernel mode stack for
|
|
|
|
* the next exception to come.
|
2020-04-14 14:56:12 -07:00
|
|
|
*/
|
2022-02-24 22:30:03 -05:00
|
|
|
li t1, MSTATUS_MPP
|
2022-05-18 16:48:17 +02:00
|
|
|
and t0, t2, t1
|
2022-02-24 22:30:03 -05:00
|
|
|
bnez t0, 1f
|
2020-04-14 14:56:12 -07:00
|
|
|
|
2022-04-06 22:03:54 -04:00
|
|
|
#ifdef CONFIG_PMP_STACK_GUARD
|
|
|
|
/* Remove kernel stack guard and Reconfigure PMP for user mode */
|
|
|
|
lr a0, ___cpu_t_current_OFFSET(s0)
|
|
|
|
call z_riscv_pmp_usermode_enable
|
|
|
|
#endif
|
|
|
|
|
2022-06-07 09:37:59 -04:00
|
|
|
/* Set our per-thread usermode flag */
|
|
|
|
li t1, 1
|
|
|
|
lui t0, %tprel_hi(is_user_mode)
|
|
|
|
add t0, t0, tp, %tprel_add(is_user_mode)
|
|
|
|
sb t1, %tprel_lo(is_user_mode)(t0)
|
2022-02-24 22:30:03 -05:00
|
|
|
|
2023-01-06 17:46:21 -05:00
|
|
|
/* preserve stack pointer for next exception entry */
|
2022-02-24 22:30:03 -05:00
|
|
|
add t0, sp, __z_arch_esf_t_SIZEOF
|
2023-01-06 17:46:21 -05:00
|
|
|
sr t0, _curr_cpu_arch_user_exc_sp(s0)
|
|
|
|
|
|
|
|
j 2f
|
2022-02-24 22:30:03 -05:00
|
|
|
1:
|
2023-01-06 17:46:21 -05:00
|
|
|
/*
|
|
|
|
* We are returning to kernel mode. Store the stack pointer to
|
|
|
|
* be re-loaded further down.
|
|
|
|
*/
|
|
|
|
addi t0, sp, __z_arch_esf_t_SIZEOF
|
|
|
|
sr t0, __z_arch_esf_t_sp_OFFSET(sp)
|
|
|
|
2:
|
2022-02-24 22:30:03 -05:00
|
|
|
#endif
|
2020-04-14 14:56:12 -07:00
|
|
|
|
2022-03-09 17:21:27 -05:00
|
|
|
/* Restore s0 (it is no longer ours) */
|
|
|
|
lr s0, __z_arch_esf_t_s0_OFFSET(sp)
|
|
|
|
|
2022-12-27 20:14:51 +01:00
|
|
|
#ifdef CONFIG_RISCV_SOC_HAS_ISR_STACKING
|
|
|
|
SOC_ISR_SW_UNSTACKING
|
|
|
|
#else
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
/* Restore caller-saved registers from thread stack */
|
2022-06-07 09:37:59 -04:00
|
|
|
DO_CALLER_SAVED(lr)
|
2022-12-27 20:14:51 +01:00
|
|
|
#endif /* CONFIG_RISCV_SOC_HAS_ISR_STACKING */
|
2022-02-24 22:30:03 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
/* retrieve saved stack pointer */
|
|
|
|
lr sp, __z_arch_esf_t_sp_OFFSET(sp)
|
|
|
|
#else
|
|
|
|
/* remove esf from the stack */
|
|
|
|
addi sp, sp, __z_arch_esf_t_SIZEOF
|
|
|
|
#endif
|
arch: added support for the riscv32 architecture
RISC-V is an open-source instruction set architecture.
Added support for the 32bit version of RISC-V to Zephyr.
1) exceptions/interrupts/faults are handled at the architecture
level via the __irq_wrapper handler. Context saving/restoring
of registers can be handled at both architecture and SOC levels.
If SOC-specific registers need to be saved, SOC level needs to
provide __soc_save_context and __soc_restore_context functions
that shall be accounted by the architecture level, when
corresponding config variable RISCV_SOC_CONTEXT_SAVE is set.
2) As RISC-V architecture does not provide a clear ISA specification
about interrupt handling, each RISC-V SOC handles it in its own
way. Hence, at the architecture level, the __irq_wrapper handler
expects the following functions to be provided by the SOC level:
__soc_is_irq: to check if the exception is the result of an
interrupt or not.
__soc_handle_irq: handle pending IRQ at SOC level (ex: clear
pending IRQ in SOC-specific IRQ register)
3) Thread/task scheduling, as well as IRQ offloading are handled via
the RISC-V system call ("ecall"), which is also handled via the
__irq_wrapper handler. The _Swap asm function just calls "ecall"
to generate an exception.
4) As there is no conventional way of handling CPU power save in
RISC-V, the default nano_cpu_idle and nano_cpu_atomic_idle
functions just unlock interrupts and return to the caller, without
issuing any CPU power saving instruction. Nonetheless, to allow
SOC-level to implement proper CPU power save, nano_cpu_idle and
nano_cpu_atomic_idle functions are defined as __weak
at the architecture level.
Change-Id: I980a161d0009f3f404ad22b226a6229fbb492389
Signed-off-by: Jean-Paul Etienne <fractalclone@gmail.com>
2017-01-11 00:24:30 +01:00
|
|
|
|
2022-08-03 12:51:34 +02:00
|
|
|
mret
|