start.S 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // change execution level to EL1
  67. mov x2, #0x3c4
  68. msr spsr_el2, x2
  69. adr x2, 5f
  70. msr elr_el2, x2
  71. eret
  72. 5: mov sp, x1
  73. // clear bss
  74. ldr x1, =__bss_start
  75. ldr w2, =__bss_size
  76. 3: cbz w2, 4f
  77. str xzr, [x1], #8
  78. sub w2, w2, #1
  79. cbnz w2, 3b
  80. // jump to C code, should not return
  81. 4: bl main
  82. // for failsafe, halt this core too
  83. b 1b