uml: fix inlines
[deliverable/linux.git] / arch / um / os-Linux / start_up.c
CommitLineData
60d339f6 1/*
ba180fd4 2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4
LT
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
0f80bc85 7#include <stdlib.h>
ba180fd4 8#include <stdarg.h>
1da177e4 9#include <unistd.h>
1da177e4 10#include <errno.h>
ba180fd4
JD
11#include <fcntl.h>
12#include <sched.h>
13#include <signal.h>
14#include <string.h>
1da177e4 15#include <sys/mman.h>
ba180fd4
JD
16#include <sys/ptrace.h>
17#include <sys/stat.h>
18#include <sys/wait.h>
1da177e4 19#include <asm/unistd.h>
1da177e4 20#include "init.h"
0f80bc85 21#include "kern_constants.h"
ba180fd4
JD
22#include "os.h"
23#include "mem_user.h"
24#include "ptrace_user.h"
1da177e4 25#include "registers.h"
ba180fd4 26#include "skas_ptrace.h"
1da177e4 27
3cdaf455 28static int ptrace_child(void)
1da177e4
LT
29{
30 int ret;
31 int pid = os_getpid(), ppid = getppid();
32 int sc_result;
33
43b00fdb 34 change_sig(SIGWINCH, 0);
ba180fd4 35 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
1da177e4
LT
36 perror("ptrace");
37 os_kill_process(pid, 0);
38 }
73c8f444 39 kill(pid, SIGSTOP);
1da177e4 40
ba180fd4
JD
41 /*
42 * This syscall will be intercepted by the parent. Don't call more than
43 * once, please.
44 */
1da177e4
LT
45 sc_result = os_getpid();
46
47 if (sc_result == pid)
ba180fd4
JD
48 /* Nothing modified by the parent, we are running normally. */
49 ret = 1;
1da177e4 50 else if (sc_result == ppid)
ba180fd4
JD
51 /*
52 * Expected in check_ptrace and check_sysemu when they succeed
53 * in modifying the stack frame
54 */
55 ret = 0;
1da177e4 56 else
ba180fd4
JD
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;
1da177e4
LT
62 _exit(ret);
63}
64
3a150e1d
JD
65static void fatal_perror(char *str)
66{
67 perror(str);
68 exit(1);
69}
70
71static 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
83static 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
3cdaf455 93static int start_ptraced_child(void)
1da177e4 94{
1da177e4 95 int pid, n, status;
60d339f6 96
3cdaf455
JD
97 pid = fork();
98 if (pid == 0)
99 ptrace_child();
100 else if (pid < 0)
101 fatal_perror("start_ptraced_child : fork failed");
ba180fd4 102
1da177e4 103 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
ba180fd4 104 if (n < 0)
3cdaf455 105 fatal_perror("check_ptrace : waitpid failed");
ba180fd4 106 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
3a150e1d 107 fatal("check_ptrace : expected SIGSTOP, got status = %d",
1da177e4
LT
108 status);
109
9eae9b13 110 return pid;
1da177e4
LT
111}
112
60d339f6
GS
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.
1da177e4 116 * So only for SYSEMU features we test mustpanic, while normal host features
60d339f6
GS
117 * must work anyway!
118 */
3cdaf455 119static int stop_ptraced_child(int pid, int exitcode, int mustexit)
1da177e4
LT
120{
121 int status, n, ret = 0;
122
ba180fd4 123 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0)
3a150e1d 124 fatal_perror("stop_ptraced_child : ptrace failed");
1da177e4 125 CATCH_EINTR(n = waitpid(pid, &status, 0));
ba180fd4 126 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
1da177e4
LT
127 int exit_with = WEXITSTATUS(status);
128 if (exit_with == 2)
3a150e1d 129 non_fatal("check_ptrace : child exited with status 2. "
cf6acedb 130 "\nDisabling SYSEMU support.\n");
3a150e1d
JD
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);
1da177e4
LT
136 ret = -1;
137 }
138
1da177e4
LT
139 return ret;
140}
141
7242a400 142/* Changed only during early boot */
cb66504d 143int ptrace_faultinfo = 1;
858259cf 144int ptrace_ldt = 1;
cb66504d 145int proc_mm = 1;
858259cf 146int skas_needs_stub = 0;
cb66504d
PBG
147
148static int __init skas0_cmd_param(char *str, int* add)
149{
150 ptrace_faultinfo = proc_mm = 0;
151 return 0;
152}
153
9e3d862e
PBG
154/* The two __uml_setup would conflict, without this stupid alias. */
155
156static int __init mode_skas0_cmd_param(char *str, int* add)
157 __attribute__((alias("skas0_cmd_param")));
158
60d339f6
GS
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
9e3d862e
PBG
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
7242a400 170/* Changed only during early boot */
60d339f6
GS
171static int force_sysemu_disabled = 0;
172
1da177e4
LT
173static 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,
60d339f6
GS
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");
1da177e4
LT
187
188static void __init check_sysemu(void)
189{
cf6acedb 190 unsigned long regs[MAX_REG_NR];
9eae9b13 191 int pid, n, status, count=0;
1da177e4 192
3a150e1d 193 non_fatal("Checking syscall emulation patch for ptrace...");
1da177e4 194 sysemu_supported = 0;
3cdaf455 195 pid = start_ptraced_child();
1da177e4 196
ba180fd4 197 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
1da177e4
LT
198 goto fail;
199
200 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
201 if (n < 0)
3a150e1d 202 fatal_perror("check_sysemu : wait failed");
ba180fd4 203 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
3a150e1d
JD
204 fatal("check_sysemu : expected SIGTRAP, got status = %d",
205 status);
1da177e4 206
ba180fd4 207 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
cf6acedb 208 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
ba180fd4 209 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
cf6acedb
JD
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());
ba180fd4 216 if (n < 0) {
cf6acedb
JD
217 non_fatal("check_sysemu : failed to modify system call "
218 "return");
219 goto fail;
220 }
1da177e4 221
3cdaf455 222 if (stop_ptraced_child(pid, 0, 0) < 0)
1da177e4
LT
223 goto fail_stopped;
224
225 sysemu_supported = 1;
3a150e1d 226 non_fatal("OK\n");
1da177e4
LT
227 set_using_sysemu(!force_sysemu_disabled);
228
3a150e1d 229 non_fatal("Checking advanced syscall emulation patch for ptrace...");
3cdaf455 230 pid = start_ptraced_child();
f9dfefe4 231
ba180fd4 232 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
3a150e1d
JD
233 (void *) PTRACE_O_TRACESYSGOOD) < 0))
234 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
f9dfefe4 235
ba180fd4 236 while (1) {
1da177e4 237 count++;
ba180fd4 238 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
1da177e4
LT
239 goto fail;
240 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
ba180fd4 241 if (n < 0)
3a150e1d
JD
242 fatal_perror("check_ptrace : wait failed");
243
ba180fd4
JD
244 if (WIFSTOPPED(status) &&
245 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
1da177e4 246 if (!count)
3a150e1d 247 fatal("check_ptrace : SYSEMU_SINGLESTEP "
f9dfefe4 248 "doesn't singlestep");
1da177e4
LT
249 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
250 os_getpid());
ba180fd4 251 if (n < 0)
3a150e1d
JD
252 fatal_perror("check_sysemu : failed to modify "
253 "system call return");
1da177e4
LT
254 break;
255 }
ba180fd4 256 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
f9dfefe4
BS
257 count++;
258 else
3a150e1d
JD
259 fatal("check_ptrace : expected SIGTRAP or "
260 "(SIGTRAP | 0x80), got status = %d", status);
1da177e4 261 }
3cdaf455 262 if (stop_ptraced_child(pid, 0, 0) < 0)
1da177e4
LT
263 goto fail_stopped;
264
265 sysemu_supported = 2;
3a150e1d 266 non_fatal("OK\n");
1da177e4 267
ba180fd4 268 if (!force_sysemu_disabled)
1da177e4
LT
269 set_using_sysemu(sysemu_supported);
270 return;
271
272fail:
3cdaf455 273 stop_ptraced_child(pid, 1, 0);
1da177e4 274fail_stopped:
3a150e1d 275 non_fatal("missing\n");
1da177e4
LT
276}
277
60d339f6 278static void __init check_ptrace(void)
1da177e4 279{
1da177e4
LT
280 int pid, syscall, n, status;
281
3a150e1d 282 non_fatal("Checking that ptrace can change system call numbers...");
3cdaf455 283 pid = start_ptraced_child();
1da177e4 284
ba180fd4 285 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
3a150e1d
JD
286 (void *) PTRACE_O_TRACESYSGOOD) < 0))
287 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
1da177e4 288
ba180fd4
JD
289 while (1) {
290 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
3a150e1d
JD
291 fatal_perror("check_ptrace : ptrace failed");
292
1da177e4 293 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
ba180fd4 294 if (n < 0)
3a150e1d
JD
295 fatal_perror("check_ptrace : wait failed");
296
ba180fd4 297 if (!WIFSTOPPED(status) ||
3a150e1d
JD
298 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
299 fatal("check_ptrace : expected (SIGTRAP|0x80), "
300 "got status = %d", status);
60d339f6 301
1da177e4
LT
302 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
303 0);
ba180fd4 304 if (syscall == __NR_getpid) {
1da177e4
LT
305 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
306 __NR_getppid);
ba180fd4 307 if (n < 0)
3a150e1d
JD
308 fatal_perror("check_ptrace : failed to modify "
309 "system call");
1da177e4
LT
310 break;
311 }
312 }
3cdaf455 313 stop_ptraced_child(pid, 0, 1);
3a150e1d 314 non_fatal("OK\n");
1da177e4
LT
315 check_sysemu();
316}
317
966a082f 318extern void check_tmpexec(void);
0f80bc85 319
36e45463 320static void __init check_coredump_limit(void)
1d94cda0
JD
321{
322 struct rlimit lim;
323 int err = getrlimit(RLIMIT_CORE, &lim);
324
ba180fd4 325 if (err) {
1d94cda0
JD
326 perror("Getting core dump limit");
327 return;
328 }
329
330 printf("Core dump limits :\n\tsoft - ");
ba180fd4 331 if (lim.rlim_cur == RLIM_INFINITY)
1d94cda0
JD
332 printf("NONE\n");
333 else printf("%lu\n", lim.rlim_cur);
334
335 printf("\thard - ");
ba180fd4 336 if (lim.rlim_max == RLIM_INFINITY)
1d94cda0
JD
337 printf("NONE\n");
338 else printf("%lu\n", lim.rlim_max);
339}
340
36e45463 341void __init os_early_checks(void)
1da177e4 342{
1d94cda0
JD
343 /* Print out the core dump limits early */
344 check_coredump_limit();
345
60d339f6 346 check_ptrace();
0f80bc85
JD
347
348 /* Need to check this early because mmapping happens before the
349 * kernel is running.
350 */
351 check_tmpexec();
1da177e4
LT
352}
353
d9838d86
BS
354static 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
366static 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
858259cf
BS
378static 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
858259cf 390static inline void check_skas3_ptrace_faultinfo(void)
1da177e4
LT
391{
392 struct ptrace_faultinfo fi;
d67b569f 393 int pid, n;
1da177e4 394
3a150e1d 395 non_fatal(" - PTRACE_FAULTINFO...");
3cdaf455 396 pid = start_ptraced_child();
1da177e4
LT
397
398 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
399 if (n < 0) {
cb66504d 400 ptrace_faultinfo = 0;
ba180fd4 401 if (errno == EIO)
3a150e1d 402 non_fatal("not found\n");
60d339f6 403 else
1da177e4 404 perror("not found");
d67b569f
JD
405 }
406 else {
cb66504d 407 if (!ptrace_faultinfo)
3a150e1d 408 non_fatal("found but disabled on command line\n");
cb66504d 409 else
3a150e1d 410 non_fatal("found\n");
1da177e4
LT
411 }
412
413 init_registers(pid);
3cdaf455 414 stop_ptraced_child(pid, 1, 1);
1da177e4
LT
415}
416
858259cf
BS
417static inline void check_skas3_ptrace_ldt(void)
418{
419#ifdef PTRACE_LDT
858259cf
BS
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
3a150e1d 427 non_fatal(" - PTRACE_LDT...");
3cdaf455 428 pid = start_ptraced_child();
858259cf
BS
429
430 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
431 if (n < 0) {
ba180fd4 432 if (errno == EIO)
3a150e1d 433 non_fatal("not found\n");
858259cf
BS
434 else {
435 perror("not found");
436 }
437 ptrace_ldt = 0;
438 }
439 else {
ba180fd4 440 if (ptrace_ldt)
3a150e1d 441 non_fatal("found\n");
858259cf 442 else
3a150e1d 443 non_fatal("found, but use is disabled\n");
858259cf
BS
444 }
445
3cdaf455 446 stop_ptraced_child(pid, 1, 1);
858259cf
BS
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
456static inline void check_skas3_proc_mm(void)
1da177e4 457{
3a150e1d 458 non_fatal(" - /proc/mm...");
73c8f444 459 if (access("/proc/mm", W_OK) < 0) {
9eae9b13 460 proc_mm = 0;
3a150e1d 461 perror("not found");
60d339f6 462 }
ba180fd4
JD
463 else if (!proc_mm)
464 non_fatal("found but disabled on command line\n");
465 else non_fatal("found\n");
858259cf
BS
466}
467
468int can_do_skas(void)
469{
3a150e1d 470 non_fatal("Checking for the skas3 patch in the host:\n");
858259cf
BS
471
472 check_skas3_proc_mm();
473 check_skas3_ptrace_faultinfo();
474 check_skas3_ptrace_ldt();
475
ba180fd4 476 if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
858259cf 477 skas_needs_stub = 1;
1da177e4 478
d67b569f 479 return 1;
1da177e4 480}
0f80bc85 481
0f80bc85
JD
482int __init parse_iomem(char *str, int *add)
483{
484 struct iomem_region *new;
73c8f444 485 struct stat64 buf;
0f80bc85 486 char *file, *driver;
73c8f444 487 int fd, size;
0f80bc85
JD
488
489 driver = str;
490 file = strchr(str,',');
ba180fd4 491 if (file == NULL) {
0f80bc85
JD
492 printf("parse_iomem : failed to parse iomem\n");
493 goto out;
494 }
495 *file = '\0';
496 file++;
73c8f444 497 fd = open(file, O_RDWR, 0);
ba180fd4 498 if (fd < 0) {
0f80bc85
JD
499 os_print_error(fd, "parse_iomem - Couldn't open io file");
500 goto out;
501 }
502
ba180fd4 503 if (fstat64(fd, &buf) < 0) {
73c8f444 504 perror("parse_iomem - cannot stat_fd file");
0f80bc85
JD
505 goto out_close;
506 }
507
508 new = malloc(sizeof(*new));
ba180fd4 509 if (new == NULL) {
0f80bc85
JD
510 perror("Couldn't allocate iomem_region struct");
511 goto out_close;
512 }
513
73c8f444 514 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
0f80bc85
JD
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
9eae9b13 525 return 0;
0f80bc85 526 out_close:
73c8f444 527 close(fd);
0f80bc85 528 out:
9eae9b13 529 return 1;
0f80bc85 530}
This page took 0.3227 seconds and 5 git commands to generate.