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