irq.c 3.4 KB

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