blob: a26e0662aa123dacdec44ead98cef1a15f191c30 [file] [log] [blame]
Jeff Dike63ae2a92006-03-27 01:14:30 -08001/*
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 Dike63ae2a92006-03-27 01:14:30 -080014#include "user.h"
15#include "process.h"
16#include "sigio.h"
17#include "irq_user.h"
18#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070019#include "um_malloc.h"
Jeff Dike63ae2a92006-03-27 01:14:30 -080020
Jeff Dikef2e62992007-02-10 01:44:23 -080021/*
22 * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd
23 * and os_free_irq_by_cb, which are called under irq_lock.
24 */
Jeff Dike63ae2a92006-03-27 01:14:30 -080025static struct pollfd *pollfds = NULL;
26static int pollfds_num = 0;
27static int pollfds_size = 0;
28
29int os_waiting_for_events(struct irq_fd *active_fds)
30{
31 struct irq_fd *irq_fd;
32 int i, n, err;
33
34 n = poll(pollfds, pollfds_num, 0);
Jesper Juhl191ef962006-05-01 12:15:57 -070035 if (n < 0) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080036 err = -errno;
Jesper Juhl191ef962006-05-01 12:15:57 -070037 if (errno != EINTR)
Jeff Dike63ae2a92006-03-27 01:14:30 -080038 printk("sigio_handler: os_waiting_for_events:"
39 " poll returned %d, errno = %d\n", n, errno);
40 return err;
41 }
42
Jesper Juhl191ef962006-05-01 12:15:57 -070043 if (n == 0)
Jeff Dike63ae2a92006-03-27 01:14:30 -080044 return 0;
45
46 irq_fd = active_fds;
47
Jesper Juhl191ef962006-05-01 12:15:57 -070048 for (i = 0; i < pollfds_num; i++) {
49 if (pollfds[i].revents != 0) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080050 irq_fd->current_events = pollfds[i].revents;
51 pollfds[i].fd = -1;
52 }
53 irq_fd = irq_fd->next;
54 }
55 return n;
56}
57
Jeff Dike63ae2a92006-03-27 01:14:30 -080058int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds)
59{
60 if (pollfds_num == pollfds_size) {
61 if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) {
62 /* return min size needed for new pollfds area */
Jeff Dikef2e62992007-02-10 01:44:23 -080063 return (pollfds_size + 1) * sizeof(pollfds[0]);
Jeff Dike63ae2a92006-03-27 01:14:30 -080064 }
65
Jesper Juhl191ef962006-05-01 12:15:57 -070066 if (pollfds != NULL) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080067 memcpy(tmp_pfd, pollfds,
68 sizeof(pollfds[0]) * pollfds_size);
69 /* remove old pollfds */
70 kfree(pollfds);
71 }
72 pollfds = tmp_pfd;
73 pollfds_size++;
Jesper Juhl191ef962006-05-01 12:15:57 -070074 } else
75 kfree(tmp_pfd); /* remove not used tmp_pfd */
Jeff Dike63ae2a92006-03-27 01:14:30 -080076
Jesper Juhl191ef962006-05-01 12:15:57 -070077 pollfds[pollfds_num] = ((struct pollfd) { .fd = fd,
78 .events = events,
79 .revents = 0 });
Jeff Dike63ae2a92006-03-27 01:14:30 -080080 pollfds_num++;
81
Jesper Juhl191ef962006-05-01 12:15:57 -070082 return 0;
Jeff Dike63ae2a92006-03-27 01:14:30 -080083}
84
85void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg,
86 struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2)
87{
88 struct irq_fd **prev;
89 int i = 0;
90
91 prev = &active_fds;
Jesper Juhl191ef962006-05-01 12:15:57 -070092 while (*prev != NULL) {
93 if ((*test)(*prev, arg)) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080094 struct irq_fd *old_fd = *prev;
Jesper Juhl191ef962006-05-01 12:15:57 -070095 if ((pollfds[i].fd != -1) &&
96 (pollfds[i].fd != (*prev)->fd)) {
Jeff Dike63ae2a92006-03-27 01:14:30 -080097 printk("os_free_irq_by_cb - mismatch between "
98 "active_fds and pollfds, fd %d vs %d\n",
99 (*prev)->fd, pollfds[i].fd);
100 goto out;
101 }
102
103 pollfds_num--;
104
105 /* This moves the *whole* array after pollfds[i]
106 * (though it doesn't spot as such)!
107 */
Jeff Dike63ae2a92006-03-27 01:14:30 -0800108 memmove(&pollfds[i], &pollfds[i + 1],
109 (pollfds_num - i) * sizeof(pollfds[0]));
110 if(*last_irq_ptr2 == &old_fd->next)
111 *last_irq_ptr2 = prev;
112
113 *prev = (*prev)->next;
114 if(old_fd->type == IRQ_WRITE)
115 ignore_sigio_fd(old_fd->fd);
116 kfree(old_fd);
117 continue;
118 }
119 prev = &(*prev)->next;
120 i++;
121 }
122 out:
123 return;
124}
125
Jeff Dike63ae2a92006-03-27 01:14:30 -0800126int os_get_pollfd(int i)
127{
Jesper Juhl191ef962006-05-01 12:15:57 -0700128 return pollfds[i].fd;
Jeff Dike63ae2a92006-03-27 01:14:30 -0800129}
130
131void os_set_pollfd(int i, int fd)
132{
133 pollfds[i].fd = fd;
134}
135
136void os_set_ioignore(void)
137{
Jeff Dike4b84c692006-09-25 23:33:04 -0700138 signal(SIGIO, SIG_IGN);
Jeff Dike63ae2a92006-03-27 01:14:30 -0800139}
140
141void init_irq_signals(int on_sigstack)
142{
Jeff Dike63ae2a92006-03-27 01:14:30 -0800143 int flags;
144
145 flags = on_sigstack ? SA_ONSTACK : 0;
Jeff Dike63ae2a92006-03-27 01:14:30 -0800146
Jeff Dike63ae2a92006-03-27 01:14:30 -0800147 set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART,
Jeff Dike61b63c52007-10-16 01:27:27 -0700148 SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
Jeff Dike63ae2a92006-03-27 01:14:30 -0800149 signal(SIGWINCH, SIG_IGN);
150}