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