irq.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /*
  22. * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd
  23. * and os_free_irq_by_cb, which are called under irq_lock.
  24. */
  25. static struct pollfd *pollfds = NULL;
  26. static int pollfds_num = 0;
  27. static int pollfds_size = 0;
  28. int os_waiting_for_events(struct irq_fd *active_fds)
  29. {
  30. struct irq_fd *irq_fd;
  31. int i, n, err;
  32. n = poll(pollfds, pollfds_num, 0);
  33. if (n < 0) {
  34. err = -errno;
  35. if (errno != EINTR)
  36. printk("sigio_handler: os_waiting_for_events:"
  37. " poll returned %d, errno = %d\n", n, errno);
  38. return err;
  39. }
  40. if (n == 0)
  41. return 0;
  42. irq_fd = active_fds;
  43. for (i = 0; i < pollfds_num; i++) {
  44. if (pollfds[i].revents != 0) {
  45. irq_fd->current_events = pollfds[i].revents;
  46. pollfds[i].fd = -1;
  47. }
  48. irq_fd = irq_fd->next;
  49. }
  50. return n;
  51. }
  52. int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
  53. {
  54. if (pollfds_num == pollfds_size) {
  55. if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
  56. /* return min size needed for new pollfds area */
  57. return (pollfds_size + 1) * sizeof(pollfds[0]);
  58. }
  59. if (pollfds != NULL) {
  60. memcpy(tmp_pfd, pollfds,
  61. sizeof(pollfds[0]) * pollfds_size);
  62. /* remove old pollfds */
  63. kfree(pollfds);
  64. }
  65. pollfds = tmp_pfd;
  66. pollfds_size++;
  67. } else
  68. kfree(tmp_pfd); /* remove not used tmp_pfd */
  69. pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
  70. .events = events,
  71. .revents = 0 });
  72. pollfds_num++;
  73. return 0;
  74. }
  75. void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
  76. struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
  77. {
  78. struct irq_fd **prev;
  79. int i = 0;
  80. prev = &active_fds;
  81. while (*prev != NULL) {
  82. if ((*test)(*prev, arg)) {
  83. struct irq_fd *old_fd = *prev;
  84. if ((pollfds[i].fd != -1) &&
  85. (pollfds[i].fd != (*prev)->fd)) {
  86. printk("os_free_irq_by_cb - mismatch between "
  87. "active_fds and pollfds, fd %d vs %d\n",
  88. (*prev)->fd, pollfds[i].fd);
  89. goto out;
  90. }
  91. pollfds_num--;
  92. /* This moves the *whole* array after pollfds[i]
  93. * (though it doesn't spot as such)!
  94. */
  95. memmove(&pollfds[i], &pollfds[i + 1],
  96. (pollfds_num - i) * sizeof(pollfds[0]));
  97. if(*last_irq_ptr2 == &old_fd->next)
  98. *last_irq_ptr2 = prev;
  99. *prev = (*prev)->next;
  100. if(old_fd->type == IRQ_WRITE)
  101. ignore_sigio_fd(old_fd->fd);
  102. kfree(old_fd);
  103. continue;
  104. }
  105. prev = &(*prev)->next;
  106. i++;
  107. }
  108. out:
  109. return;
  110. }
  111. int os_get_pollfd(int i)
  112. {
  113. return pollfds[i].fd;
  114. }
  115. void os_set_pollfd(int i, int fd)
  116. {
  117. pollfds[i].fd = fd;
  118. }
  119. void os_set_ioignore(void)
  120. {
  121. signal(SIGIO, SIG_IGN);
  122. }
  123. void init_irq_signals(int on_sigstack)
  124. {
  125. int flags;
  126. flags = on_sigstack ? SA_ONSTACK : 0;
  127. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  128. flags | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
  129. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  130. flags | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, -1);
  131. set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
  132. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  133. signal(SIGWINCH, SIG_IGN);
  134. }