uml: replace clone with fork
[safe/jmp/linux-2.6] / arch / um / os-Linux / start_up.c
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sched.h>
13 #include <signal.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <sys/ptrace.h>
17 #include <sys/stat.h>
18 #include <sys/wait.h>
19 #include <asm/unistd.h>
20 #include "init.h"
21 #include "kern_constants.h"
22 #include "os.h"
23 #include "mem_user.h"
24 #include "ptrace_user.h"
25 #include "registers.h"
26 #include "skas_ptrace.h"
27
28 static int ptrace_child(void)
29 {
30         int ret;
31         int pid = os_getpid(), ppid = getppid();
32         int sc_result;
33
34         change_sig(SIGWINCH, 0);
35         if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
36                 perror("ptrace");
37                 os_kill_process(pid, 0);
38         }
39         kill(pid, SIGSTOP);
40
41         /*
42          * This syscall will be intercepted by the parent. Don't call more than
43          * once, please.
44          */
45         sc_result = os_getpid();
46
47         if (sc_result == pid)
48                 /* Nothing modified by the parent, we are running normally. */
49                 ret = 1;
50         else if (sc_result == ppid)
51                 /*
52                  * Expected in check_ptrace and check_sysemu when they succeed
53                  * in modifying the stack frame
54                  */
55                 ret = 0;
56         else
57                 /* Serious trouble! This could be caused by a bug in host 2.6
58                  * SKAS3/2.6 patch before release -V6, together with a bug in
59                  * the UML code itself.
60                  */
61                 ret = 2;
62         _exit(ret);
63 }
64
65 static void fatal_perror(char *str)
66 {
67         perror(str);
68         exit(1);
69 }
70
71 static void fatal(char *fmt, ...)
72 {
73         va_list list;
74
75         va_start(list, fmt);
76         vprintf(fmt, list);
77         va_end(list);
78         fflush(stdout);
79
80         exit(1);
81 }
82
83 static void non_fatal(char *fmt, ...)
84 {
85         va_list list;
86
87         va_start(list, fmt);
88         vprintf(fmt, list);
89         va_end(list);
90         fflush(stdout);
91 }
92
93 static int start_ptraced_child(void)
94 {
95         int pid, n, status;
96
97         pid = fork();
98         if (pid == 0)
99                 ptrace_child();
100         else if (pid < 0)
101                 fatal_perror("start_ptraced_child : fork failed");
102
103         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
104         if (n < 0)
105                 fatal_perror("check_ptrace : waitpid failed");
106         if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
107                 fatal("check_ptrace : expected SIGSTOP, got status = %d",
108                       status);
109
110         return pid;
111 }
112
113 /* When testing for SYSEMU support, if it is one of the broken versions, we
114  * must just avoid using sysemu, not panic, but only if SYSEMU features are
115  * broken.
116  * So only for SYSEMU features we test mustpanic, while normal host features
117  * must work anyway!
118  */
119 static int stop_ptraced_child(int pid, int exitcode, int mustexit)
120 {
121         int status, n, ret = 0;
122
123         if (ptrace(PTRACE_CONT, pid, 0, 0) < 0)
124                 fatal_perror("stop_ptraced_child : ptrace failed");
125         CATCH_EINTR(n = waitpid(pid, &status, 0));
126         if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
127                 int exit_with = WEXITSTATUS(status);
128                 if (exit_with == 2)
129                         non_fatal("check_ptrace : child exited with status 2. "
130                                   "\nDisabling SYSEMU support.\n");
131                 non_fatal("check_ptrace : child exited with exitcode %d, while "
132                           "expecting %d; status 0x%x\n", exit_with,
133                           exitcode, status);
134                 if (mustexit)
135                         exit(1);
136                 ret = -1;
137         }
138
139         return ret;
140 }
141
142 /* Changed only during early boot */
143 int ptrace_faultinfo = 1;
144 int ptrace_ldt = 1;
145 int proc_mm = 1;
146 int skas_needs_stub = 0;
147
148 static int __init skas0_cmd_param(char *str, int* add)
149 {
150         ptrace_faultinfo = proc_mm = 0;
151         return 0;
152 }
153
154 /* The two __uml_setup would conflict, without this stupid alias. */
155
156 static int __init mode_skas0_cmd_param(char *str, int* add)
157         __attribute__((alias("skas0_cmd_param")));
158
159 __uml_setup("skas0", skas0_cmd_param,
160                 "skas0\n"
161                 "    Disables SKAS3 usage, so that SKAS0 is used, unless \n"
162                 "    you specify mode=tt.\n\n");
163
164 __uml_setup("mode=skas0", mode_skas0_cmd_param,
165                 "mode=skas0\n"
166                 "    Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
167                 "    specify mode=tt. Note that this was recently added - on \n"
168                 "    older kernels you must use simply \"skas0\".\n\n");
169
170 /* Changed only during early boot */
171 static int force_sysemu_disabled = 0;
172
173 static int __init nosysemu_cmd_param(char *str, int* add)
174 {
175         force_sysemu_disabled = 1;
176         return 0;
177 }
178
179 __uml_setup("nosysemu", nosysemu_cmd_param,
180 "nosysemu\n"
181 "    Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
182 "    SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
183 "    behaviour of ptrace() and helps reducing host context switch rate.\n"
184 "    To make it working, you need a kernel patch for your host, too.\n"
185 "    See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
186 "    information.\n\n");
187
188 static void __init check_sysemu(void)
189 {
190         unsigned long regs[MAX_REG_NR];
191         int pid, n, status, count=0;
192
193         non_fatal("Checking syscall emulation patch for ptrace...");
194         sysemu_supported = 0;
195         pid = start_ptraced_child();
196
197         if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
198                 goto fail;
199
200         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
201         if (n < 0)
202                 fatal_perror("check_sysemu : wait failed");
203         if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
204                 fatal("check_sysemu : expected SIGTRAP, got status = %d",
205                       status);
206
207         if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
208                 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
209         if (PT_SYSCALL_NR(regs) != __NR_getpid) {
210                 non_fatal("check_sysemu got system call number %d, "
211                           "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
212                 goto fail;
213         }
214
215         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
216         if (n < 0) {
217                 non_fatal("check_sysemu : failed to modify system call "
218                           "return");
219                 goto fail;
220         }
221
222         if (stop_ptraced_child(pid, 0, 0) < 0)
223                 goto fail_stopped;
224
225         sysemu_supported = 1;
226         non_fatal("OK\n");
227         set_using_sysemu(!force_sysemu_disabled);
228
229         non_fatal("Checking advanced syscall emulation patch for ptrace...");
230         pid = start_ptraced_child();
231
232         if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
233                    (void *) PTRACE_O_TRACESYSGOOD) < 0))
234                 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
235
236         while (1) {
237                 count++;
238                 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
239                         goto fail;
240                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
241                 if (n < 0)
242                         fatal_perror("check_ptrace : wait failed");
243
244                 if (WIFSTOPPED(status) &&
245                     (WSTOPSIG(status) == (SIGTRAP|0x80))) {
246                         if (!count)
247                                 fatal("check_ptrace : SYSEMU_SINGLESTEP "
248                                       "doesn't singlestep");
249                         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
250                                    os_getpid());
251                         if (n < 0)
252                                 fatal_perror("check_sysemu : failed to modify "
253                                              "system call return");
254                         break;
255                 }
256                 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
257                         count++;
258                 else
259                         fatal("check_ptrace : expected SIGTRAP or "
260                               "(SIGTRAP | 0x80), got status = %d", status);
261         }
262         if (stop_ptraced_child(pid, 0, 0) < 0)
263                 goto fail_stopped;
264
265         sysemu_supported = 2;
266         non_fatal("OK\n");
267
268         if (!force_sysemu_disabled)
269                 set_using_sysemu(sysemu_supported);
270         return;
271
272 fail:
273         stop_ptraced_child(pid, 1, 0);
274 fail_stopped:
275         non_fatal("missing\n");
276 }
277
278 static void __init check_ptrace(void)
279 {
280         int pid, syscall, n, status;
281
282         non_fatal("Checking that ptrace can change system call numbers...");
283         pid = start_ptraced_child();
284
285         if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
286                    (void *) PTRACE_O_TRACESYSGOOD) < 0))
287                 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
288
289         while (1) {
290                 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
291                         fatal_perror("check_ptrace : ptrace failed");
292
293                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
294                 if (n < 0)
295                         fatal_perror("check_ptrace : wait failed");
296
297                 if (!WIFSTOPPED(status) ||
298                    (WSTOPSIG(status) != (SIGTRAP | 0x80)))
299                         fatal("check_ptrace : expected (SIGTRAP|0x80), "
300                                "got status = %d", status);
301
302                 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
303                                  0);
304                 if (syscall == __NR_getpid) {
305                         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
306                                    __NR_getppid);
307                         if (n < 0)
308                                 fatal_perror("check_ptrace : failed to modify "
309                                              "system call");
310                         break;
311                 }
312         }
313         stop_ptraced_child(pid, 0, 1);
314         non_fatal("OK\n");
315         check_sysemu();
316 }
317
318 extern void check_tmpexec(void);
319
320 static void __init check_coredump_limit(void)
321 {
322         struct rlimit lim;
323         int err = getrlimit(RLIMIT_CORE, &lim);
324
325         if (err) {
326                 perror("Getting core dump limit");
327                 return;
328         }
329
330         printf("Core dump limits :\n\tsoft - ");
331         if (lim.rlim_cur == RLIM_INFINITY)
332                 printf("NONE\n");
333         else printf("%lu\n", lim.rlim_cur);
334
335         printf("\thard - ");
336         if (lim.rlim_max == RLIM_INFINITY)
337                 printf("NONE\n");
338         else printf("%lu\n", lim.rlim_max);
339 }
340
341 void __init os_early_checks(void)
342 {
343         /* Print out the core dump limits early */
344         check_coredump_limit();
345
346         check_ptrace();
347
348         /* Need to check this early because mmapping happens before the
349          * kernel is running.
350          */
351         check_tmpexec();
352 }
353
354 static int __init noprocmm_cmd_param(char *str, int* add)
355 {
356         proc_mm = 0;
357         return 0;
358 }
359
360 __uml_setup("noprocmm", noprocmm_cmd_param,
361 "noprocmm\n"
362 "    Turns off usage of /proc/mm, even if host supports it.\n"
363 "    To support /proc/mm, the host needs to be patched using\n"
364 "    the current skas3 patch.\n\n");
365
366 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
367 {
368         ptrace_faultinfo = 0;
369         return 0;
370 }
371
372 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
373 "noptracefaultinfo\n"
374 "    Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
375 "    it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
376 "    using the current skas3 patch.\n\n");
377
378 static int __init noptraceldt_cmd_param(char *str, int* add)
379 {
380         ptrace_ldt = 0;
381         return 0;
382 }
383
384 __uml_setup("noptraceldt", noptraceldt_cmd_param,
385 "noptraceldt\n"
386 "    Turns off usage of PTRACE_LDT, even if host supports it.\n"
387 "    To support PTRACE_LDT, the host needs to be patched using\n"
388 "    the current skas3 patch.\n\n");
389
390 static inline void check_skas3_ptrace_faultinfo(void)
391 {
392         struct ptrace_faultinfo fi;
393         int pid, n;
394
395         non_fatal("  - PTRACE_FAULTINFO...");
396         pid = start_ptraced_child();
397
398         n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
399         if (n < 0) {
400                 ptrace_faultinfo = 0;
401                 if (errno == EIO)
402                         non_fatal("not found\n");
403                 else
404                         perror("not found");
405         }
406         else {
407                 if (!ptrace_faultinfo)
408                         non_fatal("found but disabled on command line\n");
409                 else
410                         non_fatal("found\n");
411         }
412
413         init_registers(pid);
414         stop_ptraced_child(pid, 1, 1);
415 }
416
417 static inline void check_skas3_ptrace_ldt(void)
418 {
419 #ifdef PTRACE_LDT
420         int pid, n;
421         unsigned char ldtbuf[40];
422         struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
423                 .func = 2, /* read default ldt */
424                 .ptr = ldtbuf,
425                 .bytecount = sizeof(ldtbuf)};
426
427         non_fatal("  - PTRACE_LDT...");
428         pid = start_ptraced_child();
429
430         n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
431         if (n < 0) {
432                 if (errno == EIO)
433                         non_fatal("not found\n");
434                 else {
435                         perror("not found");
436                 }
437                 ptrace_ldt = 0;
438         }
439         else {
440                 if (ptrace_ldt)
441                         non_fatal("found\n");
442                 else
443                         non_fatal("found, but use is disabled\n");
444         }
445
446         stop_ptraced_child(pid, 1, 1);
447 #else
448         /* PTRACE_LDT might be disabled via cmdline option.
449          * We want to override this, else we might use the stub
450          * without real need
451          */
452         ptrace_ldt = 1;
453 #endif
454 }
455
456 static inline void check_skas3_proc_mm(void)
457 {
458         non_fatal("  - /proc/mm...");
459         if (access("/proc/mm", W_OK) < 0) {
460                 proc_mm = 0;
461                 perror("not found");
462         }
463         else if (!proc_mm)
464                 non_fatal("found but disabled on command line\n");
465         else non_fatal("found\n");
466 }
467
468 int can_do_skas(void)
469 {
470         non_fatal("Checking for the skas3 patch in the host:\n");
471
472         check_skas3_proc_mm();
473         check_skas3_ptrace_faultinfo();
474         check_skas3_ptrace_ldt();
475
476         if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
477                 skas_needs_stub = 1;
478
479         return 1;
480 }
481
482 int __init parse_iomem(char *str, int *add)
483 {
484         struct iomem_region *new;
485         struct stat64 buf;
486         char *file, *driver;
487         int fd, size;
488
489         driver = str;
490         file = strchr(str,',');
491         if (file == NULL) {
492                 printf("parse_iomem : failed to parse iomem\n");
493                 goto out;
494         }
495         *file = '\0';
496         file++;
497         fd = open(file, O_RDWR, 0);
498         if (fd < 0) {
499                 os_print_error(fd, "parse_iomem - Couldn't open io file");
500                 goto out;
501         }
502
503         if (fstat64(fd, &buf) < 0) {
504                 perror("parse_iomem - cannot stat_fd file");
505                 goto out_close;
506         }
507
508         new = malloc(sizeof(*new));
509         if (new == NULL) {
510                 perror("Couldn't allocate iomem_region struct");
511                 goto out_close;
512         }
513
514         size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
515
516         *new = ((struct iomem_region) { .next           = iomem_regions,
517                                         .driver         = driver,
518                                         .fd             = fd,
519                                         .size           = size,
520                                         .phys           = 0,
521                                         .virt           = 0 });
522         iomem_regions = new;
523         iomem_size += new->size + UM_KERN_PAGE_SIZE;
524
525         return 0;
526  out_close:
527         close(fd);
528  out:
529         return 1;
530 }