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>
|
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-06 10:49:15 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2019-10-25 00:08:21 +09:00
|
|
|
#include <ksched.h>
|
2022-05-06 10:49:15 +02:00
|
|
|
#include <zephyr/arch/riscv/csr.h>
|
2020-07-21 16:00:39 +02:00
|
|
|
#include <stdio.h>
|
2022-04-06 22:03:54 -04:00
|
|
|
#include <pmp.h>
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-06-07 09:37:59 -04:00
|
|
|
#ifdef CONFIG_USERSPACE
|
2020-07-21 16:00:39 +02:00
|
|
|
/*
|
2022-06-07 09:37:59 -04:00
|
|
|
* Per-thread (TLS) variable indicating whether execution is in user mode.
|
2020-07-21 16:00:39 +02:00
|
|
|
*/
|
2022-06-07 09:37:59 -04:00
|
|
|
__thread uint8_t is_user_mode;
|
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
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
void arch_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
|
2020-04-23 13:55:56 -07:00
|
|
|
char *stack_ptr, k_thread_entry_t entry,
|
2020-04-23 11:32:08 -07:00
|
|
|
void *p1, void *p2, void *p3)
|
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
|
|
|
extern void z_riscv_thread_start(void);
|
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
|
|
|
struct __esf *stack_init;
|
|
|
|
|
2020-06-07 16:10:52 +02:00
|
|
|
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE
|
|
|
|
const struct soc_esf soc_esf_init = {SOC_ESF_INIT};
|
|
|
|
#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
|
|
|
/* Initial stack frame for thread */
|
2021-01-06 14:50:21 +08:00
|
|
|
stack_init = (struct __esf *)Z_STACK_PTR_ALIGN(
|
|
|
|
Z_STACK_PTR_TO_FRAME(struct __esf, stack_ptr)
|
|
|
|
);
|
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
|
|
|
|
|
|
|
/* Setup the initial stack frame */
|
2022-08-25 11:45:38 +02:00
|
|
|
stack_init->a0 = (unsigned long)entry;
|
|
|
|
stack_init->a1 = (unsigned long)p1;
|
|
|
|
stack_init->a2 = (unsigned long)p2;
|
|
|
|
stack_init->a3 = (unsigned long)p3;
|
2020-10-02 13:09:32 -07: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
|
|
|
/*
|
|
|
|
* Following the RISC-V architecture,
|
|
|
|
* the MSTATUS register (used to globally enable/disable interrupt),
|
|
|
|
* as well as the MEPC register (used to by the core to save the
|
2022-03-16 21:07:43 +00:00
|
|
|
* value of the program counter at which an interrupt/exception occurs)
|
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
|
|
|
* need to be saved on the stack, upon an interrupt/exception
|
|
|
|
* and restored prior to returning from the interrupt/exception.
|
|
|
|
* This shall allow to handle nested interrupts.
|
|
|
|
*
|
2022-03-07 17:01:36 -05:00
|
|
|
* Given that thread startup happens through the exception exit
|
|
|
|
* path, initially set:
|
2020-01-03 18:18:24 -08:00
|
|
|
* 1) MSTATUS to MSTATUS_DEF_RESTORE in the thread stack to enable
|
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
|
|
|
* interrupts when the newly created thread will be scheduled;
|
2022-02-24 22:30:03 -05:00
|
|
|
* 2) MEPC to the address of the z_thread_entry in the 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
|
|
|
* stack.
|
|
|
|
* Hence, when going out of an interrupt/exception/context-switch,
|
|
|
|
* after scheduling the newly created thread:
|
|
|
|
* 1) interrupts will be enabled, as the MSTATUS register will be
|
|
|
|
* restored following the MSTATUS value set within the thread stack;
|
2022-02-24 22:30:03 -05:00
|
|
|
* 2) the core will jump to z_thread_entry, as the program
|
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
|
|
|
* counter will be restored following the MEPC value set within the
|
|
|
|
* thread stack.
|
|
|
|
*/
|
2020-01-03 18:18:24 -08:00
|
|
|
stack_init->mstatus = MSTATUS_DEF_RESTORE;
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2023-01-17 23:32:21 -05:00
|
|
|
#if defined(CONFIG_FPU_SHARING)
|
|
|
|
/* thread birth happens through the exception return path */
|
|
|
|
thread->arch.exception_depth = 1;
|
2021-05-19 02:43:13 +08:00
|
|
|
#elif defined(CONFIG_FPU)
|
|
|
|
/* Unshared FP mode: enable FPU of each thread. */
|
|
|
|
stack_init->mstatus |= MSTATUS_FS_INIT;
|
2020-03-11 18:15:29 -07:00
|
|
|
#endif
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_USERSPACE)
|
2021-03-14 01:26:17 +08:00
|
|
|
/* Clear user thread context */
|
2022-04-06 22:03:54 -04:00
|
|
|
z_riscv_pmp_usermode_init(thread);
|
2020-07-21 16:00:39 +02:00
|
|
|
thread->arch.priv_stack_start = 0;
|
2021-03-14 01:26:17 +08:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
|
|
|
|
|
|
|
/* Assign thread entry point and mstatus.MPRV mode. */
|
|
|
|
if (IS_ENABLED(CONFIG_USERSPACE)
|
|
|
|
&& (thread->base.user_options & K_USER)) {
|
|
|
|
/* User thread */
|
2022-08-25 11:45:38 +02:00
|
|
|
stack_init->mepc = (unsigned long)k_thread_user_mode_enter;
|
2021-03-14 01:26:17 +08:00
|
|
|
|
2020-07-21 16:00:39 +02:00
|
|
|
} else {
|
2021-03-14 01:26:17 +08:00
|
|
|
/* Supervisor thread */
|
2022-08-25 11:45:38 +02:00
|
|
|
stack_init->mepc = (unsigned long)z_thread_entry;
|
2021-03-14 01:26:17 +08:00
|
|
|
|
2020-07-21 16:00:39 +02:00
|
|
|
#if defined(CONFIG_PMP_STACK_GUARD)
|
2021-03-14 01:26:17 +08:00
|
|
|
/* Enable PMP in mstatus.MPRV mode for RISC-V machine mode
|
|
|
|
* if thread is supervisor thread.
|
|
|
|
*/
|
|
|
|
stack_init->mstatus |= MSTATUS_MPRV;
|
2020-07-21 16:00:39 +02:00
|
|
|
#endif /* CONFIG_PMP_STACK_GUARD */
|
|
|
|
}
|
2021-03-14 01:26:17 +08:00
|
|
|
|
2020-07-21 16:00:39 +02:00
|
|
|
#if defined(CONFIG_PMP_STACK_GUARD)
|
2021-03-14 01:26:17 +08:00
|
|
|
/* Setup PMP regions of PMP stack guard of thread. */
|
2022-04-06 22:03:54 -04:00
|
|
|
z_riscv_pmp_stackguard_prepare(thread);
|
2020-07-21 16:00:39 +02:00
|
|
|
#endif /* CONFIG_PMP_STACK_GUARD */
|
|
|
|
|
2020-06-07 16:10:52 +02:00
|
|
|
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE
|
|
|
|
stack_init->soc_context = soc_esf_init;
|
|
|
|
#endif
|
|
|
|
|
2022-08-25 11:45:38 +02:00
|
|
|
thread->callee_saved.sp = (unsigned long)stack_init;
|
2022-03-07 17:01:36 -05:00
|
|
|
|
|
|
|
/* where to go when returning from z_riscv_switch() */
|
2022-08-25 11:45:38 +02:00
|
|
|
thread->callee_saved.ra = (unsigned long)z_riscv_thread_start;
|
2022-03-07 17:01:36 -05:00
|
|
|
|
|
|
|
/* our switch handle is the thread pointer itself */
|
|
|
|
thread->switch_handle = 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
|
|
|
}
|
2020-03-11 18:15:29 -07:00
|
|
|
|
2020-07-21 16:00:39 +02:00
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User space entry function
|
|
|
|
*
|
|
|
|
* This function is the entry point to user mode from privileged execution.
|
|
|
|
* The conversion is one way, and threads which transition to user mode do
|
|
|
|
* not transition back later, unless they are doing system calls.
|
|
|
|
*/
|
2022-02-24 22:30:03 -05:00
|
|
|
FUNC_NORETURN void arch_user_mode_enter(k_thread_entry_t user_entry,
|
|
|
|
void *p1, void *p2, void *p3)
|
2020-07-21 16:00:39 +02:00
|
|
|
{
|
2022-08-25 11:45:38 +02:00
|
|
|
unsigned long top_of_user_stack, top_of_priv_stack;
|
|
|
|
unsigned long status;
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
/* Set up privileged stack */
|
|
|
|
#ifdef CONFIG_GEN_PRIV_STACKS
|
2022-02-24 22:30:03 -05:00
|
|
|
_current->arch.priv_stack_start =
|
2022-08-25 11:45:38 +02:00
|
|
|
(unsigned long)z_priv_stack_find(_current->stack_obj);
|
2022-05-14 15:59:57 -04:00
|
|
|
/* remove the stack guard from the main stack */
|
|
|
|
_current->stack_info.start -= K_THREAD_STACK_RESERVED;
|
|
|
|
_current->stack_info.size += K_THREAD_STACK_RESERVED;
|
2020-07-21 16:00:39 +02:00
|
|
|
#else
|
2022-08-25 11:45:38 +02:00
|
|
|
_current->arch.priv_stack_start = (unsigned long)_current->stack_obj;
|
2020-07-21 16:00:39 +02:00
|
|
|
#endif /* CONFIG_GEN_PRIV_STACKS */
|
2022-04-30 15:12:31 -04:00
|
|
|
top_of_priv_stack = Z_STACK_PTR_ALIGN(_current->arch.priv_stack_start +
|
|
|
|
K_KERNEL_STACK_RESERVED +
|
|
|
|
CONFIG_PRIVILEGED_STACK_SIZE);
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
top_of_user_stack = Z_STACK_PTR_ALIGN(
|
|
|
|
_current->stack_info.start +
|
|
|
|
_current->stack_info.size -
|
|
|
|
_current->stack_info.delta);
|
|
|
|
|
|
|
|
status = csr_read(mstatus);
|
2022-02-24 22:30:03 -05:00
|
|
|
|
|
|
|
/* Set next CPU status to user mode */
|
2020-07-21 16:00:39 +02:00
|
|
|
status = INSERT_FIELD(status, MSTATUS_MPP, PRV_U);
|
2022-02-24 22:30:03 -05:00
|
|
|
/* Enable IRQs for user mode */
|
|
|
|
status = INSERT_FIELD(status, MSTATUS_MPIE, 1);
|
|
|
|
/* Disable IRQs for m-mode until the mode switch */
|
|
|
|
status = INSERT_FIELD(status, MSTATUS_MIE, 0);
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
csr_write(mstatus, status);
|
2022-02-24 22:30:03 -05:00
|
|
|
csr_write(mepc, z_thread_entry);
|
|
|
|
|
2022-04-06 22:03:54 -04:00
|
|
|
#ifdef CONFIG_PMP_STACK_GUARD
|
|
|
|
/* reconfigure as the kernel mode stack will be different */
|
|
|
|
z_riscv_pmp_stackguard_prepare(_current);
|
|
|
|
#endif
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
/* Set up Physical Memory Protection */
|
2022-04-06 22:03:54 -04:00
|
|
|
z_riscv_pmp_usermode_prepare(_current);
|
|
|
|
z_riscv_pmp_usermode_enable(_current);
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2023-01-06 17:46:21 -05:00
|
|
|
/* preserve stack pointer for next exception entry */
|
|
|
|
arch_curr_cpu()->arch.user_exc_sp = top_of_priv_stack;
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
is_user_mode = true;
|
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
register void *a0 __asm__("a0") = user_entry;
|
|
|
|
register void *a1 __asm__("a1") = p1;
|
|
|
|
register void *a2 __asm__("a2") = p2;
|
|
|
|
register void *a3 __asm__("a3") = p3;
|
2020-07-21 16:00:39 +02:00
|
|
|
|
2022-02-24 22:30:03 -05:00
|
|
|
__asm__ volatile (
|
|
|
|
"mv sp, %4; mret"
|
|
|
|
:
|
|
|
|
: "r" (a0), "r" (a1), "r" (a2), "r" (a3), "r" (top_of_user_stack)
|
|
|
|
: "memory");
|
2020-07-21 16:00:39 +02:00
|
|
|
|
|
|
|
CODE_UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_USERSPACE */
|