trap.S 4.9 KB

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