irq.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <signal.h>
  9. #include <string.h>
  10. #include <sys/poll.h>
  11. #include <sys/types.h>
  12. #include <sys/time.h>
  13. #include "user_util.h"
  14. #include "kern_util.h"
  15. #include "user.h"
  16. #include "process.h"
  17. #include "sigio.h"
  18. #include "irq_user.h"
  19. #include "os.h"
  20. #include "um_malloc.h"
  21. static struct pollfd *pollfds = NULL;
  22. static int pollfds_num = 0;
  23. static int pollfds_size = 0;
  24. int os_waiting_for_events(struct irq_fd *active_fds)
  25. {
  26. struct irq_fd *irq_fd;
  27. int i, n, err;
  28. n = poll(pollfds, pollfds_num, 0);
  29. if (n < 0) {
  30. err = -errno;
  31. if (errno != EINTR)
  32. printk("sigio_handler: os_waiting_for_events:"
  33. " poll returned %d, errno = %d\n", n, errno);
  34. return err;
  35. }
  36. if (n == 0)
  37. return 0;
  38. irq_fd = active_fds;
  39. for (i = 0; i < pollfds_num; i++) {
  40. if (pollfds[i].revents != 0) {
  41. irq_fd->current_events = pollfds[i].revents;
  42. pollfds[i].fd = -1;
  43. }
  44. irq_fd = irq_fd->next;
  45. }
  46. return n;
  47. }
  48. int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
  49. {
  50. if (pollfds_num == pollfds_size) {
  51. if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
  52. /* return min size needed for new pollfds area */
  53. return((pollfds_size + 1) * sizeof(pollfds[0]));
  54. }
  55. if (pollfds != NULL) {
  56. memcpy(tmp_pfd, pollfds,
  57. sizeof(pollfds[0]) * pollfds_size);
  58. /* remove old pollfds */
  59. kfree(pollfds);
  60. }
  61. pollfds = tmp_pfd;
  62. pollfds_size++;
  63. } else
  64. kfree(tmp_pfd); /* remove not used tmp_pfd */
  65. pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
  66. .events = events,
  67. .revents = 0 });
  68. pollfds_num++;
  69. return 0;
  70. }
  71. void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
  72. struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
  73. {
  74. struct irq_fd **prev;
  75. int i = 0;
  76. prev = &active_fds;
  77. while (*prev != NULL) {
  78. if ((*test)(*prev, arg)) {
  79. struct irq_fd *old_fd = *prev;
  80. if ((pollfds[i].fd != -1) &&
  81. (pollfds[i].fd != (*prev)->fd)) {
  82. printk("os_free_irq_by_cb - mismatch between "
  83. "active_fds and pollfds, fd %d vs %d\n",
  84. (*prev)->fd, pollfds[i].fd);
  85. goto out;
  86. }
  87. pollfds_num--;
  88. /* This moves the *whole* array after pollfds[i]
  89. * (though it doesn't spot as such)!
  90. */
  91. memmove(&pollfds[i], &pollfds[i + 1],
  92. (pollfds_num - i) * sizeof(pollfds[0]));
  93. if(*last_irq_ptr2 == &old_fd->next)
  94. *last_irq_ptr2 = prev;
  95. *prev = (*prev)->next;
  96. if(old_fd->type == IRQ_WRITE)
  97. ignore_sigio_fd(old_fd->fd);
  98. kfree(old_fd);
  99. continue;
  100. }
  101. prev = &(*prev)->next;
  102. i++;
  103. }
  104. out:
  105. return;
  106. }
  107. int os_get_pollfd(int i)
  108. {
  109. return pollfds[i].fd;
  110. }
  111. void os_set_pollfd(int i, int fd)
  112. {
  113. pollfds[i].fd = fd;
  114. }
  115. void os_set_ioignore(void)
  116. {
  117. signal(SIGIO, SIG_IGN);
  118. }
  119. void init_irq_signals(int on_sigstack)
  120. {
  121. int flags;
  122. flags = on_sigstack ? SA_ONSTACK : 0;
  123. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  124. flags | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
  125. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  126. flags | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
  127. set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
  128. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  129. signal(SIGWINCH, SIG_IGN);
  130. }