2 * Copyright (C) 2002 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
14 #include "kern_util.h"
18 #include "um_malloc.h"
21 * Protected by sigio_lock(), also used by sigio_cleanup, which is an
24 static int write_sigio_pid
= -1;
25 static unsigned long write_sigio_stack
;
28 * These arrays are initialized before the sigio thread is started, and
29 * the descriptors closed after it is killed. So, it can't see them change.
30 * On the UML side, they are changed under the sigio_lock.
32 #define SIGIO_FDS_INIT {-1, -1}
34 static int write_sigio_fds
[2] = SIGIO_FDS_INIT
;
35 static int sigio_private
[2] = SIGIO_FDS_INIT
;
44 * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
45 * synchronizes with it.
47 static struct pollfds current_poll
;
48 static struct pollfds next_poll
;
49 static struct pollfds all_sigio_fds
;
51 static int write_sigio_thread(void *unused
)
53 struct pollfds
*fds
, tmp
;
58 signal(SIGWINCH
, SIG_IGN
);
61 n
= poll(fds
->poll
, fds
->used
, -1);
65 printk(UM_KERN_ERR
"write_sigio_thread : poll returned "
66 "%d, errno = %d\n", n
, errno
);
68 for (i
= 0; i
< fds
->used
; i
++) {
72 if (p
->fd
== sigio_private
[1]) {
73 CATCH_EINTR(n
= read(sigio_private
[1], &c
,
77 "write_sigio_thread : "
78 "read on socket failed, "
81 current_poll
= next_poll
;
83 respond_fd
= sigio_private
[1];
86 respond_fd
= write_sigio_fds
[1];
88 memmove(&fds
->poll
[i
], &fds
->poll
[i
+ 1],
89 (fds
->used
- i
) * sizeof(*fds
->poll
));
92 CATCH_EINTR(n
= write(respond_fd
, &c
, sizeof(c
)));
94 printk(UM_KERN_ERR
"write_sigio_thread : "
95 "write on socket failed, err = %d\n",
103 static int need_poll(struct pollfds
*polls
, int n
)
107 if (n
<= polls
->size
)
110 new = uml_kmalloc(n
* sizeof(struct pollfd
), UM_GFP_ATOMIC
);
112 printk(UM_KERN_ERR
"need_poll : failed to allocate new "
117 memcpy(new, polls
->poll
, polls
->used
* sizeof(struct pollfd
));
126 * Must be called with sigio_lock held, because it's needed by the marked
129 static void update_thread(void)
135 flags
= set_signals(0);
136 CATCH_EINTR(n
= write(sigio_private
[0], &c
, sizeof(c
)));
137 if (n
!= sizeof(c
)) {
138 printk(UM_KERN_ERR
"update_thread : write failed, err = %d\n",
143 CATCH_EINTR(n
= read(sigio_private
[0], &c
, sizeof(c
)));
144 if (n
!= sizeof(c
)) {
145 printk(UM_KERN_ERR
"update_thread : read failed, err = %d\n",
153 /* Critical section start */
154 if (write_sigio_pid
!= -1) {
155 os_kill_process(write_sigio_pid
, 1);
156 free_stack(write_sigio_stack
, 0);
158 write_sigio_pid
= -1;
159 close(sigio_private
[0]);
160 close(sigio_private
[1]);
161 close(write_sigio_fds
[0]);
162 close(write_sigio_fds
[1]);
163 /* Critical section end */
167 int add_sigio_fd(int fd
)
173 for (i
= 0; i
< all_sigio_fds
.used
; i
++) {
174 if (all_sigio_fds
.poll
[i
].fd
== fd
)
177 if (i
== all_sigio_fds
.used
)
180 p
= &all_sigio_fds
.poll
[i
];
182 for (i
= 0; i
< current_poll
.used
; i
++) {
183 if (current_poll
.poll
[i
].fd
== fd
)
187 n
= current_poll
.used
;
188 err
= need_poll(&next_poll
, n
+ 1);
192 memcpy(next_poll
.poll
, current_poll
.poll
,
193 current_poll
.used
* sizeof(struct pollfd
));
194 next_poll
.poll
[n
] = *p
;
195 next_poll
.used
= n
+ 1;
202 int ignore_sigio_fd(int fd
)
205 int err
= 0, i
, n
= 0;
208 * This is called from exitcalls elsewhere in UML - if
209 * sigio_cleanup has already run, then update_thread will hang
210 * or fail because the thread is no longer running.
212 if (write_sigio_pid
== -1)
216 for (i
= 0; i
< current_poll
.used
; i
++) {
217 if (current_poll
.poll
[i
].fd
== fd
)
220 if (i
== current_poll
.used
)
223 err
= need_poll(&next_poll
, current_poll
.used
- 1);
227 for (i
= 0; i
< current_poll
.used
; i
++) {
228 p
= ¤t_poll
.poll
[i
];
230 next_poll
.poll
[n
++] = *p
;
232 next_poll
.used
= current_poll
.used
- 1;
240 static struct pollfd
*setup_initial_poll(int fd
)
244 p
= uml_kmalloc(sizeof(struct pollfd
), UM_GFP_KERNEL
);
246 printk(UM_KERN_ERR
"setup_initial_poll : failed to allocate "
250 *p
= ((struct pollfd
) { .fd
= fd
,
256 static void write_sigio_workaround(void)
260 int l_write_sigio_fds
[2];
261 int l_sigio_private
[2];
262 int l_write_sigio_pid
;
264 /* We call this *tons* of times - and most ones we must just fail. */
266 l_write_sigio_pid
= write_sigio_pid
;
269 if (l_write_sigio_pid
!= -1)
272 err
= os_pipe(l_write_sigio_fds
, 1, 1);
274 printk(UM_KERN_ERR
"write_sigio_workaround - os_pipe 1 failed, "
278 err
= os_pipe(l_sigio_private
, 1, 1);
280 printk(UM_KERN_ERR
"write_sigio_workaround - os_pipe 2 failed, "
285 p
= setup_initial_poll(l_sigio_private
[1]);
292 * Did we race? Don't try to optimize this, please, it's not so likely
293 * to happen, and no more than once at the boot.
295 if (write_sigio_pid
!= -1)
298 current_poll
= ((struct pollfds
) { .poll
= p
,
302 if (write_sigio_irq(l_write_sigio_fds
[0]))
305 memcpy(write_sigio_fds
, l_write_sigio_fds
, sizeof(l_write_sigio_fds
));
306 memcpy(sigio_private
, l_sigio_private
, sizeof(l_sigio_private
));
308 write_sigio_pid
= run_helper_thread(write_sigio_thread
, NULL
,
309 CLONE_FILES
| CLONE_VM
,
312 if (write_sigio_pid
< 0)
319 write_sigio_pid
= -1;
320 write_sigio_fds
[0] = -1;
321 write_sigio_fds
[1] = -1;
322 sigio_private
[0] = -1;
323 sigio_private
[1] = -1;
325 current_poll
= ((struct pollfds
) { .poll
= NULL
,
332 close(l_sigio_private
[0]);
333 close(l_sigio_private
[1]);
335 close(l_write_sigio_fds
[0]);
336 close(l_write_sigio_fds
[1]);
339 void sigio_broken(int fd
, int read
)
343 write_sigio_workaround();
346 err
= need_poll(&all_sigio_fds
, all_sigio_fds
.used
+ 1);
348 printk(UM_KERN_ERR
"maybe_sigio_broken - failed to add pollfd "
349 "for descriptor %d\n", fd
);
353 all_sigio_fds
.poll
[all_sigio_fds
.used
++] =
354 ((struct pollfd
) { .fd
= fd
,
355 .events
= read
? POLLIN
: POLLOUT
,
361 /* Changed during early boot */
362 static int pty_output_sigio
;
363 static int pty_close_sigio
;
365 void maybe_sigio_broken(int fd
, int read
)
370 if ((read
|| pty_output_sigio
) && (!read
|| pty_close_sigio
))
373 sigio_broken(fd
, read
);
376 static void sigio_cleanup(void)
378 if (write_sigio_pid
== -1)
381 os_kill_process(write_sigio_pid
, 1);
382 free_stack(write_sigio_stack
, 0);
383 write_sigio_pid
= -1;
386 __uml_exitcall(sigio_cleanup
);
388 /* Used as a flag during SIGIO testing early in boot */
389 static int got_sigio
;
391 static void __init
handler(int sig
)
402 static void openpty_cb(void *arg
)
404 struct openpty_arg
*info
= arg
;
407 if (openpty(&info
->master
, &info
->slave
, NULL
, NULL
, NULL
))
411 static int async_pty(int master
, int slave
)
415 flags
= fcntl(master
, F_GETFL
);
419 if ((fcntl(master
, F_SETFL
, flags
| O_NONBLOCK
| O_ASYNC
) < 0) ||
420 (fcntl(master
, F_SETOWN
, os_getpid()) < 0))
423 if ((fcntl(slave
, F_SETFL
, flags
| O_NONBLOCK
) < 0))
429 static void __init
check_one_sigio(void (*proc
)(int, int))
431 struct sigaction old
, new;
432 struct openpty_arg pty
= { .master
= -1, .slave
= -1 };
433 int master
, slave
, err
;
435 initial_thread_cb(openpty_cb
, &pty
);
437 printk(UM_KERN_ERR
"check_one_sigio failed, errno = %d\n",
445 if ((master
== -1) || (slave
== -1)) {
446 printk(UM_KERN_ERR
"check_one_sigio failed to allocate a "
451 /* Not now, but complain so we now where we failed. */
454 printk(UM_KERN_ERR
"check_one_sigio : raw failed, errno = %d\n",
459 err
= async_pty(master
, slave
);
461 printk(UM_KERN_ERR
"check_one_sigio : sigio_async failed, "
466 if (sigaction(SIGIO
, NULL
, &old
) < 0) {
467 printk(UM_KERN_ERR
"check_one_sigio : sigaction 1 failed, "
468 "errno = %d\n", errno
);
473 new.sa_handler
= handler
;
474 if (sigaction(SIGIO
, &new, NULL
) < 0) {
475 printk(UM_KERN_ERR
"check_one_sigio : sigaction 2 failed, "
476 "errno = %d\n", errno
);
481 (*proc
)(master
, slave
);
486 if (sigaction(SIGIO
, &old
, NULL
) < 0)
487 printk(UM_KERN_ERR
"check_one_sigio : sigaction 3 failed, "
488 "errno = %d\n", errno
);
491 static void tty_output(int master
, int slave
)
496 printk(UM_KERN_INFO
"Checking that host ptys support output SIGIO...");
498 memset(buf
, 0, sizeof(buf
));
500 while (write(master
, buf
, sizeof(buf
)) > 0) ;
502 printk(UM_KERN_ERR
"tty_output : write failed, errno = %d\n",
504 while (((n
= read(slave
, buf
, sizeof(buf
))) > 0) &&
505 !({ barrier(); got_sigio
; }))
509 printk(UM_KERN_CONT
"Yes\n");
510 pty_output_sigio
= 1;
511 } else if (n
== -EAGAIN
)
512 printk(UM_KERN_CONT
"No, enabling workaround\n");
514 printk(UM_KERN_CONT
"tty_output : read failed, err = %d\n", n
);
517 static void tty_close(int master
, int slave
)
519 printk(UM_KERN_INFO
"Checking that host ptys support SIGIO on "
524 printk(UM_KERN_CONT
"Yes\n");
527 printk(UM_KERN_CONT
"No, enabling workaround\n");
530 static void __init
check_sigio(void)
532 if ((access("/dev/ptmx", R_OK
) < 0) &&
533 (access("/dev/ptyp0", R_OK
) < 0)) {
534 printk(UM_KERN_WARNING
"No pseudo-terminals available - "
535 "skipping pty SIGIO check\n");
538 check_one_sigio(tty_output
);
539 check_one_sigio(tty_close
);
542 /* Here because it only does the SIGIO testing for now */
543 void __init
os_check_bugs(void)