interrupt.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <types/types.h>
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #define KERNEL_INTERRUPT_GATE_TYPE (0x8e)
  7. #define USER_INTERRUPT_GATE_TYPE (0xee)
  8. #define PIC_EOI (0x20)
  9. struct regs_32 {
  10. uint32_t edi;
  11. uint32_t esi;
  12. uint32_t ebp;
  13. uint32_t esp;
  14. uint32_t ebx;
  15. uint32_t edx;
  16. uint32_t ecx;
  17. uint32_t eax;
  18. };
  19. struct PACKED interrupt_stack {
  20. struct regs_32 s_regs;
  21. void* v_eip;
  22. uint32_t cs;
  23. uint32_t eflags;
  24. uint32_t esp;
  25. uint32_t ss;
  26. };
  27. // present: When set, the page fault was caused by a page-protection violation.
  28. // When not set, it was caused by a non-present page.
  29. // write: When set, the page fault was caused by a write access.
  30. // When not set, it was caused by a read access.
  31. // user: When set, the page fault was caused while CPL = 3.
  32. // This does not necessarily mean that the page fault was a privilege violation.
  33. // from https://wiki.osdev.org/Exceptions#Page_Fault
  34. struct page_fault_error_code {
  35. uint32_t present : 1;
  36. uint32_t write : 1;
  37. uint32_t user : 1;
  38. uint32_t reserved_write : 1;
  39. uint32_t instruction_fetch : 1;
  40. uint32_t protection_key : 1;
  41. uint32_t shadow_stack : 1;
  42. uint32_t software_guard_extensions : 1;
  43. };
  44. // external interrupt handler function
  45. // stub in assembly MUST be called irqN
  46. #define SET_UP_IRQ(N, SELECTOR) \
  47. ptr_t addr_irq##N = (ptr_t)irq##N; \
  48. SET_IDT_ENTRY(0x20 + (N), (addr_irq##N), (SELECTOR), KERNEL_INTERRUPT_GATE_TYPE);
  49. #define SET_IDT_ENTRY_FN(N, FUNC_NAME, SELECTOR, TYPE) \
  50. extern void FUNC_NAME(); \
  51. ptr_t addr_##FUNC_NAME = (ptr_t)FUNC_NAME; \
  52. SET_IDT_ENTRY((N), (addr_##FUNC_NAME), (SELECTOR), (TYPE));
  53. #define SET_IDT_ENTRY(N, ADDR, SELECTOR, TYPE) \
  54. IDT[(N)].offset_low = (ADDR)&0x0000ffff; \
  55. IDT[(N)].selector = (SELECTOR); \
  56. IDT[(N)].zero = 0; \
  57. IDT[(N)].type_attr = (TYPE); \
  58. IDT[(N)].offset_high = ((ADDR)&0xffff0000) >> 16
  59. struct IDT_entry {
  60. uint16_t offset_low;
  61. uint16_t selector;
  62. uint8_t zero;
  63. uint8_t type_attr;
  64. uint16_t offset_high;
  65. };
  66. #ifndef _INTERRUPT_C_
  67. extern struct IDT_entry IDT[256];
  68. #endif
  69. void init_idt(void);
  70. void init_pic(void);
  71. // idt_descriptor: uint16_t[3]
  72. // [0] bit 0 :15 => limit
  73. // [1] bit 16:47 => address
  74. extern void asm_load_idt(uint16_t idt_descriptor[3], int sti);
  75. void int13_handler(
  76. struct regs_32 s_regs,
  77. uint32_t error_code,
  78. ptr_t eip,
  79. uint16_t cs,
  80. uint32_t eflags);
  81. void int0(void);
  82. void int1(void);
  83. void int2(void);
  84. void int3(void);
  85. void int4(void);
  86. void int5(void);
  87. void int6(void);
  88. void int7(void);
  89. void int8(void);
  90. void int9(void);
  91. void int10(void);
  92. void int11(void);
  93. void int12(void);
  94. void int13(void);
  95. void int14(void);
  96. void irq0(void);
  97. void irq1(void);
  98. void irq2(void);
  99. void irq3(void);
  100. void irq4(void);
  101. void irq5(void);
  102. void irq6(void);
  103. void irq7(void);
  104. void irq8(void);
  105. void irq9(void);
  106. void irq10(void);
  107. void irq11(void);
  108. void irq12(void);
  109. void irq13(void);
  110. void irq14(void);
  111. void irq15(void);
  112. void syscall_stub(void);
  113. #ifdef __cplusplus
  114. }
  115. #endif