blob: c45a60e9c92d14237c86a7522efc8d0da0a48292 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <signal.h>
9#include <sched.h>
10#include <errno.h>
11#include <stdarg.h>
12#include <stdlib.h>
13#include <setjmp.h>
14#include <sys/time.h>
15#include <sys/wait.h>
16#include <sys/mman.h>
17#include <asm/unistd.h>
18#include <asm/page.h>
19#include "user_util.h"
20#include "kern_util.h"
21#include "user.h"
22#include "process.h"
23#include "signal_kern.h"
24#include "signal_user.h"
25#include "sysdep/ptrace.h"
26#include "sysdep/sigcontext.h"
27#include "irq_user.h"
28#include "ptrace_user.h"
29#include "time_user.h"
30#include "init.h"
31#include "os.h"
32#include "uml-config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "choose-mode.h"
34#include "mode.h"
Jeff Diked67b5692005-07-07 17:56:49 -070035#include "tempfile.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#ifdef UML_CONFIG_MODE_SKAS
37#include "skas.h"
38#include "skas_ptrace.h"
39#include "registers.h"
40#endif
41
42void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
43{
44 int flags = 0, pages;
45
46 if(sig_stack != NULL){
47 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
48 set_sigstack(sig_stack, pages * page_size());
49 flags = SA_ONSTACK;
50 }
51 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
52}
53
54void init_new_thread_signals(int altstack)
55{
56 int flags = altstack ? SA_ONSTACK : 0;
57
58 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
59 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
60 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
61 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
62 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
63 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
64 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
65 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
66 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
67 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
69 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
70 signal(SIGHUP, SIG_IGN);
71
72 init_irq_signals(altstack);
73}
74
75struct tramp {
76 int (*tramp)(void *);
77 void *tramp_data;
78 unsigned long temp_stack;
79 int flags;
80 int pid;
81};
82
83/* See above for why sigkill is here */
84
85int sigkill = SIGKILL;
86
87int outer_tramp(void *arg)
88{
89 struct tramp *t;
90 int sig = sigkill;
91
92 t = arg;
93 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
94 t->flags, t->tramp_data);
95 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
96 kill(os_getpid(), sig);
97 _exit(0);
98}
99
100int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
101 int clone_flags, int (*tramp)(void *))
102{
103 struct tramp arg;
104 unsigned long sp;
105 int new_pid, status, err;
106
107 /* The trampoline will run on the temporary stack */
108 sp = stack_sp(temp_stack);
109
110 clone_flags |= CLONE_FILES | SIGCHLD;
111
112 arg.tramp = tramp;
113 arg.tramp_data = thread_arg;
114 arg.temp_stack = temp_stack;
115 arg.flags = clone_flags;
116
117 /* Start the process and wait for it to kill itself */
118 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
119 if(new_pid < 0)
120 return(new_pid);
121
122 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
123 if(err < 0)
124 panic("Waiting for outer trampoline failed - errno = %d",
125 errno);
126
127 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
128 panic("outer trampoline didn't exit with SIGKILL, "
129 "status = %d", status);
130
131 return(arg.pid);
132}
133
Jeff Dike98fdffc2005-06-13 15:52:14 -0700134static int ptrace_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 int ret;
137 int pid = os_getpid(), ppid = getppid();
138 int sc_result;
139
140 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
141 perror("ptrace");
142 os_kill_process(pid, 0);
143 }
144 os_stop_process(pid);
145
146 /*This syscall will be intercepted by the parent. Don't call more than
147 * once, please.*/
148 sc_result = os_getpid();
149
150 if (sc_result == pid)
151 ret = 1; /*Nothing modified by the parent, we are running
152 normally.*/
153 else if (sc_result == ppid)
154 ret = 0; /*Expected in check_ptrace and check_sysemu when they
155 succeed in modifying the stack frame*/
156 else
157 ret = 2; /*Serious trouble! This could be caused by a bug in
158 host 2.6 SKAS3/2.6 patch before release -V6, together
159 with a bug in the UML code itself.*/
160 _exit(ret);
161}
162
Jeff Dike98fdffc2005-06-13 15:52:14 -0700163static int start_ptraced_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int pid, n, status;
166
Jeff Dike98fdffc2005-06-13 15:52:14 -0700167 pid = fork();
168 if(pid == 0)
169 ptrace_child();
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if(pid < 0)
Jeff Dike98fdffc2005-06-13 15:52:14 -0700172 panic("check_ptrace : fork failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
174 if(n < 0)
175 panic("check_ptrace : wait failed, errno = %d", errno);
176 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
177 panic("check_ptrace : expected SIGSTOP, got status = %d",
178 status);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return(pid);
181}
182
183/* When testing for SYSEMU support, if it is one of the broken versions, we must
184 * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
185 * So only for SYSEMU features we test mustpanic, while normal host features
186 * must work anyway!*/
Jeff Dike98fdffc2005-06-13 15:52:14 -0700187static int stop_ptraced_child(int pid, int exitcode, int mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int status, n, ret = 0;
190
191 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dike98fdffc2005-06-13 15:52:14 -0700192 panic("stop_ptraced_child : ptrace failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 CATCH_EINTR(n = waitpid(pid, &status, 0));
194 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
195 int exit_with = WEXITSTATUS(status);
196 if (exit_with == 2)
197 printk("check_ptrace : child exited with status 2. "
198 "Serious trouble happening! Try updating your "
199 "host skas patch!\nDisabling SYSEMU support.");
200 printk("check_ptrace : child exited with exitcode %d, while "
201 "expecting %d; status 0x%x", exit_with,
202 exitcode, status);
Jeff Dike98fdffc2005-06-13 15:52:14 -0700203 if (mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 panic("\n");
205 else
206 printk("\n");
207 ret = -1;
208 }
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return ret;
211}
212
213static int force_sysemu_disabled = 0;
214
215static int __init nosysemu_cmd_param(char *str, int* add)
216{
217 force_sysemu_disabled = 1;
218 return 0;
219}
220
221__uml_setup("nosysemu", nosysemu_cmd_param,
222 "nosysemu\n"
223 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
224 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
225 " behaviour of ptrace() and helps reducing host context switch rate.\n"
226 " To make it working, you need a kernel patch for your host, too.\n"
227 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
228
229static void __init check_sysemu(void)
230{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int pid, syscall, n, status, count=0;
232
233 printk("Checking syscall emulation patch for ptrace...");
234 sysemu_supported = 0;
Jeff Dike98fdffc2005-06-13 15:52:14 -0700235 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
238 goto fail;
239
240 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
241 if (n < 0)
242 panic("check_sysemu : wait failed, errno = %d", errno);
243 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
244 panic("check_sysemu : expected SIGTRAP, "
245 "got status = %d", status);
246
247 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
248 os_getpid());
249 if(n < 0)
250 panic("check_sysemu : failed to modify system "
251 "call return, errno = %d", errno);
252
Jeff Dike98fdffc2005-06-13 15:52:14 -0700253 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto fail_stopped;
255
256 sysemu_supported = 1;
257 printk("OK\n");
258 set_using_sysemu(!force_sysemu_disabled);
259
260 printk("Checking advanced syscall emulation patch for ptrace...");
Jeff Dike98fdffc2005-06-13 15:52:14 -0700261 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 while(1){
263 count++;
264 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
265 goto fail;
266 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
267 if(n < 0)
268 panic("check_ptrace : wait failed, errno = %d", errno);
269 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
270 panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
271 "got status = %d", status);
272
273 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
274 0);
275 if(syscall == __NR_getpid){
276 if (!count)
277 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
278 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
279 os_getpid());
280 if(n < 0)
281 panic("check_sysemu : failed to modify system "
282 "call return, errno = %d", errno);
283 break;
284 }
285 }
Jeff Dike98fdffc2005-06-13 15:52:14 -0700286 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 goto fail_stopped;
288
289 sysemu_supported = 2;
290 printk("OK\n");
291
292 if ( !force_sysemu_disabled )
293 set_using_sysemu(sysemu_supported);
294 return;
295
296fail:
Jeff Dike98fdffc2005-06-13 15:52:14 -0700297 stop_ptraced_child(pid, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298fail_stopped:
299 printk("missing\n");
300}
301
302void __init check_ptrace(void)
303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int pid, syscall, n, status;
305
306 printk("Checking that ptrace can change system call numbers...");
Jeff Dike98fdffc2005-06-13 15:52:14 -0700307 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
310 panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
311
312 while(1){
313 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
314 panic("check_ptrace : ptrace failed, errno = %d",
315 errno);
316 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
317 if(n < 0)
318 panic("check_ptrace : wait failed, errno = %d", errno);
319 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
320 panic("check_ptrace : expected SIGTRAP + 0x80, "
321 "got status = %d", status);
322
323 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
324 0);
325 if(syscall == __NR_getpid){
326 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
327 __NR_getppid);
328 if(n < 0)
329 panic("check_ptrace : failed to modify system "
330 "call, errno = %d", errno);
331 break;
332 }
333 }
Jeff Dike98fdffc2005-06-13 15:52:14 -0700334 stop_ptraced_child(pid, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 printk("OK\n");
336 check_sysemu();
337}
338
339int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
340{
341 sigjmp_buf buf;
342 int n;
343
344 *jmp_ptr = &buf;
345 n = sigsetjmp(buf, 1);
346 if(n != 0)
347 return(n);
348 (*fn)(arg);
349 return(0);
350}
351
352void forward_pending_sigio(int target)
353{
354 sigset_t sigs;
355
356 if(sigpending(&sigs))
357 panic("forward_pending_sigio : sigpending failed");
358 if(sigismember(&sigs, SIGIO))
359 kill(target, SIGIO);
360}
361
Jeff Diked67b5692005-07-07 17:56:49 -0700362int ptrace_faultinfo = 0;
363int proc_mm = 1;
364
365extern void *__syscall_stub_start, __syscall_stub_end;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#ifdef UML_CONFIG_MODE_SKAS
Jeff Diked67b5692005-07-07 17:56:49 -0700368static inline void check_skas3_ptrace_support(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct ptrace_faultinfo fi;
Jeff Diked67b5692005-07-07 17:56:49 -0700371 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 printf("Checking for the skas3 patch in the host...");
Jeff Dike98fdffc2005-06-13 15:52:14 -0700374 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
377 if (n < 0) {
378 if(errno == EIO)
379 printf("not found\n");
380 else {
381 perror("not found");
382 }
Jeff Diked67b5692005-07-07 17:56:49 -0700383 }
384 else {
385 ptrace_faultinfo = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 printf("found\n");
387 }
388
389 init_registers(pid);
Jeff Dike98fdffc2005-06-13 15:52:14 -0700390 stop_ptraced_child(pid, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393int can_do_skas(void)
394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 printf("Checking for /proc/mm...");
396 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
Jeff Diked67b5692005-07-07 17:56:49 -0700397 proc_mm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 printf("not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto out;
Jeff Diked67b5692005-07-07 17:56:49 -0700400 }
401 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 printf("found\n");
403 }
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405out:
Jeff Diked67b5692005-07-07 17:56:49 -0700406 check_skas3_ptrace_support();
407 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409#else
410int can_do_skas(void)
411{
412 return(0);
413}
414#endif