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>
113 lines
3.7 KiB
C
113 lines
3.7 KiB
C
/*
|
|
* Copyright (c) 2016 Jean-Paul Etienne <fractalclone@gmail.com>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include <kernel.h>
|
|
#include <arch/cpu.h>
|
|
#include <kernel_structs.h>
|
|
#include <wait_q.h>
|
|
#include <string.h>
|
|
|
|
#if defined(CONFIG_THREAD_MONITOR)
|
|
/*
|
|
* Add a thread to the kernel's list of active threads.
|
|
*/
|
|
static ALWAYS_INLINE void thread_monitor_init(struct k_thread *thread)
|
|
{
|
|
unsigned int key;
|
|
|
|
key = irq_lock();
|
|
thread->next_thread = _kernel.threads;
|
|
_kernel.threads = thread;
|
|
irq_unlock(key);
|
|
}
|
|
#else
|
|
#define thread_monitor_init(thread) \
|
|
do {/* do nothing */ \
|
|
} while ((0))
|
|
#endif /* CONFIG_THREAD_MONITOR */
|
|
|
|
void _thread_entry_wrapper(_thread_entry_t thread,
|
|
void *arg1,
|
|
void *arg2,
|
|
void *arg3);
|
|
|
|
void _new_thread(char *stack_memory, size_t stack_size,
|
|
_thread_entry_t thread_func,
|
|
void *arg1, void *arg2, void *arg3,
|
|
int priority, unsigned options)
|
|
{
|
|
_ASSERT_VALID_PRIO(priority, thread_func);
|
|
|
|
struct k_thread *thread;
|
|
struct __esf *stack_init;
|
|
|
|
#ifdef CONFIG_INIT_STACKS
|
|
memset(stack_memory, 0xaa, stack_size);
|
|
#endif
|
|
/* Initial stack frame for thread */
|
|
stack_init = (struct __esf *)
|
|
STACK_ROUND_DOWN(stack_memory +
|
|
stack_size - sizeof(struct __esf));
|
|
|
|
/* Setup the initial stack frame */
|
|
stack_init->a0 = (uint32_t)thread_func;
|
|
stack_init->a1 = (uint32_t)arg1;
|
|
stack_init->a2 = (uint32_t)arg2;
|
|
stack_init->a3 = (uint32_t)arg3;
|
|
/*
|
|
* 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
|
|
* value of the program counter at which an interrupt/exception occcurs)
|
|
* 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.
|
|
*
|
|
* Given that context switching is performed via a system call exception
|
|
* within the RISCV32 architecture implementation, initially set:
|
|
* 1) MSTATUS to SOC_MSTATUS_DEF_RESTORE in the thread stack to enable
|
|
* interrupts when the newly created thread will be scheduled;
|
|
* 2) MEPC to the address of the _thread_entry_wrapper in the thread
|
|
* 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;
|
|
* 2) the core will jump to _thread_entry_wrapper, as the program
|
|
* counter will be restored following the MEPC value set within the
|
|
* thread stack.
|
|
*/
|
|
stack_init->mstatus = SOC_MSTATUS_DEF_RESTORE;
|
|
stack_init->mepc = (uint32_t)_thread_entry_wrapper;
|
|
|
|
/* Initialize various struct k_thread members */
|
|
thread = (struct k_thread *)stack_memory;
|
|
|
|
_init_thread_base(&thread->base, priority, K_PRESTART, options);
|
|
|
|
/* static threads overwrite it afterwards with real value */
|
|
thread->init_data = NULL;
|
|
thread->fn_abort = NULL;
|
|
|
|
#ifdef CONFIG_THREAD_CUSTOM_DATA
|
|
/* Initialize custom data field (value is opaque to kernel) */
|
|
thread->custom_data = NULL;
|
|
#endif
|
|
|
|
thread->callee_saved.sp = (uint32_t)stack_init;
|
|
|
|
thread_monitor_init(thread);
|
|
}
|