irq.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <modules/tty.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. mask = inb(0xa1) & ~(1 << (nr-8));
  38. outb(mask, 0xa1);
  39. }
  40. irq_table[nr].handler = handler;
  41. /* IRQ nr starts at 0x20 */
  42. set_int_gate(idt_table + 0x20 + nr, 0, entry);
  43. schk_leave();
  44. return;
  45. }
  46. void unregister_irq(int nr) {
  47. schk_init();
  48. __uint8_t mask;
  49. irq_table[nr].handler = NULL;
  50. if (nr < 8) {
  51. mask = inb(0x21) | 1 << nr;
  52. outb(mask, 0x21);
  53. }
  54. else {
  55. mask = inb(0xa1) | 1 << (nr - 8);
  56. outb(mask, 0xa1);
  57. }
  58. schk_leave();
  59. return;
  60. }
  61. void do_irq(const __uint64_t *rsp, __uint64_t nr) {
  62. schk_init();
  63. void (*fn) (void) = irq_table[nr - 0x20].handler;
  64. if (fn)
  65. fn();
  66. else
  67. printk(red, "unknow irq %x\n", nr - 0x20);
  68. outb(0x20, 0x20);
  69. schk_leave();
  70. return;
  71. }