uml: remove code made redundant by CHOOSE_MODE removal
[deliverable/linux.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>
1d94cda0 20#include <sys/resource.h>
1da177e4 21#include <asm/unistd.h>
0f80bc85 22#include <sys/types.h>
1da177e4
LT
23#include "kern_util.h"
24#include "user.h"
1da177e4 25#include "signal_kern.h"
1da177e4
LT
26#include "sysdep/ptrace.h"
27#include "sysdep/sigcontext.h"
28#include "irq_user.h"
29#include "ptrace_user.h"
0f80bc85 30#include "mem_user.h"
1da177e4
LT
31#include "init.h"
32#include "os.h"
33#include "uml-config.h"
d67b569f 34#include "tempfile.h"
0f80bc85 35#include "kern_constants.h"
1da177e4
LT
36#include "skas.h"
37#include "skas_ptrace.h"
38#include "registers.h"
1da177e4 39
b85e9680 40static int ptrace_child(void *arg)
1da177e4
LT
41{
42 int ret;
43 int pid = os_getpid(), ppid = getppid();
44 int sc_result;
45
43b00fdb 46 change_sig(SIGWINCH, 0);
1da177e4
LT
47 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
48 perror("ptrace");
49 os_kill_process(pid, 0);
50 }
73c8f444 51 kill(pid, SIGSTOP);
1da177e4
LT
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
3a150e1d
JD
70static void fatal_perror(char *str)
71{
72 perror(str);
73 exit(1);
74}
75
76static 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
88static 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
b85e9680 98static int start_ptraced_child(void **stack_out)
1da177e4 99{
b85e9680
JD
100 void *stack;
101 unsigned long sp;
1da177e4 102 int pid, n, status;
60d339f6 103
c539ab73
JD
104 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
105 PROT_READ | PROT_WRITE | PROT_EXEC,
b85e9680
JD
106 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
107 if(stack == MAP_FAILED)
3a150e1d 108 fatal_perror("check_ptrace : mmap failed");
c539ab73 109 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
b85e9680 110 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
1da177e4 111 if(pid < 0)
3a150e1d 112 fatal_perror("start_ptraced_child : clone failed");
1da177e4
LT
113 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
114 if(n < 0)
3a150e1d 115 fatal_perror("check_ptrace : clone failed");
1da177e4 116 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
3a150e1d 117 fatal("check_ptrace : expected SIGSTOP, got status = %d",
1da177e4
LT
118 status);
119
b85e9680 120 *stack_out = stack;
9eae9b13 121 return pid;
1da177e4
LT
122}
123
60d339f6
GS
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.
1da177e4 127 * So only for SYSEMU features we test mustpanic, while normal host features
60d339f6
GS
128 * must work anyway!
129 */
130static int stop_ptraced_child(int pid, void *stack, int exitcode,
3a150e1d 131 int mustexit)
1da177e4
LT
132{
133 int status, n, ret = 0;
134
135 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
3a150e1d 136 fatal_perror("stop_ptraced_child : ptrace failed");
1da177e4
LT
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)
3a150e1d 141 non_fatal("check_ptrace : child exited with status 2. "
cf6acedb 142 "\nDisabling SYSEMU support.\n");
3a150e1d
JD
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);
1da177e4
LT
148 ret = -1;
149 }
150
c539ab73 151 if(munmap(stack, UM_KERN_PAGE_SIZE) < 0)
3a150e1d 152 fatal_perror("check_ptrace : munmap failed");
1da177e4
LT
153 return ret;
154}
155
7242a400 156/* Changed only during early boot */
cb66504d 157int ptrace_faultinfo = 1;
858259cf 158int ptrace_ldt = 1;
cb66504d 159int proc_mm = 1;
858259cf 160int skas_needs_stub = 0;
cb66504d
PBG
161
162static int __init skas0_cmd_param(char *str, int* add)
163{
164 ptrace_faultinfo = proc_mm = 0;
165 return 0;
166}
167
9e3d862e
PBG
168/* The two __uml_setup would conflict, without this stupid alias. */
169
170static int __init mode_skas0_cmd_param(char *str, int* add)
171 __attribute__((alias("skas0_cmd_param")));
172
60d339f6
GS
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
9e3d862e
PBG
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
7242a400 184/* Changed only during early boot */
60d339f6
GS
185static int force_sysemu_disabled = 0;
186
1da177e4
LT
187static 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,
60d339f6
GS
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");
1da177e4
LT
201
202static void __init check_sysemu(void)
203{
b85e9680 204 void *stack;
cf6acedb 205 unsigned long regs[MAX_REG_NR];
9eae9b13 206 int pid, n, status, count=0;
1da177e4 207
3a150e1d 208 non_fatal("Checking syscall emulation patch for ptrace...");
1da177e4 209 sysemu_supported = 0;
b85e9680 210 pid = start_ptraced_child(&stack);
1da177e4
LT
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)
3a150e1d 217 fatal_perror("check_sysemu : wait failed");
1da177e4 218 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
3a150e1d
JD
219 fatal("check_sysemu : expected SIGTRAP, got status = %d",
220 status);
1da177e4 221
cf6acedb
JD
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 }
1da177e4 236
b85e9680 237 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
1da177e4
LT
238 goto fail_stopped;
239
240 sysemu_supported = 1;
3a150e1d 241 non_fatal("OK\n");
1da177e4
LT
242 set_using_sysemu(!force_sysemu_disabled);
243
3a150e1d 244 non_fatal("Checking advanced syscall emulation patch for ptrace...");
b85e9680 245 pid = start_ptraced_child(&stack);
f9dfefe4 246
3a150e1d
JD
247 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
248 (void *) PTRACE_O_TRACESYSGOOD) < 0))
249 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
f9dfefe4 250
1da177e4
LT
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)
3a150e1d
JD
257 fatal_perror("check_ptrace : wait failed");
258
f9dfefe4 259 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
1da177e4 260 if (!count)
3a150e1d 261 fatal("check_ptrace : SYSEMU_SINGLESTEP "
f9dfefe4 262 "doesn't singlestep");
1da177e4
LT
263 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
264 os_getpid());
265 if(n < 0)
3a150e1d
JD
266 fatal_perror("check_sysemu : failed to modify "
267 "system call return");
1da177e4
LT
268 break;
269 }
f9dfefe4
BS
270 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
271 count++;
272 else
3a150e1d
JD
273 fatal("check_ptrace : expected SIGTRAP or "
274 "(SIGTRAP | 0x80), got status = %d", status);
1da177e4 275 }
b85e9680 276 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
1da177e4
LT
277 goto fail_stopped;
278
279 sysemu_supported = 2;
3a150e1d 280 non_fatal("OK\n");
1da177e4
LT
281
282 if ( !force_sysemu_disabled )
283 set_using_sysemu(sysemu_supported);
284 return;
285
286fail:
b85e9680 287 stop_ptraced_child(pid, stack, 1, 0);
1da177e4 288fail_stopped:
3a150e1d 289 non_fatal("missing\n");
1da177e4
LT
290}
291
60d339f6 292static void __init check_ptrace(void)
1da177e4 293{
b85e9680 294 void *stack;
1da177e4
LT
295 int pid, syscall, n, status;
296
3a150e1d 297 non_fatal("Checking that ptrace can change system call numbers...");
b85e9680 298 pid = start_ptraced_child(&stack);
1da177e4 299
3a150e1d
JD
300 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
301 (void *) PTRACE_O_TRACESYSGOOD) < 0))
302 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
1da177e4
LT
303
304 while(1){
305 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
3a150e1d
JD
306 fatal_perror("check_ptrace : ptrace failed");
307
1da177e4
LT
308 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
309 if(n < 0)
3a150e1d
JD
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);
60d339f6 316
1da177e4
LT
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)
3a150e1d
JD
323 fatal_perror("check_ptrace : failed to modify "
324 "system call");
1da177e4
LT
325 break;
326 }
327 }
b85e9680 328 stop_ptraced_child(pid, stack, 0, 1);
3a150e1d 329 non_fatal("OK\n");
1da177e4
LT
330 check_sysemu();
331}
332
966a082f 333extern void check_tmpexec(void);
0f80bc85 334
36e45463 335static void __init check_coredump_limit(void)
1d94cda0
JD
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
36e45463 356void __init os_early_checks(void)
1da177e4 357{
1d94cda0
JD
358 /* Print out the core dump limits early */
359 check_coredump_limit();
360
60d339f6 361 check_ptrace();
0f80bc85
JD
362
363 /* Need to check this early because mmapping happens before the
364 * kernel is running.
365 */
366 check_tmpexec();
1da177e4
LT
367}
368
d9838d86
BS
369static 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
381static 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
858259cf
BS
393static 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
858259cf 405static inline void check_skas3_ptrace_faultinfo(void)
1da177e4
LT
406{
407 struct ptrace_faultinfo fi;
b85e9680 408 void *stack;
d67b569f 409 int pid, n;
1da177e4 410
3a150e1d 411 non_fatal(" - PTRACE_FAULTINFO...");
b85e9680 412 pid = start_ptraced_child(&stack);
1da177e4
LT
413
414 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
415 if (n < 0) {
cb66504d 416 ptrace_faultinfo = 0;
1da177e4 417 if(errno == EIO)
3a150e1d 418 non_fatal("not found\n");
60d339f6 419 else
1da177e4 420 perror("not found");
d67b569f
JD
421 }
422 else {
cb66504d 423 if (!ptrace_faultinfo)
3a150e1d 424 non_fatal("found but disabled on command line\n");
cb66504d 425 else
3a150e1d 426 non_fatal("found\n");
1da177e4
LT
427 }
428
429 init_registers(pid);
b85e9680 430 stop_ptraced_child(pid, stack, 1, 1);
1da177e4
LT
431}
432
858259cf
BS
433static 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
3a150e1d 444 non_fatal(" - PTRACE_LDT...");
858259cf
BS
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)
3a150e1d 450 non_fatal("not found\n");
858259cf
BS
451 else {
452 perror("not found");
453 }
454 ptrace_ldt = 0;
455 }
456 else {
457 if(ptrace_ldt)
3a150e1d 458 non_fatal("found\n");
858259cf 459 else
3a150e1d 460 non_fatal("found, but use is disabled\n");
858259cf
BS
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
473static inline void check_skas3_proc_mm(void)
1da177e4 474{
3a150e1d 475 non_fatal(" - /proc/mm...");
73c8f444 476 if (access("/proc/mm", W_OK) < 0) {
9eae9b13 477 proc_mm = 0;
3a150e1d 478 perror("not found");
60d339f6
GS
479 }
480 else {
cb66504d 481 if (!proc_mm)
3a150e1d 482 non_fatal("found but disabled on command line\n");
cb66504d 483 else
3a150e1d 484 non_fatal("found\n");
1da177e4 485 }
858259cf
BS
486}
487
488int can_do_skas(void)
489{
3a150e1d 490 non_fatal("Checking for the skas3 patch in the host:\n");
858259cf
BS
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;
1da177e4 498
d67b569f 499 return 1;
1da177e4 500}
0f80bc85 501
0f80bc85
JD
502int __init parse_iomem(char *str, int *add)
503{
504 struct iomem_region *new;
73c8f444 505 struct stat64 buf;
0f80bc85 506 char *file, *driver;
73c8f444 507 int fd, size;
0f80bc85
JD
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++;
73c8f444 517 fd = open(file, O_RDWR, 0);
0f80bc85
JD
518 if(fd < 0){
519 os_print_error(fd, "parse_iomem - Couldn't open io file");
520 goto out;
521 }
522
73c8f444
JD
523 if(fstat64(fd, &buf) < 0){
524 perror("parse_iomem - cannot stat_fd file");
0f80bc85
JD
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
73c8f444 534 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
0f80bc85
JD
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
9eae9b13 545 return 0;
0f80bc85 546 out_close:
73c8f444 547 close(fd);
0f80bc85 548 out:
9eae9b13 549 return 1;
0f80bc85 550}
This page took 0.352093 seconds and 5 git commands to generate.