trap.S 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 %rbx
  47. pushq %rbp
  48. pushq %rsi
  49. pushq %rdi
  50. pushq %r8
  51. pushq %r9
  52. pushq %r10
  53. pushq %r11
  54. pushq %r12
  55. pushq %r13
  56. pushq %r14
  57. pushq %r15
  58. /* segments also */
  59. movq %ds, %rax
  60. pushq %rax
  61. movq %es, %rax
  62. pushq %rax
  63. movq %fs, %rax
  64. pushq %rax
  65. movq %gs, %rax
  66. pushq %rax
  67. /* KERNEL_DS */
  68. movq $0x10, %rax
  69. movq %rax, %ds
  70. movq %rax, %es
  71. movq %rax, %fs
  72. movq %rax, %gs
  73. /* call that interrupt handler with two arguments */
  74. movq %rsp, %rdi
  75. movq ERRCODE(%rsp), %rsi
  76. movq HANDLER(%rsp), %rax
  77. callq *%rax
  78. restore_all:
  79. /* now we restore and return */
  80. popq %rax
  81. movq %gs, %rax
  82. popq %rax
  83. movq %fs, %rax
  84. popq %rax
  85. movq %es, %rax
  86. popq %rax
  87. movq %ds, %rax
  88. popq %r15
  89. popq %r14
  90. popq %r13
  91. popq %r12
  92. popq %r11
  93. popq %r10
  94. popq %r9
  95. popq %r8
  96. popq %rdi
  97. popq %rsi
  98. popq %rbp
  99. popq %rbx
  100. popq %rdx
  101. popq %rcx
  102. popq %rax
  103. /* ignore that error-code and handler */
  104. addq $16, %rsp
  105. sti
  106. /* then say goodbye to this interrupt */
  107. iretq
  108. .globl debug
  109. debug:
  110. pushq $0
  111. pushq %rax
  112. leaq do_debug(%rip), %rax
  113. xorq %rax, (%rsp)
  114. jmp error_code
  115. .globl nmi
  116. nmi:
  117. pushq $0
  118. pushq %rax
  119. leaq do_nmi(%rip), %rax
  120. xorq %rax, (%rsp)
  121. jmp error_code
  122. .globl int3
  123. int3:
  124. pushq $0
  125. pushq %rax
  126. leaq do_int3(%rip), %rax
  127. xorq %rax, (%rsp)
  128. jmp error_code
  129. .globl overflow
  130. overflow:
  131. pushq $0
  132. pushq %rax
  133. leaq do_overflow(%rip), %rax
  134. xorq %rax, (%rsp)
  135. jmp error_code
  136. .globl bound
  137. bound:
  138. pushq $0
  139. pushq %rax
  140. leaq do_bound(%rip), %rax
  141. xorq %rax, (%rsp)
  142. jmp error_code
  143. .globl invalid_code
  144. invalid_code:
  145. pushq $0
  146. pushq %rax
  147. leaq do_ud(%rip), %rax
  148. xorq %rax, (%rsp)
  149. jmp error_code
  150. .globl device_not_avaliable
  151. device_not_avaliable:
  152. pushq $0
  153. pushq %rax
  154. leaq do_no_fpu(%rip), %rax
  155. xorq %rax, (%rsp)
  156. jmp error_code
  157. .globl double_fault
  158. double_fault:
  159. /* we shoudn't push $0 because error-code is already in stack */
  160. pushq %rax
  161. leaq do_double_fault(%rip), %rax
  162. xorq %rax, (%rsp)
  163. .globl fpu_segment_overrun
  164. fpu_segment_overrun:
  165. pushq %rax
  166. leaq do_fpu_overrun(%rip), %rax
  167. xorq %rax, (%rsp)
  168. jmp error_code
  169. .globl invalid_tss
  170. invalid_tss:
  171. pushq %rax
  172. leaq do_invalid_tss(%rip), %rax
  173. xorq %rax, (%rsp)
  174. jmp error_code
  175. .globl segment_not_present
  176. segment_not_present:
  177. pushq %rax
  178. leaq do_no_segment(%rip), %rax
  179. xorq %rax, (%rsp)
  180. jmp error_code
  181. .globl stack_error
  182. stack_error:
  183. pushq %rax
  184. leaq do_stack(%rip), %rax
  185. xorq %rax, (%rsp)
  186. jmp error_code
  187. .globl general_protection
  188. general_protection:
  189. pushq %rax
  190. leaq do_gp(%rip), %rax
  191. xorq %rax, (%rsp)
  192. jmp error_code
  193. .globl page_fault
  194. page_fault:
  195. pushq %rax
  196. leaq do_page_fault(%rip), %rax
  197. xorq %rax, (%rsp)
  198. jmp error_code
  199. .globl reserved
  200. reserved:
  201. pushq $0
  202. pushq %rax
  203. leaq do_reserved(%rip), %rax
  204. xorq %rax, (%rsp)
  205. jmp error_code
  206. .globl fpu_error
  207. fpu_error:
  208. pushq $0
  209. pushq %rax
  210. leaq do_fpu_error(%rip), %rax
  211. xorq %rax, (%rsp)
  212. jmp error_code
  213. .globl alignment
  214. alignment:
  215. pushq %rax
  216. leaq do_alignment(%rip), %rax
  217. xorq %rax, (%rsp)
  218. jmp error_code
  219. .globl machine
  220. machine:
  221. pushq $0
  222. pushq %rax
  223. leaq do_machine(%rip), %rax
  224. xorq %rax, (%rsp)
  225. jmp error_code
  226. .globl simd
  227. simd:
  228. pushq $0
  229. pushq %rax
  230. leaq do_simd(%rip), %rax
  231. xorq %rax, (%rsp)
  232. jmp error_code
  233. .globl virtualization
  234. virtualization:
  235. pushq $0
  236. pushq %rax
  237. leaq do_virtualization(%rip), %rax
  238. xorq %rax, (%rsp)
  239. jmp error_code
  240. .globl ret_intr
  241. ret_intr:
  242. /* this is different with restore_all, but they do same things. */
  243. /* but interrupts haven't got an error-code */
  244. popq %rax
  245. movq %rax, %gs
  246. popq %rax
  247. movq %rax, %fs
  248. popq %rax
  249. movq %rax, %es
  250. popq %rax
  251. movq %rax, %ds
  252. popq %r15
  253. popq %r14
  254. popq %r13
  255. popq %r12
  256. popq %r11
  257. popq %r10
  258. popq %r9
  259. popq %r8
  260. popq %rdi
  261. popq %rsi
  262. popq %rbp
  263. popq %rdx
  264. popq %rbx
  265. popq %rcx
  266. popq %rax
  267. sti
  268. iretq
  269. .globl syscall
  270. syscall:
  271. /* we can't use error_code here because error_code will change %rax */
  272. pushq %rax
  273. pushq %rcx
  274. pushq %rbx
  275. pushq %rdx
  276. pushq %rbp
  277. pushq %rsi
  278. pushq %rdi
  279. pushq %r8
  280. pushq %r9
  281. pushq %r10
  282. pushq %r11
  283. pushq %r12
  284. pushq %r13
  285. pushq %r14
  286. pushq %r15
  287. movq %rbx, %rdi
  288. movq %rcx, %rsi
  289. movq %ds, %rcx
  290. pushq %rcx
  291. movq %es, %rcx
  292. pushq %rcx
  293. movq %fs, %rcx
  294. pushq %rcx
  295. movq %gs, %rcx
  296. pushq %rcx
  297. callq syscall_table(, %rax, 8)
  298. jmp ret_intr
  299. .section .rodata
  300. schkfail_msg:
  301. .asciz "kernel panic: stack check fail. \n"