int.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <modules/tty.h>
  23. #include <kernel/int.h>
  24. #include <kernel/schk.h>
  25. #include <kernel/mm.h>
  26. #include <arch/x86/int.h>
  27. #include <arch/io.h>
  28. #include <arch/sys.h>
  29. build_irq(0x20);
  30. build_irq(0x21);
  31. int int_init(void) {
  32. schk_init();
  33. /* init PIC */
  34. /* ICW: initial control word */
  35. outb(0x11, PIC0_ICW1);
  36. outb(0x20, PIC0_ICW2);
  37. outb(0x04, PIC0_ICW3);
  38. outb(0x01, PIC0_ICW4);
  39. outb(0x11, PIC1_ICW1);
  40. outb(0x28, PIC1_ICW2);
  41. outb(0x02, PIC1_ICW3);
  42. outb(0x01, PIC1_ICW4);
  43. /* disable all IRQs */
  44. outb(0xff, PIC0_IMR);
  45. outb(0xff, PIC1_IMR);
  46. set_trap_gate(idt_table + 0x00, 0, &divide_error);
  47. set_trap_gate(idt_table + 0x01, 0, &debug);
  48. set_trap_gate(idt_table + 0x02, 0, &nmi);
  49. set_trap_gate(idt_table + 0x03, 0, &int3);
  50. set_trap_gate(idt_table + 0x04, 0, &overflow);
  51. set_trap_gate(idt_table + 0x05, 0, &bound);
  52. set_trap_gate(idt_table + 0x06, 0, &invalid_code);
  53. set_trap_gate(idt_table + 0x07, 0, &device_not_avaliable);
  54. set_trap_gate(idt_table + 0x08, 0, &double_fault);
  55. set_trap_gate(idt_table + 0x09, 0, &fpu_segment_overrun);
  56. set_trap_gate(idt_table + 0x0a, 0, &invalid_tss);
  57. set_trap_gate(idt_table + 0x0b, 0, &segment_not_present);
  58. set_trap_gate(idt_table + 0x0c, 0, &stack_error);
  59. set_trap_gate(idt_table + 0x0d, 0, &general_protection);
  60. set_trap_gate(idt_table + 0x0e, 0, &page_fault);
  61. set_trap_gate(idt_table + 0x0f, 0, &reserved);
  62. set_trap_gate(idt_table + 0x10, 0, &fpu_error);
  63. set_trap_gate(idt_table + 0x11, 0, &alignment);
  64. set_trap_gate(idt_table + 0x12, 0, &machine);
  65. set_trap_gate(idt_table + 0x13, 0, &simd);
  66. set_trap_gate(idt_table + 0x14, 0, &virtualization);
  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. set_sys_gate(idt_table + 0x80, 1, &syscall);
  78. schk_leave();
  79. return 0;
  80. }
  81. void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
  82. schk_init();
  83. #define handler ((__uint64_t) addr)
  84. entry->selector = KERNEL_CS;
  85. entry->ist = ist;
  86. entry->attr = attr;
  87. entry->off_0_16 = handler & 0xffff;
  88. entry->off_16_32 = handler >> 16;
  89. entry->off_32_64 = handler >> 32;
  90. entry->zero = 0x00000000;
  91. schk_leave();
  92. return;
  93. }
  94. void die(const char *s, const __uint64_t *rsp, char nr) {
  95. /* should we use schk here? */
  96. printk(red, "int #%b: %s. \n", nr, s);
  97. printk(red, "rsp now: %q\n", rsp);
  98. printk(red, "rax\t\t\trcx\t\t\trdx\n");
  99. printk(red, "%q\t%q\t%q\n", rsp[off_rax], rsp[off_rcx], rsp[off_rdx]);
  100. printk(red, "rbx\t\t\trbp\t\t\trsp\n");
  101. printk(red, "%q\t%q\t%q\n", rsp[off_rbx], rsp[off_rbp], rsp[off_rsp]);
  102. printk(red, "rsi\t\t\trdi\t\t\tr8\n");
  103. printk(red, "%q\t%q\t%q\n", rsp[off_rsi], rsp[off_rdi], rsp[off_r8]);
  104. printk(red, "r9\t\t\tr10\t\t\tr11\n");
  105. printk(red, "%q\t%q\t%q\n", rsp[off_r9], rsp[off_r10], rsp[off_r11]);
  106. printk(red, "r12\t\t\tr13\t\t\tr14\n");
  107. printk(red, "%q\t%q\t%q\n", rsp[off_r12], rsp[off_r13], rsp[off_r14]);
  108. printk(red, "r15\t\t\trip\t\t\trflags\n");
  109. printk(red, "%q\t%q\t%q\n", rsp[off_r15], rsp[off_rip], rsp[off_rflags]);
  110. printk(red, "ds\t\t\tss\n");
  111. printk(red, "%w\t%w\n", rsp[off_ds] & 0xffff, rsp[off_ss] & 0xffff);
  112. printk(red, "instruction: %q\n", *(__uint64_t *) rsp[off_rip]);
  113. trace_mm();
  114. for (;;) hlt();
  115. return;
  116. }
  117. void do_divide_error(const __uint64_t *rsp, __uint64_t errcode) {
  118. schk_init();
  119. die("divide error", rsp, 0);
  120. schk_leave();
  121. return;
  122. }
  123. void do_debug(const __uint64_t *rsp, __uint64_t errcode) {
  124. schk_init();
  125. die("debug", rsp, 1);
  126. schk_leave();
  127. return;
  128. }
  129. void do_nmi(const __uint64_t *rsp, __uint64_t errcode) {
  130. schk_init();
  131. die("nmi", rsp, 2);
  132. schk_leave();
  133. return;
  134. }
  135. void do_int3(const __uint64_t *rsp, __uint64_t errcode) {
  136. schk_init();
  137. die("int3", rsp, 3);
  138. schk_leave();
  139. return;
  140. }
  141. void do_overflow(const __uint64_t *rsp, __uint64_t errcode) {
  142. schk_init();
  143. die("overflow", rsp, 4);
  144. schk_leave();
  145. return;
  146. }
  147. void do_bound(const __uint64_t *rsp, __uint64_t errcode) {
  148. schk_init();
  149. die("bound", rsp, 5);
  150. schk_leave();
  151. return;
  152. }
  153. void do_ud(const __uint64_t *rsp, __uint64_t errcode) {
  154. schk_init();
  155. die("undefined instruction", rsp, 6);
  156. schk_leave();
  157. return;
  158. }
  159. void do_no_fpu(const __uint64_t *rsp, __uint64_t errcode) {
  160. schk_init();
  161. die("fpu not present", rsp, 7);
  162. schk_leave();
  163. return;
  164. }
  165. void do_double_fault(const __uint64_t *rsp, __uint64_t errcode) {
  166. schk_init();
  167. die("double fault", rsp, 8);
  168. schk_leave();
  169. return;
  170. }
  171. void do_fpu_overrun(const __uint64_t *rsp, __uint64_t errcode) {
  172. schk_init();
  173. die("fpu overrun", rsp, 9);
  174. schk_leave();
  175. return;
  176. }
  177. void do_invalid_tss(const __uint64_t *rsp, __uint64_t errcode) {
  178. schk_init();
  179. die("invalid tss", rsp, 10);
  180. schk_leave();
  181. return;
  182. }
  183. void do_no_segment(const __uint64_t *rsp, __uint64_t errcode) {
  184. schk_init();
  185. die("no such segment", rsp, 11);
  186. schk_leave();
  187. return;
  188. }
  189. void do_stack(const __uint64_t *rsp, __uint64_t errcode) {
  190. schk_init();
  191. die("stack error", rsp, 12);
  192. schk_leave();
  193. return;
  194. }
  195. void do_gp(const __uint64_t *rsp, __uint64_t errcode) {
  196. schk_init();
  197. die("general protection", rsp, 13);
  198. schk_leave();
  199. return;
  200. }
  201. void do_page_fault(const __uint64_t *rsp, __uint64_t errcode) {
  202. schk_init();
  203. printk(red, "error code: %q\n", errcode);
  204. if (errcode & 1)
  205. print(red, "page protection. \n");
  206. else
  207. print(red, "page not exists. \n");
  208. if (errcode & 2)
  209. print(red, "wr ");
  210. else
  211. print(red, "rd ");
  212. if (errcode & 4)
  213. print(red, "u ");
  214. else
  215. print(red, "k ");
  216. if (errcode & 8)
  217. print(red, "rsvd set ");
  218. if (errcode & 16)
  219. print(red, "instruction");
  220. putc('\n', red);
  221. die("page fault", rsp, 14);
  222. schk_leave();
  223. return;
  224. }
  225. void do_reserved(const __uint64_t *rsp, __uint64_t errcode) {
  226. schk_init();
  227. die("reserved", rsp, 15);
  228. schk_leave();
  229. return;
  230. }
  231. void do_fpu_error(const __uint64_t *rsp, __uint64_t errcode) {
  232. schk_init();
  233. die("fpu error", rsp, 16);
  234. schk_leave();
  235. return;
  236. }
  237. void do_alignment(const __uint64_t *rsp, __uint64_t errcode) {
  238. schk_init();
  239. die("alignment error", rsp, 17);
  240. schk_leave();
  241. return;
  242. }
  243. void do_machine(const __uint64_t *rsp, __uint64_t errcode) {
  244. schk_init();
  245. die("machine check", rsp, 18);
  246. schk_leave();
  247. return;
  248. }
  249. void do_simd(const __uint64_t *rsp, __uint64_t errcode) {
  250. schk_init();
  251. die("simd error", rsp, 19);
  252. schk_leave();
  253. return;
  254. }
  255. void do_virtualization(const __uint64_t *rsp, __uint64_t errcode) {
  256. schk_init();
  257. die("virtualization error", rsp, 20);
  258. schk_leave();
  259. return;
  260. }