int.c 2.0 KB

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