2022-01-29 03:49:56 -05:00
|
|
|
/*
|
2023-07-18 04:21:15 -04:00
|
|
|
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
2022-01-29 03:49:56 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2023-05-04 11:31:31 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "portmacro.h"
|
|
|
|
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD
|
|
|
|
#include "esp_private/hw_stack_guard.h"
|
|
|
|
#endif
|
2020-11-05 23:03:21 -05:00
|
|
|
|
|
|
|
.global uxInterruptNesting
|
|
|
|
.global uxSchedulerRunning
|
|
|
|
.global xIsrStackTop
|
2023-07-18 04:21:15 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
//TODO: IDF-7566
|
|
|
|
.global xIsrStackTop1
|
|
|
|
#endif
|
2020-11-05 23:03:21 -05:00
|
|
|
.global pxCurrentTCB
|
|
|
|
.global vTaskSwitchContext
|
|
|
|
.global xPortSwitchFlag
|
2023-05-04 11:31:31 -04:00
|
|
|
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD
|
|
|
|
.global xIsrStack
|
|
|
|
.global port_offset_pxStack
|
|
|
|
.global port_offset_pxEndOfStack
|
|
|
|
.global esp_hw_stack_guard_monitor_stop
|
|
|
|
.global esp_hw_stack_guard_monitor_start
|
|
|
|
.global esp_hw_stack_guard_set_bounds
|
|
|
|
#endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */
|
2020-11-05 23:03:21 -05:00
|
|
|
|
|
|
|
.section .text
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function makes the RTOS aware about a ISR entering, it takes the
|
2023-05-04 11:31:31 -04:00
|
|
|
* current task stack saved, places into the TCB, loads the ISR stack.
|
|
|
|
* TODO: ISR nesting code improvements ?
|
2020-11-05 23:03:21 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
.global rtos_int_enter
|
|
|
|
.type rtos_int_enter, @function
|
|
|
|
rtos_int_enter:
|
2023-07-18 04:21:15 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
/* needs jira for p4 */
|
|
|
|
/* preserve the return address */
|
|
|
|
mv t1, ra
|
|
|
|
mv t2, a0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//TODO: IDF-7566
|
|
|
|
#if !CONFIG_IDF_TARGET_ESP32P4
|
2020-11-05 23:03:21 -05:00
|
|
|
/* scheduler not enabled, jump directly to ISR handler */
|
2020-12-28 23:31:54 -05:00
|
|
|
lw t0, uxSchedulerRunning
|
2020-11-04 16:34:47 -05:00
|
|
|
beq t0,zero, rtos_enter_end
|
2023-07-18 04:21:15 -04:00
|
|
|
#else
|
|
|
|
/* scheduler not enabled, jump directly to ISR handler */
|
|
|
|
csrr t6, mhartid /* t6 = coreID */
|
|
|
|
slli t6, t6, 2 /* t6 = coreID * 4 */
|
|
|
|
la t0, uxSchedulerRunning /* t0 = &uxSchedulerRunning */
|
|
|
|
add t0, t0, t6 /* t0 = &uxSchedulerRunning[coreID] */
|
|
|
|
lw t0, (t0) /* t0 = uxSchedulerRunning[coreID] */
|
|
|
|
beq t0,zero, rtos_enter_end
|
|
|
|
#endif
|
2020-11-05 23:03:21 -05:00
|
|
|
|
2020-11-04 16:34:47 -05:00
|
|
|
/* increments the ISR nesting count */
|
2023-07-18 04:21:15 -04:00
|
|
|
la t3, uxInterruptNesting
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
//TODO: IDF-7566
|
|
|
|
add t3, t3, t6
|
|
|
|
#endif
|
|
|
|
lw t4, 0x0(t3)
|
|
|
|
addi t5,t4,1
|
|
|
|
sw t5, 0x0(t3)
|
2020-12-28 23:31:54 -05:00
|
|
|
|
2020-11-04 16:34:47 -05:00
|
|
|
/* If reached here from another low-prio ISR, skip stack pushing to TCB */
|
2023-07-18 04:21:15 -04:00
|
|
|
bne t4,zero, rtos_enter_end
|
2020-11-04 16:34:47 -05:00
|
|
|
|
2023-05-04 11:31:31 -04:00
|
|
|
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD
|
|
|
|
/* esp_hw_stack_guard_monitor_stop(); */
|
|
|
|
ESP_HW_STACK_GUARD_MONITOR_STOP_CPU0
|
|
|
|
#endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */
|
|
|
|
|
2020-11-04 16:34:47 -05:00
|
|
|
/* Save current TCB and load the ISR stack */
|
2023-07-18 04:21:15 -04:00
|
|
|
//TODO: IDF-7566
|
|
|
|
#if !CONFIG_IDF_TARGET_ESP32P4
|
2020-11-05 23:03:21 -05:00
|
|
|
lw t0, pxCurrentTCB
|
2023-05-04 11:31:31 -04:00
|
|
|
sw sp, 0x0(t0)
|
2020-11-05 23:03:21 -05:00
|
|
|
lw sp, xIsrStackTop
|
2023-07-18 04:21:15 -04:00
|
|
|
#else
|
|
|
|
la t0, pxCurrentTCB /* t0 = &pxCurrentTCB */
|
|
|
|
add t0, t0, t6 /* t0 = &pxCurrentTCB[coreID] */
|
|
|
|
lw t0, (t0) /* t0 = pxCurrentTCB[coreID] */
|
|
|
|
sw t2, 0x0(t0)
|
|
|
|
lw sp, xIsrStackTop
|
|
|
|
csrr t6, mhartid
|
|
|
|
beq t6, zero, rtos_enter_end
|
|
|
|
lw sp, xIsrStackTop1
|
|
|
|
#endif
|
2020-11-05 23:03:21 -05:00
|
|
|
|
2023-05-04 11:31:31 -04:00
|
|
|
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD
|
|
|
|
/* esp_hw_stack_guard_set_bounds(xIsrStack, xIsrStackTop); */
|
|
|
|
la a0, xIsrStack
|
|
|
|
mv a1, sp
|
|
|
|
ESP_HW_STACK_GUARD_SET_BOUNDS_CPU0
|
|
|
|
ESP_HW_STACK_GUARD_MONITOR_START_CPU0
|
|
|
|
#endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */
|
|
|
|
|
2020-11-04 16:34:47 -05:00
|
|
|
rtos_enter_end:
|
2023-07-18 04:21:15 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
/* needs jira for p4 */
|
|
|
|
mv ra, t1
|
|
|
|
#endif
|
2020-11-05 23:03:21 -05:00
|
|
|
ret
|
|
|
|
|
|
|
|
/**
|
2023-05-04 11:31:31 -04:00
|
|
|
* Recovers the next task to run stack pointer.
|
2020-11-05 23:03:21 -05:00
|
|
|
*/
|
|
|
|
.global rtos_int_exit
|
|
|
|
.type rtos_int_exit, @function
|
|
|
|
rtos_int_exit:
|
|
|
|
/* may skip RTOS aware interrupt since scheduler was not started */
|
2023-07-18 04:21:15 -04:00
|
|
|
|
|
|
|
//TODO: IDF-7566
|
|
|
|
#if !CONFIG_IDF_TARGET_ESP32P4
|
2020-12-28 23:31:54 -05:00
|
|
|
lw t0, uxSchedulerRunning
|
2023-07-18 04:21:15 -04:00
|
|
|
#else
|
|
|
|
csrr t1, mhartid
|
|
|
|
slli t1, t1, 2
|
|
|
|
la t0, uxSchedulerRunning /* t0 = &uxSchedulerRunning */
|
|
|
|
add t0, t0, t1 /* t0 = &uxSchedulerRunning[coreID] */
|
|
|
|
lw t0, (t0)
|
|
|
|
#endif
|
2020-12-28 23:31:54 -05:00
|
|
|
beq t0,zero, rtos_exit_end
|
2020-11-04 16:34:47 -05:00
|
|
|
|
|
|
|
/* update nesting interrupts counter */
|
2020-11-13 14:03:50 -05:00
|
|
|
la t2, uxInterruptNesting
|
2023-07-18 04:21:15 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
//TODO: IDF-7566
|
|
|
|
add t2, t2, t1
|
|
|
|
#endif
|
2020-11-13 14:03:50 -05:00
|
|
|
lw t3, 0x0(t2)
|
2020-11-04 16:34:47 -05:00
|
|
|
|
2020-11-19 13:14:54 -05:00
|
|
|
/* Already zero, protect against underflow */
|
2020-11-13 14:03:50 -05:00
|
|
|
beq t3, zero, isr_skip_decrement
|
|
|
|
addi t3,t3, -1
|
|
|
|
sw t3, 0x0(t2)
|
2020-11-04 16:34:47 -05:00
|
|
|
|
|
|
|
isr_skip_decrement:
|
|
|
|
|
2020-11-13 14:03:50 -05:00
|
|
|
/* may still have interrupts pending, skip section below and exit */
|
2020-12-28 23:31:54 -05:00
|
|
|
bne t3,zero,rtos_exit_end
|
2020-11-05 23:03:21 -05:00
|
|
|
|
|
|
|
/* Schedule the next task if a yield is pending */
|
|
|
|
la t0, xPortSwitchFlag
|
2023-07-18 04:21:15 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
//TODO: IDF-7566
|
|
|
|
add t0, t0, t1
|
|
|
|
#endif
|
2020-11-05 23:03:21 -05:00
|
|
|
lw t2, 0x0(t0)
|
|
|
|
beq t2, zero, no_switch
|
|
|
|
|
2021-02-18 22:26:21 -05:00
|
|
|
/* preserve return address and schedule next task
|
|
|
|
stack pointer for riscv should always be 16 byte aligned */
|
|
|
|
addi sp,sp,-16
|
2021-01-26 06:05:41 -05:00
|
|
|
sw ra, 0(sp)
|
2020-11-05 23:03:21 -05:00
|
|
|
call vTaskSwitchContext
|
2021-01-26 06:05:41 -05:00
|
|
|
lw ra, 0(sp)
|
2021-02-18 22:26:21 -05:00
|
|
|
addi sp, sp, 16
|
2020-11-05 23:03:21 -05:00
|
|
|
|
|
|
|
/* Clears the switch pending flag */
|
|
|
|
la t0, xPortSwitchFlag
|
2023-07-18 04:21:15 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
//TODO: IDF-7566
|
|
|
|
/* c routine vTaskSwitchContext may change the temp registers, so we read again */
|
|
|
|
csrr t3, mhartid
|
|
|
|
slli t3, t3, 2
|
|
|
|
add t0, t0, t3
|
|
|
|
#endif
|
2020-11-05 23:03:21 -05:00
|
|
|
mv t2, zero
|
|
|
|
sw t2, 0x0(t0)
|
|
|
|
|
|
|
|
no_switch:
|
2023-05-04 11:31:31 -04:00
|
|
|
|
2023-07-18 04:21:15 -04:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32P4 //TODO: IDF-7566
|
2023-05-04 11:31:31 -04:00
|
|
|
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD
|
|
|
|
/* esp_hw_stack_guard_monitor_stop(); */
|
|
|
|
ESP_HW_STACK_GUARD_MONITOR_STOP_CPU0
|
|
|
|
#endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */
|
|
|
|
|
|
|
|
/* Recover the stack of next task */
|
|
|
|
lw t0, pxCurrentTCB
|
|
|
|
lw sp, 0x0(t0)
|
|
|
|
|
|
|
|
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD
|
|
|
|
/* esp_hw_stack_guard_set_bounds(pxCurrentTCB[0]->pxStack,
|
|
|
|
* pxCurrentTCB[0]->pxEndOfStack);
|
|
|
|
*/
|
|
|
|
lw a0, PORT_OFFSET_PX_STACK(t0)
|
|
|
|
lw a1, PORT_OFFSET_PX_END_OF_STACK(t0)
|
|
|
|
ESP_HW_STACK_GUARD_SET_BOUNDS_CPU0
|
|
|
|
/* esp_hw_stack_guard_monitor_start(); */
|
|
|
|
ESP_HW_STACK_GUARD_MONITOR_START_CPU0
|
|
|
|
#endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */
|
2020-11-05 23:03:21 -05:00
|
|
|
|
2023-07-18 04:21:15 -04:00
|
|
|
#else
|
|
|
|
/* Recover the stack of next task and prepare to exit : */
|
|
|
|
la a0, pxCurrentTCB
|
|
|
|
/* We may come here from a branch, so we re-cal here */
|
|
|
|
csrr t3, mhartid
|
|
|
|
slli t3, t3, 2
|
|
|
|
add a0, a0, t3 /* a0 = &pxCurrentTCB[coreID] */
|
|
|
|
lw a0, (a0) /* a0 = pxCurrentTCB[coreID] */
|
|
|
|
lw a0, 0x0(a0) /* a0 = previous sp */
|
|
|
|
#endif //#if !CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
|
2020-11-04 16:34:47 -05:00
|
|
|
rtos_exit_end:
|
2020-11-05 23:03:21 -05:00
|
|
|
ret
|