crt0.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file crt0.S
  15. * @brief Generic ARM startup file.
  16. *
  17. * @addtogroup ARMCAx_GCC_STARTUP
  18. * @{
  19. */
  20. /**
  21. * @brief Constructors invocation switch.
  22. */
  23. #if !defined(CRT0_CALL_CONSTRUCTORS) || defined(__DOXYGEN__)
  24. #define CRT0_CALL_CONSTRUCTORS TRUE
  25. #endif
  26. /**
  27. * @brief Destructors invocation switch.
  28. */
  29. #if !defined(CRT0_CALL_DESTRUCTORS) || defined(__DOXYGEN__)
  30. #define CRT0_CALL_DESTRUCTORS TRUE
  31. #endif
  32. #if !defined(__DOXYGEN__)
  33. .set MODE_USR, 0x10
  34. .set MODE_FIQ, 0x11
  35. .set MODE_IRQ, 0x12
  36. .set MODE_SVC, 0x13
  37. .set MODE_MON, 0x16
  38. .set MODE_ABT, 0x17
  39. .set MODE_UND, 0x1B
  40. .set MODE_SYS, 0x1F
  41. .set I_BIT, 0x80
  42. .set F_BIT, 0x40
  43. .text
  44. .code 32
  45. .balign 4
  46. /*
  47. * Reset handler.
  48. */
  49. .global Reset_Handler
  50. Reset_Handler:
  51. /*
  52. * Stack pointers initialization.
  53. */
  54. ldr r0, =__stacks_end__
  55. /* Undefined */
  56. msr CPSR_c, #MODE_UND | I_BIT | F_BIT
  57. mov sp, r0
  58. ldr r1, =__und_stack_size__
  59. sub r0, r0, r1
  60. /* Abort */
  61. msr CPSR_c, #MODE_ABT | I_BIT | F_BIT
  62. mov sp, r0
  63. ldr r1, =__abt_stack_size__
  64. sub r0, r0, r1
  65. /* FIQ */
  66. msr CPSR_c, #MODE_FIQ | I_BIT | F_BIT
  67. mov sp, r0
  68. ldr r1, =__fiq_stack_size__
  69. sub r0, r0, r1
  70. /* IRQ */
  71. msr CPSR_c, #MODE_IRQ | I_BIT | F_BIT
  72. mov sp, r0
  73. ldr r1, =__irq_stack_size__
  74. sub r0, r0, r1
  75. /* Supervisor */
  76. msr CPSR_c, #MODE_SVC | I_BIT | F_BIT
  77. mov sp, r0
  78. ldr r1, =__svc_stack_size__
  79. sub r0, r0, r1
  80. /* Monitor */
  81. msr CPSR_c, #MODE_MON | I_BIT | F_BIT
  82. mov sp, r0
  83. ldr r1, =__mon_stack_size__
  84. sub r0, r0, r1
  85. /* System */
  86. msr CPSR_c, #MODE_SYS | I_BIT | F_BIT
  87. mov sp, r0
  88. // ldr r1, =__sys_stack_size__
  89. // sub r0, r0, r1
  90. /*
  91. * Early initialization.
  92. */
  93. bl __early_init
  94. /*
  95. * Data initialization.
  96. * NOTE: It assumes that the DATA size is a multiple of 4.
  97. */
  98. ldr r1, =_textdata
  99. ldr r2, =_data
  100. ldr r3, =_edata
  101. dataloop:
  102. cmp r2, r3
  103. ldrlo r0, [r1], #4
  104. strlo r0, [r2], #4
  105. blo dataloop
  106. /*
  107. * BSS initialization.
  108. * NOTE: It assumes that the BSS size is a multiple of 4.
  109. */
  110. mov r0, #0
  111. ldr r1, =_bss_start
  112. ldr r2, =_bss_end
  113. bssloop:
  114. cmp r1, r2
  115. strlo r0, [r1], #4
  116. blo bssloop
  117. /*
  118. * Late initialization.
  119. */
  120. bl __core_init
  121. bl __late_init
  122. #if 0 /* Constructors initialized after halInit() */
  123. #if CRT0_CALL_CONSTRUCTORS == TRUE
  124. /* Constructors invocation.*/
  125. ldr r4, =__init_array_start
  126. ldr r5, =__init_array_end
  127. initloop:
  128. cmp r4, r5
  129. bge endinitloop
  130. ldr r1, [r4], #4
  131. blx r1
  132. b initloop
  133. endinitloop:
  134. #endif /* CRT0_CALL_CONSTRUCTORS */
  135. #endif /* if 0 */
  136. /*
  137. * Main program invocation.
  138. */
  139. bl main
  140. #if CRT0_CALL_DESTRUCTORS == TRUE
  141. /* Destructors invocation.*/
  142. ldr r4, =__fini_array_start
  143. ldr r5, =__fini_array_end
  144. finiloop:
  145. cmp r4, r5
  146. bge endfiniloop
  147. ldr r1, [r4], #4
  148. blx r1
  149. b finiloop
  150. endfiniloop:
  151. #endif
  152. b __default_exit
  153. #endif /* !defined(__DOXYGEN__) */
  154. /** @} */