irq_armv7m.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2013-2023 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * -----------------------------------------------------------------------------
  19. *
  20. * Project: CMSIS-RTOS RTX
  21. * Title: ARMv7-M Exception handlers
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. .syntax unified
  26. #include "rtx_def.h"
  27. #if (defined(__ARM_FP) && (__ARM_FP > 0))
  28. .equ FPU_USED, 1
  29. #else
  30. .equ FPU_USED, 0
  31. #endif
  32. .equ I_T_RUN_OFS, 20 // osRtxInfo.thread.run offset
  33. .equ TCB_SP_OFS, 56 // TCB.SP offset
  34. .equ TCB_SF_OFS, 34 // TCB.stack_frame offset
  35. .equ TCB_ZONE_OFS, 68 // TCB.zone offset
  36. .equ FPCCR, 0xE000EF34 // FPCCR Address
  37. .equ osRtxErrorStackOverflow, 1 // Stack overflow
  38. .equ osRtxErrorSVC, 6 // Invalid SVC function called
  39. .section ".rodata"
  40. .global irqRtxLib // Non weak library reference
  41. irqRtxLib:
  42. .byte 0
  43. .thumb
  44. .section ".text"
  45. .align 2
  46. .eabi_attribute Tag_ABI_align_preserved, 1
  47. .thumb_func
  48. .type SVC_Handler, %function
  49. .global SVC_Handler
  50. .fnstart
  51. .cantunwind
  52. SVC_Handler:
  53. tst lr,#0x04 // Determine return stack from EXC_RETURN bit 2
  54. ite eq
  55. mrseq r0,msp // Get MSP if return stack is MSP
  56. mrsne r0,psp // Get PSP if return stack is PSP
  57. ldr r1,[r0,#24] // Load saved PC from stack
  58. ldrb r1,[r1,#-2] // Load SVC number
  59. cmp r1,#0 // Check SVC number
  60. bne SVC_User // Branch if not SVC 0
  61. #ifdef RTX_SVC_PTR_CHECK
  62. ldr r12,[r0,#16] // Load function address from stack
  63. sub r1,r12,#1 // Clear T-bit of function address
  64. lsls r2,r1,#30 // Check if 4-byte aligned
  65. beq SVC_PtrBoundsCheck // Branch if address is aligned
  66. SVC_PtrInvalid:
  67. push {r0,lr} // Save SP and EXC_RETURN
  68. movs r0,#osRtxErrorSVC // Parameter: code
  69. mov r1,r12 // Parameter: object_id
  70. bl osRtxKernelErrorNotify // Call osRtxKernelErrorNotify
  71. pop {r12,lr} // Restore SP and EXC_RETURN
  72. b SVC_Context // Branch to context handling
  73. SVC_PtrBoundsCheck:
  74. ldr r2,=Image$$RTX_SVC_VENEERS$$Base
  75. ldr r3,=Image$$RTX_SVC_VENEERS$$Length
  76. subs r2,r1,r2 // Subtract SVC table base address
  77. cmp r2,r3 // Compare with SVC table boundaries
  78. bhs SVC_PtrInvalid // Branch if address is out of bounds
  79. #endif // RTX_SVC_PTR_CHECK
  80. push {r0,lr} // Save SP and EXC_RETURN
  81. ldm r0,{r0-r3,r12} // Load function parameters and address from stack
  82. blx r12 // Call service function
  83. pop {r12,lr} // Restore SP and EXC_RETURN
  84. str r0,[r12] // Store function return value
  85. SVC_Context:
  86. ldr r3,=osRtxInfo+I_T_RUN_OFS // Load address of osRtxInfo.thread.run
  87. ldm r3,{r1,r2} // Load osRtxInfo.thread.run: curr & next
  88. cmp r1,r2 // Check if thread switch is required
  89. it eq
  90. bxeq lr // Exit when threads are the same
  91. str r2,[r3] // osRtxInfo.thread.run: curr = next
  92. .if (FPU_USED != 0)
  93. cbnz r1,SVC_ContextSave // Branch if running thread is not deleted
  94. SVC_FP_LazyState:
  95. tst lr,#0x10 // Determine stack frame from EXC_RETURN bit 4
  96. bne SVC_ContextRestore // Branch if not extended stack frame
  97. ldr r3,=FPCCR // FPCCR Address
  98. ldr r0,[r3] // Load FPCCR
  99. bic r0,r0,#1 // Clear LSPACT (Lazy state preservation)
  100. str r0,[r3] // Store FPCCR
  101. b SVC_ContextRestore // Branch to context restore handling
  102. .else
  103. cbz r1,SVC_ContextRestore // Branch if running thread is deleted
  104. .endif
  105. SVC_ContextSave:
  106. #ifdef RTX_STACK_CHECK
  107. sub r12,r12,#32 // Calculate SP: space for R4..R11
  108. .if (FPU_USED != 0)
  109. tst lr,#0x10 // Determine stack frame from EXC_RETURN bit 4
  110. it eq // If extended stack frame
  111. subeq r12,r12,#64 // Additional space for S16..S31
  112. strb lr, [r1,#TCB_SF_OFS] // Store stack frame information
  113. .endif
  114. str r12,[r1,#TCB_SP_OFS] // Store SP
  115. push {r1,r2} // Save osRtxInfo.thread.run: curr & next
  116. mov r0,r1 // Parameter: osRtxInfo.thread.run.curr
  117. bl osRtxThreadStackCheck // Check if thread stack is overrun
  118. pop {r1,r2} // Restore osRtxInfo.thread.run: curr & next
  119. cbnz r0,SVC_ContextSaveRegs // Branch when stack check is ok
  120. .if (FPU_USED != 0)
  121. mov r4,r1 // Assign osRtxInfo.thread.run.curr to R4
  122. .endif
  123. movs r0,#osRtxErrorStackOverflow // Parameter: r0=code, r1=object_id
  124. bl osRtxKernelErrorNotify // Call osRtxKernelErrorNotify
  125. ldr r3,=osRtxInfo+I_T_RUN_OFS // Load address of osRtxInfo.thread.run
  126. ldr r2,[r3,#4] // Load osRtxInfo.thread.run: next
  127. str r2,[r3] // osRtxInfo.thread.run: curr = next
  128. movs r1,#0 // Simulate deleted running thread
  129. .if (FPU_USED != 0)
  130. ldrsb lr,[r4,#TCB_SF_OFS] // Load stack frame information
  131. b SVC_FP_LazyState // Branch to FP lazy state handling
  132. .else
  133. b SVC_ContextRestore // Branch to context restore handling
  134. .endif
  135. SVC_ContextSaveRegs:
  136. ldr r12,[r1,#TCB_SP_OFS] // Load SP
  137. .if (FPU_USED != 0)
  138. ldrsb lr, [r1,#TCB_SF_OFS] // Load stack frame information
  139. tst lr,#0x10 // Determine stack frame from EXC_RETURN bit 4
  140. it eq // If extended stack frame
  141. vstmiaeq r12!,{s16-s31} // Save VFP S16..S31
  142. .endif
  143. stm r12,{r4-r11} // Save R4..R11
  144. #else
  145. stmdb r12!,{r4-r11} // Save R4..R11
  146. .if (FPU_USED != 0)
  147. tst lr,#0x10 // Determine stack frame from EXC_RETURN bit 4
  148. it eq // If extended stack frame
  149. vstmdbeq r12!,{s16-s31} // Save VFP S16.S31
  150. strb lr, [r1,#TCB_SF_OFS] // Store stack frame information
  151. .endif
  152. str r12,[r1,#TCB_SP_OFS] // Store SP
  153. #endif // RTX_STACK_CHECK
  154. SVC_ContextRestore:
  155. movs r4,r2 // Assign osRtxInfo.thread.run.next to R4, clear Z flag
  156. #ifdef RTX_EXECUTION_ZONE
  157. ldrb r0,[r2,#TCB_ZONE_OFS] // Load osRtxInfo.thread.run.next: zone
  158. cbz r1,SVC_ZoneSetup // Branch if running thread is deleted (Z flag unchanged)
  159. ldrb r1,[r1,#TCB_ZONE_OFS] // Load osRtxInfo.thread.run.curr: zone
  160. cmp r0,r1 // Check if next:zone == curr:zone
  161. SVC_ZoneSetup:
  162. it ne // If zone has changed or running thread is deleted
  163. blne osZoneSetup_Callback // Setup zone for next thread
  164. #endif // RTX_EXECUTION_ZONE
  165. ldr r0,[r4,#TCB_SP_OFS] // Load SP
  166. .if (FPU_USED != 0)
  167. ldrsb lr,[r4,#TCB_SF_OFS] // Load stack frame information
  168. tst lr,#0x10 // Determine stack frame from EXC_RETURN bit 4
  169. it eq // If extended stack frame
  170. vldmiaeq r0!,{s16-s31} // Restore VFP S16..S31
  171. .else
  172. mvn lr,#~0xFFFFFFFD // Set EXC_RETURN value
  173. .endif
  174. ldmia r0!,{r4-r11} // Restore R4..R11
  175. msr psp,r0 // Set PSP
  176. SVC_Exit:
  177. bx lr // Exit from handler
  178. SVC_User:
  179. ldr r2,=osRtxUserSVC // Load address of SVC table
  180. ldr r3,[r2] // Load SVC maximum number
  181. cmp r1,r3 // Check SVC number range
  182. bhi SVC_Exit // Branch if out of range
  183. push {r0,lr} // Save SP and EXC_RETURN
  184. ldr r12,[r2,r1,lsl #2] // Load address of SVC function
  185. ldm r0,{r0-r3} // Load function parameters from stack
  186. blx r12 // Call service function
  187. pop {r12,lr} // Restore SP and EXC_RETURN
  188. str r0,[r12] // Store function return value
  189. bx lr // Return from handler
  190. .fnend
  191. .size SVC_Handler, .-SVC_Handler
  192. .thumb_func
  193. .type PendSV_Handler, %function
  194. .global PendSV_Handler
  195. .fnstart
  196. .cantunwind
  197. PendSV_Handler:
  198. push {r0,lr} // Save EXC_RETURN
  199. bl osRtxPendSV_Handler // Call osRtxPendSV_Handler
  200. pop {r0,lr} // Restore EXC_RETURN
  201. mrs r12,psp // Save PSP to R12
  202. b SVC_Context // Branch to context handling
  203. .fnend
  204. .size PendSV_Handler, .-PendSV_Handler
  205. .thumb_func
  206. .type SysTick_Handler, %function
  207. .global SysTick_Handler
  208. .fnstart
  209. .cantunwind
  210. SysTick_Handler:
  211. push {r0,lr} // Save EXC_RETURN
  212. bl osRtxTick_Handler // Call osRtxTick_Handler
  213. pop {r0,lr} // Restore EXC_RETURN
  214. mrs r12,psp // Save PSP to R12
  215. b SVC_Context // Branch to context handling
  216. .fnend
  217. .size SysTick_Handler, .-SysTick_Handler
  218. #ifdef RTX_SAFETY_FEATURES
  219. .thumb_func
  220. .type osFaultResume, %function
  221. .global osFaultResume
  222. .fnstart
  223. .cantunwind
  224. osFaultResume:
  225. mrs r12,psp // Save PSP to R12
  226. b SVC_Context // Branch to context handling
  227. .fnend
  228. .size osFaultResume, .-osFaultResume
  229. #endif // RTX_SAFETY_FEATURES
  230. .end