1/* 2 * Copyright (c) 2019 Synopsys. 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7/** 8 * @file 9 * @brief Thread context switching 10 * 11 * This module implements the routines necessary for thread context switching 12 * on ARCv2 CPUs. 13 * 14 * See isr_wrapper.S for details. 15 */ 16 17#include <zephyr/kernel_structs.h> 18#include <offsets_short.h> 19#include <zephyr/toolchain.h> 20#include <zephyr/linker/sections.h> 21#include <zephyr/arch/cpu.h> 22#include <v2/irq.h> 23#include <swap_macros.h> 24#include <zephyr/arch/arc/asm-compat/assembler.h> 25 26GTEXT(z_arc_switch) 27 28/** 29 * 30 * @brief Initiate a cooperative context switch 31 * 32 * The arch_switch routine is invoked by various kernel services to effect 33 * a cooperative context switch. Prior to invoking arch_switch, the caller 34 * disables interrupts via irq_lock() 35 36 * Given that arch_switch() is called to effect a cooperative context switch, 37 * the caller-saved integer registers are saved on the stack by the function 38 * call preamble to arch_switch. This creates a custom stack frame that will 39 * be popped when returning from arch_switch, but is not suitable for handling 40 * a return from an exception. Thus, the fact that the thread is pending because 41 * of a cooperative call to arch_switch() has to be recorded via the 42 * _CAUSE_COOP code in the relinquish_cause of the thread's k_thread structure. 43 * The _rirq_exit()/_firq_exit() code will take care of doing the right thing 44 * to restore the thread status. 45 * 46 * When arch_switch() is invoked, we know the decision to perform a context 47 * switch or not has already been taken and a context switch must happen. 48 * 49 * 50 * C function prototype: 51 * 52 * void arch_switch(void *switch_to, void **switched_from); 53 * 54 */ 55 56SECTION_FUNC(TEXT, z_arc_switch) 57 58 /* 59 * r0 = new_thread->switch_handle = switch_to thread, 60 * r1 = &old_thread->switch_handle 61 * get old_thread from r1 62 */ 63 64 SUBR r2, r1, ___thread_t_switch_handle_OFFSET 65 66 /* _thread_arch.relinquish_cause is 32 bit despite of platform bittnes */ 67 _st32_huge_offset _CAUSE_COOP, r2, _thread_offset_to_relinquish_cause, r3 68 69 /* 70 * Save status32 and blink on the stack before the callee-saved registers. 71 * This is the same layout as the start of an IRQ stack frame. 72 */ 73 LRR r3, [_ARC_V2_STATUS32] 74 PUSHR r3 75 76#ifdef CONFIG_ARC_HAS_SECURE 77#ifdef CONFIG_ARC_SECURE_FIRMWARE 78 lr r3, [_ARC_V2_SEC_STAT] 79#else 80 mov_s r3, 0 81#endif 82 push_s r3 83#endif 84 85 PUSHR blink 86 87 _store_old_thread_callee_regs 88 89/* disable stack checking here, as sp will be changed to target 90 * thread'sp 91 */ 92 _disable_stack_checking r3 93 94 MOVR r2, r0 95 96 _load_new_thread_callee_regs 97 98 breq r3, _CAUSE_RIRQ, _switch_return_from_rirq 99 nop_s 100 breq r3, _CAUSE_FIRQ, _switch_return_from_firq 101 nop_s 102 103 /* fall through to _switch_return_from_coop */ 104 105.align 4 106_switch_return_from_coop: 107 108 POPR blink /* pc into blink */ 109#ifdef CONFIG_ARC_HAS_SECURE 110 pop_s r3 /* pop SEC_STAT */ 111#ifdef CONFIG_ARC_SECURE_FIRMWARE 112 sflag r3 113#endif 114#endif 115 POPR r3 /* status32 into r3 */ 116 kflag r3 /* write status32 */ 117 118#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING 119 PUSHR blink 120 121 bl z_thread_mark_switched_in 122 123 POPR blink 124#endif 125 j_s [blink] 126 127 128.align 4 129_switch_return_from_rirq: 130_switch_return_from_firq: 131 132 _set_misc_regs_irq_switch_from_irq 133 134 /* use lowest interrupt priority to simulate 135 * a interrupt return to load left regs of new 136 * thread 137 */ 138 139 LRR r3, [_ARC_V2_AUX_IRQ_ACT] 140#ifdef CONFIG_ARC_SECURE_FIRMWARE 141 or r3, r3, (1 << (ARC_N_IRQ_START_LEVEL - 1)) 142#else 143 ORR r3, r3, (1 << (CONFIG_NUM_IRQ_PRIO_LEVELS - 1)) 144#endif 145 146#ifdef CONFIG_ARC_NORMAL_FIRMWARE 147 mov_s r0, _ARC_V2_AUX_IRQ_ACT 148 mov_s r1, r3 149 mov_s r6, ARC_S_CALL_AUX_WRITE 150 sjli SJLI_CALL_ARC_SECURE 151#else 152 SRR r3, [_ARC_V2_AUX_IRQ_ACT] 153#endif 154#ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING 155 PUSHR blink 156 157 bl z_thread_mark_switched_in 158 159 POPR blink 160#endif 161 rtie 162