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