2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
12 #include <sys/types.h>
14 #include "user_util.h"
15 #include "kern_util.h"
18 #include "signal_user.h"
35 static struct irq_fd
*active_fds
= NULL
;
36 static struct irq_fd
**last_irq_ptr
= &active_fds
;
38 static struct pollfd
*pollfds
= NULL
;
39 static int pollfds_num
= 0;
40 static int pollfds_size
= 0;
42 extern int io_count
, intr_count
;
44 void sigio_handler(int sig
, union uml_pt_regs
*regs
)
46 struct irq_fd
*irq_fd
, *next
;
49 if(smp_sigio_handler()) return;
51 n
= poll(pollfds
, pollfds_num
, 0);
53 if(errno
== EINTR
) continue;
54 printk("sigio_handler : poll returned %d, "
55 "errno = %d\n", n
, errno
);
61 for(i
= 0; i
< pollfds_num
; i
++){
62 if(pollfds
[i
].revents
!= 0){
63 irq_fd
->current_events
= pollfds
[i
].revents
;
66 irq_fd
= irq_fd
->next
;
69 for(irq_fd
= active_fds
; irq_fd
!= NULL
; irq_fd
= next
){
71 if(irq_fd
->current_events
!= 0){
72 irq_fd
->current_events
= 0;
73 do_IRQ(irq_fd
->irq
, regs
);
75 /* This is here because the next irq may be
76 * freed in the handler. If a console goes
77 * away, both the read and write irqs will be
78 * freed. After do_IRQ, ->next will point to
80 * Irqs can't be freed inside their handlers,
81 * so the next best thing is to have them
82 * marked as needing freeing, so that they
87 free_irq(irq_fd
->irq
, irq_fd
->id
);
93 int activate_ipi(int fd
, int pid
)
95 return(os_set_fd_async(fd
, pid
));
98 static void maybe_sigio_broken(int fd
, int type
)
101 if((type
== IRQ_WRITE
) && !pty_output_sigio
){
102 write_sigio_workaround();
105 else if((type
== IRQ_READ
) && !pty_close_sigio
){
106 write_sigio_workaround();
112 int activate_fd(int irq
, int fd
, int type
, void *dev_id
)
114 struct pollfd
*tmp_pfd
;
115 struct irq_fd
*new_fd
, *irq_fd
;
117 int pid
, events
, err
, n
, size
;
120 err
= os_set_fd_async(fd
, pid
);
124 new_fd
= um_kmalloc(sizeof(*new_fd
));
129 if(type
== IRQ_READ
) events
= POLLIN
| POLLPRI
;
130 else events
= POLLOUT
;
131 *new_fd
= ((struct irq_fd
) { .next
= NULL
,
141 /* Critical section - locked by a spinlock because this stuff can
142 * be changed from interrupt handlers. The stuff above is done
143 * outside the lock because it allocates memory.
146 /* Actually, it only looks like it can be called from interrupt
147 * context. The culprit is reactivate_fd, which calls
148 * maybe_sigio_broken, which calls write_sigio_workaround,
149 * which calls activate_fd. However, write_sigio_workaround should
150 * only be called once, at boot time. That would make it clear that
151 * this is called only from process context, and can be locked with
155 for(irq_fd
= active_fds
; irq_fd
!= NULL
; irq_fd
= irq_fd
->next
){
156 if((irq_fd
->fd
== fd
) && (irq_fd
->type
== type
)){
157 printk("Registering fd %d twice\n", fd
);
158 printk("Irqs : %d, %d\n", irq_fd
->irq
, irq
);
159 printk("Ids : 0x%x, 0x%x\n", irq_fd
->id
, dev_id
);
165 if(n
== pollfds_size
){
167 /* Here we have to drop the lock in order to call
168 * kmalloc, which might sleep. If something else
169 * came in and changed the pollfds array, we free
170 * the buffer and try again.
173 size
= (pollfds_num
+ 1) * sizeof(pollfds
[0]);
174 tmp_pfd
= um_kmalloc(size
);
178 if(n
== pollfds_size
)
183 memcpy(tmp_pfd
, pollfds
,
184 sizeof(pollfds
[0]) * pollfds_size
);
191 if(type
== IRQ_WRITE
)
194 pollfds
[pollfds_num
] = ((struct pollfd
) { .fd
= fd
,
199 *last_irq_ptr
= new_fd
;
200 last_irq_ptr
= &new_fd
->next
;
204 /* This calls activate_fd, so it has to be outside the critical
207 maybe_sigio_broken(fd
, type
);
218 static void free_irq_by_cb(int (*test
)(struct irq_fd
*, void *), void *arg
)
220 struct irq_fd
**prev
;
226 while(*prev
!= NULL
){
227 if((*test
)(*prev
, arg
)){
228 struct irq_fd
*old_fd
= *prev
;
229 if((pollfds
[i
].fd
!= -1) &&
230 (pollfds
[i
].fd
!= (*prev
)->fd
)){
231 printk("free_irq_by_cb - mismatch between "
232 "active_fds and pollfds, fd %d vs %d\n",
233 (*prev
)->fd
, pollfds
[i
].fd
);
236 memcpy(&pollfds
[i
], &pollfds
[i
+ 1],
237 (pollfds_num
- i
- 1) * sizeof(pollfds
[0]));
239 if(last_irq_ptr
== &old_fd
->next
)
241 *prev
= (*prev
)->next
;
242 if(old_fd
->type
== IRQ_WRITE
)
243 ignore_sigio_fd(old_fd
->fd
);
247 prev
= &(*prev
)->next
;
259 static int same_irq_and_dev(struct irq_fd
*irq
, void *d
)
261 struct irq_and_dev
*data
= d
;
263 return((irq
->irq
== data
->irq
) && (irq
->id
== data
->dev
));
266 void free_irq_by_irq_and_dev(int irq
, void *dev
)
268 struct irq_and_dev data
= ((struct irq_and_dev
) { .irq
= irq
,
271 free_irq_by_cb(same_irq_and_dev
, &data
);
274 static int same_fd(struct irq_fd
*irq
, void *fd
)
276 return(irq
->fd
== *((int *) fd
));
279 void free_irq_by_fd(int fd
)
281 free_irq_by_cb(same_fd
, &fd
);
284 static struct irq_fd
*find_irq_by_fd(int fd
, int irqnum
, int *index_out
)
289 for(irq
=active_fds
; irq
!= NULL
; irq
= irq
->next
){
290 if((irq
->fd
== fd
) && (irq
->irq
== irqnum
)) break;
294 printk("find_irq_by_fd doesn't have descriptor %d\n", fd
);
297 if((pollfds
[i
].fd
!= -1) && (pollfds
[i
].fd
!= fd
)){
298 printk("find_irq_by_fd - mismatch between active_fds and "
299 "pollfds, fd %d vs %d, need %d\n", irq
->fd
,
309 void free_irq_later(int irq
, void *dev_id
)
311 struct irq_fd
*irq_fd
;
315 for(irq_fd
= active_fds
; irq_fd
!= NULL
; irq_fd
= irq_fd
->next
){
316 if((irq_fd
->irq
== irq
) && (irq_fd
->id
== dev_id
))
320 printk("free_irq_later found no irq, irq = %d, "
321 "dev_id = 0x%p\n", irq
, dev_id
);
329 void reactivate_fd(int fd
, int irqnum
)
336 irq
= find_irq_by_fd(fd
, irqnum
, &i
);
342 pollfds
[i
].fd
= irq
->fd
;
346 /* This calls activate_fd, so it has to be outside the critical
349 maybe_sigio_broken(fd
, irq
->type
);
352 void deactivate_fd(int fd
, int irqnum
)
359 irq
= find_irq_by_fd(fd
, irqnum
, &i
);
367 int deactivate_all_fds(void)
372 for(irq
=active_fds
;irq
!= NULL
;irq
= irq
->next
){
373 err
= os_clear_fd_async(irq
->fd
);
381 void forward_ipi(int fd
, int pid
)
385 err
= os_set_owner(fd
, pid
);
387 printk("forward_ipi: set_owner failed, fd = %d, me = %d, "
388 "target = %d, err = %d\n", fd
, os_getpid(), pid
, -err
);
391 void forward_interrupts(int pid
)
398 for(irq
=active_fds
;irq
!= NULL
;irq
= irq
->next
){
399 err
= os_set_owner(irq
->fd
, pid
);
401 /* XXX Just remove the irq rather than
402 * print out an infinite stream of these
404 printk("Failed to forward %d to pid %d, err = %d\n",
413 void init_irq_signals(int on_sigstack
)
418 flags
= on_sigstack
? SA_ONSTACK
: 0;
419 if(timer_irq_inited
) h
= (__sighandler_t
) alarm_handler
;
420 else h
= boot_timer_handler
;
422 set_handler(SIGVTALRM
, h
, flags
| SA_RESTART
,
423 SIGUSR1
, SIGIO
, SIGWINCH
, SIGALRM
, -1);
424 set_handler(SIGIO
, (__sighandler_t
) sig_handler
, flags
| SA_RESTART
,
425 SIGUSR1
, SIGIO
, SIGWINCH
, SIGALRM
, SIGVTALRM
, -1);
426 signal(SIGWINCH
, SIG_IGN
);
430 * Overrides for Emacs so that we follow Linus's tabbing style.
431 * Emacs will notice this stuff at the end of the file and automatically
432 * adjust the settings for this buffer only. This must remain at the end
434 * ---------------------------------------------------------------------------
436 * c-file-style: "linux"