irq.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/console.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. build_irq(0x22);
  31. build_irq(0x23);
  32. build_irq(0x24);
  33. build_irq(0x25);
  34. build_irq(0x26);
  35. build_irq(0x27);
  36. build_irq(0x28);
  37. build_irq(0x29);
  38. build_irq(0x2a);
  39. build_irq(0x2b);
  40. build_irq(0x2c);
  41. build_irq(0x2d);
  42. build_irq(0x2e);
  43. build_irq(0x2f);
  44. struct irq_entry irq_table[MAX_IRQ_NR];
  45. void register_irq(int nr, void *entry, const void *handler) {
  46. schk_init();
  47. __uint8_t mask;
  48. if (nr < 8) {
  49. mask = inb(0x21) & ~(1 << nr);
  50. outb(mask, 0x21);
  51. }
  52. else {
  53. /* enable slave-irq */
  54. mask = inb(0x21) & ~(1 << 2);
  55. outb(mask, 0x21);
  56. mask = inb(0xa1) & ~(1 << (nr - 8));
  57. outb(mask, 0xa1);
  58. }
  59. irq_table[nr].handler = handler;
  60. /* IRQ nr starts at 0x20 */
  61. set_int_gate(idt_table + 0x20 + nr, 0, entry);
  62. schk_leave();
  63. return;
  64. }
  65. void unregister_irq(int nr) {
  66. schk_init();
  67. __uint8_t mask;
  68. irq_table[nr].handler = NULL;
  69. if (nr < 8) {
  70. mask = inb(0x21) | 1 << nr;
  71. outb(mask, 0x21);
  72. }
  73. else {
  74. mask = inb(0xa1) | 1 << (nr - 8);
  75. outb(mask, 0xa1);
  76. }
  77. schk_leave();
  78. return;
  79. }
  80. void do_irq(const __uint64_t *rsp, __uint64_t nr) {
  81. schk_init();
  82. void (*fn) (void) = irq_table[nr - 0x20].handler;
  83. if (fn)
  84. fn();
  85. else
  86. printk(red, "unknow irq %x\n", nr - 0x20);
  87. if (nr > 0x27) {
  88. /* slave-irq ok */
  89. outb(0x62, PIC0_OCW2);
  90. /* this.irqnr ok */
  91. outb(0x60 - 0x28 + nr, PIC1_OCW2);
  92. }
  93. else
  94. outb(0x60 - 0x20 + nr, PIC0_OCW2);
  95. schk_leave();
  96. return;
  97. }
  98. void trace_irq(void) {
  99. schk_init();
  100. printk(yellow, "tracing irq: \n");
  101. __uint8_t slave = 0, master = 0;
  102. int i;
  103. master = inb(0x21);
  104. slave = inb(0xa1);
  105. printk(yellow, "\tmaster: %b slave: %b\n");
  106. for (i = 0; i < 8; i++)
  107. if (!(master & (1 << i)))
  108. printk(yellow, "\tirq %b (master) enabled. \n", i);
  109. for (i = 0; i < 8; i++)
  110. if (!(slave & (1 << i)))
  111. printk(yellow, "\tirq %b (slave) enabled. \n", i + 8);
  112. schk_leave();
  113. return;
  114. }