int.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <arch/x86/int.h>
  24. #include <arch/io.h>
  25. #include <arch/sys.h>
  26. #include <modules/tty.h>
  27. int init_int(void) {
  28. /* init PIC */
  29. /* disable all irqs */
  30. /* IMR: interrupt mask register */
  31. outb(0xff, PIC0_IMR);
  32. outb(0xff, PIC1_IMR);
  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. /* enable all interrupts */
  43. outb(0x00, PIC0_IMR);
  44. outb(0x00, PIC1_IMR);
  45. set_int_gate(idt_table + 0x00, 0, divide_error);
  46. set_int_gate(idt_table + 0x80, 0, syscall);
  47. return 0;
  48. }
  49. void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
  50. #define handler ((__uint64_t) addr)
  51. entry->selector = KERNEL_CS;
  52. entry->ist = ist;
  53. entry->attr = attr;
  54. entry->off_0_16 = handler & 0xffff;
  55. entry->off_16_32 = handler >> 16;
  56. entry->off_32_64 = handler >> 32;
  57. entry->zero = 0x00000000;
  58. return;
  59. }
  60. void syscall(void) {
  61. print(0x04, 0x00, "int #0x80: system call. \n");
  62. for (int i;i<200;i++){
  63. print(i, 0x00, "int #0x80: system call. \n");
  64. };
  65. while(1);
  66. }
  67. void die(char *s, __uint64_t *rsp, char nr) {
  68. print(0x04, 0x00, "int #%b: %s. \n");
  69. for (;;);
  70. }
  71. void do_divide_error(__uint64_t *rsp) {
  72. die("divide error", rsp, 0);
  73. return;
  74. }