]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/um/os-Linux/start_up.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux-2.6-omap-h63xx.git] / 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 void ptrace_child(void)
29 {
30         int ret;
31         /* Calling os_getpid because some libcs cached getpid incorrectly */
32         int pid = os_getpid(), ppid = getppid();
33         int sc_result;
34
35         if (change_sig(SIGWINCH, 0) < 0 ||
36             ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
37                 perror("ptrace");
38                 kill(pid, SIGKILL);
39         }
40         kill(pid, SIGSTOP);
41
42         /*
43          * This syscall will be intercepted by the parent. Don't call more than
44          * once, please.
45          */
46         sc_result = os_getpid();
47
48         if (sc_result == pid)
49                 /* Nothing modified by the parent, we are running normally. */
50                 ret = 1;
51         else if (sc_result == ppid)
52                 /*
53                  * Expected in check_ptrace and check_sysemu when they succeed
54                  * in modifying the stack frame
55                  */
56                 ret = 0;
57         else
58                 /* Serious trouble! This could be caused by a bug in host 2.6
59                  * SKAS3/2.6 patch before release -V6, together with a bug in
60                  * the UML code itself.
61                  */
62                 ret = 2;
63
64         exit(ret);
65 }
66
67 static void fatal_perror(const char *str)
68 {
69         perror(str);
70         exit(1);
71 }
72
73 static void fatal(char *fmt, ...)
74 {
75         va_list list;
76
77         va_start(list, fmt);
78         vfprintf(stderr, fmt, list);
79         va_end(list);
80
81         exit(1);
82 }
83
84 static void non_fatal(char *fmt, ...)
85 {
86         va_list list;
87
88         va_start(list, fmt);
89         vfprintf(stderr, fmt, list);
90         va_end(list);
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         int pid;
344
345         /* Print out the core dump limits early */
346         check_coredump_limit();
347
348         check_ptrace();
349
350         /* Need to check this early because mmapping happens before the
351          * kernel is running.
352          */
353         check_tmpexec();
354
355         pid = start_ptraced_child();
356         if (init_registers(pid))
357                 fatal("Failed to initialize default registers");
358         stop_ptraced_child(pid, 1, 1);
359 }
360
361 static int __init noprocmm_cmd_param(char *str, int* add)
362 {
363         proc_mm = 0;
364         return 0;
365 }
366
367 __uml_setup("noprocmm", noprocmm_cmd_param,
368 "noprocmm\n"
369 "    Turns off usage of /proc/mm, even if host supports it.\n"
370 "    To support /proc/mm, the host needs to be patched using\n"
371 "    the current skas3 patch.\n\n");
372
373 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
374 {
375         ptrace_faultinfo = 0;
376         return 0;
377 }
378
379 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
380 "noptracefaultinfo\n"
381 "    Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
382 "    it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
383 "    using the current skas3 patch.\n\n");
384
385 static int __init noptraceldt_cmd_param(char *str, int* add)
386 {
387         ptrace_ldt = 0;
388         return 0;
389 }
390
391 __uml_setup("noptraceldt", noptraceldt_cmd_param,
392 "noptraceldt\n"
393 "    Turns off usage of PTRACE_LDT, even if host supports it.\n"
394 "    To support PTRACE_LDT, the host needs to be patched using\n"
395 "    the current skas3 patch.\n\n");
396
397 static inline void check_skas3_ptrace_faultinfo(void)
398 {
399         struct ptrace_faultinfo fi;
400         int pid, n;
401
402         non_fatal("  - PTRACE_FAULTINFO...");
403         pid = start_ptraced_child();
404
405         n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
406         if (n < 0) {
407                 ptrace_faultinfo = 0;
408                 if (errno == EIO)
409                         non_fatal("not found\n");
410                 else
411                         perror("not found");
412         }
413         else {
414                 if (!ptrace_faultinfo)
415                         non_fatal("found but disabled on command line\n");
416                 else
417                         non_fatal("found\n");
418         }
419
420         stop_ptraced_child(pid, 1, 1);
421 }
422
423 static inline void check_skas3_ptrace_ldt(void)
424 {
425 #ifdef PTRACE_LDT
426         int pid, n;
427         unsigned char ldtbuf[40];
428         struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
429                 .func = 2, /* read default ldt */
430                 .ptr = ldtbuf,
431                 .bytecount = sizeof(ldtbuf)};
432
433         non_fatal("  - PTRACE_LDT...");
434         pid = start_ptraced_child();
435
436         n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
437         if (n < 0) {
438                 if (errno == EIO)
439                         non_fatal("not found\n");
440                 else {
441                         perror("not found");
442                 }
443                 ptrace_ldt = 0;
444         }
445         else {
446                 if (ptrace_ldt)
447                         non_fatal("found\n");
448                 else
449                         non_fatal("found, but use is disabled\n");
450         }
451
452         stop_ptraced_child(pid, 1, 1);
453 #else
454         /* PTRACE_LDT might be disabled via cmdline option.
455          * We want to override this, else we might use the stub
456          * without real need
457          */
458         ptrace_ldt = 1;
459 #endif
460 }
461
462 static inline void check_skas3_proc_mm(void)
463 {
464         non_fatal("  - /proc/mm...");
465         if (access("/proc/mm", W_OK) < 0) {
466                 proc_mm = 0;
467                 perror("not found");
468         }
469         else if (!proc_mm)
470                 non_fatal("found but disabled on command line\n");
471         else non_fatal("found\n");
472 }
473
474 void can_do_skas(void)
475 {
476         non_fatal("Checking for the skas3 patch in the host:\n");
477
478         check_skas3_proc_mm();
479         check_skas3_ptrace_faultinfo();
480         check_skas3_ptrace_ldt();
481
482         if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
483                 skas_needs_stub = 1;
484 }
485
486 int __init parse_iomem(char *str, int *add)
487 {
488         struct iomem_region *new;
489         struct stat64 buf;
490         char *file, *driver;
491         int fd, size;
492
493         driver = str;
494         file = strchr(str,',');
495         if (file == NULL) {
496                 fprintf(stderr, "parse_iomem : failed to parse iomem\n");
497                 goto out;
498         }
499         *file = '\0';
500         file++;
501         fd = open(file, O_RDWR, 0);
502         if (fd < 0) {
503                 perror("parse_iomem - Couldn't open io file");
504                 goto out;
505         }
506
507         if (fstat64(fd, &buf) < 0) {
508                 perror("parse_iomem - cannot stat_fd file");
509                 goto out_close;
510         }
511
512         new = malloc(sizeof(*new));
513         if (new == NULL) {
514                 perror("Couldn't allocate iomem_region struct");
515                 goto out_close;
516         }
517
518         size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
519
520         *new = ((struct iomem_region) { .next           = iomem_regions,
521                                         .driver         = driver,
522                                         .fd             = fd,
523                                         .size           = size,
524                                         .phys           = 0,
525                                         .virt           = 0 });
526         iomem_regions = new;
527         iomem_size += new->size + UM_KERN_PAGE_SIZE;
528
529         return 0;
530  out_close:
531         close(fd);
532  out:
533         return 1;
534 }