trap.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #define __ASM__
  20. #include <kernel/int.h>
  21. .globl __stack_chk_fail
  22. __stack_chk_fail:
  23. /* panic and do stack trace */
  24. movq schkfail_msg(%rip), %rdi
  25. call panic
  26. scf_die:
  27. hlt
  28. jmp scf_die
  29. .globl divide_error
  30. divide_error:
  31. /* that fake error-code */
  32. pushq $0
  33. /* we are going to change %rax */
  34. pushq %rax
  35. /* function handler */
  36. leaq do_divide_error(%rip), %rax
  37. /* restore %rax, and save handler to stack */
  38. /* but now %rax is not in stack, push it later in error_code */
  39. xorq %rax, (%rsp)
  40. error_code:
  41. cli
  42. /* and save other registers seriously now */
  43. pushq %rax
  44. pushq %rcx
  45. pushq %rdx
  46. pushq %rbp
  47. pushq %rsi
  48. pushq %rdi
  49. pushq %r8
  50. pushq %r9
  51. pushq %r10
  52. pushq %r11
  53. pushq %r12
  54. pushq %r13
  55. pushq %r14
  56. pushq %r15
  57. /* segments also */
  58. movq %ds, %rax
  59. pushq %rax
  60. movq %es, %rax
  61. pushq %rax
  62. movq %fs, %rax
  63. pushq %rax
  64. movq %gs, %rax
  65. pushq %rax
  66. /* KERNEL_DS */
  67. movq $0x10, %rax
  68. movq %rax, %ds
  69. movq %rax, %es
  70. movq %rax, %fs
  71. movq %rax, %gs
  72. /* call that interrupt handler with two arguments */
  73. movq %rsp, %rdi
  74. movq ERRCODE(%rsp), %rsi
  75. movq HANDLER(%rsp), %rax
  76. callq *%rax
  77. restore_all:
  78. /* now we restore and return */
  79. popq %rax
  80. movq %gs, %rax
  81. popq %rax
  82. movq %fs, %rax
  83. popq %rax
  84. movq %es, %rax
  85. popq %rax
  86. movq %ds, %rax
  87. popq %r15
  88. popq %r14
  89. popq %r13
  90. popq %r12
  91. popq %r11
  92. popq %r10
  93. popq %r9
  94. popq %r8
  95. popq %rdi
  96. popq %rsi
  97. popq %rbp
  98. popq %rdx
  99. popq %rcx
  100. popq %rax
  101. /* ignore that error-code and handler */
  102. addq $16, %rsp
  103. sti
  104. /* then say goodbye to this interrupt */
  105. iretq
  106. .globl debug
  107. debug:
  108. pushq $0
  109. pushq %rax
  110. leaq do_debug(%rip), %rax
  111. xorq %rax, (%rsp)
  112. jmp error_code
  113. .globl nmi
  114. nmi:
  115. pushq $0
  116. pushq %rax
  117. leaq do_nmi(%rip), %rax
  118. xorq %rax, (%rsp)
  119. jmp error_code
  120. .globl int3
  121. int3:
  122. pushq $0
  123. pushq %rax
  124. leaq do_int3(%rip), %rax
  125. xorq %rax, (%rsp)
  126. jmp error_code
  127. .globl overflow
  128. overflow:
  129. pushq $0
  130. pushq %rax
  131. leaq do_overflow(%rip), %rax
  132. xorq %rax, (%rsp)
  133. jmp error_code
  134. .globl bound
  135. bound:
  136. pushq $0
  137. pushq %rax
  138. leaq do_bound(%rip), %rax
  139. xorq %rax, (%rsp)
  140. jmp error_code
  141. .globl invalid_code
  142. invalid_code:
  143. pushq $0
  144. pushq %rax
  145. leaq do_ud(%rip), %rax
  146. xorq %rax, (%rsp)
  147. jmp error_code
  148. .globl device_not_avaliable
  149. device_not_avaliable:
  150. pushq $0
  151. pushq %rax
  152. leaq do_no_fpu(%rip), %rax
  153. xorq %rax, (%rsp)
  154. jmp error_code
  155. .globl double_fault
  156. double_fault:
  157. /* we shoudn't push $0 because error-code is already in stack */
  158. pushq %rax
  159. leaq do_double_fault(%rip), %rax
  160. xorq %rax, (%rsp)
  161. .globl fpu_segment_overrun
  162. fpu_segment_overrun:
  163. pushq %rax
  164. leaq do_fpu_overrun(%rip), %rax
  165. xorq %rax, (%rsp)
  166. jmp error_code
  167. .globl invalid_tss
  168. invalid_tss:
  169. pushq %rax
  170. leaq do_invalid_tss(%rip), %rax
  171. xorq %rax, (%rsp)
  172. jmp error_code
  173. .globl segment_not_present
  174. segment_not_present:
  175. pushq %rax
  176. leaq do_no_segment(%rip), %rax
  177. xorq %rax, (%rsp)
  178. jmp error_code
  179. .globl stack_error
  180. stack_error:
  181. pushq %rax
  182. leaq do_stack(%rip), %rax
  183. xorq %rax, (%rsp)
  184. jmp error_code
  185. .globl general_protection
  186. general_protection:
  187. pushq %rax
  188. leaq do_gp(%rip), %rax
  189. xorq %rax, (%rsp)
  190. jmp error_code
  191. .globl page_fault
  192. page_fault:
  193. pushq %rax
  194. leaq do_page_fault(%rip), %rax
  195. xorq %rax, (%rsp)
  196. jmp error_code
  197. .globl reserved
  198. reserved:
  199. pushq $0
  200. pushq %rax
  201. leaq do_reserved(%rip), %rax
  202. xorq %rax, (%rsp)
  203. jmp error_code
  204. .globl fpu_error
  205. fpu_error:
  206. pushq $0
  207. pushq %rax
  208. leaq do_fpu_error(%rip), %rax
  209. xorq %rax, (%rsp)
  210. jmp error_code
  211. .globl alignment
  212. alignment:
  213. pushq %rax
  214. leaq do_alignment(%rip), %rax
  215. xorq %rax, (%rsp)
  216. jmp error_code
  217. .globl machine
  218. machine:
  219. pushq $0
  220. pushq %rax
  221. leaq do_machine(%rip), %rax
  222. xorq %rax, (%rsp)
  223. jmp error_code
  224. .globl simd
  225. simd:
  226. pushq $0
  227. pushq %rax
  228. leaq do_simd(%rip), %rax
  229. xorq %rax, (%rsp)
  230. jmp error_code
  231. .globl virtualization
  232. virtualization:
  233. pushq $0
  234. pushq %rax
  235. leaq do_virtualization(%rip), %rax
  236. xorq %rax, (%rsp)
  237. jmp error_code
  238. .globl ret_intr
  239. ret_intr:
  240. /* this is different with restore_all, but they do same things. */
  241. /* but interrupts haven't got an error-code */
  242. popq %rax
  243. movq %rax, %gs
  244. popq %rax
  245. movq %rax, %fs
  246. popq %rax
  247. movq %rax, %es
  248. popq %rax
  249. movq %rax, %ds
  250. popq %r15
  251. popq %r14
  252. popq %r13
  253. popq %r12
  254. popq %r11
  255. popq %r10
  256. popq %r9
  257. popq %r8
  258. popq %rdi
  259. popq %rsi
  260. popq %rbp
  261. popq %rdx
  262. popq %rbx
  263. popq %rcx
  264. popq %rax
  265. sti
  266. iretq
  267. .section .rodata
  268. schkfail_msg:
  269. .asciz "kernel panic: stack check fail. \n"