zephyr/arch/arm/core/thread.c

135 lines
3.8 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief New thread creation for ARM Cortex-M
*
* Core thread related primitives for the ARM Cortex-M processor architecture.
*/
#include <kernel.h>
#include <toolchain.h>
kernel/arch: consolidate tTCS and TNANO definitions There was a lot of duplication between architectures for the definition of threads and the "nanokernel" guts. These have been consolidated. Now, a common file kernel/unified/include/kernel_structs.h holds the common definitions. Architectures provide two files to complement it: kernel_arch_data.h and kernel_arch_func.h. The first one contains at least the struct _thread_arch and struct _kernel_arch data structures, as well as the struct _callee_saved and struct _caller_saved register layouts. The second file contains anything that needs what is provided by the common stuff in kernel_structs.h. Those two files are only meant to be included in kernel_structs.h in very specific locations. The thread data structure has been separated into three major parts: common struct _thread_base and struct k_thread, and arch-specific struct _thread_arch. The first and third ones are included in the second. The struct s_NANO data structure has been split into two: common struct _kernel and arch-specific struct _kernel_arch. The latter is included in the former. Offsets files have also changed: nano_offsets.h has been renamed kernel_offsets.h and is still included by the arch-specific offsets.c. Also, since the thread and kernel data structures are now made of sub-structures, offsets have to be added to make up the full offset. Some of these additions have been consolidated in shorter symbols, available from kernel/unified/include/offsets_short.h, which includes an arch-specific offsets_arch_short.h. Most of the code include offsets_short.h now instead of offsets.h. Change-Id: I084645cb7e6db8db69aeaaf162963fe157045d5a Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-11-08 23:36:50 +08:00
#include <kernel_structs.h>
#include <wait_q.h>
#ifdef CONFIG_INIT_STACKS
#include <string.h>
#endif /* CONFIG_INIT_STACKS */
#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 */
/**
*
* @brief Initialize a new thread from its stack space
*
* The control structure (thread) is put at the lower address of the stack. An
* initial context, to be "restored" by __pendsv(), is put at the other end of
* the stack, and thus reusable by the stack when not needed anymore.
*
* The initial context is an exception stack frame (ESF) since exiting the
* PendSV exception will want to pop an ESF. Interestingly, even if the lsb of
* an instruction address to jump to must always be set since the CPU always
* runs in thumb mode, the ESF expects the real address of the instruction,
* with the lsb *not* set (instructions are always aligned on 16 bit halfwords).
* Since the compiler automatically sets the lsb of function addresses, we have
* to unset it manually before storing it in the 'pc' field of the ESF.
*
* <options> is currently unused.
*
* @param pStackMem the aligned stack memory
* @param stackSize stack size in bytes
* @param pEntry the entry point
* @param parameter1 entry point to the first param
* @param parameter2 entry point to the second param
* @param parameter3 entry point to the third param
unified/arm: add unified kernel support for ARM arch The ARM architecture port is fitted with support for the unified kernel, namely: - the interrupt/exception exit code now pends PendSV if the current thread is not a coop thread and if the scheduler is not locked - fiber_abort is replaced by k_thread_abort(), which takes a thread ID as a parameter (i.e. does not only operate on the current thread) - the _nanokernel.flags cache of _current.flags is not used anymore (could be a source of bugs) and is not needed in the scheduling algo - there is no 'task' field in the _nanokernel anymore: PendSV not calls _get_next_ready_thread instead - the _nanokernel.fiber field is replaced by a more sophisticated ready_q, based on the microkernel's priority-bitmap-based one - thread initialization initializes new fields in the tcs, and does not initialize obsolete ones - nano_private includes nano_internal.h from the unified directory - The FIBER, TASK and PREEMPTIBLE flags do not exist anymore: the thread priority drives the behaviour - the tcs uses a dlist for queuing in both ready and wait queues instead of a custom singly-linked list - other new fields in the tcs include a schedule-lock count, a back-pointer to init data (when the task is static) and a pointer to swap data, needed when a thread pending on _Swap() must be passed more then just one value (e.g. k_stack_pop() needs an error code and data) - the 'fiber' and 'task' fields of _nanokernel are replaced with an O(1) ready queue (taken from the microkernel) - fiberRtnValueSet() is aliased to _set_thread_return_value since it also operates on preempt threads now - _set_thread_return_value_with_data() sets the swap_data field in addition to a return value from _Swap() - convenience aliases are created for shorter names: - _current is defined as _nanokernel.current - _ready_q is defined as _nanokernel.ready_q - _Swap() sets the threads's return code to -EAGAIN before swapping out to prevent timeouts to have to set it (solves hard issues in some kernel objects). Change-Id: I36c03c362bc2908dae064ec67e6b8469fc573983 Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-09-03 04:20:19 +08:00
* @param priority thread priority
* @param options thread options: K_ESSENTIAL, K_FP_REGS
*
* @return N/A
*/
void _new_thread(char *pStackMem, size_t stackSize,
_thread_entry_t pEntry,
void *parameter1, void *parameter2, void *parameter3,
int priority, unsigned int options)
{
_ASSERT_VALID_PRIO(priority, pEntry);
__ASSERT(!((u32_t)pStackMem & (STACK_ALIGN - 1)),
"stack is not aligned properly\n"
"%d-byte alignment required\n", STACK_ALIGN);
char *stackEnd = pStackMem + stackSize;
struct __esf *pInitCtx;
struct k_thread *thread = (struct k_thread *) pStackMem;
#ifdef CONFIG_INIT_STACKS
memset(pStackMem, 0xaa, stackSize);
#endif
/* carve the thread entry struct from the "base" of the stack */
pInitCtx = (struct __esf *)(STACK_ROUND_DOWN(stackEnd) -
sizeof(struct __esf));
pInitCtx->pc = ((u32_t)_thread_entry) & 0xfffffffe;
pInitCtx->a1 = (u32_t)pEntry;
pInitCtx->a2 = (u32_t)parameter1;
pInitCtx->a3 = (u32_t)parameter2;
pInitCtx->a4 = (u32_t)parameter3;
pInitCtx->xpsr =
0x01000000UL; /* clear all, thumb bit is 1, even if RO */
_init_thread_base(&thread->base, priority, _THREAD_PRESTART, options);
unified/arm: add unified kernel support for ARM arch The ARM architecture port is fitted with support for the unified kernel, namely: - the interrupt/exception exit code now pends PendSV if the current thread is not a coop thread and if the scheduler is not locked - fiber_abort is replaced by k_thread_abort(), which takes a thread ID as a parameter (i.e. does not only operate on the current thread) - the _nanokernel.flags cache of _current.flags is not used anymore (could be a source of bugs) and is not needed in the scheduling algo - there is no 'task' field in the _nanokernel anymore: PendSV not calls _get_next_ready_thread instead - the _nanokernel.fiber field is replaced by a more sophisticated ready_q, based on the microkernel's priority-bitmap-based one - thread initialization initializes new fields in the tcs, and does not initialize obsolete ones - nano_private includes nano_internal.h from the unified directory - The FIBER, TASK and PREEMPTIBLE flags do not exist anymore: the thread priority drives the behaviour - the tcs uses a dlist for queuing in both ready and wait queues instead of a custom singly-linked list - other new fields in the tcs include a schedule-lock count, a back-pointer to init data (when the task is static) and a pointer to swap data, needed when a thread pending on _Swap() must be passed more then just one value (e.g. k_stack_pop() needs an error code and data) - the 'fiber' and 'task' fields of _nanokernel are replaced with an O(1) ready queue (taken from the microkernel) - fiberRtnValueSet() is aliased to _set_thread_return_value since it also operates on preempt threads now - _set_thread_return_value_with_data() sets the swap_data field in addition to a return value from _Swap() - convenience aliases are created for shorter names: - _current is defined as _nanokernel.current - _ready_q is defined as _nanokernel.ready_q - _Swap() sets the threads's return code to -EAGAIN before swapping out to prevent timeouts to have to set it (solves hard issues in some kernel objects). Change-Id: I36c03c362bc2908dae064ec67e6b8469fc573983 Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-09-03 04:20:19 +08:00
/* 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
#ifdef CONFIG_THREAD_MONITOR
/*
* In debug mode thread->entry give direct access to the thread entry
* and the corresponding parameters.
*/
thread->entry = (struct __thread_entry *)(pInitCtx);
#endif
thread->callee_saved.psp = (u32_t)pInitCtx;
thread->arch.basepri = 0;
kernel/arm: fix race condition when setting _Swap() return value There was a possible race condition when setting the return value of a thread that is pending, from an ISR. A kernel function causes a thread to pend, with the following series of steps: - disable interrupts - move current thread to wait_q - call _Swap Depending if running on M3/4 or M0+, _Swap will either issue a svc #0, or pend PendSV directly. The same problem exists in both cases. M3/4: __svc will: - enable interrupts - trigger __pendsv M0+: _Swap() will enable interrupts. __pendsv will: - save register context including PSP into the thread struct If an interrupt occurs between interrupts being enabled them and __pendsv saving PSP, and the ISR sets the pending thread's return value, this will happen: - sees the thread in a wait_q - removes it - makes it ready - calls _set_thread_return_value - _set_thread_return_value looks at the thread's saved PSP to poke the value In this scenario, PSP hasn't yet been updated by __pendsv so it's a stale value from the previous context switch, resulting in unpredictable word on the stack getting set to the return value. There is no way to fix this issue and still have the return value being delivered directly in the pending thread's exception stack frame, in the M0+ case. There will always be a window between the unlocking of interrupts and PendSV being handled. On M3/4, it could be possible with the mix of SVC and PendSV, since the exception stack frame is created in the __svc handler. However, because we want to keep the two implementations as close as possible, and there were talks of moving M3/4 to using PendSV only, to save an exception, the approach taken solves both cases. The approach taken is similar to the ARC and Nios2 ports, where there is a field in the thread structure that holds the return value. _Swap() then loads r0/a1 with that value just before returning. Fixes ZEP-1289. Change-Id: Iee7e06fe3f8ded84aff918fd43408c7f589344d9 Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-11-16 07:45:43 +08:00
/* swap_return_value can contain garbage */
/*
* initial values in all other registers/thread entries are
* irrelevant.
*/
thread_monitor_init(thread);
}