trap.S 4.9 KB

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