irq.c 2.9 KB

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