2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
14 #include "kern_constants.h"
15 #include "kern_util.h"
19 #include "um_malloc.h"
23 * Protected by sigio_lock(), also used by sigio_cleanup, which is an
26 static int write_sigio_pid
= -1;
27 static unsigned long write_sigio_stack
;
30 * These arrays are initialized before the sigio thread is started, and
31 * the descriptors closed after it is killed. So, it can't see them change.
32 * On the UML side, they are changed under the sigio_lock.
34 #define SIGIO_FDS_INIT {-1, -1}
36 static int write_sigio_fds
[2] = SIGIO_FDS_INIT
;
37 static int sigio_private
[2] = SIGIO_FDS_INIT
;
46 * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
47 * synchronizes with it.
49 static struct pollfds current_poll
;
50 static struct pollfds next_poll
;
51 static struct pollfds all_sigio_fds
;
53 static int write_sigio_thread(void *unused
)
55 struct pollfds
*fds
, tmp
;
60 signal(SIGWINCH
, SIG_IGN
);
63 n
= poll(fds
->poll
, fds
->used
, -1);
67 printk(UM_KERN_ERR
"write_sigio_thread : poll returned "
68 "%d, errno = %d\n", n
, errno
);
70 for (i
= 0; i
< fds
->used
; i
++) {
74 if (p
->fd
== sigio_private
[1]) {
75 CATCH_EINTR(n
= read(sigio_private
[1], &c
,
79 "write_sigio_thread : "
80 "read on socket failed, "
83 current_poll
= next_poll
;
85 respond_fd
= sigio_private
[1];
88 respond_fd
= write_sigio_fds
[1];
90 memmove(&fds
->poll
[i
], &fds
->poll
[i
+ 1],
91 (fds
->used
- i
) * sizeof(*fds
->poll
));
94 CATCH_EINTR(n
= write(respond_fd
, &c
, sizeof(c
)));
96 printk(UM_KERN_ERR
"write_sigio_thread : "
97 "write on socket failed, err = %d\n",
105 static int need_poll(struct pollfds
*polls
, int n
)
109 if (n
<= polls
->size
)
112 new = kmalloc(n
* sizeof(struct pollfd
), UM_GFP_ATOMIC
);
114 printk(UM_KERN_ERR
"need_poll : failed to allocate new "
119 memcpy(new, polls
->poll
, polls
->used
* sizeof(struct pollfd
));
128 * Must be called with sigio_lock held, because it's needed by the marked
131 static void update_thread(void)
137 flags
= set_signals(0);
138 CATCH_EINTR(n
= write(sigio_private
[0], &c
, sizeof(c
)));
139 if (n
!= sizeof(c
)) {
140 printk(UM_KERN_ERR
"update_thread : write failed, err = %d\n",
145 CATCH_EINTR(n
= read(sigio_private
[0], &c
, sizeof(c
)));
146 if (n
!= sizeof(c
)) {
147 printk(UM_KERN_ERR
"update_thread : read failed, err = %d\n",
155 /* Critical section start */
156 if (write_sigio_pid
!= -1) {
157 os_kill_process(write_sigio_pid
, 1);
158 free_stack(write_sigio_stack
, 0);
160 write_sigio_pid
= -1;
161 close(sigio_private
[0]);
162 close(sigio_private
[1]);
163 close(write_sigio_fds
[0]);
164 close(write_sigio_fds
[1]);
165 /* Critical section end */
169 int add_sigio_fd(int fd
)
175 for (i
= 0; i
< all_sigio_fds
.used
; i
++) {
176 if (all_sigio_fds
.poll
[i
].fd
== fd
)
179 if (i
== all_sigio_fds
.used
)
182 p
= &all_sigio_fds
.poll
[i
];
184 for (i
= 0; i
< current_poll
.used
; i
++) {
185 if (current_poll
.poll
[i
].fd
== fd
)
189 n
= current_poll
.used
;
190 err
= need_poll(&next_poll
, n
+ 1);
194 memcpy(next_poll
.poll
, current_poll
.poll
,
195 current_poll
.used
* sizeof(struct pollfd
));
196 next_poll
.poll
[n
] = *p
;
197 next_poll
.used
= n
+ 1;
204 int ignore_sigio_fd(int fd
)
207 int err
= 0, i
, n
= 0;
210 * This is called from exitcalls elsewhere in UML - if
211 * sigio_cleanup has already run, then update_thread will hang
212 * or fail because the thread is no longer running.
214 if (write_sigio_pid
== -1)
218 for (i
= 0; i
< current_poll
.used
; i
++) {
219 if (current_poll
.poll
[i
].fd
== fd
)
222 if (i
== current_poll
.used
)
225 err
= need_poll(&next_poll
, current_poll
.used
- 1);
229 for (i
= 0; i
< current_poll
.used
; i
++) {
230 p
= ¤t_poll
.poll
[i
];
232 next_poll
.poll
[n
++] = *p
;
234 next_poll
.used
= current_poll
.used
- 1;
242 static struct pollfd
*setup_initial_poll(int fd
)
246 p
= kmalloc(sizeof(struct pollfd
), UM_GFP_KERNEL
);
248 printk(UM_KERN_ERR
"setup_initial_poll : failed to allocate "
252 *p
= ((struct pollfd
) { .fd
= fd
,
258 static void write_sigio_workaround(void)
262 int l_write_sigio_fds
[2];
263 int l_sigio_private
[2];
264 int l_write_sigio_pid
;
266 /* We call this *tons* of times - and most ones we must just fail. */
268 l_write_sigio_pid
= write_sigio_pid
;
271 if (l_write_sigio_pid
!= -1)
274 err
= os_pipe(l_write_sigio_fds
, 1, 1);
276 printk(UM_KERN_ERR
"write_sigio_workaround - os_pipe 1 failed, "
280 err
= os_pipe(l_sigio_private
, 1, 1);
282 printk(UM_KERN_ERR
"write_sigio_workaround - os_pipe 2 failed, "
287 p
= setup_initial_poll(l_sigio_private
[1]);
294 * Did we race? Don't try to optimize this, please, it's not so likely
295 * to happen, and no more than once at the boot.
297 if (write_sigio_pid
!= -1)
300 current_poll
= ((struct pollfds
) { .poll
= p
,
304 if (write_sigio_irq(l_write_sigio_fds
[0]))
307 memcpy(write_sigio_fds
, l_write_sigio_fds
, sizeof(l_write_sigio_fds
));
308 memcpy(sigio_private
, l_sigio_private
, sizeof(l_sigio_private
));
310 write_sigio_pid
= run_helper_thread(write_sigio_thread
, NULL
,
311 CLONE_FILES
| CLONE_VM
,
314 if (write_sigio_pid
< 0)
321 write_sigio_pid
= -1;
322 write_sigio_fds
[0] = -1;
323 write_sigio_fds
[1] = -1;
324 sigio_private
[0] = -1;
325 sigio_private
[1] = -1;
327 current_poll
= ((struct pollfds
) { .poll
= NULL
,
334 close(l_sigio_private
[0]);
335 close(l_sigio_private
[1]);
337 close(l_write_sigio_fds
[0]);
338 close(l_write_sigio_fds
[1]);
341 /* Changed during early boot */
342 static int pty_output_sigio
= 0;
343 static int pty_close_sigio
= 0;
345 void maybe_sigio_broken(int fd
, int read
)
352 if ((read
|| pty_output_sigio
) && (!read
|| pty_close_sigio
))
355 write_sigio_workaround();
358 err
= need_poll(&all_sigio_fds
, all_sigio_fds
.used
+ 1);
360 printk(UM_KERN_ERR
"maybe_sigio_broken - failed to add pollfd "
361 "for descriptor %d\n", fd
);
365 all_sigio_fds
.poll
[all_sigio_fds
.used
++] =
366 ((struct pollfd
) { .fd
= fd
,
367 .events
= read
? POLLIN
: POLLOUT
,
373 static void sigio_cleanup(void)
375 if (write_sigio_pid
== -1)
378 os_kill_process(write_sigio_pid
, 1);
379 free_stack(write_sigio_stack
, 0);
380 write_sigio_pid
= -1;
383 __uml_exitcall(sigio_cleanup
);
385 /* Used as a flag during SIGIO testing early in boot */
386 static volatile int got_sigio
= 0;
388 static void __init
handler(int sig
)
399 static void openpty_cb(void *arg
)
401 struct openpty_arg
*info
= arg
;
404 if (openpty(&info
->master
, &info
->slave
, NULL
, NULL
, NULL
))
408 static int async_pty(int master
, int slave
)
412 flags
= fcntl(master
, F_GETFL
);
416 if ((fcntl(master
, F_SETFL
, flags
| O_NONBLOCK
| O_ASYNC
) < 0) ||
417 (fcntl(master
, F_SETOWN
, os_getpid()) < 0))
420 if ((fcntl(slave
, F_SETFL
, flags
| O_NONBLOCK
) < 0))
426 static void __init
check_one_sigio(void (*proc
)(int, int))
428 struct sigaction old
, new;
429 struct openpty_arg pty
= { .master
= -1, .slave
= -1 };
430 int master
, slave
, err
;
432 initial_thread_cb(openpty_cb
, &pty
);
434 printk(UM_KERN_ERR
"check_one_sigio failed, errno = %d\n",
442 if ((master
== -1) || (slave
== -1)) {
443 printk(UM_KERN_ERR
"check_one_sigio failed to allocate a "
448 /* Not now, but complain so we now where we failed. */
451 printk(UM_KERN_ERR
"check_one_sigio : raw failed, errno = %d\n",
456 err
= async_pty(master
, slave
);
458 printk(UM_KERN_ERR
"check_one_sigio : sigio_async failed, "
463 if (sigaction(SIGIO
, NULL
, &old
) < 0) {
464 printk(UM_KERN_ERR
"check_one_sigio : sigaction 1 failed, "
465 "errno = %d\n", errno
);
470 new.sa_handler
= handler
;
471 if (sigaction(SIGIO
, &new, NULL
) < 0) {
472 printk(UM_KERN_ERR
"check_one_sigio : sigaction 2 failed, "
473 "errno = %d\n", errno
);
478 (*proc
)(master
, slave
);
483 if (sigaction(SIGIO
, &old
, NULL
) < 0)
484 printk(UM_KERN_ERR
"check_one_sigio : sigaction 3 failed, "
485 "errno = %d\n", errno
);
488 static void tty_output(int master
, int slave
)
493 printk(UM_KERN_INFO
"Checking that host ptys support output SIGIO...");
495 memset(buf
, 0, sizeof(buf
));
497 while (write(master
, buf
, sizeof(buf
)) > 0) ;
499 printk(UM_KERN_ERR
"tty_output : write failed, errno = %d\n",
501 while (((n
= read(slave
, buf
, sizeof(buf
))) > 0) && !got_sigio
)
505 printk(UM_KERN_CONT
"Yes\n");
506 pty_output_sigio
= 1;
507 } else if (n
== -EAGAIN
)
508 printk(UM_KERN_CONT
"No, enabling workaround\n");
510 printk(UM_KERN_CONT
"tty_output : read failed, err = %d\n", n
);
513 static void tty_close(int master
, int slave
)
515 printk(UM_KERN_INFO
"Checking that host ptys support SIGIO on "
520 printk(UM_KERN_CONT
"Yes\n");
523 printk(UM_KERN_CONT
"No, enabling workaround\n");
526 void __init
check_sigio(void)
528 if ((access("/dev/ptmx", R_OK
) < 0) &&
529 (access("/dev/ptyp0", R_OK
) < 0)) {
530 printk(UM_KERN_WARNING
"No pseudo-terminals available - "
531 "skipping pty SIGIO check\n");
534 check_one_sigio(tty_output
);
535 check_one_sigio(tty_close
);
538 /* Here because it only does the SIGIO testing for now */
539 void __init
os_check_bugs(void)