start.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2018 bzt (bztsrc@github)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. .section ".text.boot"
  26. .global _start
  27. _start:
  28. // read cpu id, stop slave cores
  29. mrs x1, mpidr_el1
  30. and x1, x1, #3
  31. cbz x1, 2f
  32. // cpu id > 0, stop
  33. 1: wfe
  34. b 1b
  35. 2: // cpu id == 0
  36. // set top of stack just before our code (stack grows to a lower address per AAPCS64)
  37. ldr x1, =_start
  38. // set up EL1
  39. mrs x0, CurrentEL
  40. and x0, x0, #12 // clear reserved bits
  41. // running at EL3?
  42. cmp x0, #12
  43. bne 5f
  44. // should never be executed, just for completeness
  45. mov x2, #0x5b1
  46. msr scr_el3, x2
  47. mov x2, #0x3c9
  48. msr spsr_el3, x2
  49. adr x2, 5f
  50. msr elr_el3, x2
  51. eret
  52. // running at EL2?
  53. 5: cmp x0, #4
  54. beq 5f
  55. msr sp_el1, x1
  56. // enable CNTP for EL1
  57. mrs x0, cnthctl_el2
  58. orr x0, x0, #3
  59. msr cnthctl_el2, x0
  60. msr cntvoff_el2, xzr
  61. // disable coprocessor traps
  62. mov x0, #0x33FF
  63. msr cptr_el2, x0
  64. msr hstr_el2, xzr
  65. mov x0, #(3 << 20)
  66. msr cpacr_el1, x0
  67. // enable AArch64 in EL1
  68. mov x0, #(1 << 31) // AArch64
  69. orr x0, x0, #(1 << 1) // SWIO hardwired on Pi3
  70. msr hcr_el2, x0
  71. mrs x0, hcr_el2
  72. // Setup SCTLR access
  73. mov x2, #0x0800
  74. movk x2, #0x30d0, lsl #16
  75. msr sctlr_el1, x2
  76. // set up exception handlers
  77. ldr x2, =_vectors
  78. msr vbar_el1, x2
  79. // change execution level to EL1
  80. mov x2, #0x3c4
  81. msr spsr_el2, x2
  82. adr x2, 5f
  83. msr elr_el2, x2
  84. // clear EL1 system registers
  85. msr elr_el1, xzr
  86. msr far_el1, xzr
  87. eret
  88. 5: mov sp, x1
  89. // clear bss
  90. ldr x1, =__bss_start
  91. ldr w2, =__bss_size
  92. 3: cbz w2, 4f
  93. str xzr, [x1], #8
  94. sub w2, w2, #1
  95. cbnz w2, 3b
  96. // jump to C code, should not return
  97. 4: bl main
  98. // for failsafe, halt this core too
  99. b 1b
  100. // save registers before we call any C code
  101. dbg_saveregs:
  102. str x0, [sp, #-16]! // push x0
  103. ldr x0, =dbg_regs+8
  104. str x1, [x0], #8 // dbg_regs[1]=x1
  105. ldr x1, [sp, #16] // pop x1
  106. str x1, [x0, #-16]! // dbg_regs[0]=x1 (x0)
  107. add x0, x0, #16
  108. str x2, [x0], #8 // dbg_regs[2]=x2
  109. str x3, [x0], #8 // ...etc.
  110. str x4, [x0], #8
  111. str x5, [x0], #8
  112. str x6, [x0], #8
  113. str x7, [x0], #8
  114. str x8, [x0], #8
  115. str x9, [x0], #8
  116. str x10, [x0], #8
  117. str x11, [x0], #8
  118. str x12, [x0], #8
  119. str x13, [x0], #8
  120. str x14, [x0], #8
  121. str x15, [x0], #8
  122. str x16, [x0], #8
  123. str x17, [x0], #8
  124. str x18, [x0], #8
  125. str x19, [x0], #8
  126. str x20, [x0], #8
  127. str x21, [x0], #8
  128. str x22, [x0], #8
  129. str x23, [x0], #8
  130. str x24, [x0], #8
  131. str x25, [x0], #8
  132. str x26, [x0], #8
  133. str x27, [x0], #8
  134. str x28, [x0], #8
  135. str x29, [x0], #8
  136. ldr x1, [sp, #16] // pop x30
  137. str x1, [x0], #8
  138. // also read and store some system registers
  139. mrs x1, elr_el1
  140. str x1, [x0], #8
  141. mrs x1, spsr_el1
  142. str x1, [x0], #8
  143. mrs x1, esr_el1
  144. str x1, [x0], #8
  145. mrs x1, far_el1
  146. str x1, [x0], #8
  147. mrs x1, sctlr_el1
  148. str x1, [x0], #8
  149. mrs x1, tcr_el1
  150. str x1, [x0], #8
  151. ret
  152. // important, code has to be properly aligned
  153. .align 11
  154. _vectors:
  155. // synchronous
  156. .align 7
  157. str x30, [sp, #-16]! // push x30
  158. bl dbg_saveregs
  159. mov x0, #0
  160. bl dbg_decodeexc
  161. bl dbg_main
  162. eret
  163. // IRQ
  164. .align 7
  165. str x30, [sp, #-16]! // push x30
  166. bl dbg_saveregs
  167. mov x0, #1
  168. bl dbg_decodeexc
  169. bl dbg_main
  170. eret
  171. // FIQ
  172. .align 7
  173. str x30, [sp, #-16]! // push x30
  174. bl dbg_saveregs
  175. mov x0, #2
  176. bl dbg_decodeexc
  177. bl dbg_main
  178. eret
  179. // SError
  180. .align 7
  181. str x30, [sp, #-16]! // push x30
  182. bl dbg_saveregs
  183. mov x0, #3
  184. bl dbg_decodeexc
  185. bl dbg_main
  186. eret