lwp_iar.S 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ;/*
  2. ; * File : lwp_iar.S
  3. ; * This file is part of RT-Thread RTOS
  4. ; * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. ; *
  6. ; * This program is free software; you can redistribute it and/or modify
  7. ; * it under the terms of the GNU General Public License as published by
  8. ; * the Free Software Foundation; either version 2 of the License, or
  9. ; * (at your option) any later version.
  10. ; *
  11. ; * This program is distributed in the hope that it will be useful,
  12. ; * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ; * GNU General Public License for more details.
  15. ; *
  16. ; * You should have received a copy of the GNU General Public License along
  17. ; * with this program; if not, write to the Free Software Foundation, Inc.,
  18. ; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. ; *
  20. ; * Change Logs:
  21. ; * Date Author Notes
  22. ; */
  23. SECTION .text:CODE(2)
  24. THUMB
  25. REQUIRE8
  26. PRESERVE8
  27. ;/*
  28. ; * void* lwp_get_sys_api(rt_uint32_t number);
  29. ; */
  30. IMPORT lwp_get_sys_api
  31. IMPORT lwp_get_kernel_sp
  32. IMPORT lwp_set_kernel_sp
  33. ;/*
  34. ; * void lwp_user_entry(u32 R0_text_addr, u32 R1_data_addr);
  35. ; */
  36. EXPORT lwp_user_entry
  37. lwp_user_entry:
  38. PUSH {R0-R1} ; push text&data addr.
  39. MOV R0, SP ; v1 = SP
  40. BL lwp_set_kernel_sp ; lwp_set_kernel_sp(v1)
  41. POP {R0-R1} ; pop app address to R1.
  42. ; set CPU to user-thread mode.
  43. MRS R2, CONTROL
  44. ORR R2, R2, #0x03 ; use PSP, user-thread mode.
  45. MSR CONTROL, R2
  46. ; set data address.
  47. MOV R9, R1
  48. ; run app, only Thumb-mode.
  49. ORR R0, R0, #0x01
  50. BX R0
  51. ;/*
  52. ; * void SVC_Handler(void);
  53. ; */
  54. EXPORT SVC_Handler
  55. SVC_Handler:
  56. PUSH {LR}
  57. ; get user SP.
  58. TST LR, #0x4
  59. ITE EQ
  60. MRSEQ R1, MSP
  61. MRSNE R1, PSP
  62. PUSH {R1} ; push app SP.
  63. MOV R2, R1
  64. #if defined ( __ARMVFP__ )
  65. TST LR, #0x10
  66. IT EQ
  67. VSTMDBEQ R2!, {D8 - D15}
  68. #endif
  69. STMFD R2!, {R4 - R11} ; push app R4-R11 to app stack , and R1 not change.
  70. #if defined ( __ARMVFP__ )
  71. MOV R4, #0x00 ; flag = 0
  72. TST LR, #0x10 ; if(!EXC_RETURN[4])
  73. IT EQ
  74. MOVEQ R4, #0x01 ; flag = 1
  75. STMFD R2!, {R4} ; push flag
  76. #endif
  77. ; get SVC number.
  78. LDR R0, [R1, #24] ; get the app LR.
  79. LDRB R0, [R0, #-2] ; get the SVC No. from instruction.
  80. ; get kernel system API
  81. BL lwp_get_sys_api
  82. ; if(api == NULL) return;
  83. CMP R0, #0
  84. ITTT EQ
  85. POPEQ {R1}
  86. POPEQ {LR}
  87. BXEQ LR
  88. ; push api
  89. PUSH {R0}
  90. ; get kernel SP to R0.
  91. BL lwp_get_kernel_sp
  92. POP {R2} ; pop api to R2.
  93. POP {R1} ; pop app SP to R1.
  94. ; copy R1(app SP) to R0(server SP).
  95. LDMFD R1, {R4 - R11} ; pop exception_stack_frame to r4 - r11 register
  96. STMFD R0!, {R4 - R11} ; push exception_stack_frame to server SP.
  97. POP {LR}
  98. PUSH {LR}
  99. ; save app SP.
  100. PUSH {R0 - R3}
  101. #if defined ( __ARMVFP__ )
  102. TST LR, #0x10
  103. ITE EQ
  104. SUBEQ R0, R1, #0x64 ; keep {R4 - R11}, {D8-D15}, FLAG
  105. SUBNE R0, R1, #0x24 ; keep {R4 - R11}, FLAG
  106. #else
  107. SUB R0, R1, #0x20 ; keep {R4 - R11}
  108. #endif
  109. BL lwp_set_kernel_sp
  110. POP {R0 - R3}
  111. ; set to thread-privilege mode.
  112. MRS R3, CONTROL
  113. BIC R3, R3, #0x01
  114. ORR R3, R3, #0x02
  115. MSR CONTROL, R3
  116. ; call api.
  117. LDR R3, =svc_exit
  118. STR R3, [R0, #20] ; update LR
  119. STR R2, [R0, #24] ; update api to PC
  120. MSR PSP, R0 ; update stack pointer
  121. POP {LR} ; 0xFFFFFFED
  122. ORR LR, LR, #0x10
  123. BX LR
  124. ;/*
  125. ; * void svc_exit(void);
  126. ; */
  127. EXPORT svc_exit
  128. svc_exit:
  129. ; get user SP.
  130. PUSH {R0} ; push result to SP.
  131. BL lwp_get_kernel_sp
  132. #if defined ( __ARMVFP__ )
  133. LDMFD R0!, {R3} ; pop flag
  134. #endif
  135. LDMFD R0!, {R4 - R11} ; pop app {R4 - R11}
  136. #if defined ( __ARMVFP__ )
  137. CMP R3, #0 ; if(flag_r3 != 0)
  138. IT NE
  139. VLDMIANE R0!, {D8 - D15} ; pop FPU register s16~s31
  140. #endif
  141. ADD R0, R0, #16 ; skip R0-R3
  142. LDMFD R0!, {R12, LR} ;
  143. LDMFD R0!, {R1} ; pop PC to R1
  144. LDMFD R0!, {R2} ; pop PSR to R2
  145. #if defined ( __ARMVFP__ )
  146. CMP R3, #0 ; if(flag_r3 != 0)
  147. ITTT NE
  148. VLDMIANE R0!, {D0 - D7} ; pop FPU register s16~s31
  149. LDMIANE R0!, {R3}
  150. VMSRNE FPSCR, R3
  151. #endif
  152. ; align to 2 words
  153. ADD R0, R0, #0x07
  154. BIC R0, R0, #0x07
  155. PUSH {R0} ; push user-SP to SP
  156. ; save server SP.
  157. ADD R0, SP, #0x08 ; [user-SP, result]
  158. PUSH {R1 - R2, LR}
  159. BL lwp_set_kernel_sp
  160. POP {R1 - R2, LR}
  161. POP {R3} ; pop user-SP to R3
  162. POP {R0} ; restore API result.
  163. MSR APSR, R2 ; restore PSR
  164. MSR PSP, R3 ; restore app stack pointer
  165. ; restore to PSP & thread-unprivilege mode.
  166. MRS R2, CONTROL
  167. ORR R2, R2, #0x03
  168. MSR CONTROL, R2
  169. ; return to lwp.
  170. ORR R1, R1, #0x01 ; only Thumb-mode.
  171. BX R1 ; return to user app.
  172. END