trap.S 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 SAVE_REGS() \
  20. pushq %rax; \
  21. pushq %rcx; \
  22. pushq %rdx; \
  23. pushq %rbx; \
  24. /* we shouldn't push it because it's really in stack */ \
  25. pushq %rsp; \
  26. pushq %rbp; \
  27. pushq %rsi; \
  28. pushq %rdi; \
  29. pushq %r8; \
  30. pushq %r9; \
  31. pushq %r10; \
  32. pushq %r11; \
  33. pushq %r12; \
  34. pushq %r13; \
  35. pushq %r14; \
  36. pushq %r15; \
  37. /* and, segment registers */ \
  38. movq %ds, %rax; \
  39. pushq %rax; \
  40. movq %es, %rax; \
  41. pushq %rax; \
  42. movq %fs, %rax; \
  43. pushq %rax; \
  44. movq %gs, %rax; \
  45. pushq %rax; \
  46. movq %ss, %rax; \
  47. pushq %rax
  48. restore_all:
  49. popq %rax
  50. movq %rax, %ss
  51. popq %rax
  52. movq %rax, %gs
  53. popq %rax
  54. movq %rax, %fs
  55. popq %rax
  56. movq %rax, %es
  57. popq %rax
  58. movq %rax, %ds
  59. popq %r15
  60. popq %r14
  61. popq %r13
  62. popq %r12
  63. popq %r11
  64. popq %r10
  65. popq %r9
  66. popq %r8
  67. popq %rdi
  68. popq %rsi
  69. popq %rbp
  70. popq %rsp
  71. popq %rbx
  72. popq %rdx
  73. popq %rcx
  74. popq %rax
  75. /* add it because we don't want errcode or func */
  76. addq $0x10, %rsp
  77. /* iretq will pop ss if it need */
  78. iretq
  79. .globl divide_error
  80. divide_error:
  81. /* error code: 0 */
  82. pushq $0
  83. pushq %rax
  84. SAVE_REGS();
  85. /* first argument for do_divide_error */
  86. movq %rsp, %rdi
  87. leaq do_divide_error(%rip), %rcx
  88. callq *%rcx
  89. /* return */
  90. die: hlt
  91. jmp die
  92. jmp restore_all