irq.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 set_irq(int nr, const void *handler) {
  46. schk_init();
  47. irq_table[nr].handler = handler;
  48. schk_leave();
  49. return;
  50. }
  51. void register_irq(int nr, void *entry, const void *handler) {
  52. schk_init();
  53. __uint8_t mask;
  54. if (nr < 8) {
  55. mask = inb(0x21) & ~(1 << nr);
  56. outb(mask, 0x21);
  57. }
  58. else {
  59. /* enable slave-irq */
  60. mask = inb(0x21) & ~(1 << 2);
  61. outb(mask, 0x21);
  62. mask = inb(0xa1) & ~(1 << (nr - 8));
  63. outb(mask, 0xa1);
  64. }
  65. irq_table[nr].handler = handler;
  66. /* IRQ nr starts at 0x20 */
  67. set_int_gate(idt_table + 0x20 + nr, 0, entry);
  68. schk_leave();
  69. return;
  70. }
  71. void unregister_irq(int nr) {
  72. schk_init();
  73. __uint8_t mask;
  74. irq_table[nr].handler = NULL;
  75. if (nr < 8) {
  76. mask = inb(0x21) | 1 << nr;
  77. outb(mask, 0x21);
  78. }
  79. else {
  80. mask = inb(0xa1) | 1 << (nr - 8);
  81. outb(mask, 0xa1);
  82. }
  83. schk_leave();
  84. return;
  85. }
  86. void do_irq(const __uint64_t *rsp, __uint64_t nr) {
  87. schk_init();
  88. void (*fn) (void) = irq_table[nr - 0x20].handler;
  89. if (fn)
  90. fn();
  91. else
  92. printk(red, "unknow irq %x\n", nr - 0x20);
  93. if (nr > 0x27) {
  94. /* slave-irq ok */
  95. outb(0x62, PIC0_OCW2);
  96. /* this.irqnr ok */
  97. outb(0x60 - 0x28 + nr, PIC1_OCW2);
  98. }
  99. else
  100. outb(0x60 - 0x20 + nr, PIC0_OCW2);
  101. schk_leave();
  102. return;
  103. }
  104. void trace_irq(void) {
  105. schk_init();
  106. printk(yellow, "tracing irq: \n");
  107. __uint8_t slave = 0, master = 0;
  108. int i;
  109. master = inb(0x21);
  110. slave = inb(0xa1);
  111. printk(yellow, "\tmaster: %b slave: %b\n");
  112. for (i = 0; i < 8; i++)
  113. if (!(master & (1 << i)))
  114. printk(yellow, "\tirq %b (master) enabled. \n", i);
  115. for (i = 0; i < 8; i++)
  116. if (!(slave & (1 << i)))
  117. printk(yellow, "\tirq %b (slave) enabled. \n", i + 8);
  118. schk_leave();
  119. return;
  120. }