irq.c 2.8 KB

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