2 * Copyright (C) 2002 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
14 #include "kern_constants.h"
15 #include "kern_util.h"
20 #include "um_malloc.h"
24 * Protected by sigio_lock(), also used by sigio_cleanup, which is an
27 static int write_sigio_pid = -1;
28 static unsigned long write_sigio_stack;
31 * These arrays are initialized before the sigio thread is started, and
32 * the descriptors closed after it is killed. So, it can't see them change.
33 * On the UML side, they are changed under the sigio_lock.
35 #define SIGIO_FDS_INIT {-1, -1}
37 static int write_sigio_fds[2] = SIGIO_FDS_INIT;
38 static int sigio_private[2] = SIGIO_FDS_INIT;
47 * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
48 * synchronizes with it.
50 static struct pollfds current_poll;
51 static struct pollfds next_poll;
52 static struct pollfds all_sigio_fds;
54 static int write_sigio_thread(void *unused)
56 struct pollfds *fds, tmp;
61 signal(SIGWINCH, SIG_IGN);
64 n = poll(fds->poll, fds->used, -1);
68 printk(UM_KERN_ERR "write_sigio_thread : poll returned "
69 "%d, errno = %d\n", n, errno);
71 for (i = 0; i < fds->used; i++) {
75 if (p->fd == sigio_private[1]) {
76 CATCH_EINTR(n = read(sigio_private[1], &c,
80 "write_sigio_thread : "
81 "read on socket failed, "
84 current_poll = next_poll;
86 respond_fd = sigio_private[1];
89 respond_fd = write_sigio_fds[1];
91 memmove(&fds->poll[i], &fds->poll[i + 1],
92 (fds->used - i) * sizeof(*fds->poll));
95 CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
97 printk(UM_KERN_ERR "write_sigio_thread : "
98 "write on socket failed, err = %d\n",
106 static int need_poll(struct pollfds *polls, int n)
110 if (n <= polls->size)
113 new = uml_kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
115 printk(UM_KERN_ERR "need_poll : failed to allocate new "
120 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
129 * Must be called with sigio_lock held, because it's needed by the marked
132 static void update_thread(void)
138 flags = set_signals(0);
139 CATCH_EINTR(n = write(sigio_private[0], &c, sizeof(c)));
140 if (n != sizeof(c)) {
141 printk(UM_KERN_ERR "update_thread : write failed, err = %d\n",
146 CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
147 if (n != sizeof(c)) {
148 printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
156 /* Critical section start */
157 if (write_sigio_pid != -1) {
158 os_kill_process(write_sigio_pid, 1);
159 free_stack(write_sigio_stack, 0);
161 write_sigio_pid = -1;
162 close(sigio_private[0]);
163 close(sigio_private[1]);
164 close(write_sigio_fds[0]);
165 close(write_sigio_fds[1]);
166 /* Critical section end */
170 int add_sigio_fd(int fd)
176 for (i = 0; i < all_sigio_fds.used; i++) {
177 if (all_sigio_fds.poll[i].fd == fd)
180 if (i == all_sigio_fds.used)
183 p = &all_sigio_fds.poll[i];
185 for (i = 0; i < current_poll.used; i++) {
186 if (current_poll.poll[i].fd == fd)
190 n = current_poll.used;
191 err = need_poll(&next_poll, n + 1);
195 memcpy(next_poll.poll, current_poll.poll,
196 current_poll.used * sizeof(struct pollfd));
197 next_poll.poll[n] = *p;
198 next_poll.used = n + 1;
205 int ignore_sigio_fd(int fd)
208 int err = 0, i, n = 0;
211 * This is called from exitcalls elsewhere in UML - if
212 * sigio_cleanup has already run, then update_thread will hang
213 * or fail because the thread is no longer running.
215 if (write_sigio_pid == -1)
219 for (i = 0; i < current_poll.used; i++) {
220 if (current_poll.poll[i].fd == fd)
223 if (i == current_poll.used)
226 err = need_poll(&next_poll, current_poll.used - 1);
230 for (i = 0; i < current_poll.used; i++) {
231 p = ¤t_poll.poll[i];
233 next_poll.poll[n++] = *p;
235 next_poll.used = current_poll.used - 1;
243 static struct pollfd *setup_initial_poll(int fd)
247 p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
249 printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
253 *p = ((struct pollfd) { .fd = fd,
259 static void write_sigio_workaround(void)
263 int l_write_sigio_fds[2];
264 int l_sigio_private[2];
265 int l_write_sigio_pid;
267 /* We call this *tons* of times - and most ones we must just fail. */
269 l_write_sigio_pid = write_sigio_pid;
272 if (l_write_sigio_pid != -1)
275 err = os_pipe(l_write_sigio_fds, 1, 1);
277 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
281 err = os_pipe(l_sigio_private, 1, 1);
283 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
288 p = setup_initial_poll(l_sigio_private[1]);
295 * Did we race? Don't try to optimize this, please, it's not so likely
296 * to happen, and no more than once at the boot.
298 if (write_sigio_pid != -1)
301 current_poll = ((struct pollfds) { .poll = p,
305 if (write_sigio_irq(l_write_sigio_fds[0]))
308 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
309 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
311 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
312 CLONE_FILES | CLONE_VM,
315 if (write_sigio_pid < 0)
322 write_sigio_pid = -1;
323 write_sigio_fds[0] = -1;
324 write_sigio_fds[1] = -1;
325 sigio_private[0] = -1;
326 sigio_private[1] = -1;
328 current_poll = ((struct pollfds) { .poll = NULL,
335 close(l_sigio_private[0]);
336 close(l_sigio_private[1]);
338 close(l_write_sigio_fds[0]);
339 close(l_write_sigio_fds[1]);
342 void sigio_broken(int fd, int read)
346 write_sigio_workaround();
349 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
351 printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
352 "for descriptor %d\n", fd);
356 all_sigio_fds.poll[all_sigio_fds.used++] =
357 ((struct pollfd) { .fd = fd,
358 .events = read ? POLLIN : POLLOUT,
364 /* Changed during early boot */
365 static int pty_output_sigio;
366 static int pty_close_sigio;
368 void maybe_sigio_broken(int fd, int read)
373 if ((read || pty_output_sigio) && (!read || pty_close_sigio))
376 sigio_broken(fd, read);
379 static void sigio_cleanup(void)
381 if (write_sigio_pid == -1)
384 os_kill_process(write_sigio_pid, 1);
385 free_stack(write_sigio_stack, 0);
386 write_sigio_pid = -1;
389 __uml_exitcall(sigio_cleanup);
391 /* Used as a flag during SIGIO testing early in boot */
392 static int got_sigio;
394 static void __init handler(int sig)
405 static void openpty_cb(void *arg)
407 struct openpty_arg *info = arg;
410 if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
414 static int async_pty(int master, int slave)
418 flags = fcntl(master, F_GETFL);
422 if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
423 (fcntl(master, F_SETOWN, os_getpid()) < 0))
426 if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
432 static void __init check_one_sigio(void (*proc)(int, int))
434 struct sigaction old, new;
435 struct openpty_arg pty = { .master = -1, .slave = -1 };
436 int master, slave, err;
438 initial_thread_cb(openpty_cb, &pty);
440 printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
448 if ((master == -1) || (slave == -1)) {
449 printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
454 /* Not now, but complain so we now where we failed. */
457 printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
462 err = async_pty(master, slave);
464 printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
469 if (sigaction(SIGIO, NULL, &old) < 0) {
470 printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
471 "errno = %d\n", errno);
476 new.sa_handler = handler;
477 if (sigaction(SIGIO, &new, NULL) < 0) {
478 printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
479 "errno = %d\n", errno);
484 (*proc)(master, slave);
489 if (sigaction(SIGIO, &old, NULL) < 0)
490 printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
491 "errno = %d\n", errno);
494 static void tty_output(int master, int slave)
499 printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
501 memset(buf, 0, sizeof(buf));
503 while (write(master, buf, sizeof(buf)) > 0) ;
505 printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
507 while (((n = read(slave, buf, sizeof(buf))) > 0) &&
508 !({ barrier(); got_sigio; }))
512 printk(UM_KERN_CONT "Yes\n");
513 pty_output_sigio = 1;
514 } else if (n == -EAGAIN)
515 printk(UM_KERN_CONT "No, enabling workaround\n");
517 printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
520 static void tty_close(int master, int slave)
522 printk(UM_KERN_INFO "Checking that host ptys support SIGIO on "
527 printk(UM_KERN_CONT "Yes\n");
530 printk(UM_KERN_CONT "No, enabling workaround\n");
533 void __init check_sigio(void)
535 if ((access("/dev/ptmx", R_OK) < 0) &&
536 (access("/dev/ptyp0", R_OK) < 0)) {
537 printk(UM_KERN_WARNING "No pseudo-terminals available - "
538 "skipping pty SIGIO check\n");
541 check_one_sigio(tty_output);
542 check_one_sigio(tty_close);
545 /* Here because it only does the SIGIO testing for now */
546 void __init os_check_bugs(void)