blob: 1244179e6c83d583660f9a935fc563a3b281909c [file] [log] [blame]
Gennady Sharapov60d339f62005-09-03 15:57:47 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
Jeff Dike8e367062006-03-27 01:14:32 -08006#include <pty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <stdio.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -07008#include <stddef.h>
9#include <stdarg.h>
10#include <stdlib.h>
11#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <unistd.h>
13#include <signal.h>
14#include <sched.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070015#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <sys/time.h>
18#include <sys/wait.h>
19#include <sys/mman.h>
Jeff Dike1d94cda2007-05-06 14:51:00 -070020#include <sys/resource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/unistd.h>
22#include <asm/page.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070023#include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "user_util.h"
25#include "kern_util.h"
26#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "signal_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "sysdep/ptrace.h"
29#include "sysdep/sigcontext.h"
30#include "irq_user.h"
31#include "ptrace_user.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070032#include "mem_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "init.h"
34#include "os.h"
35#include "uml-config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "choose-mode.h"
37#include "mode.h"
Jeff Diked67b5692005-07-07 17:56:49 -070038#include "tempfile.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070039#include "kern_constants.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef UML_CONFIG_MODE_SKAS
42#include "skas.h"
43#include "skas_ptrace.h"
44#include "registers.h"
45#endif
46
Jeff Dikeb85e9682005-07-28 21:16:01 -070047static int ptrace_child(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 int ret;
50 int pid = os_getpid(), ppid = getppid();
51 int sc_result;
52
Jeff Dike43b00fd2006-02-07 12:58:42 -080053 change_sig(SIGWINCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
55 perror("ptrace");
56 os_kill_process(pid, 0);
57 }
Jeff Dike73c8f4442007-02-10 01:44:20 -080058 kill(pid, SIGSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 /*This syscall will be intercepted by the parent. Don't call more than
61 * once, please.*/
62 sc_result = os_getpid();
63
64 if (sc_result == pid)
65 ret = 1; /*Nothing modified by the parent, we are running
66 normally.*/
67 else if (sc_result == ppid)
68 ret = 0; /*Expected in check_ptrace and check_sysemu when they
69 succeed in modifying the stack frame*/
70 else
71 ret = 2; /*Serious trouble! This could be caused by a bug in
72 host 2.6 SKAS3/2.6 patch before release -V6, together
73 with a bug in the UML code itself.*/
74 _exit(ret);
75}
76
Jeff Dike3a150e12007-02-10 01:44:28 -080077static void fatal_perror(char *str)
78{
79 perror(str);
80 exit(1);
81}
82
83static void 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 exit(1);
93}
94
95static void non_fatal(char *fmt, ...)
96{
97 va_list list;
98
99 va_start(list, fmt);
100 vprintf(fmt, list);
101 va_end(list);
102 fflush(stdout);
103}
104
Jeff Dikeb85e9682005-07-28 21:16:01 -0700105static int start_ptraced_child(void **stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700107 void *stack;
108 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int pid, n, status;
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700110
Jeff Dikeb85e9682005-07-28 21:16:01 -0700111 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
112 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
113 if(stack == MAP_FAILED)
Jeff Dike3a150e12007-02-10 01:44:28 -0800114 fatal_perror("check_ptrace : mmap failed");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700115 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
116 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if(pid < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800118 fatal_perror("start_ptraced_child : clone failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
120 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800121 fatal_perror("check_ptrace : clone failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800123 fatal("check_ptrace : expected SIGSTOP, got status = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 status);
125
Jeff Dikeb85e9682005-07-28 21:16:01 -0700126 *stack_out = stack;
Jeff Dike9eae9b12007-02-10 01:44:20 -0800127 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700130/* When testing for SYSEMU support, if it is one of the broken versions, we
131 * must just avoid using sysemu, not panic, but only if SYSEMU features are
132 * broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700134 * must work anyway!
135 */
136static int stop_ptraced_child(int pid, void *stack, int exitcode,
Jeff Dike3a150e12007-02-10 01:44:28 -0800137 int mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int status, n, ret = 0;
140
141 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800142 fatal_perror("stop_ptraced_child : ptrace failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 CATCH_EINTR(n = waitpid(pid, &status, 0));
144 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
145 int exit_with = WEXITSTATUS(status);
146 if (exit_with == 2)
Jeff Dike3a150e12007-02-10 01:44:28 -0800147 non_fatal("check_ptrace : child exited with status 2. "
148 "Serious trouble happening! Try updating "
149 "your host skas patch!\nDisabling SYSEMU "
150 "support.");
151 non_fatal("check_ptrace : child exited with exitcode %d, while "
152 "expecting %d; status 0x%x\n", exit_with,
153 exitcode, status);
154 if (mustexit)
155 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 ret = -1;
157 }
158
Jeff Dikeb85e9682005-07-28 21:16:01 -0700159 if(munmap(stack, PAGE_SIZE) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800160 fatal_perror("check_ptrace : munmap failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return ret;
162}
163
Jeff Dike7242a402007-02-10 01:44:19 -0800164/* Changed only during early boot */
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700165int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800166int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700167int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800168int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700169
170static int __init skas0_cmd_param(char *str, int* add)
171{
172 ptrace_faultinfo = proc_mm = 0;
173 return 0;
174}
175
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200176/* The two __uml_setup would conflict, without this stupid alias. */
177
178static int __init mode_skas0_cmd_param(char *str, int* add)
179 __attribute__((alias("skas0_cmd_param")));
180
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700181__uml_setup("skas0", skas0_cmd_param,
182 "skas0\n"
183 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
184 " you specify mode=tt.\n\n");
185
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200186__uml_setup("mode=skas0", mode_skas0_cmd_param,
187 "mode=skas0\n"
188 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
189 " specify mode=tt. Note that this was recently added - on \n"
190 " older kernels you must use simply \"skas0\".\n\n");
191
Jeff Dike7242a402007-02-10 01:44:19 -0800192/* Changed only during early boot */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700193static int force_sysemu_disabled = 0;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int __init nosysemu_cmd_param(char *str, int* add)
196{
197 force_sysemu_disabled = 1;
198 return 0;
199}
200
201__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700202"nosysemu\n"
203" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
204" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
205" behaviour of ptrace() and helps reducing host context switch rate.\n"
206" To make it working, you need a kernel patch for your host, too.\n"
207" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
208" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210static void __init check_sysemu(void)
211{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700212 void *stack;
Jeff Dike9eae9b12007-02-10 01:44:20 -0800213 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jeff Dike3a150e12007-02-10 01:44:28 -0800215 non_fatal("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 sysemu_supported = 0;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700217 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
220 goto fail;
221
222 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
223 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800224 fatal_perror("check_sysemu : wait failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800226 fatal("check_sysemu : expected SIGTRAP, got status = %d",
227 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
230 os_getpid());
231 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800232 fatal_perror("check_sysemu : failed to modify system call "
233 "return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jeff Dikeb85e9682005-07-28 21:16:01 -0700235 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto fail_stopped;
237
238 sysemu_supported = 1;
Jeff Dike3a150e12007-02-10 01:44:28 -0800239 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 set_using_sysemu(!force_sysemu_disabled);
241
Jeff Dike3a150e12007-02-10 01:44:28 -0800242 non_fatal("Checking advanced syscall emulation patch for ptrace...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700243 pid = start_ptraced_child(&stack);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700244
Jeff Dike3a150e12007-02-10 01:44:28 -0800245 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
246 (void *) PTRACE_O_TRACESYSGOOD) < 0))
247 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 while(1){
250 count++;
251 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
252 goto fail;
253 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
254 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800255 fatal_perror("check_ptrace : wait failed");
256
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700257 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!count)
Jeff Dike3a150e12007-02-10 01:44:28 -0800259 fatal("check_ptrace : SYSEMU_SINGLESTEP "
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700260 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
262 os_getpid());
263 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800264 fatal_perror("check_sysemu : failed to modify "
265 "system call return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267 }
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700268 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
269 count++;
270 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800271 fatal("check_ptrace : expected SIGTRAP or "
272 "(SIGTRAP | 0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700274 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto fail_stopped;
276
277 sysemu_supported = 2;
Jeff Dike3a150e12007-02-10 01:44:28 -0800278 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if ( !force_sysemu_disabled )
281 set_using_sysemu(sysemu_supported);
282 return;
283
284fail:
Jeff Dikeb85e9682005-07-28 21:16:01 -0700285 stop_ptraced_child(pid, stack, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286fail_stopped:
Jeff Dike3a150e12007-02-10 01:44:28 -0800287 non_fatal("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700290static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700292 void *stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int pid, syscall, n, status;
294
Jeff Dike3a150e12007-02-10 01:44:28 -0800295 non_fatal("Checking that ptrace can change system call numbers...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700296 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Jeff Dike3a150e12007-02-10 01:44:28 -0800298 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
299 (void *) PTRACE_O_TRACESYSGOOD) < 0))
300 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 while(1){
303 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800304 fatal_perror("check_ptrace : ptrace failed");
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
307 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800308 fatal_perror("check_ptrace : wait failed");
309
310 if(!WIFSTOPPED(status) ||
311 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
312 fatal("check_ptrace : expected (SIGTRAP|0x80), "
313 "got status = %d", status);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
316 0);
317 if(syscall == __NR_getpid){
318 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
319 __NR_getppid);
320 if(n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800321 fatal_perror("check_ptrace : failed to modify "
322 "system call");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
324 }
325 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700326 stop_ptraced_child(pid, stack, 0, 1);
Jeff Dike3a150e12007-02-10 01:44:28 -0800327 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 check_sysemu();
329}
330
Rob Landley966a082f2006-04-18 22:21:43 -0700331extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700332
Jeff Dike1d94cda2007-05-06 14:51:00 -0700333static void check_coredump_limit(void)
334{
335 struct rlimit lim;
336 int err = getrlimit(RLIMIT_CORE, &lim);
337
338 if(err){
339 perror("Getting core dump limit");
340 return;
341 }
342
343 printf("Core dump limits :\n\tsoft - ");
344 if(lim.rlim_cur == RLIM_INFINITY)
345 printf("NONE\n");
346 else printf("%lu\n", lim.rlim_cur);
347
348 printf("\thard - ");
349 if(lim.rlim_max == RLIM_INFINITY)
350 printf("NONE\n");
351 else printf("%lu\n", lim.rlim_max);
352}
353
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700354void os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jeff Dike1d94cda2007-05-06 14:51:00 -0700356 /* Print out the core dump limits early */
357 check_coredump_limit();
358
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700359 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700360
361 /* Need to check this early because mmapping happens before the
362 * kernel is running.
363 */
364 check_tmpexec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700367static int __init noprocmm_cmd_param(char *str, int* add)
368{
369 proc_mm = 0;
370 return 0;
371}
372
373__uml_setup("noprocmm", noprocmm_cmd_param,
374"noprocmm\n"
375" Turns off usage of /proc/mm, even if host supports it.\n"
376" To support /proc/mm, the host needs to be patched using\n"
377" the current skas3 patch.\n\n");
378
379static int __init noptracefaultinfo_cmd_param(char *str, int* add)
380{
381 ptrace_faultinfo = 0;
382 return 0;
383}
384
385__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
386"noptracefaultinfo\n"
387" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
388" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
389" using the current skas3 patch.\n\n");
390
Bodo Stroesser858259c2005-11-07 00:58:55 -0800391static int __init noptraceldt_cmd_param(char *str, int* add)
392{
393 ptrace_ldt = 0;
394 return 0;
395}
396
397__uml_setup("noptraceldt", noptraceldt_cmd_param,
398"noptraceldt\n"
399" Turns off usage of PTRACE_LDT, even if host supports it.\n"
400" To support PTRACE_LDT, the host needs to be patched using\n"
401" the current skas3 patch.\n\n");
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#ifdef UML_CONFIG_MODE_SKAS
Bodo Stroesser858259c2005-11-07 00:58:55 -0800404static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct ptrace_faultinfo fi;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700407 void *stack;
Jeff Diked67b5692005-07-07 17:56:49 -0700408 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jeff Dike3a150e12007-02-10 01:44:28 -0800410 non_fatal(" - PTRACE_FAULTINFO...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700411 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
414 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700415 ptrace_faultinfo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if(errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800417 non_fatal("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700418 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700420 }
421 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700422 if (!ptrace_faultinfo)
Jeff Dike3a150e12007-02-10 01:44:28 -0800423 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700424 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800425 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
428 init_registers(pid);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700429 stop_ptraced_child(pid, stack, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Bodo Stroesser858259c2005-11-07 00:58:55 -0800432static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800434#ifdef PTRACE_LDT
435 void *stack;
436 int pid, n;
437 unsigned char ldtbuf[40];
438 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
439 .func = 2, /* read default ldt */
440 .ptr = ldtbuf,
441 .bytecount = sizeof(ldtbuf)};
442
Jeff Dike3a150e12007-02-10 01:44:28 -0800443 non_fatal(" - PTRACE_LDT...");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800444 pid = start_ptraced_child(&stack);
445
446 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
447 if (n < 0) {
448 if(errno == EIO)
Jeff Dike3a150e12007-02-10 01:44:28 -0800449 non_fatal("not found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800450 else {
451 perror("not found");
452 }
453 ptrace_ldt = 0;
454 }
455 else {
456 if(ptrace_ldt)
Jeff Dike3a150e12007-02-10 01:44:28 -0800457 non_fatal("found\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800458 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800459 non_fatal("found, but use is disabled\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800460 }
461
462 stop_ptraced_child(pid, stack, 1, 1);
463#else
464 /* PTRACE_LDT might be disabled via cmdline option.
465 * We want to override this, else we might use the stub
466 * without real need
467 */
468 ptrace_ldt = 1;
469#endif
470}
471
472static inline void check_skas3_proc_mm(void)
473{
Jeff Dike3a150e12007-02-10 01:44:28 -0800474 non_fatal(" - /proc/mm...");
Jeff Dike73c8f4442007-02-10 01:44:20 -0800475 if (access("/proc/mm", W_OK) < 0) {
Jeff Dike9eae9b12007-02-10 01:44:20 -0800476 proc_mm = 0;
Jeff Dike3a150e12007-02-10 01:44:28 -0800477 perror("not found");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700478 }
479 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700480 if (!proc_mm)
Jeff Dike3a150e12007-02-10 01:44:28 -0800481 non_fatal("found but disabled on command line\n");
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700482 else
Jeff Dike3a150e12007-02-10 01:44:28 -0800483 non_fatal("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800485}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Bodo Stroesser858259c2005-11-07 00:58:55 -0800487int can_do_skas(void)
488{
Jeff Dike3a150e12007-02-10 01:44:28 -0800489 non_fatal("Checking for the skas3 patch in the host:\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800490
491 check_skas3_proc_mm();
492 check_skas3_ptrace_faultinfo();
493 check_skas3_ptrace_ldt();
494
495 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
496 skas_needs_stub = 1;
497
Jeff Diked67b5692005-07-07 17:56:49 -0700498 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500#else
501int can_do_skas(void)
502{
Jeff Dike9eae9b12007-02-10 01:44:20 -0800503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505#endif
Jeff Dike0f80bc82005-09-16 19:27:50 -0700506
Jeff Dike0f80bc82005-09-16 19:27:50 -0700507int __init parse_iomem(char *str, int *add)
508{
509 struct iomem_region *new;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800510 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700511 char *file, *driver;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800512 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700513
514 driver = str;
515 file = strchr(str,',');
516 if(file == NULL){
517 printf("parse_iomem : failed to parse iomem\n");
518 goto out;
519 }
520 *file = '\0';
521 file++;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800522 fd = open(file, O_RDWR, 0);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700523 if(fd < 0){
524 os_print_error(fd, "parse_iomem - Couldn't open io file");
525 goto out;
526 }
527
Jeff Dike73c8f4442007-02-10 01:44:20 -0800528 if(fstat64(fd, &buf) < 0){
529 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700530 goto out_close;
531 }
532
533 new = malloc(sizeof(*new));
534 if(new == NULL){
535 perror("Couldn't allocate iomem_region struct");
536 goto out_close;
537 }
538
Jeff Dike73c8f4442007-02-10 01:44:20 -0800539 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700540
541 *new = ((struct iomem_region) { .next = iomem_regions,
542 .driver = driver,
543 .fd = fd,
544 .size = size,
545 .phys = 0,
546 .virt = 0 });
547 iomem_regions = new;
548 iomem_size += new->size + UM_KERN_PAGE_SIZE;
549
Jeff Dike9eae9b12007-02-10 01:44:20 -0800550 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700551 out_close:
Jeff Dike73c8f4442007-02-10 01:44:20 -0800552 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700553 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800554 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700555}
556
Jeff Dike8e367062006-03-27 01:14:32 -0800557
558/* Changed during early boot */
559int pty_output_sigio = 0;
560int pty_close_sigio = 0;
561
562/* Used as a flag during SIGIO testing early in boot */
563static volatile int got_sigio = 0;
564
565static void __init handler(int sig)
566{
567 got_sigio = 1;
568}
569
570struct openpty_arg {
571 int master;
572 int slave;
573 int err;
574};
575
576static void openpty_cb(void *arg)
577{
578 struct openpty_arg *info = arg;
579
580 info->err = 0;
581 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
582 info->err = -errno;
583}
584
Jeff Dike73c8f4442007-02-10 01:44:20 -0800585static int async_pty(int master, int slave)
586{
587 int flags;
588
589 flags = fcntl(master, F_GETFL);
590 if(flags < 0)
591 return -errno;
592
593 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
594 (fcntl(master, F_SETOWN, os_getpid()) < 0))
595 return -errno;
596
597 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
598 return -errno;
599
600 return(0);
601}
602
Jeff Dike8e367062006-03-27 01:14:32 -0800603static void __init check_one_sigio(void (*proc)(int, int))
604{
605 struct sigaction old, new;
606 struct openpty_arg pty = { .master = -1, .slave = -1 };
607 int master, slave, err;
608
609 initial_thread_cb(openpty_cb, &pty);
610 if(pty.err){
611 printk("openpty failed, errno = %d\n", -pty.err);
612 return;
613 }
614
615 master = pty.master;
616 slave = pty.slave;
617
618 if((master == -1) || (slave == -1)){
619 printk("openpty failed to allocate a pty\n");
620 return;
621 }
622
623 /* Not now, but complain so we now where we failed. */
624 err = raw(master);
625 if (err < 0)
626 panic("check_sigio : __raw failed, errno = %d\n", -err);
627
Jeff Dike73c8f4442007-02-10 01:44:20 -0800628 err = async_pty(master, slave);
Jeff Dike8e367062006-03-27 01:14:32 -0800629 if(err < 0)
630 panic("tty_fds : sigio_async failed, err = %d\n", -err);
631
632 if(sigaction(SIGIO, NULL, &old) < 0)
633 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
634 new = old;
635 new.sa_handler = handler;
636 if(sigaction(SIGIO, &new, NULL) < 0)
637 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
638
639 got_sigio = 0;
640 (*proc)(master, slave);
641
642 close(master);
643 close(slave);
644
645 if(sigaction(SIGIO, &old, NULL) < 0)
646 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
647}
648
649static void tty_output(int master, int slave)
650{
651 int n;
652 char buf[512];
653
654 printk("Checking that host ptys support output SIGIO...");
655
656 memset(buf, 0, sizeof(buf));
657
658 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
659 if(errno != EAGAIN)
660 panic("check_sigio : write failed, errno = %d\n", errno);
661 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
662
663 if(got_sigio){
664 printk("Yes\n");
665 pty_output_sigio = 1;
666 }
667 else if(n == -EAGAIN) printk("No, enabling workaround\n");
668 else panic("check_sigio : read failed, err = %d\n", n);
669}
670
671static void tty_close(int master, int slave)
672{
673 printk("Checking that host ptys support SIGIO on close...");
674
675 close(slave);
676 if(got_sigio){
677 printk("Yes\n");
678 pty_close_sigio = 1;
679 }
680 else printk("No, enabling workaround\n");
681}
682
683void __init check_sigio(void)
684{
685 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
686 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
687 printk("No pseudo-terminals available - skipping pty SIGIO "
688 "check\n");
689 return;
690 }
691 check_one_sigio(tty_output);
692 check_one_sigio(tty_close);
693}
694
695void os_check_bugs(void)
696{
697 check_ptrace();
698 check_sigio();
Jeff Dike8e367062006-03-27 01:14:32 -0800699}
700