int.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /* initial PIT timer */
  68. outb(0x34, 0x43);
  69. /* real time = 1.1932HZ / this */
  70. /* about 10ms */
  71. #define TIMER_HZ_LOW 0x9c
  72. #define TIMER_HZ_HIGH 0x2e
  73. outb(TIMER_HZ_LOW, 0x40);
  74. outb(TIMER_HZ_HIGH, 0x40);
  75. register_irq(0x00, irq0x20_interrupt, do_timer);
  76. register_irq(0x01, irq0x21_interrupt, do_keyboard);
  77. schk_leave();
  78. return 0;
  79. }
  80. void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
  81. schk_init();
  82. #define handler ((__uint64_t) addr)
  83. entry->selector = KERNEL_CS;
  84. entry->ist = ist;
  85. entry->attr = attr;
  86. entry->off_0_16 = handler & 0xffff;
  87. entry->off_16_32 = handler >> 16;
  88. entry->off_32_64 = handler >> 32;
  89. entry->zero = 0x00000000;
  90. schk_leave();
  91. return;
  92. }
  93. void syscall(void) {
  94. schk_init();
  95. print(0x00ff0000, 0x00000000, "int #0x80: system call. \n");
  96. schk_leave();
  97. return;
  98. }
  99. void die(char *s, __uint64_t *rsp, char nr) {
  100. /* should we use schk here? */
  101. printk(0x00ff0000, 0x00000000, "int #%b: %s. \n", nr, s);
  102. printk(0x00ff0000, 0x00000000, "rsp: %q\n", rsp);
  103. printk(0x00ff0000, 0x00000000, "rip: %q\n", rsp - RIP);
  104. for (;;) hlt();
  105. return;
  106. }
  107. void do_divide_error(__uint64_t *rsp, __uint64_t errcode) {
  108. schk_init();
  109. die("divide error", rsp, 0);
  110. schk_leave();
  111. return;
  112. }
  113. void do_debug(__uint64_t *rsp, __uint64_t errcode) {
  114. schk_init();
  115. die("debug", rsp, 1);
  116. schk_leave();
  117. return;
  118. }
  119. void do_nmi(__uint64_t *rsp, __uint64_t errcode) {
  120. schk_init();
  121. die("nmi", rsp, 2);
  122. schk_leave();
  123. return;
  124. }
  125. void do_int3(__uint64_t *rsp, __uint64_t errcode) {
  126. schk_init();
  127. die("int3", rsp, 3);
  128. schk_leave();
  129. return;
  130. }
  131. void do_overflow(__uint64_t *rsp, __uint64_t errcode) {
  132. schk_init();
  133. die("overflow", rsp, 4);
  134. schk_leave();
  135. return;
  136. }
  137. void do_bound(__uint64_t *rsp, __uint64_t errcode) {
  138. schk_init();
  139. die("bound", rsp, 5);
  140. schk_leave();
  141. return;
  142. }
  143. void do_ud(__uint64_t *rsp, __uint64_t errcode) {
  144. schk_init();
  145. die("undefined instruction", rsp, 6);
  146. schk_leave();
  147. return;
  148. }
  149. void do_no_fpu(__uint64_t *rsp, __uint64_t errcode) {
  150. schk_init();
  151. die("fpu not present", rsp, 7);
  152. schk_leave();
  153. return;
  154. }
  155. void do_double_fault(__uint64_t *rsp, __uint64_t errcode) {
  156. schk_init();
  157. die("double fault", rsp, 8);
  158. schk_leave();
  159. return;
  160. }
  161. void do_fpu_overrun(__uint64_t *rsp, __uint64_t errcode) {
  162. schk_init();
  163. die("fpu overrun", rsp, 9);
  164. schk_leave();
  165. return;
  166. }
  167. void do_invalid_tss(__uint64_t *rsp, __uint64_t errcode) {
  168. schk_init();
  169. die("invalid tss", rsp, 10);
  170. schk_leave();
  171. return;
  172. }
  173. void do_no_segment(__uint64_t *rsp, __uint64_t errcode) {
  174. schk_init();
  175. die("no such segment", rsp, 11);
  176. schk_leave();
  177. return;
  178. }
  179. void do_stack(__uint64_t *rsp, __uint64_t errcode) {
  180. schk_init();
  181. die("stack error", rsp, 12);
  182. schk_leave();
  183. return;
  184. }
  185. void do_gp(__uint64_t *rsp, __uint64_t errcode) {
  186. schk_init();
  187. die("general protection", rsp, 13);
  188. schk_leave();
  189. return;
  190. }
  191. void do_page_fault(__uint64_t *rsp, __uint64_t errcode) {
  192. schk_init();
  193. die("page fault", rsp, 14);
  194. schk_leave();
  195. return;
  196. }
  197. void do_reserved(__uint64_t *rsp, __uint64_t errcode) {
  198. schk_init();
  199. die("reserved", rsp, 15);
  200. schk_leave();
  201. return;
  202. }
  203. void do_fpu_error(__uint64_t *rsp, __uint64_t errcode) {
  204. schk_init();
  205. die("fpu error", rsp, 16);
  206. schk_leave();
  207. return;
  208. }
  209. void do_alignment(__uint64_t *rsp, __uint64_t errcode) {
  210. schk_init();
  211. die("alignment error", rsp, 17);
  212. schk_leave();
  213. return;
  214. }
  215. void do_machine(__uint64_t *rsp, __uint64_t errcode) {
  216. schk_init();
  217. die("machine check", rsp, 18);
  218. schk_leave();
  219. return;
  220. }
  221. void do_simd(__uint64_t *rsp, __uint64_t errcode) {
  222. schk_init();
  223. die("simd error", rsp, 19);
  224. schk_leave();
  225. return;
  226. }
  227. void do_virtualization(__uint64_t *rsp, __uint64_t errcode) {
  228. schk_init();
  229. die("virtualization error", rsp, 20);
  230. schk_leave();
  231. return;
  232. }