blob: 404bb63a74a9ae24274d94e7231ccb69dc36bd32 [file] [log] [blame]
Gennady Sharapov60d339f2005-09-03 15:57:47 -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/ptrace.h>
16#include <linux/ptrace.h>
17#include <sys/wait.h>
18#include <sys/mman.h>
19#include <asm/ptrace.h>
20#include <asm/unistd.h>
21#include <asm/page.h>
22#include "user_util.h"
23#include "kern_util.h"
24#include "user.h"
25#include "signal_kern.h"
Gennady Sharapov60d339f2005-09-03 15:57:47 -070026#include "sysdep/ptrace.h"
27#include "sysdep/sigcontext.h"
28#include "irq_user.h"
29#include "ptrace_user.h"
30#include "time_user.h"
31#include "init.h"
32#include "os.h"
33#include "uml-config.h"
34#include "choose-mode.h"
35#include "mode.h"
36#include "tempfile.h"
37
Jeff Dike0f80bc82005-09-16 19:27:50 -070038int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
39 int must_succeed)
40{
41 int err;
42
43 err = os_protect_memory((void *) addr, len, r, w, x);
44 if(err < 0){
45 if(must_succeed)
46 panic("protect failed, err = %d", -err);
47 else return(err);
48 }
49 return(0);
50}
51
Gennady Sharapovea2ba7d2006-01-08 01:01:31 -080052void kill_child_dead(int pid)
53{
54 kill(pid, SIGKILL);
55 kill(pid, SIGCONT);
56 do {
57 int n;
58 CATCH_EINTR(n = waitpid(pid, NULL, 0));
59 if (n > 0)
60 kill(pid, SIGCONT);
61 else
62 break;
63 } while(1);
64}
65
Gennady Sharapov4fef0c12006-01-18 17:42:41 -080066void stop(void)
67{
68 while(1) sleep(1000000);
69}
70
71int wait_for_stop(int pid, int sig, int cont_type, void *relay)
72{
73 sigset_t *relay_signals = relay;
74 int status, ret;
75
76 while(1){
77 CATCH_EINTR(ret = waitpid(pid, &status, WUNTRACED));
78 if((ret < 0) ||
79 !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
80 if(ret < 0){
81 printk("wait failed, errno = %d\n",
82 errno);
83 }
84 else if(WIFEXITED(status))
85 printk("process %d exited with status %d\n",
86 pid, WEXITSTATUS(status));
87 else if(WIFSIGNALED(status))
88 printk("process %d exited with signal %d\n",
89 pid, WTERMSIG(status));
90 else if((WSTOPSIG(status) == SIGVTALRM) ||
91 (WSTOPSIG(status) == SIGALRM) ||
92 (WSTOPSIG(status) == SIGIO) ||
93 (WSTOPSIG(status) == SIGPROF) ||
94 (WSTOPSIG(status) == SIGCHLD) ||
95 (WSTOPSIG(status) == SIGWINCH) ||
96 (WSTOPSIG(status) == SIGINT)){
97 ptrace(cont_type, pid, 0, WSTOPSIG(status));
98 continue;
99 }
100 else if((relay_signals != NULL) &&
101 sigismember(relay_signals, WSTOPSIG(status))){
102 ptrace(cont_type, pid, 0, WSTOPSIG(status));
103 continue;
104 }
105 else printk("process %d stopped with signal %d\n",
106 pid, WSTOPSIG(status));
107 panic("wait_for_stop failed to wait for %d to stop "
108 "with %d\n", pid, sig);
109 }
110 return(status);
111 }
112}
113
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700114/*
115 *-------------------------
116 * only for tt mode (will be deleted in future...)
117 *-------------------------
118 */
119
120struct tramp {
121 int (*tramp)(void *);
122 void *tramp_data;
123 unsigned long temp_stack;
124 int flags;
125 int pid;
126};
127
128/* See above for why sigkill is here */
129
130int sigkill = SIGKILL;
131
132int outer_tramp(void *arg)
133{
134 struct tramp *t;
135 int sig = sigkill;
136
137 t = arg;
138 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
139 t->flags, t->tramp_data);
140 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
141 kill(os_getpid(), sig);
142 _exit(0);
143}
144
145int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
146 int clone_flags, int (*tramp)(void *))
147{
148 struct tramp arg;
149 unsigned long sp;
150 int new_pid, status, err;
151
152 /* The trampoline will run on the temporary stack */
153 sp = stack_sp(temp_stack);
154
155 clone_flags |= CLONE_FILES | SIGCHLD;
156
157 arg.tramp = tramp;
158 arg.tramp_data = thread_arg;
159 arg.temp_stack = temp_stack;
160 arg.flags = clone_flags;
161
162 /* Start the process and wait for it to kill itself */
163 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
164 if(new_pid < 0)
165 return(new_pid);
166
167 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
168 if(err < 0)
169 panic("Waiting for outer trampoline failed - errno = %d",
170 errno);
171
172 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
173 panic("outer trampoline didn't exit with SIGKILL, "
174 "status = %d", status);
175
176 return(arg.pid);
177}
178
179void forward_pending_sigio(int target)
180{
181 sigset_t sigs;
182
183 if(sigpending(&sigs))
184 panic("forward_pending_sigio : sigpending failed");
185 if(sigismember(&sigs, SIGIO))
186 kill(target, SIGIO);
187}
188