Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <signal.h> |
| 10 | #include <string.h> |
| 11 | #include <sys/poll.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/time.h> |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 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" |
Paolo 'Blaisorblade' Giarrusso | c13e569 | 2006-10-19 23:28:20 -0700 | [diff] [blame] | 20 | #include "um_malloc.h" |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 21 | |
Jeff Dike | f2e6299 | 2007-02-10 01:44:23 -0800 | [diff] [blame] | 22 | /* |
| 23 | * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd |
| 24 | * and os_free_irq_by_cb, which are called under irq_lock. |
| 25 | */ |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 26 | static struct pollfd *pollfds = NULL; |
| 27 | static int pollfds_num = 0; |
| 28 | static int pollfds_size = 0; |
| 29 | |
| 30 | int os_waiting_for_events(struct irq_fd *active_fds) |
| 31 | { |
| 32 | struct irq_fd *irq_fd; |
| 33 | int i, n, err; |
| 34 | |
| 35 | n = poll(pollfds, pollfds_num, 0); |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 36 | if (n < 0) { |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 37 | err = -errno; |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 38 | if (errno != EINTR) |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 39 | printk("sigio_handler: os_waiting_for_events:" |
| 40 | " poll returned %d, errno = %d\n", n, errno); |
| 41 | return err; |
| 42 | } |
| 43 | |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 44 | if (n == 0) |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 45 | return 0; |
| 46 | |
| 47 | irq_fd = active_fds; |
| 48 | |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 49 | for (i = 0; i < pollfds_num; i++) { |
| 50 | if (pollfds[i].revents != 0) { |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 51 | irq_fd->current_events = pollfds[i].revents; |
| 52 | pollfds[i].fd = -1; |
| 53 | } |
| 54 | irq_fd = irq_fd->next; |
| 55 | } |
| 56 | return n; |
| 57 | } |
| 58 | |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 59 | int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds) |
| 60 | { |
| 61 | if (pollfds_num == pollfds_size) { |
| 62 | if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) { |
| 63 | /* return min size needed for new pollfds area */ |
Jeff Dike | f2e6299 | 2007-02-10 01:44:23 -0800 | [diff] [blame] | 64 | return (pollfds_size + 1) * sizeof(pollfds[0]); |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 67 | if (pollfds != NULL) { |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 68 | memcpy(tmp_pfd, pollfds, |
| 69 | sizeof(pollfds[0]) * pollfds_size); |
| 70 | /* remove old pollfds */ |
| 71 | kfree(pollfds); |
| 72 | } |
| 73 | pollfds = tmp_pfd; |
| 74 | pollfds_size++; |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 75 | } else |
| 76 | kfree(tmp_pfd); /* remove not used tmp_pfd */ |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 77 | |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 78 | pollfds[pollfds_num] = ((struct pollfd) { .fd = fd, |
| 79 | .events = events, |
| 80 | .revents = 0 }); |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 81 | pollfds_num++; |
| 82 | |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 83 | return 0; |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg, |
| 87 | struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2) |
| 88 | { |
| 89 | struct irq_fd **prev; |
| 90 | int i = 0; |
| 91 | |
| 92 | prev = &active_fds; |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 93 | while (*prev != NULL) { |
| 94 | if ((*test)(*prev, arg)) { |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 95 | struct irq_fd *old_fd = *prev; |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 96 | if ((pollfds[i].fd != -1) && |
| 97 | (pollfds[i].fd != (*prev)->fd)) { |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 98 | printk("os_free_irq_by_cb - mismatch between " |
| 99 | "active_fds and pollfds, fd %d vs %d\n", |
| 100 | (*prev)->fd, pollfds[i].fd); |
| 101 | goto out; |
| 102 | } |
| 103 | |
| 104 | pollfds_num--; |
| 105 | |
| 106 | /* This moves the *whole* array after pollfds[i] |
| 107 | * (though it doesn't spot as such)! |
| 108 | */ |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 109 | memmove(&pollfds[i], &pollfds[i + 1], |
| 110 | (pollfds_num - i) * sizeof(pollfds[0])); |
| 111 | if(*last_irq_ptr2 == &old_fd->next) |
| 112 | *last_irq_ptr2 = prev; |
| 113 | |
| 114 | *prev = (*prev)->next; |
| 115 | if(old_fd->type == IRQ_WRITE) |
| 116 | ignore_sigio_fd(old_fd->fd); |
| 117 | kfree(old_fd); |
| 118 | continue; |
| 119 | } |
| 120 | prev = &(*prev)->next; |
| 121 | i++; |
| 122 | } |
| 123 | out: |
| 124 | return; |
| 125 | } |
| 126 | |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 127 | int os_get_pollfd(int i) |
| 128 | { |
Jesper Juhl | 191ef96 | 2006-05-01 12:15:57 -0700 | [diff] [blame] | 129 | return pollfds[i].fd; |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void os_set_pollfd(int i, int fd) |
| 133 | { |
| 134 | pollfds[i].fd = fd; |
| 135 | } |
| 136 | |
| 137 | void os_set_ioignore(void) |
| 138 | { |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 139 | signal(SIGIO, SIG_IGN); |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void init_irq_signals(int on_sigstack) |
| 143 | { |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 144 | int flags; |
| 145 | |
| 146 | flags = on_sigstack ? SA_ONSTACK : 0; |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 147 | |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 148 | set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART, |
Jeff Dike | 61b63c5 | 2007-10-16 01:27:27 -0700 | [diff] [blame] | 149 | SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); |
Jeff Dike | 63ae2a9 | 2006-03-27 01:14:30 -0800 | [diff] [blame] | 150 | signal(SIGWINCH, SIG_IGN); |
| 151 | } |