[PATCH] uml: fix TT mode by reverting "use fork instead of clone"
[deliverable/linux.git] / arch / um / kernel / process.c
1 /*
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"
33 #include "choose-mode.h"
34 #include "mode.h"
35 #include "tempfile.h"
36 #ifdef UML_CONFIG_MODE_SKAS
37 #include "skas.h"
38 #include "skas_ptrace.h"
39 #include "registers.h"
40 #endif
41
42 void 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
54 void 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);
68 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
75 struct 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
85 int sigkill = SIGKILL;
86
87 int 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
100 int 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
134 static int ptrace_child(void *arg)
135 {
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
163 static int start_ptraced_child(void **stack_out)
164 {
165 void *stack;
166 unsigned long sp;
167 int pid, n, status;
168
169 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
170 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
171 if(stack == MAP_FAILED)
172 panic("check_ptrace : mmap failed, errno = %d", errno);
173 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
174 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
175 if(pid < 0)
176 panic("check_ptrace : clone failed, errno = %d", errno);
177 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
178 if(n < 0)
179 panic("check_ptrace : wait failed, errno = %d", errno);
180 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
181 panic("check_ptrace : expected SIGSTOP, got status = %d",
182 status);
183
184 *stack_out = stack;
185 return(pid);
186 }
187
188 /* When testing for SYSEMU support, if it is one of the broken versions, we must
189 * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
190 * So only for SYSEMU features we test mustpanic, while normal host features
191 * must work anyway!*/
192 static int stop_ptraced_child(int pid, void *stack, int exitcode, int mustpanic)
193 {
194 int status, n, ret = 0;
195
196 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
197 panic("check_ptrace : ptrace failed, errno = %d", errno);
198 CATCH_EINTR(n = waitpid(pid, &status, 0));
199 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
200 int exit_with = WEXITSTATUS(status);
201 if (exit_with == 2)
202 printk("check_ptrace : child exited with status 2. "
203 "Serious trouble happening! Try updating your "
204 "host skas patch!\nDisabling SYSEMU support.");
205 printk("check_ptrace : child exited with exitcode %d, while "
206 "expecting %d; status 0x%x", exit_with,
207 exitcode, status);
208 if (mustpanic)
209 panic("\n");
210 else
211 printk("\n");
212 ret = -1;
213 }
214
215 if(munmap(stack, PAGE_SIZE) < 0)
216 panic("check_ptrace : munmap failed, errno = %d", errno);
217 return ret;
218 }
219
220 static int force_sysemu_disabled = 0;
221
222 int ptrace_faultinfo = 1;
223 int proc_mm = 1;
224
225 static int __init skas0_cmd_param(char *str, int* add)
226 {
227 ptrace_faultinfo = proc_mm = 0;
228 return 0;
229 }
230
231 static int __init nosysemu_cmd_param(char *str, int* add)
232 {
233 force_sysemu_disabled = 1;
234 return 0;
235 }
236
237 __uml_setup("skas0", skas0_cmd_param,
238 "skas0\n"
239 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
240 " specify mode=tt.\n\n");
241
242 __uml_setup("nosysemu", nosysemu_cmd_param,
243 "nosysemu\n"
244 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
245 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
246 " behaviour of ptrace() and helps reducing host context switch rate.\n"
247 " To make it working, you need a kernel patch for your host, too.\n"
248 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
249
250 static void __init check_sysemu(void)
251 {
252 void *stack;
253 int pid, syscall, n, status, count=0;
254
255 printk("Checking syscall emulation patch for ptrace...");
256 sysemu_supported = 0;
257 pid = start_ptraced_child(&stack);
258
259 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
260 goto fail;
261
262 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
263 if (n < 0)
264 panic("check_sysemu : wait failed, errno = %d", errno);
265 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
266 panic("check_sysemu : expected SIGTRAP, "
267 "got status = %d", status);
268
269 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
270 os_getpid());
271 if(n < 0)
272 panic("check_sysemu : failed to modify system "
273 "call return, errno = %d", errno);
274
275 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
276 goto fail_stopped;
277
278 sysemu_supported = 1;
279 printk("OK\n");
280 set_using_sysemu(!force_sysemu_disabled);
281
282 printk("Checking advanced syscall emulation patch for ptrace...");
283 pid = start_ptraced_child(&stack);
284 while(1){
285 count++;
286 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
287 goto fail;
288 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
289 if(n < 0)
290 panic("check_ptrace : wait failed, errno = %d", errno);
291 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
292 panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
293 "got status = %d", status);
294
295 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
296 0);
297 if(syscall == __NR_getpid){
298 if (!count)
299 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
300 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
301 os_getpid());
302 if(n < 0)
303 panic("check_sysemu : failed to modify system "
304 "call return, errno = %d", errno);
305 break;
306 }
307 }
308 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
309 goto fail_stopped;
310
311 sysemu_supported = 2;
312 printk("OK\n");
313
314 if ( !force_sysemu_disabled )
315 set_using_sysemu(sysemu_supported);
316 return;
317
318 fail:
319 stop_ptraced_child(pid, stack, 1, 0);
320 fail_stopped:
321 printk("missing\n");
322 }
323
324 void __init check_ptrace(void)
325 {
326 void *stack;
327 int pid, syscall, n, status;
328
329 printk("Checking that ptrace can change system call numbers...");
330 pid = start_ptraced_child(&stack);
331
332 if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
333 panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
334
335 while(1){
336 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
337 panic("check_ptrace : ptrace failed, errno = %d",
338 errno);
339 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
340 if(n < 0)
341 panic("check_ptrace : wait failed, errno = %d", errno);
342 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
343 panic("check_ptrace : expected SIGTRAP + 0x80, "
344 "got status = %d", status);
345
346 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
347 0);
348 if(syscall == __NR_getpid){
349 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
350 __NR_getppid);
351 if(n < 0)
352 panic("check_ptrace : failed to modify system "
353 "call, errno = %d", errno);
354 break;
355 }
356 }
357 stop_ptraced_child(pid, stack, 0, 1);
358 printk("OK\n");
359 check_sysemu();
360 }
361
362 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
363 {
364 sigjmp_buf buf;
365 int n;
366
367 *jmp_ptr = &buf;
368 n = sigsetjmp(buf, 1);
369 if(n != 0)
370 return(n);
371 (*fn)(arg);
372 return(0);
373 }
374
375 void forward_pending_sigio(int target)
376 {
377 sigset_t sigs;
378
379 if(sigpending(&sigs))
380 panic("forward_pending_sigio : sigpending failed");
381 if(sigismember(&sigs, SIGIO))
382 kill(target, SIGIO);
383 }
384
385 extern void *__syscall_stub_start, __syscall_stub_end;
386
387 #ifdef UML_CONFIG_MODE_SKAS
388
389 static inline void check_skas3_ptrace_support(void)
390 {
391 struct ptrace_faultinfo fi;
392 void *stack;
393 int pid, n;
394
395 printf("Checking for the skas3 patch in the host...");
396 pid = start_ptraced_child(&stack);
397
398 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
399 if (n < 0) {
400 ptrace_faultinfo = 0;
401 if(errno == EIO)
402 printf("not found\n");
403 else {
404 perror("not found");
405 }
406 }
407 else {
408 if (!ptrace_faultinfo)
409 printf("found but disabled on command line\n");
410 else
411 printf("found\n");
412 }
413
414 init_registers(pid);
415 stop_ptraced_child(pid, stack, 1, 1);
416 }
417
418 int can_do_skas(void)
419 {
420 printf("Checking for /proc/mm...");
421 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
422 proc_mm = 0;
423 printf("not found\n");
424 } else {
425 if (!proc_mm)
426 printf("found but disabled on command line\n");
427 else
428 printf("found\n");
429 }
430
431 check_skas3_ptrace_support();
432 return 1;
433 }
434 #else
435 int can_do_skas(void)
436 {
437 return(0);
438 }
439 #endif
This page took 0.046776 seconds and 5 git commands to generate.