2022-05-17 03:21:15 -04:00
/ *
2023-05-04 11:31:31 -04:00
* SPDX- F i l e C o p y r i g h t T e x t : 2 0 2 2 - 2 0 2 3 E s p r e s s i f S y s t e m s ( S h a n g h a i ) C O L T D
2022-05-17 03:21:15 -04:00
*
* SPDX- L i c e n s e - I d e n t i f i e r : A p a c h e - 2 . 0
* /
2023-05-04 11:31:31 -04:00
# include " s d k c o n f i g . h "
# include " p o r t m a c r o . h "
# if C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D
# include " e s p _ p r i v a t e / h w _ s t a c k _ g u a r d . h "
# endif
2022-05-17 03:21:15 -04:00
.global uxInterruptNesting
.global uxSchedulerRunning
.global xIsrStackTop
.global pxCurrentTCBs
.global vTaskSwitchContext
.global xPortSwitchFlag
2023-05-04 11:31:31 -04:00
# if C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D
.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 / * C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D * /
2022-05-17 03:21:15 -04:00
.section .text
/ * *
* This f u n c t i o n m a k e s t h e R T O S a w a r e a b o u t a I S R e n t e r i n g , i t t a k e s t h e
2023-05-04 11:31:31 -04:00
* current t a s k s t a c k s a v e d , p l a c e s i n t o t h e T C B , l o a d s t h e I S R s t a c k .
* TODO : ISR n e s t i n g c o d e i m p r o v e m e n t s ?
2022-05-17 03:21:15 -04:00
* /
.global rtos_int_enter
.type rtos_ i n t _ e n t e r , @function
rtos_int_enter :
/* scheduler not enabled, jump directly to ISR handler */
lw t 0 , u x S c h e d u l e r R u n n i n g
beq t 0 ,z e r o , r t o s _ e n t e r _ e n d
/* increments the ISR nesting count */
la t 3 , u x I n t e r r u p t N e s t i n g
lw t 4 , 0 x0 ( t 3 )
addi t 5 ,t 4 ,1
sw t 5 , 0 x0 ( t 3 )
/* If reached here from another low-prio ISR, skip stack pushing to TCB */
bne t 4 ,z e r o , r t o s _ e n t e r _ e n d
2023-05-04 11:31:31 -04:00
# if C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D
/* esp_hw_stack_guard_monitor_stop(); */
ESP_ H W _ S T A C K _ G U A R D _ M O N I T O R _ S T O P _ C P U 0
# endif / * C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D * /
2022-05-17 03:21:15 -04:00
/* Save current TCB and load the ISR stack */
lw t 0 , p x C u r r e n t T C B s
2023-05-04 11:31:31 -04:00
sw s p , 0 x0 ( t 0 )
2022-05-17 03:21:15 -04:00
lw s p , x I s r S t a c k T o p
2023-05-04 11:31:31 -04:00
# if C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D
/* esp_hw_stack_guard_set_bounds(xIsrStack, xIsrStackTop); */
la a0 , x I s r S t a c k
mv a1 , s p
ESP_ H W _ S T A C K _ G U A R D _ S E T _ B O U N D S _ C P U 0
/* esp_hw_stack_guard_monitor_start(); */
ESP_ H W _ S T A C K _ G U A R D _ M O N I T O R _ S T A R T _ C P U 0
# endif / * C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D * /
2022-05-17 03:21:15 -04:00
rtos_enter_end :
ret
/ * *
2023-05-04 11:31:31 -04:00
* Restores t h e c o n t e x t o f t h e n e x t t a s k .
2022-05-17 03:21:15 -04:00
* /
.global rtos_int_exit
.type rtos_ i n t _ e x i t , @function
rtos_int_exit :
/* may skip RTOS aware interrupt since scheduler was not started */
lw t 0 , u x S c h e d u l e r R u n n i n g
beq t 0 ,z e r o , r t o s _ e x i t _ e n d
/* update nesting interrupts counter */
la t 2 , u x I n t e r r u p t N e s t i n g
lw t 3 , 0 x0 ( t 2 )
/* Already zero, protect against underflow */
beq t 3 , z e r o , i s r _ s k i p _ d e c r e m e n t
addi t 3 ,t 3 , - 1
sw t 3 , 0 x0 ( t 2 )
isr_skip_decrement :
/* may still have interrupts pending, skip section below and exit */
bne t 3 ,z e r o ,r t o s _ e x i t _ e n d
/* Schedule the next task if a yield is pending */
la t 0 , x P o r t S w i t c h F l a g
lw t 2 , 0 x0 ( t 0 )
beq t 2 , z e r o , n o _ s w i t c h
/ * preserve r e t u r n a d d r e s s a n d s c h e d u l e n e x t t a s k
stack p o i n t e r f o r r i s c v s h o u l d a l w a y s b e 1 6 b y t e a l i g n e d * /
addi s p ,s p ,- 1 6
sw r a , 0 ( s p )
/* vTaskSwitchContext(xCoreID) now expects xCoreID as an argument, so the assembly calls below have been modified. xCoreID is hard-wired to 0 for single-core risc-v. */
li a0 , 0
call v T a s k S w i t c h C o n t e x t
lw r a , 0 ( s p )
addi s p , s p , 1 6
/* Clears the switch pending flag */
la t 0 , x P o r t S w i t c h F l a g
mv t 2 , z e r o
sw t 2 , 0 x0 ( t 0 )
no_switch :
2023-05-04 11:31:31 -04:00
# if C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D
/* esp_hw_stack_guard_monitor_stop(); */
ESP_ H W _ S T A C K _ G U A R D _ M O N I T O R _ S T O P _ C P U 0
# endif / * C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D * /
/* Recover the stack of next task */
lw t 0 , p x C u r r e n t T C B s
lw s p , 0 x0 ( t 0 )
# if C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D
/ * esp_ h w _ s t a c k _ g u a r d _ s e t _ b o u n d s ( p x C u r r e n t T C B s [ 0 ] - > p x S t a c k ,
* pxCurrentTCBs[ 0 ] - > p x E n d O f S t a c k ) ;
* /
lw a0 , P O R T _ O F F S E T _ P X _ S T A C K ( t 0 )
lw a1 , P O R T _ O F F S E T _ P X _ E N D _ O F _ S T A C K ( t 0 )
ESP_ H W _ S T A C K _ G U A R D _ S E T _ B O U N D S _ C P U 0
/* esp_hw_stack_guard_monitor_start(); */
ESP_ H W _ S T A C K _ G U A R D _ M O N I T O R _ S T A R T _ C P U 0
# endif / * C O N F I G _ E S P _ S Y S T E M _ H W _ S T A C K _ G U A R D * /
2022-05-17 03:21:15 -04:00
rtos_exit_end :
ret