irq.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. struct irq_entry irq_table[MAX_IRQ_NR];
  29. void register_irq(int nr, void *entry, const void *handler) {
  30. schk_init();
  31. __uint8_t mask;
  32. if (nr < 8) {
  33. mask = inb(0x21) & ~(1 << nr);
  34. outb(mask, 0x21);
  35. }
  36. else {
  37. /* enable slave-irq */
  38. mask = inb(0x21) & ~(1 << 2);
  39. outb(mask, 0x21);
  40. mask = inb(0xa1) & ~(1 << (nr - 8));
  41. outb(mask, 0xa1);
  42. }
  43. irq_table[nr].handler = handler;
  44. /* IRQ nr starts at 0x20 */
  45. set_int_gate(idt_table + 0x20 + nr, 0, entry);
  46. schk_leave();
  47. return;
  48. }
  49. void unregister_irq(int nr) {
  50. schk_init();
  51. __uint8_t mask;
  52. irq_table[nr].handler = NULL;
  53. if (nr < 8) {
  54. mask = inb(0x21) | 1 << nr;
  55. outb(mask, 0x21);
  56. }
  57. else {
  58. mask = inb(0xa1) | 1 << (nr - 8);
  59. outb(mask, 0xa1);
  60. }
  61. schk_leave();
  62. return;
  63. }
  64. void do_irq(const __uint64_t *rsp, __uint64_t nr) {
  65. schk_init();
  66. void (*fn) (void) = irq_table[nr - 0x20].handler;
  67. if (fn)
  68. fn();
  69. else
  70. printk(red, "unknow irq %x\n", nr - 0x20);
  71. outb(0x20, 0x20);
  72. schk_leave();
  73. return;
  74. }
  75. void trace_irq(void) {
  76. schk_init();
  77. printk(yellow, "tracing irq: \n");
  78. __uint8_t slave = 0, master = 0;
  79. int i;
  80. master = inb(0x21);
  81. slave = inb(0xa1);
  82. printk(yellow, "\tmaster: %b slave: %b\n");
  83. for (i = 0; i < 8; i++)
  84. if (!(master & (1 << i)))
  85. printk(yellow, "\tirq %b (master) enabled. \n", i);
  86. for (i = 0; i < 8; i++)
  87. if (!(slave & (1 << i)))
  88. printk(yellow, "\tirq %b (slave) enabled. \n", i + 8);
  89. schk_leave();
  90. return;
  91. }