asm.S 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Entry point of all programs (_start).
  3. It initializes DWARF call frame information, the stack pointer, the
  4. frame pointer (needed for closures to work in start_rust) and the global
  5. pointer. Then it calls _start_rust.
  6. */
  7. .section .init, "ax"
  8. .global _start
  9. _start:
  10. .cfi_startproc
  11. .cfi_undefined ra
  12. csrw mideleg, 0
  13. csrw medeleg, 0
  14. csrw mie, 0
  15. csrw mip, 0
  16. li x1, 0
  17. li x2, 0
  18. li x3, 0
  19. li x4, 0
  20. li x5, 0
  21. li x6, 0
  22. li x7, 0
  23. li x8, 0
  24. li x9, 0
  25. li x10,0
  26. li x11,0
  27. li x12,0
  28. li x13,0
  29. li x14,0
  30. li x15,0
  31. li x16,0
  32. li x17,0
  33. li x18,0
  34. li x19,0
  35. li x20,0
  36. li x21,0
  37. li x22,0
  38. li x23,0
  39. li x24,0
  40. li x25,0
  41. li x26,0
  42. li x27,0
  43. li x28,0
  44. li x29,0
  45. li x30,0
  46. li x31,0
  47. .option push
  48. .option norelax
  49. la gp, __global_pointer$
  50. .option pop
  51. // Check hart id
  52. csrr a2, mhartid
  53. lui t0, %hi(_max_hart_id)
  54. add t0, t0, %lo(_max_hart_id)
  55. bgtu a2, t0, abort
  56. // Allocate stacks
  57. la sp, _stack_start
  58. lui t0, %hi(_hart_stack_size)
  59. add t0, t0, %lo(_hart_stack_size)
  60. mul t0, a2, t0
  61. sub sp, sp, t0
  62. // Set frame pointer
  63. add s0, sp, zero
  64. // Set trap handler
  65. la t0, _start_trap
  66. csrw mtvec, t0
  67. // Park non-zero harts
  68. bnez a2, abort
  69. jal zero, _start_rust
  70. .cfi_endproc
  71. /*
  72. Trap entry point (_start_trap)
  73. Saves caller saved registers ra, t0..6, a0..7, calls _start_trap_rust,
  74. restores caller saved registers and then returns.
  75. */
  76. .section .trap, "ax"
  77. .global _start_trap
  78. _start_trap:
  79. addi sp, sp, -16*4
  80. sw ra, 0*4(sp)
  81. sw t0, 1*4(sp)
  82. sw t1, 2*4(sp)
  83. sw t2, 3*4(sp)
  84. sw t3, 4*4(sp)
  85. sw t4, 5*4(sp)
  86. sw t5, 6*4(sp)
  87. sw t6, 7*4(sp)
  88. sw a0, 8*4(sp)
  89. sw a1, 9*4(sp)
  90. sw a2, 10*4(sp)
  91. sw a3, 11*4(sp)
  92. sw a4, 12*4(sp)
  93. sw a5, 13*4(sp)
  94. sw a6, 14*4(sp)
  95. sw a7, 15*4(sp)
  96. jal ra, _start_trap_rust
  97. lw ra, 0*4(sp)
  98. lw t0, 1*4(sp)
  99. lw t1, 2*4(sp)
  100. lw t2, 3*4(sp)
  101. lw t3, 4*4(sp)
  102. lw t4, 5*4(sp)
  103. lw t5, 6*4(sp)
  104. lw t6, 7*4(sp)
  105. lw a0, 8*4(sp)
  106. lw a1, 9*4(sp)
  107. lw a2, 10*4(sp)
  108. lw a3, 11*4(sp)
  109. lw a4, 12*4(sp)
  110. lw a5, 13*4(sp)
  111. lw a6, 14*4(sp)
  112. lw a7, 15*4(sp)
  113. addi sp, sp, 16*4
  114. mret
  115. /* Make sure there is an abort when linking */
  116. .section .init
  117. .globl abort
  118. abort:
  119. j abort