2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/sched.h"
7 #include "linux/signal.h"
8 #include "linux/kernel.h"
9 #include "linux/interrupt.h"
10 #include "linux/ptrace.h"
11 #include "asm/system.h"
12 #include "asm/pgalloc.h"
13 #include "asm/ptrace.h"
14 #include "asm/tlbflush.h"
16 #include "kern_util.h"
19 #include "sigcontext.h"
23 #include "mode_kern.h"
27 void switch_to_tt(void *prev, void *next)
29 struct task_struct *from, *to, *prev_sched;
31 int err, vtalrm, alrm, prof, cpu;
37 cpu = task_thread_info(from)->cpu;
39 forward_interrupts(to->thread.mode.tt.extern_pid);
41 forward_ipi(cpu_data[cpu].ipi_pipe[0], to->thread.mode.tt.extern_pid);
43 local_irq_save(flags);
45 vtalrm = change_sig(SIGVTALRM, 0);
46 alrm = change_sig(SIGALRM, 0);
47 prof = change_sig(SIGPROF, 0);
49 forward_pending_sigio(to->thread.mode.tt.extern_pid);
53 /* Notice that here we "up" the semaphore on which "to" is waiting, and
54 * below (the read) we wait on this semaphore (which is implemented by
55 * switch_pipe) and go sleeping. Thus, after that, we have resumed in
56 * "to", and can't use any more the value of "from" (which is outdated),
57 * nor the value in "to" (since it was the task which stole us the CPU,
58 * which we don't care about). */
60 err = os_write_file(to->thread.mode.tt.switch_pipe[1], &c, sizeof(c));
62 panic("write of switch_pipe failed, err = %d", -err);
64 if(from->thread.mode.tt.switch_pipe[0] == -1)
65 os_kill_process(os_getpid(), 0);
67 err = os_read_file(from->thread.mode.tt.switch_pipe[0], &c, sizeof(c));
69 panic("read of switch_pipe failed, errno = %d", -err);
71 /* If the process that we have just scheduled away from has exited,
72 * then it needs to be killed here. The reason is that, even though
73 * it will kill itself when it next runs, that may be too late. Its
74 * stack will be freed, possibly before then, and if that happens,
75 * we have a use-after-free situation. So, it gets killed here
76 * in case it has not already killed itself.
78 prev_sched = current->thread.prev_sched;
79 if(prev_sched->thread.mode.tt.switch_pipe[0] == -1)
80 os_kill_process(prev_sched->thread.mode.tt.extern_pid, 1);
82 change_sig(SIGVTALRM, vtalrm);
83 change_sig(SIGALRM, alrm);
84 change_sig(SIGPROF, prof);
86 arch_switch_to_tt(prev_sched, current);
89 local_irq_restore(flags);
92 void release_thread_tt(struct task_struct *task)
94 int pid = task->thread.mode.tt.extern_pid;
97 * We first have to kill the other process, before
98 * closing its switch_pipe. Else it might wake up
99 * and receive "EOF" before we could kill it.
101 if(os_getpid() != pid)
102 os_kill_process(pid, 0);
104 os_close_file(task->thread.mode.tt.switch_pipe[0]);
105 os_close_file(task->thread.mode.tt.switch_pipe[1]);
106 /* use switch_pipe as flag: thread is released */
107 task->thread.mode.tt.switch_pipe[0] = -1;
110 void suspend_new_thread(int fd)
115 os_stop_process(os_getpid());
116 err = os_read_file(fd, &c, sizeof(c));
118 panic("read failed in suspend_new_thread, err = %d", -err);
121 void schedule_tail(struct task_struct *prev);
123 static void new_thread_handler(int sig)
125 unsigned long disable;
129 fn = current->thread.request.u.thread.proc;
130 arg = current->thread.request.u.thread.arg;
132 UPT_SC(¤t->thread.regs.regs) = (void *) (&sig + 1);
133 disable = (1 << (SIGVTALRM - 1)) | (1 << (SIGALRM - 1)) |
134 (1 << (SIGIO - 1)) | (1 << (SIGPROF - 1));
135 SC_SIGMASK(UPT_SC(¤t->thread.regs.regs)) &= ~disable;
137 suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
140 if(current->thread.prev_sched != NULL)
141 schedule_tail(current->thread.prev_sched);
142 current->thread.prev_sched = NULL;
144 init_new_thread_signals();
146 free_page(current->thread.temp_stack);
147 set_cmdline("(kernel thread)");
149 change_sig(SIGUSR1, 1);
150 change_sig(SIGPROF, 1);
152 if(!run_kernel_thread(fn, arg, ¤t->thread.exec_buf))
155 /* XXX No set_user_mode here because a newly execed process will
156 * immediately segfault on its non-existent IP, coming straight back
157 * to the signal handler, which will call set_user_mode on its way
158 * out. This should probably change since it's confusing.
162 static int new_thread_proc(void *stack)
164 /* local_irq_disable is needed to block out signals until this thread is
165 * properly scheduled. Otherwise, the tracing thread will get mighty
166 * upset about any signals that arrive before that.
167 * This has the complication that it sets the saved signal mask in
168 * the sigcontext to block signals. This gets restored when this
169 * thread (or a descendant, since they get a copy of this sigcontext)
170 * returns to userspace.
171 * So, this is compensated for elsewhere.
172 * XXX There is still a small window until local_irq_disable() actually
173 * finishes where signals are possible - shouldn't be a problem in
174 * practice since SIGIO hasn't been forwarded here yet, and the
175 * local_irq_disable should finish before a SIGVTALRM has time to be
180 init_new_thread_stack(stack, new_thread_handler);
181 os_usr1_process(os_getpid());
182 change_sig(SIGUSR1, 1);
186 /* Signal masking - signals are blocked at the start of fork_tramp. They
187 * are re-enabled when finish_fork_handler is entered by fork_tramp hitting
188 * itself with a SIGUSR1. set_user_mode has to be run with SIGUSR1 off,
189 * so it is blocked before it's called. They are re-enabled on sigreturn
190 * despite the fact that they were blocked when the SIGUSR1 was issued because
191 * copy_thread copies the parent's sigcontext, including the signal mask
192 * onto the signal frame.
195 void finish_fork_handler(int sig)
197 UPT_SC(¤t->thread.regs.regs) = (void *) (&sig + 1);
198 suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
201 if(current->thread.prev_sched != NULL)
202 schedule_tail(current->thread.prev_sched);
203 current->thread.prev_sched = NULL;
206 change_sig(SIGVTALRM, 1);
208 if(current->mm != current->parent->mm)
209 protect_memory(uml_reserved, high_physmem - uml_reserved, 1,
211 task_protections((unsigned long) current_thread);
213 free_page(current->thread.temp_stack);
215 change_sig(SIGUSR1, 0);
216 set_user_mode(current);
219 int fork_tramp(void *stack)
223 init_new_thread_stack(stack, finish_fork_handler);
225 os_usr1_process(os_getpid());
226 change_sig(SIGUSR1, 1);
230 int copy_thread_tt(int nr, unsigned long clone_flags, unsigned long sp,
231 unsigned long stack_top, struct task_struct * p,
232 struct pt_regs *regs)
234 int (*tramp)(void *);
238 if(current->thread.forking)
241 tramp = new_thread_proc;
242 p->thread.request.u.thread = current->thread.request.u.thread;
245 err = os_pipe(p->thread.mode.tt.switch_pipe, 1, 1);
247 printk("copy_thread : pipe failed, err = %d\n", -err);
251 stack = alloc_stack(0, 0);
253 printk(KERN_ERR "copy_thread : failed to allocate "
254 "temporary stack\n");
258 clone_flags &= CLONE_VM;
259 p->thread.temp_stack = stack;
260 new_pid = start_fork_tramp(task_stack_page(p), stack, clone_flags, tramp);
262 printk(KERN_ERR "copy_thread : clone failed - errno = %d\n",
267 if(current->thread.forking){
268 sc_to_sc(UPT_SC(&p->thread.regs.regs), UPT_SC(®s->regs));
269 SC_SET_SYSCALL_RETURN(UPT_SC(&p->thread.regs.regs), 0);
271 SC_SP(UPT_SC(&p->thread.regs.regs)) = sp;
273 p->thread.mode.tt.extern_pid = new_pid;
275 current->thread.request.op = OP_FORK;
276 current->thread.request.u.fork.pid = new_pid;
277 os_usr1_process(os_getpid());
279 /* Enable the signal and then disable it to ensure that it is handled
280 * here, and nowhere else.
282 change_sig(SIGUSR1, 1);
284 change_sig(SIGUSR1, 0);
291 current->thread.request.op = OP_REBOOT;
292 os_usr1_process(os_getpid());
293 change_sig(SIGUSR1, 1);
298 current->thread.request.op = OP_HALT;
299 os_usr1_process(os_getpid());
300 change_sig(SIGUSR1, 1);
303 void kill_off_processes_tt(void)
305 struct task_struct *p;
310 if(p->thread.mode.tt.extern_pid != me)
311 os_kill_process(p->thread.mode.tt.extern_pid, 0);
313 if(init_task.thread.mode.tt.extern_pid != me)
314 os_kill_process(init_task.thread.mode.tt.extern_pid, 0);
317 void initial_thread_cb_tt(void (*proc)(void *), void *arg)
319 if(os_getpid() == tracing_pid){
323 current->thread.request.op = OP_CB;
324 current->thread.request.u.cb.proc = proc;
325 current->thread.request.u.cb.arg = arg;
326 os_usr1_process(os_getpid());
327 change_sig(SIGUSR1, 1);
329 change_sig(SIGUSR1, 0);
333 int do_proc_op(void *t, int proc_id)
335 struct task_struct *task;
336 struct thread_struct *thread;
340 thread = &task->thread;
341 op = thread->request.op;
347 pid = thread->request.u.exec.pid;
348 do_exec(thread->mode.tt.extern_pid, pid);
349 thread->mode.tt.extern_pid = pid;
350 cpu_tasks[task_thread_info(task)->cpu].pid = pid;
353 attach_process(thread->request.u.fork.pid);
356 (*thread->request.u.cb.proc)(thread->request.u.cb.arg);
362 tracer_panic("Bad op in do_proc_op");
365 thread->request.op = OP_NONE;
369 void init_idle_tt(void)
374 extern void start_kernel(void);
376 static int start_kernel_proc(void *unused)
383 cpu_tasks[0].pid = pid;
384 cpu_tasks[0].task = current;
386 cpu_online_map = cpumask_of_cpu(0);
388 if(debug) os_stop_process(pid);
393 void set_tracing(void *task, int tracing)
395 ((struct task_struct *) task)->thread.mode.tt.tracing = tracing;
398 int is_tracing(void *t)
400 return (((struct task_struct *) t)->thread.mode.tt.tracing);
403 int set_user_mode(void *t)
405 struct task_struct *task;
407 task = t ? t : current;
408 if(task->thread.mode.tt.tracing)
410 task->thread.request.op = OP_TRACE_ON;
411 os_usr1_process(os_getpid());
415 void set_init_pid(int pid)
419 init_task.thread.mode.tt.extern_pid = pid;
420 err = os_pipe(init_task.thread.mode.tt.switch_pipe, 1, 1);
422 panic("Can't create switch pipe for init_task, errno = %d",
426 int start_uml_tt(void)
431 pages = (1 << CONFIG_KERNEL_STACK_ORDER);
432 sp = task_stack_page(&init_task) +
433 pages * PAGE_SIZE - sizeof(unsigned long);
434 return(tracer(start_kernel_proc, sp));
437 int external_pid_tt(struct task_struct *task)
439 return(task->thread.mode.tt.extern_pid);
442 int thread_pid_tt(struct task_struct *task)
444 return(task->thread.mode.tt.extern_pid);
447 int is_valid_pid(int pid)
449 struct task_struct *task;
451 read_lock(&tasklist_lock);
452 for_each_process(task){
453 if(task->thread.mode.tt.extern_pid == pid){
454 read_unlock(&tasklist_lock);
458 read_unlock(&tasklist_lock);