1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6 #include <linux/minmax.h>
15 #include <kern_util.h>
19 #include <um_malloc.h>
22 * Protected by sigio_lock(), also used by sigio_cleanup, which is an
25 static int write_sigio_pid = -1;
26 static unsigned long write_sigio_stack;
29 * These arrays are initialized before the sigio thread is started, and
30 * the descriptors closed after it is killed. So, it can't see them change.
31 * On the UML side, they are changed under the sigio_lock.
33 #define SIGIO_FDS_INIT {-1, -1}
35 static int write_sigio_fds[2] = SIGIO_FDS_INIT;
36 static int sigio_private[2] = SIGIO_FDS_INIT;
45 * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
46 * synchronizes with it.
48 static struct pollfds current_poll;
49 static struct pollfds next_poll;
50 static struct pollfds all_sigio_fds;
52 static int write_sigio_thread(void *unused)
59 os_fix_helper_signals();
62 n = poll(fds->poll, fds->used, -1);
66 printk(UM_KERN_ERR "write_sigio_thread : poll returned "
67 "%d, errno = %d\n", n, errno);
69 for (i = 0; i < fds->used; i++) {
73 if (p->fd == sigio_private[1]) {
74 CATCH_EINTR(n = read(sigio_private[1], &c,
78 "write_sigio_thread : "
79 "read on socket failed, "
81 swap(current_poll, next_poll);
82 respond_fd = sigio_private[1];
85 respond_fd = write_sigio_fds[1];
87 memmove(&fds->poll[i], &fds->poll[i + 1],
88 (fds->used - i) * sizeof(*fds->poll));
91 CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
93 printk(UM_KERN_ERR "write_sigio_thread : "
94 "write on socket failed, err = %d\n",
102 static int need_poll(struct pollfds *polls, int n)
106 if (n <= polls->size)
109 new = uml_kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
111 printk(UM_KERN_ERR "need_poll : failed to allocate new "
116 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
125 * Must be called with sigio_lock held, because it's needed by the marked
128 static void update_thread(void)
134 flags = um_set_signals_trace(0);
135 CATCH_EINTR(n = write(sigio_private[0], &c, sizeof(c)));
136 if (n != sizeof(c)) {
137 printk(UM_KERN_ERR "update_thread : write failed, err = %d\n",
142 CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
143 if (n != sizeof(c)) {
144 printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
149 um_set_signals_trace(flags);
152 /* Critical section start */
153 if (write_sigio_pid != -1) {
154 os_kill_process(write_sigio_pid, 1);
155 free_stack(write_sigio_stack, 0);
157 write_sigio_pid = -1;
158 close(sigio_private[0]);
159 close(sigio_private[1]);
160 close(write_sigio_fds[0]);
161 close(write_sigio_fds[1]);
162 /* Critical section end */
163 um_set_signals_trace(flags);
166 int __add_sigio_fd(int fd)
171 for (i = 0; i < all_sigio_fds.used; i++) {
172 if (all_sigio_fds.poll[i].fd == fd)
175 if (i == all_sigio_fds.used)
178 p = &all_sigio_fds.poll[i];
180 for (i = 0; i < current_poll.used; i++) {
181 if (current_poll.poll[i].fd == fd)
185 n = current_poll.used;
186 err = need_poll(&next_poll, n + 1);
190 memcpy(next_poll.poll, current_poll.poll,
191 current_poll.used * sizeof(struct pollfd));
192 next_poll.poll[n] = *p;
193 next_poll.used = n + 1;
200 int add_sigio_fd(int fd)
205 err = __add_sigio_fd(fd);
211 int __ignore_sigio_fd(int fd)
217 * This is called from exitcalls elsewhere in UML - if
218 * sigio_cleanup has already run, then update_thread will hang
219 * or fail because the thread is no longer running.
221 if (write_sigio_pid == -1)
224 for (i = 0; i < current_poll.used; i++) {
225 if (current_poll.poll[i].fd == fd)
228 if (i == current_poll.used)
231 err = need_poll(&next_poll, current_poll.used - 1);
235 for (i = 0; i < current_poll.used; i++) {
236 p = ¤t_poll.poll[i];
238 next_poll.poll[n++] = *p;
240 next_poll.used = current_poll.used - 1;
247 int ignore_sigio_fd(int fd)
252 err = __ignore_sigio_fd(fd);
258 static struct pollfd *setup_initial_poll(int fd)
262 p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
264 printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
268 *p = ((struct pollfd) { .fd = fd,
274 static void write_sigio_workaround(void)
278 int l_write_sigio_fds[2];
279 int l_sigio_private[2];
280 int l_write_sigio_pid;
282 /* We call this *tons* of times - and most ones we must just fail. */
284 l_write_sigio_pid = write_sigio_pid;
287 if (l_write_sigio_pid != -1)
290 err = os_pipe(l_write_sigio_fds, 1, 1);
292 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
296 err = os_pipe(l_sigio_private, 1, 1);
298 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
303 p = setup_initial_poll(l_sigio_private[1]);
310 * Did we race? Don't try to optimize this, please, it's not so likely
311 * to happen, and no more than once at the boot.
313 if (write_sigio_pid != -1)
316 current_poll = ((struct pollfds) { .poll = p,
320 if (write_sigio_irq(l_write_sigio_fds[0]))
323 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
324 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
326 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
327 CLONE_FILES | CLONE_VM,
330 if (write_sigio_pid < 0)
337 write_sigio_pid = -1;
338 write_sigio_fds[0] = -1;
339 write_sigio_fds[1] = -1;
340 sigio_private[0] = -1;
341 sigio_private[1] = -1;
343 current_poll = ((struct pollfds) { .poll = NULL,
350 close(l_sigio_private[0]);
351 close(l_sigio_private[1]);
353 close(l_write_sigio_fds[0]);
354 close(l_write_sigio_fds[1]);
357 void sigio_broken(int fd)
361 write_sigio_workaround();
364 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
366 printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
367 "for descriptor %d\n", fd);
371 all_sigio_fds.poll[all_sigio_fds.used++] =
372 ((struct pollfd) { .fd = fd,
379 /* Changed during early boot */
380 static int pty_output_sigio;
382 void maybe_sigio_broken(int fd)
387 if (pty_output_sigio)
393 static void sigio_cleanup(void)
395 if (write_sigio_pid == -1)
398 os_kill_process(write_sigio_pid, 1);
399 free_stack(write_sigio_stack, 0);
400 write_sigio_pid = -1;
403 __uml_exitcall(sigio_cleanup);
405 /* Used as a flag during SIGIO testing early in boot */
406 static int got_sigio;
408 static void __init handler(int sig)
419 static void openpty_cb(void *arg)
421 struct openpty_arg *info = arg;
424 if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
428 static int async_pty(int master, int slave)
432 flags = fcntl(master, F_GETFL);
436 if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
437 (fcntl(master, F_SETOWN, os_getpid()) < 0))
440 if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
446 static void __init check_one_sigio(void (*proc)(int, int))
448 struct sigaction old, new;
449 struct openpty_arg pty = { .master = -1, .slave = -1 };
450 int master, slave, err;
452 initial_thread_cb(openpty_cb, &pty);
454 printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
462 if ((master == -1) || (slave == -1)) {
463 printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
468 /* Not now, but complain so we now where we failed. */
471 printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
476 err = async_pty(master, slave);
478 printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
483 if (sigaction(SIGIO, NULL, &old) < 0) {
484 printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
485 "errno = %d\n", errno);
490 new.sa_handler = handler;
491 if (sigaction(SIGIO, &new, NULL) < 0) {
492 printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
493 "errno = %d\n", errno);
498 (*proc)(master, slave);
503 if (sigaction(SIGIO, &old, NULL) < 0)
504 printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
505 "errno = %d\n", errno);
508 static void tty_output(int master, int slave)
513 printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
515 memset(buf, 0, sizeof(buf));
517 while (write(master, buf, sizeof(buf)) > 0) ;
519 printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
521 while (((n = read(slave, buf, sizeof(buf))) > 0) &&
522 !({ barrier(); got_sigio; }))
526 printk(UM_KERN_CONT "Yes\n");
527 pty_output_sigio = 1;
528 } else if (n == -EAGAIN)
529 printk(UM_KERN_CONT "No, enabling workaround\n");
531 printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
534 static void __init check_sigio(void)
536 if ((access("/dev/ptmx", R_OK) < 0) &&
537 (access("/dev/ptyp0", R_OK) < 0)) {
538 printk(UM_KERN_WARNING "No pseudo-terminals available - "
539 "skipping pty SIGIO check\n");
542 check_one_sigio(tty_output);
545 /* Here because it only does the SIGIO testing for now */
546 void __init os_check_bugs(void)