irq_armv6m.s 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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: ARMv6-M Exception handlers
  22. ; *
  23. ; * -----------------------------------------------------------------------------
  24. ; */
  25. NAME irq_armv6m.s
  26. #include "rtx_def.h"
  27. I_T_RUN_OFS EQU 20 ; osRtxInfo.thread.run offset
  28. TCB_SP_OFS EQU 56 ; TCB.SP offset
  29. TCB_ZONE_OFS EQU 68 ; TCB.zone offset
  30. osRtxErrorStackOverflow\
  31. EQU 1 ; Stack overflow
  32. osRtxErrorSVC EQU 6 ; Invalid SVC function called
  33. PRESERVE8
  34. SECTION .rodata:DATA:NOROOT(2)
  35. EXPORT irqRtxLib
  36. irqRtxLib DCB 0 ; Non weak library reference
  37. THUMB
  38. SECTION .text:CODE:NOROOT(2)
  39. SVC_Handler
  40. EXPORT SVC_Handler
  41. IMPORT osRtxUserSVC
  42. IMPORT osRtxInfo
  43. #ifdef RTX_STACK_CHECK
  44. IMPORT osRtxThreadStackCheck
  45. IMPORT osRtxKernelErrorNotify
  46. #endif
  47. #ifdef RTX_SVC_PTR_CHECK
  48. IMPORT |Image$$RTX_SVC_VENEERS$$Base|
  49. IMPORT |Image$$RTX_SVC_VENEERS$$Length|
  50. IMPORT osRtxKernelErrorNotify
  51. #endif
  52. #ifdef RTX_EXECUTION_ZONE
  53. IMPORT osZoneSetup_Callback
  54. #endif
  55. MOV R0,LR
  56. LSRS R0,R0,#3 ; Determine return stack from EXC_RETURN bit 2
  57. BCC SVC_MSP ; Branch if return stack is MSP
  58. MRS R0,PSP ; Get PSP
  59. SVC_Number
  60. LDR R1,[R0,#24] ; Load saved PC from stack
  61. SUBS R1,R1,#2 ; Point to SVC instruction
  62. LDRB R1,[R1] ; Load SVC number
  63. CMP R1,#0 ; Check SVC number
  64. BNE SVC_User ; Branch if not SVC 0
  65. #ifdef RTX_SVC_PTR_CHECK
  66. SUBS R1,R7,#0x01 ; Clear T-bit of function address
  67. LSLS R2,R1,#29 ; Check if 8-byte aligned
  68. BEQ SVC_PtrBoundsCheck ; Branch if address is aligned
  69. SVC_PtrInvalid
  70. PUSH {R0,LR} ; Save SP and EXC_RETURN
  71. MOVS R0,#osRtxErrorSVC ; Parameter: code
  72. MOV R1,R7 ; Parameter: object_id
  73. BL osRtxKernelErrorNotify ; Call osRtxKernelErrorNotify
  74. POP {R2,R3} ; Restore SP and EXC_RETURN
  75. MOV LR,R3 ; Set EXC_RETURN
  76. B SVC_Context ; Branch to context handling
  77. SVC_PtrBoundsCheck
  78. LDR R2,=|Image$$RTX_SVC_VENEERS$$Base|
  79. LDR R3,=|Image$$RTX_SVC_VENEERS$$Length|
  80. SUBS R2,R1,R2 ; Subtract SVC table base address
  81. CMP R2,R3 ; Compare with SVC table boundaries
  82. BHS SVC_PtrInvalid ; Branch if address is out of bounds
  83. #endif
  84. PUSH {R0,LR} ; Save SP and EXC_RETURN
  85. LDMIA R0,{R0-R3} ; Load function parameters from stack
  86. BLX R7 ; Call service function
  87. POP {R2,R3} ; Restore SP and EXC_RETURN
  88. STR R0,[R2] ; Store function return value
  89. MOV LR,R3 ; Set EXC_RETURN
  90. SVC_Context
  91. LDR R3,=osRtxInfo+I_T_RUN_OFS; Load address of osRtxInfo.thread.run
  92. LDMIA R3!,{R1,R2} ; Load osRtxInfo.thread.run: curr & next
  93. CMP R1,R2 ; Check if thread switch is required
  94. BEQ SVC_Exit ; Branch when threads are the same
  95. SUBS R3,R3,#8 ; Adjust address
  96. STR R2,[R3] ; osRtxInfo.thread.run: curr = next
  97. CMP R1,#0
  98. BEQ SVC_ContextRestore ; Branch if running thread is deleted
  99. SVC_ContextSave
  100. MRS R0,PSP ; Get PSP
  101. SUBS R0,R0,#32 ; Calculate SP: space for R4..R11
  102. STR R0,[R1,#TCB_SP_OFS] ; Store SP
  103. #ifdef RTX_STACK_CHECK
  104. PUSH {R1,R2} ; Save osRtxInfo.thread.run: curr & next
  105. MOV R0,R1 ; Parameter: osRtxInfo.thread.run.curr
  106. BL osRtxThreadStackCheck ; Check if thread stack is overrun
  107. POP {R1,R2} ; Restore osRtxInfo.thread.run: curr & next
  108. CMP R0,#0
  109. BNE SVC_ContextSaveRegs ; Branch when stack check is ok
  110. MOVS R0,#osRtxErrorStackOverflow ; Parameter: r0=code, r1=object_id
  111. BL osRtxKernelErrorNotify ; Call osRtxKernelErrorNotify
  112. LDR R3,=osRtxInfo+I_T_RUN_OFS ; Load address of osRtxInfo.thread.run
  113. LDR R2,[R3,#4] ; Load osRtxInfo.thread.run: next
  114. STR R2,[R3] ; osRtxInfo.thread.run: curr = next
  115. MOVS R1,#0 ; Simulate deleted running thread
  116. B SVC_ContextRestore ; Branch to context restore handling
  117. SVC_ContextSaveRegs
  118. LDR R0,[R1,#TCB_SP_OFS] ; Load SP
  119. #endif
  120. STMIA R0!,{R4-R7} ; Save R4..R7
  121. MOV R4,R8
  122. MOV R5,R9
  123. MOV R6,R10
  124. MOV R7,R11
  125. STMIA R0!,{R4-R7} ; Save R8..R11
  126. SVC_ContextRestore
  127. MOVS R4,R2 ; Assign osRtxInfo.thread.run.next to R4
  128. #ifdef RTX_EXECUTION_ZONE
  129. MOVS R3,#TCB_ZONE_OFS ; Get TCB.zone offset
  130. LDRB R0,[R2,R3] ; Load osRtxInfo.thread.run.next: zone
  131. CMP R1,#0
  132. BEQ SVC_ZoneSetup ; Branch if running thread is deleted
  133. LDRB R1,[R1,R3] ; Load osRtxInfo.thread.run.curr: zone
  134. CMP R0,R1 ; Check if next:zone == curr:zone
  135. BEQ SVC_ContextRestore_N ; Branch if zone has not changed
  136. SVC_ZoneSetup
  137. BL osZoneSetup_Callback ; Setup zone for next thread
  138. #endif
  139. SVC_ContextRestore_N
  140. LDR R0,[R4,#TCB_SP_OFS] ; Load SP
  141. ADDS R0,R0,#16 ; Adjust address
  142. LDMIA R0!,{R4-R7} ; Restore R8..R11
  143. MOV R8,R4
  144. MOV R9,R5
  145. MOV R10,R6
  146. MOV R11,R7
  147. MSR PSP,R0 ; Set PSP
  148. SUBS R0,R0,#32 ; Adjust address
  149. LDMIA R0!,{R4-R7} ; Restore R4..R7
  150. MOVS R0,#2 ; Binary complement of 0xFFFFFFFD
  151. MVNS R0,R0 ; Set EXC_RETURN value
  152. BX R0 ; Exit from handler
  153. SVC_MSP
  154. MRS R0,MSP ; Get MSP
  155. B SVC_Number
  156. SVC_Exit
  157. BX LR ; Exit from handler
  158. SVC_User
  159. LDR R2,=osRtxUserSVC ; Load address of SVC table
  160. LDR R3,[R2] ; Load SVC maximum number
  161. CMP R1,R3 ; Check SVC number range
  162. BHI SVC_Exit ; Branch if out of range
  163. PUSH {R0,LR} ; Save SP and EXC_RETURN
  164. LSLS R1,R1,#2
  165. LDR R3,[R2,R1] ; Load address of SVC function
  166. MOV R12,R3
  167. LDMIA R0,{R0-R3} ; Load function parameters from stack
  168. BLX R12 ; Call service function
  169. POP {R2,R3} ; Restore SP and EXC_RETURN
  170. STR R0,[R2] ; Store function return value
  171. BX R3 ; Return from handler
  172. PendSV_Handler
  173. EXPORT PendSV_Handler
  174. IMPORT osRtxPendSV_Handler
  175. PUSH {R0,LR} ; Save EXC_RETURN
  176. BL osRtxPendSV_Handler ; Call osRtxPendSV_Handler
  177. POP {R0,R1} ; Restore EXC_RETURN
  178. MOV LR,R1 ; Set EXC_RETURN
  179. B SVC_Context ; Branch to context handling
  180. SysTick_Handler
  181. EXPORT SysTick_Handler
  182. IMPORT osRtxTick_Handler
  183. PUSH {R0,LR} ; Save EXC_RETURN
  184. BL osRtxTick_Handler ; Call osRtxTick_Handler
  185. POP {R0,R1} ; Restore EXC_RETURN
  186. MOV LR,R1 ; Set EXC_RETURN
  187. B SVC_Context ; Branch to context handling
  188. #ifdef RTX_SAFETY_FEATURES
  189. osFaultResume PROC
  190. EXPORT osFaultResume
  191. B SVC_Context ; Branch to context handling
  192. ALIGN
  193. ENDP
  194. #endif
  195. END