isr.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2011-2014 Kevin Lange
  5. * Copyright (C) 2015 Dale Weiler
  6. *
  7. * Interrupt Service Requests
  8. */
  9. #include <kernel/system.h>
  10. #include <kernel/logging.h>
  11. #include <kernel/module.h>
  12. #include <kernel/printf.h>
  13. /* The count is treated as is when setting up IDT gates. However there is an
  14. * additional ISR for the system call vector which is handled explicitly since
  15. * it's mapped at a different address.
  16. */
  17. #define ISR_COUNT 32
  18. static struct {
  19. size_t index;
  20. void (*stub)(void);
  21. } isrs[32 + 1] __attribute__((used));
  22. static irq_handler_t isr_routines[256] = { 0 };
  23. void isrs_install_handler(size_t isrs, irq_handler_t handler) {
  24. isr_routines[isrs] = handler;
  25. }
  26. void isrs_uninstall_handler(size_t isrs) {
  27. isr_routines[isrs] = 0;
  28. }
  29. void isrs_install(void) {
  30. char buffer[16];
  31. for (int i = 0; i < ISR_COUNT; i++) {
  32. sprintf(buffer, "_isr%d", i);
  33. isrs[i].index = i;
  34. isrs[i].stub = symbol_find(buffer);
  35. }
  36. isrs[ISR_COUNT].index = SYSCALL_VECTOR;
  37. isrs[ISR_COUNT].stub = symbol_find("_isr127");
  38. for (int i = 0; i < ISR_COUNT + 1; i++) {
  39. idt_set_gate(isrs[i].index, isrs[i].stub, 0x08, 0x8E);
  40. }
  41. }
  42. static const char *exception_messages[32] = {
  43. "Division by zero",
  44. "Debug",
  45. "Non-maskable interrupt",
  46. "Breakpoint",
  47. "Detected overflow",
  48. "Out-of-bounds",
  49. "Invalid opcode",
  50. "No coprocessor",
  51. "Double fault",
  52. "Coprocessor segment overrun",
  53. "Bad TSS",
  54. "Segment not present",
  55. "Stack fault",
  56. "General protection fault",
  57. "Page fault",
  58. "Unknown interrupt",
  59. "Coprocessor fault",
  60. "Alignment check",
  61. "Machine check",
  62. "Reserved",
  63. "Reserved",
  64. "Reserved",
  65. "Reserved",
  66. "Reserved",
  67. "Reserved",
  68. "Reserved",
  69. "Reserved",
  70. "Reserved",
  71. "Reserved",
  72. "Reserved",
  73. "Reserved",
  74. "Reserved"
  75. };
  76. void fault_handler(struct regs * r) {
  77. irq_handler_t handler = isr_routines[r->int_no];
  78. if (handler) {
  79. handler(r);
  80. } else {
  81. debug_print(CRITICAL, "Unhandled exception: [%d] %s", r->int_no, exception_messages[r->int_no]);
  82. HALT_AND_CATCH_FIRE("Process caused an unhandled exception", r);
  83. STOP;
  84. }
  85. }