If the offset within the thread struct to the ARC arch-specific 'relinquish_cause' member is too large, ld_s instructions referencing it will not compile. This happens easily if CONFIG_THREAD_NAME reserves a name buffer within the thread struct, since all the arch-specific members come last. Use the regular 'ld' instruction instead. Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
220 lines
5.0 KiB
ArmAsm
220 lines
5.0 KiB
ArmAsm
/*
|
|
* Copyright (c) 2019 Synopsys.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @file
|
|
* @brief Thread context switching
|
|
*
|
|
* This module implements the routines necessary for thread context switching
|
|
* on ARCv2 CPUs.
|
|
*
|
|
* See isr_wrapper.S for details.
|
|
*/
|
|
|
|
#include <kernel_structs.h>
|
|
#include <offsets_short.h>
|
|
#include <toolchain.h>
|
|
#include <arch/cpu.h>
|
|
#include <v2/irq.h>
|
|
#include <swap_macros.h>
|
|
|
|
GTEXT(z_arch_switch)
|
|
|
|
/**
|
|
*
|
|
* @brief Initiate a cooperative context switch
|
|
*
|
|
* The z_arch_switch routine is invoked by various kernel services to effect
|
|
* a cooperative context switch. Prior to invoking z_arch_switch, the caller
|
|
* disables interrupts via irq_lock()
|
|
|
|
* Given that z_arch_switch() is called to effect a cooperative context switch,
|
|
* the caller-saved integer registers are saved on the stack by the function
|
|
* call preamble to z_arch_switch. This creates a custom stack frame that will
|
|
* be popped when returning from z_arch_switch, but is not suitable for handling
|
|
* a return from an exception. Thus, the fact that the thread is pending because
|
|
* of a cooperative call to z_arch_switch() has to be recorded via the
|
|
* _CAUSE_COOP code in the relinquish_cause of the thread's k_thread structure.
|
|
* The _rirq_exit()/_firq_exit() code will take care of doing the right thing
|
|
* to restore the thread status.
|
|
*
|
|
* When z_arch_switch() is invoked, we know the decision to perform a context
|
|
* switch or not has already been taken and a context switch must happen.
|
|
*
|
|
*
|
|
* C function prototype:
|
|
*
|
|
* void z_arch_switch(void *switch_to, void **switched_from);
|
|
*
|
|
*/
|
|
|
|
SECTION_FUNC(TEXT, z_arch_switch)
|
|
|
|
#ifdef CONFIG_EXECUTION_BENCHMARKING
|
|
push_s r0
|
|
push_s r1
|
|
push_s blink
|
|
|
|
bl read_timer_start_of_swap
|
|
|
|
pop_s blink
|
|
pop_s r1
|
|
pop_s r0
|
|
#endif
|
|
/*
|
|
* r0 = new_thread->switch_handle = switch_to thread,
|
|
* r1 = &old_thread->switch_handle = &switch_from thread
|
|
*/
|
|
|
|
ld_s r2, [r1]
|
|
/*
|
|
* r2 may be dummy_thread in z_cstart, dummy_thread->switch_handle
|
|
* must be 0
|
|
*/
|
|
breq r2, 0, _switch_to_target_thread
|
|
|
|
st _CAUSE_COOP, [r2, _thread_offset_to_relinquish_cause]
|
|
|
|
/*
|
|
* Save status32 and blink on the stack before the callee-saved registers.
|
|
* This is the same layout as the start of an IRQ stack frame.
|
|
*/
|
|
lr r3, [_ARC_V2_STATUS32]
|
|
push_s r3
|
|
|
|
#ifdef CONFIG_ARC_HAS_SECURE
|
|
lr r3, [_ARC_V2_SEC_STAT]
|
|
push_s r3
|
|
#endif
|
|
|
|
push_s blink
|
|
|
|
_save_callee_saved_regs
|
|
|
|
#ifdef CONFIG_ARC_STACK_CHECKING
|
|
/* disable stack checking here, as sp will be changed to target
|
|
* thread'sp
|
|
*/
|
|
#ifdef CONFIG_ARC_HAS_SECURE
|
|
bclr r3, r3, _ARC_V2_SEC_STAT_SSC_BIT
|
|
/* sflag r3 */
|
|
/* sflag instruction is not supported in current ARC GNU */
|
|
.long 0x00ff302f
|
|
#else
|
|
bclr r3, r3, _ARC_V2_STATUS32_SC_BIT
|
|
kflag r3
|
|
#endif
|
|
#endif
|
|
|
|
_switch_to_target_thread:
|
|
|
|
mov r2, r0
|
|
|
|
/* entering here, r2 contains the new current thread */
|
|
#ifdef CONFIG_ARC_STACK_CHECKING
|
|
_load_stack_check_regs
|
|
#endif
|
|
|
|
_load_callee_saved_regs
|
|
|
|
#if defined(CONFIG_MPU_STACK_GUARD) || defined(CONFIG_USERSPACE)
|
|
push_s r2
|
|
bl configure_mpu_thread
|
|
pop_s r2
|
|
#endif
|
|
|
|
ld r3, [r2, _thread_offset_to_relinquish_cause]
|
|
|
|
breq r3, _CAUSE_RIRQ, _switch_return_from_rirq
|
|
nop
|
|
breq r3, _CAUSE_FIRQ, _switch_return_from_firq
|
|
nop
|
|
|
|
/* fall through to _switch_return_from_coop */
|
|
|
|
.balign 4
|
|
_switch_return_from_coop:
|
|
|
|
lr ilink, [_ARC_V2_STATUS32]
|
|
bbit1 ilink, _ARC_V2_STATUS32_AE_BIT, _return_from_exc
|
|
|
|
pop_s blink /* pc into blink */
|
|
#ifdef CONFIG_ARC_HAS_SECURE
|
|
pop_s r3 /* pop SEC_STAT */
|
|
/* sflag r3 */
|
|
/* sflag instruction is not supported in current ARC GNU */
|
|
.long 0x00ff302f
|
|
#endif
|
|
pop_s r3 /* status32 into r3 */
|
|
kflag r3 /* write status32 */
|
|
|
|
#ifdef CONFIG_EXECUTION_BENCHMARKING
|
|
b _capture_value_for_benchmarking
|
|
#endif
|
|
return_loc:
|
|
j_s [blink]
|
|
|
|
|
|
.balign 4
|
|
_switch_return_from_rirq:
|
|
_switch_return_from_firq:
|
|
|
|
lr r3, [_ARC_V2_STATUS32]
|
|
bbit1 r3, _ARC_V2_STATUS32_AE_BIT, _return_from_exc_irq
|
|
|
|
/* pretend interrupt happened to use rtie instruction */
|
|
|
|
#ifdef CONFIG_ARC_HAS_SECURE
|
|
lr r3, [_ARC_V2_SEC_STAT]
|
|
/* set SEC_STAT.IRM = SECURE for interrupt return */
|
|
bset r3, r3, 3
|
|
/* sflag r3 */
|
|
/* sflag instruction is not supported in current ARC GNU */
|
|
.long 0x00ff302f
|
|
#endif
|
|
|
|
lr r3, [_ARC_V2_AUX_IRQ_ACT]
|
|
brne r3, 0, _switch_already_in_irq
|
|
|
|
/* use lowest interrupt priority */
|
|
or r3, r3, (1 << (CONFIG_NUM_IRQ_PRIO_LEVELS-1))
|
|
sr r3, [_ARC_V2_AUX_IRQ_ACT]
|
|
|
|
_switch_already_in_irq:
|
|
rtie
|
|
|
|
.balign 4
|
|
_return_from_exc_irq:
|
|
_pop_irq_stack_frame
|
|
sub_s sp, sp, ___isf_t_status32_OFFSET - ___isf_t_pc_OFFSET + 4
|
|
|
|
_return_from_exc:
|
|
|
|
/* put the return address to eret */
|
|
ld ilink, [sp] /* pc into ilink */
|
|
sr ilink, [_ARC_V2_ERET]
|
|
|
|
/* SEC_STAT is bypassed when CONFIG_ARC_HAS_SECURE */
|
|
|
|
/* put status32 into estatus */
|
|
ld ilink, [sp, ___isf_t_status32_OFFSET - ___isf_t_pc_OFFSET]
|
|
sr ilink, [_ARC_V2_ERSTATUS]
|
|
add_s sp, sp, ___isf_t_status32_OFFSET - ___isf_t_pc_OFFSET + 4
|
|
|
|
rtie
|
|
|
|
|
|
#ifdef CONFIG_EXECUTION_BENCHMARKING
|
|
.balign 4
|
|
_capture_value_for_benchmarking:
|
|
push_s blink
|
|
|
|
bl read_timer_end_of_swap
|
|
|
|
pop_s blink
|
|
b return_loc
|
|
#endif /* CONFIG_EXECUTION_BENCHMARKING */
|