arch_int.c 7.1 KB

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