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