start.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // enable AArch64 in EL1
  62. mov x0, #(1 << 31) // AArch64
  63. orr x0, x0, #(1 << 1) // SWIO hardwired on Pi3
  64. msr hcr_el2, x0
  65. mrs x0, hcr_el2
  66. // Setup SCTLR access
  67. mov x2, #0x0800
  68. movk x2, #0x30d0, lsl #16
  69. msr sctlr_el1, x2
  70. // set up exception handlers
  71. ldr x2, =_vectors
  72. msr vbar_el1, x2
  73. // change execution level to EL1
  74. mov x2, #0x3c4
  75. msr spsr_el2, x2
  76. adr x2, 5f
  77. msr elr_el2, x2
  78. eret
  79. 5: mov sp, x1
  80. // clear bss
  81. ldr x1, =__bss_start
  82. ldr w2, =__bss_size
  83. 3: cbz w2, 4f
  84. str xzr, [x1], #8
  85. sub w2, w2, #1
  86. cbnz w2, 3b
  87. // jump to C code, should not return
  88. 4: bl main
  89. // for failsafe, halt this core too
  90. b 1b
  91. // important, code has to be properly aligned
  92. .align 11
  93. _vectors:
  94. // synchronous
  95. .align 7
  96. mov x0, #0
  97. mrs x1, esr_el1
  98. mrs x2, elr_el1
  99. mrs x3, spsr_el1
  100. mrs x4, far_el1
  101. b exc_handler
  102. // IRQ
  103. .align 7
  104. mov x0, #1
  105. mrs x1, esr_el1
  106. mrs x2, elr_el1
  107. mrs x3, spsr_el1
  108. mrs x4, far_el1
  109. b exc_handler
  110. // FIQ
  111. .align 7
  112. mov x0, #2
  113. mrs x1, esr_el1
  114. mrs x2, elr_el1
  115. mrs x3, spsr_el1
  116. mrs x4, far_el1
  117. b exc_handler
  118. // SError
  119. .align 7
  120. mov x0, #3
  121. mrs x1, esr_el1
  122. mrs x2, elr_el1
  123. mrs x3, spsr_el1
  124. mrs x4, far_el1
  125. b exc_handler