irq.c 3.1 KB

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