start.S 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // save arguments in registers (we will need them later for the new kernel)
  29. // I choosed x10-x13 because instructions generated from C by gcc does not
  30. // touch them. You can check that with "aarch64-elf-objdump -d kernel8.elf"
  31. mov x10, x0
  32. mov x11, x1
  33. mov x12, x2
  34. mov x13, x3
  35. // IMPORTANT: because of a change in firmware, this code will only run on the
  36. // BSP core, therefore there's no need to check the cpu's id. A spin-loop on a
  37. // non-relocated address would be otherwise bad, thanks for the heads up @mrvn
  38. // See https://github.com/raspberrypi/tools/blob/master/armstubs/armstub8.S
  39. // relocate our code from load address to link address
  40. ldr x1, =0x80000
  41. ldr x2, =_start
  42. ldr w3, =__loader_size
  43. 1: ldr x4, [x1], #8
  44. str x4, [x2], #8
  45. sub w3, w3, #1
  46. cbnz w3, 1b
  47. // set top of stack just before our code (stack grows to a lower address per AAPCS64)
  48. ldr x1, =_start
  49. mov sp, x1
  50. // clear bss
  51. ldr x1, =__bss_start
  52. ldr w2, =__bss_size
  53. 3: cbz w2, 4f
  54. str xzr, [x1], #8
  55. sub w2, w2, #1
  56. cbnz w2, 3b
  57. // jump to relocated C code, should not return
  58. 4: bl main-1024
  59. // for failsafe, halt this core too
  60. b 1b