1
0

lwp_gcc.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * File : lwp_gcc.c
  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. .cpu cortex-m3
  24. .syntax unified
  25. .thumb
  26. .text
  27. /*
  28. * void* lwp_get_sys_api(rt_uint32_t number);
  29. */
  30. .global lwp_get_sys_api
  31. .global lwp_get_kernel_sp
  32. .global lwp_set_kernel_sp
  33. /*
  34. * void lwp_user_entry(args, text, data);
  35. */
  36. .global lwp_user_entry
  37. .type lwp_user_entry, % function
  38. lwp_user_entry:
  39. PUSH {R0-R3} @; push text&data addr.
  40. MOV R0, SP @; v1 = SP
  41. BL lwp_set_kernel_sp @; lwp_set_kernel_sp(v1)
  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. POP {R0-R3} @; pop app address to R1.
  47. @; set data address.
  48. MOV R9, R2
  49. @; run app, only Thumb-mode.
  50. ORR R1, R1, #0x01
  51. BX R1
  52. /*
  53. * void SVC_Handler(void);
  54. */
  55. .global SVC_Handler
  56. .type SVC_Handler, % function
  57. SVC_Handler:
  58. PUSH {LR}
  59. @; get user SP.
  60. TST LR, #0x4
  61. ITE EQ
  62. MRSEQ R1, MSP
  63. MRSNE R1, PSP
  64. PUSH {R1} @; push app SP.
  65. @; get SVC number.
  66. mov R0, R7
  67. @; get kernel system API
  68. BL lwp_get_sys_api
  69. PUSH {R0} @; push api
  70. @; get kernel SP to R0.
  71. BL lwp_get_kernel_sp
  72. POP {R2} @; pop api to R2.
  73. POP {R1} @; pop app SP to R1.
  74. stmfd r0!, {r1} @; save app SP to kernel SP
  75. @;push app parm5~6 to kernel SP
  76. STMFD R0!, {R4 - R5}
  77. @; copy R1(app SP) to R0(kernel SP).
  78. push {r8-r11}
  79. LDMFD R1, {R4 - R11} @; pop exception_stack_frame to r4 - r11 register
  80. STMFD R0!, {R4 - R11} @; push exception_stack_frame to server SP.
  81. pop {r8-r11}
  82. LDR R3, =svc_exit
  83. STR R3, [R0, #20] @; update LR
  84. STR R2, [R0, #24] @; update api to PC
  85. MSR PSP, R0 @; update SP, API is executed with kernel SP
  86. @; set to thread-privilege mode.
  87. MRS R3, CONTROL
  88. BIC R3, R3, #0x01
  89. ORR R3, R3, #0x02
  90. MSR CONTROL, R3
  91. POP {LR} @; 0xFFFFFFED
  92. ORR LR, LR, #0x10
  93. BX LR
  94. /*
  95. * void svc_exit(void);
  96. */
  97. .global svc_exit
  98. .type svc_exit, % function
  99. svc_exit:
  100. @; get user SP.
  101. PUSH {R0} @; push result to SP.
  102. BL lwp_get_kernel_sp
  103. ldr r3, [r0, #-4]
  104. pop {r0}
  105. ldr lr, [r3, #20]
  106. ldr r1, [r3, #24] @; load pc
  107. add r3, #32 @; exception_stack_frame size
  108. MSR PSP, R3 @; restore app stack pointer
  109. @; restore to PSP & thread-unprivilege mode.
  110. MRS R2, CONTROL
  111. ORR R2, R2, #0x03
  112. MSR CONTROL, R2
  113. @; return to lwp.
  114. ORR R1, R1, #0x01 @; only Thumb-mode.
  115. BX R1 @; return to user app.