arch_int.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/int.h>
  26. #include <arch/string.h>
  27. #include <sys/types.h>
  28. #include <config.h>
  29. /* head$(BITS).S */
  30. /* extern struct idt_entry *idt; things interesting.
  31. * a struct idt_entry *idt, and that is a pointer. idt, is
  32. * the address.
  33. *
  34. * a struct idt_entry idt[], and that is an array. */
  35. extern struct idt_entry idt[256];
  36. extern void ret_intr(void);
  37. int do_trap(int nr);
  38. static int set_gate(struct idt_entry *, void (*) (void), int attr);
  39. #define set_int_gate(nr, handler) set_gate(idt + nr, handler, 0x8e)
  40. #define set_trap_gate(nr, handler) set_gate(idt + nr, handler, 0x8f)
  41. #define set_sys_gate(nr, handler) set_gate(idt + nr, handler, 0xef)
  42. #if CONFIG_64BIT
  43. /* errcodes later */
  44. #define build_trap_entry(nr) \
  45. void trap_##nr##_entry(void); \
  46. __asm__ ( \
  47. ".globl trap_"#nr"_entry\n\t" \
  48. "trap_"#nr"_entry:\n\t" \
  49. IRQ_SAVE_REGS \
  50. "movq $"#nr", %rdi\n\t" \
  51. "pushq ret_intr(%rip)\n\t" \
  52. "jmp do_trap\n\t" \
  53. );
  54. #else
  55. #define build_trap_entry(nr) \
  56. void trap_##nr##_entry(void); \
  57. __asm__ ( \
  58. ".globl trap_"#nr"_entry\n\t" \
  59. "trap_"#nr"_entry:\n\t" \
  60. "pushl $"#nr"\n\t" \
  61. "call do_trap\n\t" \
  62. "addl $4, %esp\n\t" \
  63. "jmp ret_intr\n\t" \
  64. );
  65. #endif
  66. #include "__build_traps_generated.c"
  67. /* set the handlers for traps, 0 - 31 */
  68. int __init arch_traps_init(void) {
  69. #include "__arch_traps_generated.c"
  70. return 0;
  71. }
  72. const static char *trap_msg[32] = {
  73. "division by zero",
  74. "debug trap",
  75. "NMI",
  76. "breakpoint",
  77. "overflow",
  78. "bound",
  79. "invalid op-code",
  80. "device not avaliable",
  81. "double fault",
  82. "coprocessor segment overrun",
  83. "invalid tss",
  84. "segment not present",
  85. "stack-segment fault",
  86. "general protection",
  87. "page fault",
  88. "reserved",
  89. "x87 floating-point exception",
  90. "alignment check",
  91. "machine check",
  92. "simd floating-point exception",
  93. "virtualization exception",
  94. "control protection exception",
  95. "reserved",
  96. "hypervisor injection exception",
  97. "vmm communication exception",
  98. "security exception",
  99. "reserved"
  100. };
  101. int do_trap(int nr) {
  102. kmlog("Oops! trap %d: %s\n", nr, trap_msg[nr]);
  103. for (;;)
  104. ;
  105. return 0;
  106. }
  107. static int set_gate(struct idt_entry *entry, void (* handler) (void), int attr) {
  108. /* no ist support in long-mode, well, that's not so useful */
  109. __archptr_t p = (__archptr_t) handler;
  110. entry->off_0 = p & 0xffffu;
  111. entry->seg = 8;
  112. #if CONFIG_64BIT
  113. entry->ist =
  114. #else
  115. entry->resv =
  116. #endif
  117. 0;
  118. entry->attr = attr;
  119. entry->off_1 = (p >> 16u) & 0xffffu;
  120. #if CONFIG_64BIT
  121. entry->off_2 = p >> 32u;
  122. entry->zero = 0;
  123. #endif
  124. return 0;
  125. }
  126. arch_init(arch_traps_init);