int.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX 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. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/graphic.h>
  23. #include <kernel/int.h>
  24. #include <kernel/schk.h>
  25. #include <arch/x86/int.h>
  26. #include <arch/io.h>
  27. #include <arch/sys.h>
  28. build_irq(0x20);
  29. build_irq(0x21);
  30. int int_init(void) {
  31. schk_init();
  32. /* init PIC */
  33. /* ICW: initial control word */
  34. outb(0x11, PIC0_ICW1);
  35. outb(0x20, PIC0_ICW2);
  36. outb(0x04, PIC0_ICW3);
  37. outb(0x01, PIC0_ICW4);
  38. outb(0x11, PIC1_ICW1);
  39. outb(0x28, PIC1_ICW2);
  40. outb(0x02, PIC1_ICW3);
  41. outb(0x01, PIC1_ICW4);
  42. /* disable all IRQs */
  43. outb(0xff, PIC0_IMR);
  44. outb(0xff, PIC1_IMR);
  45. set_trap_gate(idt_table + 0x00, 0, &divide_error);
  46. set_trap_gate(idt_table + 0x01, 0, &debug);
  47. set_trap_gate(idt_table + 0x02, 0, &nmi);
  48. set_trap_gate(idt_table + 0x03, 0, &int3);
  49. set_trap_gate(idt_table + 0x04, 0, &overflow);
  50. set_trap_gate(idt_table + 0x05, 0, &bound);
  51. set_trap_gate(idt_table + 0x06, 0, &invalid_code);
  52. set_trap_gate(idt_table + 0x07, 0, &device_not_avaliable);
  53. set_trap_gate(idt_table + 0x08, 0, &double_fault);
  54. set_trap_gate(idt_table + 0x09, 0, &fpu_segment_overrun);
  55. set_trap_gate(idt_table + 0x0a, 0, &invalid_tss);
  56. set_trap_gate(idt_table + 0x0b, 0, &segment_not_present);
  57. set_trap_gate(idt_table + 0x0c, 0, &stack_error);
  58. set_trap_gate(idt_table + 0x0d, 0, &general_protection);
  59. set_trap_gate(idt_table + 0x0e, 0, &page_fault);
  60. set_trap_gate(idt_table + 0x0f, 0, &reserved);
  61. set_trap_gate(idt_table + 0x10, 0, &fpu_error);
  62. set_trap_gate(idt_table + 0x11, 0, &alignment);
  63. set_trap_gate(idt_table + 0x12, 0, &machine);
  64. set_trap_gate(idt_table + 0x13, 0, &simd);
  65. set_trap_gate(idt_table + 0x14, 0, &virtualization);
  66. set_int_gate(idt_table + 0x80, 0, &syscall);
  67. register_irq(0x00, irq0x20_interrupt, do_timer);
  68. register_irq(0x01, irq0x21_interrupt, do_keyboard);
  69. schk_leave();
  70. return 0;
  71. }
  72. void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
  73. schk_init();
  74. #define handler ((__uint64_t) addr)
  75. entry->selector = KERNEL_CS;
  76. entry->ist = ist;
  77. entry->attr = attr;
  78. entry->off_0_16 = handler & 0xffff;
  79. entry->off_16_32 = handler >> 16;
  80. entry->off_32_64 = handler >> 32;
  81. entry->zero = 0x00000000;
  82. schk_leave();
  83. return;
  84. }
  85. void syscall(void) {
  86. schk_init();
  87. print(0x00ff0000, 0x00000000, "int #0x80: system call. \n");
  88. schk_leave();
  89. return;
  90. }
  91. void die(char *s, __uint64_t *rsp, char nr) {
  92. /* should we use schk here? */
  93. printk(0x00ff0000, 0x00000000, "int #%b: %s. \n", nr, s);
  94. printk(0x00ff0000, 0x00000000, "rsp: %q\n", rsp);
  95. for (;;) hlt();
  96. return;
  97. }
  98. void do_divide_error(__uint64_t *rsp) {
  99. schk_init();
  100. die("divide error", rsp, 0);
  101. schk_leave();
  102. return;
  103. }
  104. void do_debug(__uint64_t *rsp) {
  105. schk_init();
  106. die("debug", rsp, 1);
  107. schk_leave();
  108. return;
  109. }
  110. void do_nmi(__uint64_t *rsp) {
  111. schk_init();
  112. die("nmi", rsp, 2);
  113. schk_leave();
  114. return;
  115. }
  116. void do_int3(__uint64_t *rsp) {
  117. schk_init();
  118. die("int3", rsp, 3);
  119. schk_leave();
  120. return;
  121. }
  122. void do_overflow(__uint64_t *rsp) {
  123. schk_init();
  124. die("overflow", rsp, 4);
  125. schk_leave();
  126. return;
  127. }
  128. void do_bound(__uint64_t *rsp) {
  129. schk_init();
  130. die("bound", rsp, 5);
  131. schk_leave();
  132. return;
  133. }
  134. void do_ud(__uint64_t *rsp) {
  135. schk_init();
  136. die("undefined instruction", rsp, 6);
  137. schk_leave();
  138. return;
  139. }
  140. void do_no_fpu(__uint64_t *rsp) {
  141. schk_init();
  142. die("fpu not present", rsp, 7);
  143. schk_leave();
  144. return;
  145. }
  146. void do_double_fault(__uint64_t *rsp) {
  147. schk_init();
  148. die("double fault", rsp, 8);
  149. schk_leave();
  150. return;
  151. }
  152. void do_fpu_overrun(__uint64_t *rsp) {
  153. schk_init();
  154. die("fpu overrun", rsp, 9);
  155. schk_leave();
  156. return;
  157. }
  158. void do_invalid_tss(__uint64_t *rsp) {
  159. schk_init();
  160. die("invalid tss", rsp, 10);
  161. schk_leave();
  162. return;
  163. }
  164. void do_no_segment(__uint64_t *rsp) {
  165. schk_init();
  166. die("no such segment", rsp, 11);
  167. schk_leave();
  168. return;
  169. }
  170. void do_stack(__uint64_t *rsp) {
  171. schk_init();
  172. die("stack error", rsp, 12);
  173. schk_leave();
  174. return;
  175. }
  176. void do_gp(__uint64_t *rsp) {
  177. schk_init();
  178. die("general protection", rsp, 13);
  179. schk_leave();
  180. return;
  181. }
  182. void do_page_fault(__uint64_t *rsp) {
  183. schk_init();
  184. die("page fault", rsp, 14);
  185. schk_leave();
  186. return;
  187. }
  188. void do_reserved(__uint64_t *rsp) {
  189. schk_init();
  190. die("reserved", rsp, 15);
  191. schk_leave();
  192. return;
  193. }
  194. void do_fpu_error(__uint64_t *rsp) {
  195. schk_init();
  196. die("fpu error", rsp, 16);
  197. schk_leave();
  198. return;
  199. }
  200. void do_alignment(__uint64_t *rsp) {
  201. schk_init();
  202. die("alignment error", rsp, 17);
  203. schk_leave();
  204. return;
  205. }
  206. void do_machine(__uint64_t *rsp) {
  207. schk_init();
  208. die("machine check", rsp, 18);
  209. schk_leave();
  210. return;
  211. }
  212. void do_simd(__uint64_t *rsp) {
  213. schk_init();
  214. die("simd error", rsp, 19);
  215. schk_leave();
  216. return;
  217. }
  218. void do_virtualization(__uint64_t *rsp) {
  219. schk_init();
  220. die("virtualization error", rsp, 20);
  221. schk_leave();
  222. return;
  223. }