arch_int.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* arch/x86/kernel/arch_int.c
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #include <kernel/init.h>
  21. #include <kernel/module.h>
  22. #include <kernel/mm.h>
  23. #include <kernel/kernel.h>
  24. #include <kernel/kmlog.h>
  25. #include <arch/archinfo.h>
  26. #include <arch/mm.h>
  27. #include <arch/asm.h>
  28. #include <arch/int.h>
  29. #include <arch/string.h>
  30. #include <sys/types.h>
  31. #include <config.h>
  32. /* head$(BITS).S */
  33. /* extern struct idt_entry *idt; things interesting.
  34. * a struct idt_entry *idt, and that is a pointer. idt, is
  35. * the address.
  36. *
  37. * a struct idt_entry idt[], and that is an array. */
  38. extern struct idt_entry idt[256];
  39. extern void ret_intr(void);
  40. int do_trap(int nr, __archptr_t sp);
  41. static int set_gate(struct idt_entry *, void (*) (void), int attr);
  42. #define set_int_gate(nr, handler) set_gate(idt + nr, handler, 0x8e)
  43. #define set_trap_gate(nr, handler) set_gate(idt + nr, handler, 0x8f)
  44. #define set_sys_gate(nr, handler) set_gate(idt + nr, handler, 0xef)
  45. #if CONFIG_64BIT
  46. /* errcodes later */
  47. __asm__ (
  48. ".globl common_trap\n\t"
  49. "common_trap:\n\t"
  50. /* rax already saved */
  51. "pushq %rcx\n\t"
  52. "pushq %rbx\n\t"
  53. "pushq %rdx\n\t"
  54. "pushq %rbp\n\t"
  55. "pushq %rsi\n\t"
  56. "pushq %rdi\n\t"
  57. "pushq %r8\n\t"
  58. "pushq %r9\n\t"
  59. "pushq %r10\n\t"
  60. "pushq %r11\n\t"
  61. "pushq %r12\n\t"
  62. "pushq %r13\n\t"
  63. "pushq %r14\n\t"
  64. "pushq %r15\n\t"
  65. "movq %rax, %rdi\n\t"
  66. "movq %ds, %rax\n\t"
  67. "pushq %rax\n\t"
  68. "movq %es, %rax\n\t"
  69. "pushq %rax\n\t"
  70. "movq %fs, %rax\n\t"
  71. "pushq %rax\n\t"
  72. "movq %gs, %rax\n\t"
  73. "pushq %rax\n\t"
  74. "movq $0x10, %rax\n\t"
  75. "movq %rax, %ds\n\t"
  76. "movq %rax, %es\n\t"
  77. "movq %rax, %fs\n\t"
  78. "movq %rax, %gs\n\t"
  79. "movq %rax, %ss\n\t"
  80. "movq %rsp, %rsi\n\t"
  81. "pushq ret_intr(%rip)\n\t"
  82. "jmp do_trap\n\t"
  83. );
  84. #define build_trap_entry_errcode(nr) \
  85. void trap_##nr##_entry(void); \
  86. __asm__ ( \
  87. ".globl trap_"#nr"_entry\n\t" \
  88. "trap_"#nr"_entry:\n\t" \
  89. "cli\n\t" \
  90. "pushq %rax\n\t" \
  91. "movq $"#nr", %rax\n\t" \
  92. "jmp common_trap\n\t" \
  93. );
  94. #define build_trap_entry_nerrcode(nr) \
  95. void trap_##nr##_entry(void); \
  96. __asm__ ( \
  97. ".globl trap_"#nr"_entry\n\t" \
  98. "trap_"#nr"_entry:\n\t" \
  99. "cli\n\t" \
  100. "pushq $0\n\t" \
  101. "pushq %rax\n\t" \
  102. "movq $"#nr", %rax\n\t" \
  103. "jmp common_trap\n\t" \
  104. )
  105. #else
  106. __asm__ (
  107. ".globl common_trap\n\t"
  108. "common_trap:\n\t"
  109. "pushl %ecx\n\t"
  110. "pushl %ebx\n\t"
  111. "pushl %edx\n\t"
  112. "pushl %ebp\n\t"
  113. "pushl %esi\n\t"
  114. "pushl %edi\n\t"
  115. "movl %eax, %edi\n\t"
  116. "movl %ds, %eax\n\t"
  117. "pushl %eax\n\t"
  118. "movl %es, %eax\n\t"
  119. "pushl %eax\n\t"
  120. "movl %fs, %eax\n\t"
  121. "pushl %eax\n\t"
  122. "movl %gs, %eax\n\t"
  123. "pushl %eax\n\t"
  124. "movl $0x10, %eax\n\t"
  125. "movl %eax, %ds\n\t"
  126. "movl %eax, %es\n\t"
  127. "movl %eax, %fs\n\t"
  128. "movl %eax, %gs\n\t"
  129. "movl %eax, %ss\n\t"
  130. "movl %esp, %esi\n\t"
  131. "pushl %esi\n\t"
  132. "pushl %edi\n\t"
  133. "call do_trap\n\t"
  134. "addl $8, %esp\n\t"
  135. "jmp ret_intr\n\t"
  136. );
  137. #define build_trap_entry_errcode(nr) \
  138. void trap_##nr##_entry(void); \
  139. __asm__ ( \
  140. ".globl trap_"#nr"_entry\n\t" \
  141. "trap_"#nr"_entry:\n\t" \
  142. "cli\n\t" \
  143. "pushl %eax\n\t" \
  144. "movl $"#nr", %eax\n\t" \
  145. "jmp common_trap\n\t" \
  146. );
  147. #define build_trap_entry_nerrcode(nr) \
  148. void trap_##nr##_entry(void); \
  149. __asm__ ( \
  150. ".globl trap_"#nr"_entry\n\t" \
  151. "trap_"#nr"_entry:\n\t" \
  152. "cli\n\t" \
  153. "pushl $0\n\t" \
  154. "pushl %eax\n\t" \
  155. "movl $"#nr", %eax\n\t" \
  156. "jmp common_trap\n\t" \
  157. )
  158. #endif
  159. build_trap_entry_nerrcode(0);
  160. build_trap_entry_nerrcode(1);
  161. build_trap_entry_nerrcode(2);
  162. build_trap_entry_nerrcode(3);
  163. build_trap_entry_nerrcode(4);
  164. build_trap_entry_nerrcode(5);
  165. build_trap_entry_nerrcode(6);
  166. build_trap_entry_nerrcode(7);
  167. build_trap_entry_errcode(8);
  168. build_trap_entry_nerrcode(9);
  169. build_trap_entry_errcode(10);
  170. build_trap_entry_errcode(11);
  171. build_trap_entry_errcode(12);
  172. build_trap_entry_errcode(13);
  173. build_trap_entry_errcode(14);
  174. build_trap_entry_nerrcode(15);
  175. build_trap_entry_nerrcode(16);
  176. build_trap_entry_errcode(17);
  177. build_trap_entry_nerrcode(18);
  178. build_trap_entry_nerrcode(19);
  179. build_trap_entry_nerrcode(20);
  180. /* set the handlers for traps, 0 - 31 */
  181. int __init arch_traps_init(void) {
  182. #include "__arch_traps_generated.c"
  183. return 0;
  184. }
  185. const static char *trap_msg[32] = {
  186. "division by zero",
  187. "debug trap",
  188. "NMI",
  189. "breakpoint",
  190. "overflow",
  191. "bound",
  192. "invalid op-code",
  193. "device not avaliable",
  194. "double fault",
  195. "coprocessor segment overrun",
  196. "invalid tss",
  197. "segment not present",
  198. "stack-segment fault",
  199. "general protection",
  200. "page fault",
  201. "reserved",
  202. "x87 floating-point exception",
  203. "alignment check",
  204. "machine check",
  205. "simd floating-point exception",
  206. "virtualization exception",
  207. "control protection exception",
  208. "reserved",
  209. "hypervisor injection exception",
  210. "vmm communication exception",
  211. "security exception",
  212. "reserved"
  213. };
  214. int do_trap(int nr, __archptr_t sp) {
  215. struct registers *regs = (struct registers *) sp;
  216. kmlog(KERN_ABANDON "Oops! trap %d: %s\n", nr, trap_msg[nr]);
  217. kmlog(KERN_ABANDON "sp at %p\n", sp);
  218. /* okay, we use wrong tabs in protected-mode but i think that's ok :-) */
  219. kmlog(KERN_ABANDON "ax\t\t\tcx\t\t\tbx\n");
  220. kmlog(KERN_ABANDON "%p\t%p\t%p\n", regs->ax, regs->cx, regs->bx);
  221. kmlog(KERN_ABANDON "dx\t\t\tbp\t\t\tsp\n");
  222. kmlog(KERN_ABANDON "%p\t%p\t%p\n", regs->dx, regs->bp, regs->sp);
  223. #if CONFIG_64BIT
  224. kmlog(KERN_ABANDON "si\t\t\tdi\t\t\tr8\n");
  225. kmlog(KERN_ABANDON "%p\t%p\t%p\n", regs->si, regs->di, regs->r8);
  226. kmlog(KERN_ABANDON "r9\t\t\tr10\t\t\tr11\n");
  227. kmlog(KERN_ABANDON "%p\t%p\t%p\n", regs->r9, regs->r10, regs->r11);
  228. kmlog(KERN_ABANDON "r12\t\t\tr13\t\t\tr14\n");
  229. kmlog(KERN_ABANDON "%p\t%p\t%p\n", regs->r12, regs->r13, regs->r14);
  230. kmlog(KERN_ABANDON "r15\n%d\n", regs->r15);
  231. #else
  232. kmlog(KERN_ABANDON "si\t\t\tdi\n");
  233. kmlog(KERN_ABANDON "%p\t%p\n", regs->si, regs->di);
  234. #endif
  235. kmlog(KERN_ABANDON "ds: \t\t%p\n", regs->ds);
  236. kmlog(KERN_ABANDON "ss: \t\t%p\n", regs->ss);
  237. kmlog(KERN_ABANDON "flags: \t\t%p\n", regs->flags);
  238. kmlog(KERN_ABANDON "cs:ip: \t\t%d:%p\n", regs->cs, regs->ip);
  239. for (;;)
  240. cpu_relax();
  241. return 0;
  242. }
  243. static int set_gate(struct idt_entry *entry, void (* handler) (void), int attr) {
  244. /* no ist support in long-mode, well, that's not so useful */
  245. __archptr_t p = (__archptr_t) handler;
  246. entry->off_0 = p & 0xffffu;
  247. entry->seg = 8;
  248. #if CONFIG_64BIT
  249. entry->ist =
  250. #else
  251. entry->resv =
  252. #endif
  253. 0;
  254. entry->attr = attr;
  255. entry->off_1 = (p >> 16u) & 0xffffu;
  256. #if CONFIG_64BIT
  257. entry->off_2 = p >> 32u;
  258. entry->zero = 0;
  259. #endif
  260. return 0;
  261. }
  262. arch_init(arch_traps_init);