trap_util.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Early initialization code for riscv
  3. *
  4. * Copyright 2015 Google Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; version 2 of
  9. * the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
  14. * GNU General Public License for more details.
  15. */
  16. #define LOG_REGBYTES 3
  17. #define REGBYTES (1 << LOG_REGBYTES)
  18. .macro restore_regs
  19. # restore x registers
  20. #if 0
  21. WTF
  22. LOAD x1,1*REGBYTES(a0)
  23. LOAD x2,2*REGBYTES(a0)
  24. LOAD x3,3*REGBYTES(a0)
  25. LOAD x4,4*REGBYTES(a0)
  26. LOAD x5,5*REGBYTES(a0)
  27. LOAD x6,6*REGBYTES(a0)
  28. LOAD x7,7*REGBYTES(a0)
  29. LOAD x8,8*REGBYTES(a0)
  30. LOAD x9,9*REGBYTES(a0)
  31. LOAD x11,11*REGBYTES(a0)
  32. LOAD x12,12*REGBYTES(a0)
  33. LOAD x13,13*REGBYTES(a0)
  34. LOAD x14,14*REGBYTES(a0)
  35. LOAD x15,15*REGBYTES(a0)
  36. LOAD x16,16*REGBYTES(a0)
  37. LOAD x17,17*REGBYTES(a0)
  38. LOAD x18,18*REGBYTES(a0)
  39. LOAD x19,19*REGBYTES(a0)
  40. LOAD x20,20*REGBYTES(a0)
  41. LOAD x21,21*REGBYTES(a0)
  42. LOAD x22,22*REGBYTES(a0)
  43. LOAD x23,23*REGBYTES(a0)
  44. LOAD x24,24*REGBYTES(a0)
  45. LOAD x25,25*REGBYTES(a0)
  46. LOAD x26,26*REGBYTES(a0)
  47. LOAD x27,27*REGBYTES(a0)
  48. LOAD x28,28*REGBYTES(a0)
  49. LOAD x29,29*REGBYTES(a0)
  50. LOAD x30,30*REGBYTES(a0)
  51. LOAD x31,31*REGBYTES(a0)
  52. # restore a0 last
  53. LOAD x10,10*REGBYTES(a0)
  54. #endif
  55. .endm
  56. .macro save_tf
  57. # save gprs
  58. #if 0
  59. STORE x1,1*REGBYTES(x2)
  60. STORE x3,3*REGBYTES(x2)
  61. STORE x4,4*REGBYTES(x2)
  62. STORE x5,5*REGBYTES(x2)
  63. STORE x6,6*REGBYTES(x2)
  64. STORE x7,7*REGBYTES(x2)
  65. STORE x8,8*REGBYTES(x2)
  66. STORE x9,9*REGBYTES(x2)
  67. STORE x10,10*REGBYTES(x2)
  68. STORE x11,11*REGBYTES(x2)
  69. STORE x12,12*REGBYTES(x2)
  70. STORE x13,13*REGBYTES(x2)
  71. STORE x14,14*REGBYTES(x2)
  72. STORE x15,15*REGBYTES(x2)
  73. STORE x16,16*REGBYTES(x2)
  74. STORE x17,17*REGBYTES(x2)
  75. STORE x18,18*REGBYTES(x2)
  76. STORE x19,19*REGBYTES(x2)
  77. STORE x20,20*REGBYTES(x2)
  78. STORE x21,21*REGBYTES(x2)
  79. STORE x22,22*REGBYTES(x2)
  80. STORE x23,23*REGBYTES(x2)
  81. STORE x24,24*REGBYTES(x2)
  82. STORE x25,25*REGBYTES(x2)
  83. STORE x26,26*REGBYTES(x2)
  84. STORE x27,27*REGBYTES(x2)
  85. STORE x28,28*REGBYTES(x2)
  86. STORE x29,29*REGBYTES(x2)
  87. STORE x30,30*REGBYTES(x2)
  88. STORE x31,31*REGBYTES(x2)
  89. #endif
  90. # get sr, epc, badvaddr, cause
  91. csrrw t0,sscratch,x0
  92. csrr s0,sstatus
  93. csrr t1,sepc
  94. csrr t2,sbadaddr
  95. csrr t3,scause
  96. #if 0
  97. STORE t0,2*REGBYTES(x2)
  98. STORE s0,32*REGBYTES(x2)
  99. STORE t1,33*REGBYTES(x2)
  100. STORE t2,34*REGBYTES(x2)
  101. STORE t3,35*REGBYTES(x2)
  102. #endif
  103. # get faulting insn, if it wasn't a fetch-related trap
  104. li x5,-1
  105. // STORE x5,36*REGBYTES(x2)
  106. 1:
  107. .endm
  108. .text
  109. .global supervisor_trap_entry
  110. supervisor_trap_entry:
  111. csrw mscratch, sp
  112. # load in the top of the machine stack
  113. li sp, 0x80FFF0 - 64
  114. 1:addi sp,sp,-320
  115. save_tf
  116. move a0,sp
  117. jal trap_handler
  118. .global trap_entry
  119. trap_entry:
  120. csrw mscratch, sp
  121. # SMP isn't supported yet, to avoid overwriting the same stack with different
  122. # harts that handle traps at the same time.
  123. # someday this gets fixed.
  124. //csrr sp, mhartid
  125. csrr sp, 0xf14
  126. .Lsmp_hang:
  127. bnez sp, .Lsmp_hang
  128. # TODO: Use the old stack pointer (plus an offset) for exceptions in machine
  129. # mode, to avoid overwriting stack data.
  130. li sp, 0x8000fff0
  131. save_tf
  132. move a0,sp
  133. jal trap_handler
  134. .global supervisor_call_return
  135. supervisor_call_return:
  136. csrr a0, mscratch
  137. restore_regs
  138. # go back into supervisor call
  139. .word 0x30200073 # mret