int.c 7.4 KB

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