assembly.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # main has to be in assembly because there's not telling
  2. # what stack firmware has set up. It calls the
  3. # bsp function with stack in a0 and configstring
  4. # pointer in a1.
  5. .text
  6. .globl bsp
  7. .globl m0
  8. .globl m0stack
  9. .globl main
  10. main:
  11. csrw sscratch, x0
  12. mv a3, a0
  13. lui a5,%hi(bsp)
  14. add a5,a5,%lo(bsp)
  15. mv a0,a5
  16. //li a1, $m0
  17. lui a5,%hi(m0)
  18. add a5,a5,%lo(m0)
  19. mv a1,a5
  20. //li a2, $m0stack
  21. lui a5,%hi(m0stack)
  22. add a5,a5,%lo(m0stack)
  23. li a6, 32768
  24. add a5, a5, a6
  25. mv a2,a5
  26. mv tp, a1
  27. mv ra,a0
  28. mv sp, a2
  29. mv a0, a2
  30. mv a1, a3
  31. ret
  32. # startmach starts a function after setting up the mach
  33. # pointer. The function is in a0, the mach pointer in a1.
  34. .globl startmach
  35. startmach:
  36. csrw sscratch, x0
  37. mv tp, a1
  38. mv ra,a0
  39. mv sp, a2
  40. mv a0, a2
  41. ret
  42. .globl machp
  43. machp:
  44. mv a0,tp
  45. ret
  46. /*
  47. * Label consists of a stack pointer and a programme counter
  48. * 0(%rdi) is the SP, 8(%rdi) is the PC
  49. */
  50. .global gotolabel
  51. gotolabel:
  52. LD sp,0(a0) // X14
  53. LD s0,16(a0) // X2
  54. // X0 is zero
  55. // X1 and X2 are done.
  56. LD x3, 48(a0)
  57. // Don't restore X4, that's reserved for Mach *
  58. //LD x4, 56(a0)
  59. LD x5, 64(a0)
  60. LD x6, 72(a0)
  61. LD x7, 80(a0)
  62. LD x8, 88(a0)
  63. LD x9, 96(a0)
  64. //LD x10, 104(a0) this is a0
  65. LD x11, 112(a0)
  66. LD x12, 120(a0)
  67. LD x13, 128(a0)
  68. // X14 done already
  69. LD x15, 144(a0)
  70. LD x16, 152(a0)
  71. LD x17, 160(a0)
  72. LD x18, 168(a0)
  73. LD x19, 176(a0)
  74. LD x20, 184(a0)
  75. LD x21, 192(a0)
  76. LD x22, 200(a0)
  77. LD x23, 208(a0)
  78. LD x24, 216(a0)
  79. LD x25, 224(a0)
  80. LD x26, 232(a0)
  81. LD x27, 240(a0)
  82. LD x28, 248(a0)
  83. LD x29, 256(a0)
  84. LD x30, 264(a0)
  85. LD x30, 272(a0)
  86. LD a0,8(a0)
  87. MV ra,a0 // X1 (non zero by definition I hope.
  88. RET
  89. /* save all registers on this stack, the save stack
  90. * in the label struct.
  91. */
  92. .global slim_setlabel
  93. slim_setlabel:
  94. // don't save X0 of course. Or x31
  95. /* not so slim! */
  96. SD sp,0(a0) // X14
  97. SD ra,8(a0) // X1
  98. SD s0,16(a0) // X2
  99. // X0 is zero
  100. // X1 and X2 are done.
  101. sd x3, 48(a0)
  102. sd x4, 56(a0)
  103. sd x5, 64(a0)
  104. sd x6, 72(a0)
  105. sd x7, 80(a0)
  106. sd x8, 88(a0)
  107. sd x9, 96(a0)
  108. sd x10, 104(a0)
  109. sd x11, 112(a0)
  110. sd x12, 120(a0)
  111. sd x13, 128(a0)
  112. // X14 done already
  113. sd x15, 144(a0)
  114. sd x16, 152(a0)
  115. sd x17, 160(a0)
  116. sd x18, 168(a0)
  117. sd x19, 176(a0)
  118. sd x20, 184(a0)
  119. sd x21, 192(a0)
  120. sd x22, 200(a0)
  121. sd x23, 208(a0)
  122. sd x24, 216(a0)
  123. sd x25, 224(a0)
  124. sd x26, 232(a0)
  125. sd x27, 240(a0)
  126. sd x28, 248(a0)
  127. sd x29, 256(a0)
  128. sd x30, 264(a0)
  129. sd x31, 272(a0)
  130. mv a0,zero
  131. RET
  132. .globl stack
  133. stack:
  134. mv a0, sp
  135. ret
  136. #if 0
  137. .globl rdtsc
  138. rdtsc:
  139. RDCYCLE a0
  140. RET
  141. #endif
  142. /* set up uregs, then jump to a0 from uregs */
  143. .globl touser
  144. touser:
  145. csrw sscratch, tp
  146. LD sp,8(a0) // X14
  147. LD s0,16(a0) // X2
  148. // X0 is zero
  149. // X1 and X2 are done.
  150. LD x3, 48(a0)
  151. LD x4, 56(a0)
  152. LD x5, 64(a0)
  153. LD x6, 72(a0)
  154. LD x7, 80(a0)
  155. LD x8, 88(a0)
  156. LD x9, 96(a0)
  157. // a0 and a1 are restored below.
  158. LD x12, 120(a0)
  159. LD x13, 128(a0)
  160. // X14 done already
  161. LD x15, 144(a0)
  162. LD x16, 152(a0)
  163. LD x17, 160(a0)
  164. LD x18, 168(a0)
  165. LD x19, 176(a0)
  166. LD x20, 184(a0)
  167. LD x21, 192(a0)
  168. LD x22, 200(a0)
  169. LD x23, 208(a0)
  170. LD x24, 216(a0)
  171. LD x25, 224(a0)
  172. LD x26, 232(a0)
  173. LD x27, 240(a0)
  174. LD x28, 248(a0)
  175. LD x29, 256(a0)
  176. LD x30, 264(a0)
  177. LD x30, 272(a0)
  178. LD a1,0(a0)
  179. /* Jump to a0 in S-mode */
  180. //csrr t0, mstatus
  181. //li t1, ~(1<<8) // clear SPP
  182. //and t0, t0, t1
  183. //csrw mstatus, t0
  184. csrw sepc, a1
  185. LD a1, 112(a0)
  186. LD a0, 104(a0)
  187. .long 0x10200073
  188. sret