]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/um/os-Linux/start_up.c
[PATCH] uml: style fixes in startup code
[mirror_ubuntu-bionic-kernel.git] / arch / um / os-Linux / start_up.c
CommitLineData
60d339f6 1/*
1da177e4
LT
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
8e367065 6#include <pty.h>
1da177e4 7#include <stdio.h>
0f80bc85
JD
8#include <stddef.h>
9#include <stdarg.h>
10#include <stdlib.h>
11#include <string.h>
1da177e4
LT
12#include <unistd.h>
13#include <signal.h>
14#include <sched.h>
0f80bc85 15#include <fcntl.h>
1da177e4 16#include <errno.h>
1da177e4
LT
17#include <sys/time.h>
18#include <sys/wait.h>
19#include <sys/mman.h>
20#include <asm/unistd.h>
21#include <asm/page.h>
0f80bc85 22#include <sys/types.h>
1da177e4
LT
23#include "user_util.h"
24#include "kern_util.h"
25#include "user.h"
1da177e4 26#include "signal_kern.h"
1da177e4
LT
27#include "sysdep/ptrace.h"
28#include "sysdep/sigcontext.h"
29#include "irq_user.h"
30#include "ptrace_user.h"
0f80bc85 31#include "mem_user.h"
1da177e4
LT
32#include "init.h"
33#include "os.h"
34#include "uml-config.h"
1da177e4
LT
35#include "choose-mode.h"
36#include "mode.h"
d67b569f 37#include "tempfile.h"
0f80bc85
JD
38#include "kern_constants.h"
39
1da177e4
LT
40#ifdef UML_CONFIG_MODE_SKAS
41#include "skas.h"
42#include "skas_ptrace.h"
43#include "registers.h"
44#endif
45
b85e9680 46static int ptrace_child(void *arg)
1da177e4
LT
47{
48 int ret;
49 int pid = os_getpid(), ppid = getppid();
50 int sc_result;
51
43b00fdb 52 change_sig(SIGWINCH, 0);
1da177e4
LT
53 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
54 perror("ptrace");
55 os_kill_process(pid, 0);
56 }
57 os_stop_process(pid);
58
59 /*This syscall will be intercepted by the parent. Don't call more than
60 * once, please.*/
61 sc_result = os_getpid();
62
63 if (sc_result == pid)
64 ret = 1; /*Nothing modified by the parent, we are running
65 normally.*/
66 else if (sc_result == ppid)
67 ret = 0; /*Expected in check_ptrace and check_sysemu when they
68 succeed in modifying the stack frame*/
69 else
70 ret = 2; /*Serious trouble! This could be caused by a bug in
71 host 2.6 SKAS3/2.6 patch before release -V6, together
72 with a bug in the UML code itself.*/
73 _exit(ret);
74}
75
b85e9680 76static int start_ptraced_child(void **stack_out)
1da177e4 77{
b85e9680
JD
78 void *stack;
79 unsigned long sp;
1da177e4 80 int pid, n, status;
60d339f6 81
b85e9680
JD
82 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
83 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
84 if(stack == MAP_FAILED)
85 panic("check_ptrace : mmap failed, errno = %d", errno);
86 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
87 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
1da177e4 88 if(pid < 0)
60d339f6 89 panic("start_ptraced_child : clone failed, errno = %d", errno);
1da177e4
LT
90 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
91 if(n < 0)
60d339f6 92 panic("check_ptrace : clone failed, errno = %d", errno);
1da177e4
LT
93 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
94 panic("check_ptrace : expected SIGSTOP, got status = %d",
95 status);
96
b85e9680 97 *stack_out = stack;
9eae9b13 98 return pid;
1da177e4
LT
99}
100
60d339f6
GS
101/* When testing for SYSEMU support, if it is one of the broken versions, we
102 * must just avoid using sysemu, not panic, but only if SYSEMU features are
103 * broken.
1da177e4 104 * So only for SYSEMU features we test mustpanic, while normal host features
60d339f6
GS
105 * must work anyway!
106 */
107static int stop_ptraced_child(int pid, void *stack, int exitcode,
108 int mustpanic)
1da177e4
LT
109{
110 int status, n, ret = 0;
111
112 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
b85e9680 113 panic("check_ptrace : ptrace failed, errno = %d", errno);
1da177e4
LT
114 CATCH_EINTR(n = waitpid(pid, &status, 0));
115 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
116 int exit_with = WEXITSTATUS(status);
117 if (exit_with == 2)
51694948 118 printf("check_ptrace : child exited with status 2. "
1da177e4
LT
119 "Serious trouble happening! Try updating your "
120 "host skas patch!\nDisabling SYSEMU support.");
51694948 121 printf("check_ptrace : child exited with exitcode %d, while "
1da177e4
LT
122 "expecting %d; status 0x%x", exit_with,
123 exitcode, status);
b85e9680 124 if (mustpanic)
1da177e4
LT
125 panic("\n");
126 else
51694948 127 printf("\n");
1da177e4
LT
128 ret = -1;
129 }
130
b85e9680
JD
131 if(munmap(stack, PAGE_SIZE) < 0)
132 panic("check_ptrace : munmap failed, errno = %d", errno);
1da177e4
LT
133 return ret;
134}
135
7242a400 136/* Changed only during early boot */
cb66504d 137int ptrace_faultinfo = 1;
858259cf 138int ptrace_ldt = 1;
cb66504d 139int proc_mm = 1;
858259cf 140int skas_needs_stub = 0;
cb66504d
PBG
141
142static int __init skas0_cmd_param(char *str, int* add)
143{
144 ptrace_faultinfo = proc_mm = 0;
145 return 0;
146}
147
9e3d862e
PBG
148/* The two __uml_setup would conflict, without this stupid alias. */
149
150static int __init mode_skas0_cmd_param(char *str, int* add)
151 __attribute__((alias("skas0_cmd_param")));
152
60d339f6
GS
153__uml_setup("skas0", skas0_cmd_param,
154 "skas0\n"
155 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
156 " you specify mode=tt.\n\n");
157
9e3d862e
PBG
158__uml_setup("mode=skas0", mode_skas0_cmd_param,
159 "mode=skas0\n"
160 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
161 " specify mode=tt. Note that this was recently added - on \n"
162 " older kernels you must use simply \"skas0\".\n\n");
163
7242a400 164/* Changed only during early boot */
60d339f6
GS
165static int force_sysemu_disabled = 0;
166
1da177e4
LT
167static int __init nosysemu_cmd_param(char *str, int* add)
168{
169 force_sysemu_disabled = 1;
170 return 0;
171}
172
173__uml_setup("nosysemu", nosysemu_cmd_param,
60d339f6
GS
174"nosysemu\n"
175" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
176" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
177" behaviour of ptrace() and helps reducing host context switch rate.\n"
178" To make it working, you need a kernel patch for your host, too.\n"
179" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
180" information.\n\n");
1da177e4
LT
181
182static void __init check_sysemu(void)
183{
b85e9680 184 void *stack;
9eae9b13 185 int pid, n, status, count=0;
1da177e4 186
51694948 187 printf("Checking syscall emulation patch for ptrace...");
1da177e4 188 sysemu_supported = 0;
b85e9680 189 pid = start_ptraced_child(&stack);
1da177e4
LT
190
191 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
192 goto fail;
193
194 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
195 if (n < 0)
196 panic("check_sysemu : wait failed, errno = %d", errno);
197 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
198 panic("check_sysemu : expected SIGTRAP, "
199 "got status = %d", status);
200
201 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
202 os_getpid());
203 if(n < 0)
204 panic("check_sysemu : failed to modify system "
205 "call return, errno = %d", errno);
206
b85e9680 207 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
1da177e4
LT
208 goto fail_stopped;
209
210 sysemu_supported = 1;
51694948 211 printf("OK\n");
1da177e4
LT
212 set_using_sysemu(!force_sysemu_disabled);
213
51694948 214 printf("Checking advanced syscall emulation patch for ptrace...");
b85e9680 215 pid = start_ptraced_child(&stack);
f9dfefe4
BS
216
217 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
218 (void *) PTRACE_O_TRACESYSGOOD) < 0)
219 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
220 errno);
221
1da177e4
LT
222 while(1){
223 count++;
224 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
225 goto fail;
226 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
227 if(n < 0)
228 panic("check_ptrace : wait failed, errno = %d", errno);
f9dfefe4 229 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
1da177e4 230 if (!count)
f9dfefe4
BS
231 panic("check_ptrace : SYSEMU_SINGLESTEP "
232 "doesn't singlestep");
1da177e4
LT
233 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
234 os_getpid());
235 if(n < 0)
236 panic("check_sysemu : failed to modify system "
237 "call return, errno = %d", errno);
238 break;
239 }
f9dfefe4
BS
240 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
241 count++;
242 else
243 panic("check_ptrace : expected SIGTRAP or "
244 "(SIGTRAP|0x80), got status = %d", status);
1da177e4 245 }
b85e9680 246 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
1da177e4
LT
247 goto fail_stopped;
248
249 sysemu_supported = 2;
51694948 250 printf("OK\n");
1da177e4
LT
251
252 if ( !force_sysemu_disabled )
253 set_using_sysemu(sysemu_supported);
254 return;
255
256fail:
b85e9680 257 stop_ptraced_child(pid, stack, 1, 0);
1da177e4 258fail_stopped:
51694948 259 printf("missing\n");
1da177e4
LT
260}
261
60d339f6 262static void __init check_ptrace(void)
1da177e4 263{
b85e9680 264 void *stack;
1da177e4
LT
265 int pid, syscall, n, status;
266
51694948 267 printf("Checking that ptrace can change system call numbers...");
b85e9680 268 pid = start_ptraced_child(&stack);
1da177e4 269
60d339f6
GS
270 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
271 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
1da177e4
LT
272
273 while(1){
274 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
60d339f6 275 panic("check_ptrace : ptrace failed, errno = %d",
1da177e4
LT
276 errno);
277 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
278 if(n < 0)
279 panic("check_ptrace : wait failed, errno = %d", errno);
f9dfefe4
BS
280 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
281 panic("check_ptrace : expected (SIGTRAP|0x80), "
1da177e4 282 "got status = %d", status);
60d339f6 283
1da177e4
LT
284 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
285 0);
286 if(syscall == __NR_getpid){
287 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
288 __NR_getppid);
289 if(n < 0)
290 panic("check_ptrace : failed to modify system "
291 "call, errno = %d", errno);
292 break;
293 }
294 }
b85e9680 295 stop_ptraced_child(pid, stack, 0, 1);
51694948 296 printf("OK\n");
1da177e4
LT
297 check_sysemu();
298}
299
966a082f 300extern void check_tmpexec(void);
0f80bc85 301
60d339f6 302void os_early_checks(void)
1da177e4 303{
60d339f6 304 check_ptrace();
0f80bc85
JD
305
306 /* Need to check this early because mmapping happens before the
307 * kernel is running.
308 */
309 check_tmpexec();
1da177e4
LT
310}
311
d9838d86
BS
312static int __init noprocmm_cmd_param(char *str, int* add)
313{
314 proc_mm = 0;
315 return 0;
316}
317
318__uml_setup("noprocmm", noprocmm_cmd_param,
319"noprocmm\n"
320" Turns off usage of /proc/mm, even if host supports it.\n"
321" To support /proc/mm, the host needs to be patched using\n"
322" the current skas3 patch.\n\n");
323
324static int __init noptracefaultinfo_cmd_param(char *str, int* add)
325{
326 ptrace_faultinfo = 0;
327 return 0;
328}
329
330__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
331"noptracefaultinfo\n"
332" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
333" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
334" using the current skas3 patch.\n\n");
335
858259cf
BS
336static int __init noptraceldt_cmd_param(char *str, int* add)
337{
338 ptrace_ldt = 0;
339 return 0;
340}
341
342__uml_setup("noptraceldt", noptraceldt_cmd_param,
343"noptraceldt\n"
344" Turns off usage of PTRACE_LDT, even if host supports it.\n"
345" To support PTRACE_LDT, the host needs to be patched using\n"
346" the current skas3 patch.\n\n");
347
1da177e4 348#ifdef UML_CONFIG_MODE_SKAS
858259cf 349static inline void check_skas3_ptrace_faultinfo(void)
1da177e4
LT
350{
351 struct ptrace_faultinfo fi;
b85e9680 352 void *stack;
d67b569f 353 int pid, n;
1da177e4 354
858259cf 355 printf(" - PTRACE_FAULTINFO...");
b85e9680 356 pid = start_ptraced_child(&stack);
1da177e4
LT
357
358 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
359 if (n < 0) {
cb66504d 360 ptrace_faultinfo = 0;
1da177e4
LT
361 if(errno == EIO)
362 printf("not found\n");
60d339f6 363 else
1da177e4 364 perror("not found");
d67b569f
JD
365 }
366 else {
cb66504d
PBG
367 if (!ptrace_faultinfo)
368 printf("found but disabled on command line\n");
369 else
370 printf("found\n");
1da177e4
LT
371 }
372
373 init_registers(pid);
b85e9680 374 stop_ptraced_child(pid, stack, 1, 1);
1da177e4
LT
375}
376
858259cf
BS
377static inline void check_skas3_ptrace_ldt(void)
378{
379#ifdef PTRACE_LDT
380 void *stack;
381 int pid, n;
382 unsigned char ldtbuf[40];
383 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
384 .func = 2, /* read default ldt */
385 .ptr = ldtbuf,
386 .bytecount = sizeof(ldtbuf)};
387
388 printf(" - PTRACE_LDT...");
389 pid = start_ptraced_child(&stack);
390
391 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
392 if (n < 0) {
393 if(errno == EIO)
394 printf("not found\n");
395 else {
396 perror("not found");
397 }
398 ptrace_ldt = 0;
399 }
400 else {
401 if(ptrace_ldt)
402 printf("found\n");
403 else
404 printf("found, but use is disabled\n");
405 }
406
407 stop_ptraced_child(pid, stack, 1, 1);
408#else
409 /* PTRACE_LDT might be disabled via cmdline option.
410 * We want to override this, else we might use the stub
411 * without real need
412 */
413 ptrace_ldt = 1;
414#endif
415}
416
417static inline void check_skas3_proc_mm(void)
1da177e4 418{
858259cf 419 printf(" - /proc/mm...");
1da177e4 420 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
9eae9b13 421 proc_mm = 0;
1da177e4 422 printf("not found\n");
60d339f6
GS
423 }
424 else {
cb66504d
PBG
425 if (!proc_mm)
426 printf("found but disabled on command line\n");
427 else
428 printf("found\n");
1da177e4 429 }
858259cf
BS
430}
431
432int can_do_skas(void)
433{
434 printf("Checking for the skas3 patch in the host:\n");
435
436 check_skas3_proc_mm();
437 check_skas3_ptrace_faultinfo();
438 check_skas3_ptrace_ldt();
439
440 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
441 skas_needs_stub = 1;
1da177e4 442
d67b569f 443 return 1;
1da177e4
LT
444}
445#else
446int can_do_skas(void)
447{
9eae9b13 448 return 0;
1da177e4
LT
449}
450#endif
0f80bc85 451
0f80bc85
JD
452int __init parse_iomem(char *str, int *add)
453{
454 struct iomem_region *new;
455 struct uml_stat buf;
456 char *file, *driver;
457 int fd, err, size;
458
459 driver = str;
460 file = strchr(str,',');
461 if(file == NULL){
462 printf("parse_iomem : failed to parse iomem\n");
463 goto out;
464 }
465 *file = '\0';
466 file++;
467 fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
468 if(fd < 0){
469 os_print_error(fd, "parse_iomem - Couldn't open io file");
470 goto out;
471 }
472
473 err = os_stat_fd(fd, &buf);
474 if(err < 0){
475 os_print_error(err, "parse_iomem - cannot stat_fd file");
476 goto out_close;
477 }
478
479 new = malloc(sizeof(*new));
480 if(new == NULL){
481 perror("Couldn't allocate iomem_region struct");
482 goto out_close;
483 }
484
485 size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
486
487 *new = ((struct iomem_region) { .next = iomem_regions,
488 .driver = driver,
489 .fd = fd,
490 .size = size,
491 .phys = 0,
492 .virt = 0 });
493 iomem_regions = new;
494 iomem_size += new->size + UM_KERN_PAGE_SIZE;
495
9eae9b13 496 return 0;
0f80bc85
JD
497 out_close:
498 os_close_file(fd);
499 out:
9eae9b13 500 return 1;
0f80bc85
JD
501}
502
8e367065
JD
503
504/* Changed during early boot */
505int pty_output_sigio = 0;
506int pty_close_sigio = 0;
507
508/* Used as a flag during SIGIO testing early in boot */
509static volatile int got_sigio = 0;
510
511static void __init handler(int sig)
512{
513 got_sigio = 1;
514}
515
516struct openpty_arg {
517 int master;
518 int slave;
519 int err;
520};
521
522static void openpty_cb(void *arg)
523{
524 struct openpty_arg *info = arg;
525
526 info->err = 0;
527 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
528 info->err = -errno;
529}
530
531static void __init check_one_sigio(void (*proc)(int, int))
532{
533 struct sigaction old, new;
534 struct openpty_arg pty = { .master = -1, .slave = -1 };
535 int master, slave, err;
536
537 initial_thread_cb(openpty_cb, &pty);
538 if(pty.err){
539 printk("openpty failed, errno = %d\n", -pty.err);
540 return;
541 }
542
543 master = pty.master;
544 slave = pty.slave;
545
546 if((master == -1) || (slave == -1)){
547 printk("openpty failed to allocate a pty\n");
548 return;
549 }
550
551 /* Not now, but complain so we now where we failed. */
552 err = raw(master);
553 if (err < 0)
554 panic("check_sigio : __raw failed, errno = %d\n", -err);
555
556 err = os_sigio_async(master, slave);
557 if(err < 0)
558 panic("tty_fds : sigio_async failed, err = %d\n", -err);
559
560 if(sigaction(SIGIO, NULL, &old) < 0)
561 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
562 new = old;
563 new.sa_handler = handler;
564 if(sigaction(SIGIO, &new, NULL) < 0)
565 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
566
567 got_sigio = 0;
568 (*proc)(master, slave);
569
570 close(master);
571 close(slave);
572
573 if(sigaction(SIGIO, &old, NULL) < 0)
574 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
575}
576
577static void tty_output(int master, int slave)
578{
579 int n;
580 char buf[512];
581
582 printk("Checking that host ptys support output SIGIO...");
583
584 memset(buf, 0, sizeof(buf));
585
586 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
587 if(errno != EAGAIN)
588 panic("check_sigio : write failed, errno = %d\n", errno);
589 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
590
591 if(got_sigio){
592 printk("Yes\n");
593 pty_output_sigio = 1;
594 }
595 else if(n == -EAGAIN) printk("No, enabling workaround\n");
596 else panic("check_sigio : read failed, err = %d\n", n);
597}
598
599static void tty_close(int master, int slave)
600{
601 printk("Checking that host ptys support SIGIO on close...");
602
603 close(slave);
604 if(got_sigio){
605 printk("Yes\n");
606 pty_close_sigio = 1;
607 }
608 else printk("No, enabling workaround\n");
609}
610
611void __init check_sigio(void)
612{
613 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
614 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
615 printk("No pseudo-terminals available - skipping pty SIGIO "
616 "check\n");
617 return;
618 }
619 check_one_sigio(tty_output);
620 check_one_sigio(tty_close);
621}
622
623void os_check_bugs(void)
624{
625 check_ptrace();
626 check_sigio();
8e367065
JD
627}
628