2424; */
2525
2626
27+ IF :LNOT::DEF:RTX_STACK_CHECK
28+ RTX_STACK_CHECK EQU 0
29+ ENDIF
30+
2731 IF ({FPU}= "FPv4-SP" )
2832FPU_USED EQU 1
2933 ELSE
@@ -36,6 +40,10 @@ TCB_SF_OFS EQU 34 ; TCB.stack_frame offset
3640
3741FPCCR EQU 0xE000EF34 ; FPCCR Address
3842
43+ osRtxErrorStackOverflow\
44+ EQU 1 ; Stack overflow
45+
46+
3947 PRESERVE8
4048 THUMB
4149
@@ -52,6 +60,10 @@ SVC_Handler PROC
5260 EXPORT SVC_Handler
5361 IMPORT osRtxUserSVC
5462 IMPORT osRtxInfo
63+ IF RTX_STACK_CHECK != 0
64+ IMPORT osRtxThreadStackCheck
65+ IMPORT osRtxKernelErrorNotify
66+ ENDIF
5567
5668 TST LR , # 0x04 ; Determine return stack from EXC_RETURN bit 2
5769 ITE EQ
@@ -60,7 +72,8 @@ SVC_Handler PROC
6072
6173 LDR R1 ,[ R0 , # 24 ] ; Load saved PC from stack
6274 LDRB R1 ,[ R1 , # - 2 ] ; Load SVC number
63- CBNZ R1 , SVC_User ; Branch if not SVC 0
75+ CMP R1 , # 0 ; Check SVC number
76+ BNE SVC_User ; Branch if not SVC 0
6477
6578 PUSH {R0 , LR} ; Save SP and EXC_RETURN
6679 LDM R0 , {R0 - R3 , R12 } ; Load function parameters and address from stack
@@ -75,31 +88,73 @@ SVC_Context
7588 IT EQ
7689 BXEQ LR ; Exit when threads are the same
7790
91+ STR R2 ,[ R3 ] ; osRtxInfo.thread.run: curr = next
92+
7893 IF FPU_USED != 0
7994 CBNZ R1 , SVC_ContextSave ; Branch if running thread is not deleted
95+ SVC_FP_LazyState
8096 TST LR , # 0x10 ; Determine stack frame from EXC_RETURN bit 4
81- BNE SVC_ContextSwitch ; Branch if not extended stack frame
97+ BNE SVC_ContextRestore ; Branch if not extended stack frame
8298 LDR R3 , =FPCCR ; FPCCR Address
8399 LDR R0 ,[ R3 ] ; Load FPCCR
84100 BIC R0 , R0 , # 1 ; Clear LSPACT (Lazy state preservation)
85101 STR R0 ,[ R3 ] ; Store FPCCR
86- B SVC_ContextSwitch ; Branch to context switch handling
102+ B SVC_ContextRestore ; Branch to context restore handling
87103 ELSE
88- CBZ R1 , SVC_ContextSwitch ; Branch if running thread is deleted
104+ CBZ R1 , SVC_ContextRestore ; Branch if running thread is deleted
89105 ENDIF
90106
91107SVC_ContextSave
92- STMDB R12 ! , {R4 - R11 } ; Save R4..R11
108+ IF RTX_STACK_CHECK != 0
109+ SUB R12 , R12 , # 32 ; Calculate SP: space for R4..R11
93110 IF FPU_USED != 0
94111 TST LR , # 0x10 ; Determine stack frame from EXC_RETURN bit 4
95112 IT EQ ; If extended stack frame
96- VSTMDBEQ R12 ! , {S16 - S31} ; Save VFP S16.S31
113+ SUBEQ R12 , R12 , # 64 ; Additional space for S16. .S31
97114 STRB LR , [ R1 , #TCB_SF_OFS ] ; Store stack frame information
98115 ENDIF
99116 STR R12 ,[ R1 , #TCB_SP_OFS ] ; Store SP
100117
101- SVC_ContextSwitch
118+ PUSH {R1 , R2} ; Save osRtxInfo.thread.run: curr & next
119+ MOV R0 , R1 ; Parameter: osRtxInfo.thread.run.curr
120+ BL osRtxThreadStackCheck ; Check if thread stack is overrun
121+ POP {R1 , R2} ; Restore osRtxInfo.thread.run: curr & next
122+ CBNZ R0 , SVC_ContextSaveRegs ; Branch when stack check is ok
123+
124+ IF FPU_USED != 0
125+ MOV R4 , R1 ; Save osRtxInfo.thread.run.curr
126+ ENDIF
127+ MOV R0 , #osRtxErrorStackOverflow ; Parameter: r0=code, r1=object_id
128+ BL osRtxKernelErrorNotify ; Call osRtxKernelErrorNotify
129+ LDR R3 , =osRtxInfo + I_T_RUN_OFS ; Load address of osRtxInfo.thread.run
130+ LDR R2 ,[ R3 , # 4 ] ; Load osRtxInfo.thread.run: next
102131 STR R2 ,[ R3 ] ; osRtxInfo.thread.run: curr = next
132+ IF FPU_USED != 0
133+ LDRB LR ,[ R4 , #TCB_SF_OFS ] ; Load stack frame information
134+ B SVC_FP_LazyState ; Branch to FP lazy state handling
135+ ELSE
136+ B SVC_ContextRestore ; Branch to context restore handling
137+ ENDIF
138+
139+ SVC_ContextSaveRegs
140+ LDR R12 ,[ R1 , #TCB_SP_OFS ] ; Load SP
141+ IF FPU_USED != 0
142+ LDRB LR , [ R1 , #TCB_SF_OFS ] ; Load stack frame information
143+ TST LR , # 0x10 ; Determine stack frame from EXC_RETURN bit 4
144+ IT EQ ; If extended stack frame
145+ VSTMIAEQ R12 ! , {S16 - S31} ; Save VFP S16..S31
146+ ENDIF
147+ STM R12 , {R4 - R11 } ; Save R4..R11
148+ ELSE
149+ STMDB R12 ! , {R4 - R11 } ; Save R4..R11
150+ IF FPU_USED != 0
151+ TST LR , # 0x10 ; Determine stack frame from EXC_RETURN bit 4
152+ IT EQ ; If extended stack frame
153+ VSTMDBEQ R12 ! , {S16 - S31} ; Save VFP S16.S31
154+ STRB LR , [ R1 , #TCB_SF_OFS ] ; Store stack frame information
155+ ENDIF
156+ STR R12 ,[ R1 , #TCB_SP_OFS ] ; Store SP
157+ ENDIF
103158
104159SVC_ContextRestore
105160 LDR R0 ,[ R2 , #TCB_SP_OFS ] ; Load SP
0 commit comments