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"
17 #include "user_util.h"
20 #include "sigcontext.h"
21 #include "time_user.h"
25 #include "mode_kern.h"
29 void switch_to_tt(void *prev
, void *next
)
31 struct task_struct
*from
, *to
, *prev_sched
;
33 int err
, vtalrm
, alrm
, prof
, cpu
;
39 cpu
= task_thread_info(from
)->cpu
;
41 forward_interrupts(to
->thread
.mode
.tt
.extern_pid
);
43 forward_ipi(cpu_data
[cpu
].ipi_pipe
[0], to
->thread
.mode
.tt
.extern_pid
);
45 local_irq_save(flags
);
47 vtalrm
= change_sig(SIGVTALRM
, 0);
48 alrm
= change_sig(SIGALRM
, 0);
49 prof
= change_sig(SIGPROF
, 0);
51 forward_pending_sigio(to
->thread
.mode
.tt
.extern_pid
);
55 err
= os_write_file(to
->thread
.mode
.tt
.switch_pipe
[1], &c
, sizeof(c
));
57 panic("write of switch_pipe failed, err = %d", -err
);
59 if(from
->thread
.mode
.tt
.switch_pipe
[0] == -1)
60 os_kill_process(os_getpid(), 0);
62 err
= os_read_file(from
->thread
.mode
.tt
.switch_pipe
[0], &c
, sizeof(c
));
64 panic("read of switch_pipe failed, errno = %d", -err
);
66 /* If the process that we have just scheduled away from has exited,
67 * then it needs to be killed here. The reason is that, even though
68 * it will kill itself when it next runs, that may be too late. Its
69 * stack will be freed, possibly before then, and if that happens,
70 * we have a use-after-free situation. So, it gets killed here
71 * in case it has not already killed itself.
73 prev_sched
= current
->thread
.prev_sched
;
74 if(prev_sched
->thread
.mode
.tt
.switch_pipe
[0] == -1)
75 os_kill_process(prev_sched
->thread
.mode
.tt
.extern_pid
, 1);
77 change_sig(SIGVTALRM
, vtalrm
);
78 change_sig(SIGALRM
, alrm
);
79 change_sig(SIGPROF
, prof
);
84 local_irq_restore(flags
);
87 void release_thread_tt(struct task_struct
*task
)
89 int pid
= task
->thread
.mode
.tt
.extern_pid
;
92 * We first have to kill the other process, before
93 * closing its switch_pipe. Else it might wake up
94 * and receive "EOF" before we could kill it.
96 if(os_getpid() != pid
)
97 os_kill_process(pid
, 0);
99 os_close_file(task
->thread
.mode
.tt
.switch_pipe
[0]);
100 os_close_file(task
->thread
.mode
.tt
.switch_pipe
[1]);
101 /* use switch_pipe as flag: thread is released */
102 task
->thread
.mode
.tt
.switch_pipe
[0] = -1;
105 void suspend_new_thread(int fd
)
110 os_stop_process(os_getpid());
111 err
= os_read_file(fd
, &c
, sizeof(c
));
113 panic("read failed in suspend_new_thread, err = %d", -err
);
116 void schedule_tail(task_t
*prev
);
118 static void new_thread_handler(int sig
)
120 unsigned long disable
;
124 fn
= current
->thread
.request
.u
.thread
.proc
;
125 arg
= current
->thread
.request
.u
.thread
.arg
;
127 UPT_SC(¤t
->thread
.regs
.regs
) = (void *) (&sig
+ 1);
128 disable
= (1 << (SIGVTALRM
- 1)) | (1 << (SIGALRM
- 1)) |
129 (1 << (SIGIO
- 1)) | (1 << (SIGPROF
- 1));
130 SC_SIGMASK(UPT_SC(¤t
->thread
.regs
.regs
)) &= ~disable
;
132 suspend_new_thread(current
->thread
.mode
.tt
.switch_pipe
[0]);
135 if(current
->thread
.prev_sched
!= NULL
)
136 schedule_tail(current
->thread
.prev_sched
);
137 current
->thread
.prev_sched
= NULL
;
139 init_new_thread_signals(1);
141 free_page(current
->thread
.temp_stack
);
142 set_cmdline("(kernel thread)");
144 change_sig(SIGUSR1
, 1);
145 change_sig(SIGVTALRM
, 1);
146 change_sig(SIGPROF
, 1);
148 if(!run_kernel_thread(fn
, arg
, ¤t
->thread
.exec_buf
))
151 /* XXX No set_user_mode here because a newly execed process will
152 * immediately segfault on its non-existent IP, coming straight back
153 * to the signal handler, which will call set_user_mode on its way
154 * out. This should probably change since it's confusing.
158 static int new_thread_proc(void *stack
)
160 /* local_irq_disable is needed to block out signals until this thread is
161 * properly scheduled. Otherwise, the tracing thread will get mighty
162 * upset about any signals that arrive before that.
163 * This has the complication that it sets the saved signal mask in
164 * the sigcontext to block signals. This gets restored when this
165 * thread (or a descendant, since they get a copy of this sigcontext)
166 * returns to userspace.
167 * So, this is compensated for elsewhere.
168 * XXX There is still a small window until local_irq_disable() actually
169 * finishes where signals are possible - shouldn't be a problem in
170 * practice since SIGIO hasn't been forwarded here yet, and the
171 * local_irq_disable should finish before a SIGVTALRM has time to be
176 init_new_thread_stack(stack
, new_thread_handler
);
177 os_usr1_process(os_getpid());
178 change_sig(SIGUSR1
, 1);
182 /* Signal masking - signals are blocked at the start of fork_tramp. They
183 * are re-enabled when finish_fork_handler is entered by fork_tramp hitting
184 * itself with a SIGUSR1. set_user_mode has to be run with SIGUSR1 off,
185 * so it is blocked before it's called. They are re-enabled on sigreturn
186 * despite the fact that they were blocked when the SIGUSR1 was issued because
187 * copy_thread copies the parent's sigcontext, including the signal mask
188 * onto the signal frame.
191 void finish_fork_handler(int sig
)
193 UPT_SC(¤t
->thread
.regs
.regs
) = (void *) (&sig
+ 1);
194 suspend_new_thread(current
->thread
.mode
.tt
.switch_pipe
[0]);
197 if(current
->thread
.prev_sched
!= NULL
)
198 schedule_tail(current
->thread
.prev_sched
);
199 current
->thread
.prev_sched
= NULL
;
202 change_sig(SIGVTALRM
, 1);
204 if(current
->mm
!= current
->parent
->mm
)
205 protect_memory(uml_reserved
, high_physmem
- uml_reserved
, 1,
207 task_protections((unsigned long) current_thread
);
209 free_page(current
->thread
.temp_stack
);
211 change_sig(SIGUSR1
, 0);
212 set_user_mode(current
);
215 int fork_tramp(void *stack
)
219 init_new_thread_stack(stack
, finish_fork_handler
);
221 os_usr1_process(os_getpid());
222 change_sig(SIGUSR1
, 1);
226 int copy_thread_tt(int nr
, unsigned long clone_flags
, unsigned long sp
,
227 unsigned long stack_top
, struct task_struct
* p
,
228 struct pt_regs
*regs
)
230 int (*tramp
)(void *);
234 if(current
->thread
.forking
)
237 tramp
= new_thread_proc
;
238 p
->thread
.request
.u
.thread
= current
->thread
.request
.u
.thread
;
241 err
= os_pipe(p
->thread
.mode
.tt
.switch_pipe
, 1, 1);
243 printk("copy_thread : pipe failed, err = %d\n", -err
);
247 stack
= alloc_stack(0, 0);
249 printk(KERN_ERR
"copy_thread : failed to allocate "
250 "temporary stack\n");
254 clone_flags
&= CLONE_VM
;
255 p
->thread
.temp_stack
= stack
;
256 new_pid
= start_fork_tramp(task_stack_page(p
), stack
, clone_flags
, tramp
);
258 printk(KERN_ERR
"copy_thread : clone failed - errno = %d\n",
263 if(current
->thread
.forking
){
264 sc_to_sc(UPT_SC(&p
->thread
.regs
.regs
), UPT_SC(®s
->regs
));
265 SC_SET_SYSCALL_RETURN(UPT_SC(&p
->thread
.regs
.regs
), 0);
267 SC_SP(UPT_SC(&p
->thread
.regs
.regs
)) = sp
;
269 p
->thread
.mode
.tt
.extern_pid
= new_pid
;
271 current
->thread
.request
.op
= OP_FORK
;
272 current
->thread
.request
.u
.fork
.pid
= new_pid
;
273 os_usr1_process(os_getpid());
275 /* Enable the signal and then disable it to ensure that it is handled
276 * here, and nowhere else.
278 change_sig(SIGUSR1
, 1);
280 change_sig(SIGUSR1
, 0);
287 current
->thread
.request
.op
= OP_REBOOT
;
288 os_usr1_process(os_getpid());
289 change_sig(SIGUSR1
, 1);
294 current
->thread
.request
.op
= OP_HALT
;
295 os_usr1_process(os_getpid());
296 change_sig(SIGUSR1
, 1);
299 void kill_off_processes_tt(void)
301 struct task_struct
*p
;
306 if(p
->thread
.mode
.tt
.extern_pid
!= me
)
307 os_kill_process(p
->thread
.mode
.tt
.extern_pid
, 0);
309 if(init_task
.thread
.mode
.tt
.extern_pid
!= me
)
310 os_kill_process(init_task
.thread
.mode
.tt
.extern_pid
, 0);
313 void initial_thread_cb_tt(void (*proc
)(void *), void *arg
)
315 if(os_getpid() == tracing_pid
){
319 current
->thread
.request
.op
= OP_CB
;
320 current
->thread
.request
.u
.cb
.proc
= proc
;
321 current
->thread
.request
.u
.cb
.arg
= arg
;
322 os_usr1_process(os_getpid());
323 change_sig(SIGUSR1
, 1);
325 change_sig(SIGUSR1
, 0);
329 int do_proc_op(void *t
, int proc_id
)
331 struct task_struct
*task
;
332 struct thread_struct
*thread
;
336 thread
= &task
->thread
;
337 op
= thread
->request
.op
;
343 pid
= thread
->request
.u
.exec
.pid
;
344 do_exec(thread
->mode
.tt
.extern_pid
, pid
);
345 thread
->mode
.tt
.extern_pid
= pid
;
346 cpu_tasks
[task_thread_info(task
)->cpu
].pid
= pid
;
349 attach_process(thread
->request
.u
.fork
.pid
);
352 (*thread
->request
.u
.cb
.proc
)(thread
->request
.u
.cb
.arg
);
358 tracer_panic("Bad op in do_proc_op");
361 thread
->request
.op
= OP_NONE
;
365 void init_idle_tt(void)
370 extern void start_kernel(void);
372 static int start_kernel_proc(void *unused
)
379 cpu_tasks
[0].pid
= pid
;
380 cpu_tasks
[0].task
= current
;
382 cpu_online_map
= cpumask_of_cpu(0);
384 if(debug
) os_stop_process(pid
);
389 void set_tracing(void *task
, int tracing
)
391 ((struct task_struct
*) task
)->thread
.mode
.tt
.tracing
= tracing
;
394 int is_tracing(void *t
)
396 return (((struct task_struct
*) t
)->thread
.mode
.tt
.tracing
);
399 int set_user_mode(void *t
)
401 struct task_struct
*task
;
403 task
= t
? t
: current
;
404 if(task
->thread
.mode
.tt
.tracing
)
406 task
->thread
.request
.op
= OP_TRACE_ON
;
407 os_usr1_process(os_getpid());
411 void set_init_pid(int pid
)
415 init_task
.thread
.mode
.tt
.extern_pid
= pid
;
416 err
= os_pipe(init_task
.thread
.mode
.tt
.switch_pipe
, 1, 1);
418 panic("Can't create switch pipe for init_task, errno = %d",
422 int start_uml_tt(void)
427 pages
= (1 << CONFIG_KERNEL_STACK_ORDER
);
428 sp
= task_stack_page(&init_task
) +
429 pages
* PAGE_SIZE
- sizeof(unsigned long);
430 return(tracer(start_kernel_proc
, sp
));
433 int external_pid_tt(struct task_struct
*task
)
435 return(task
->thread
.mode
.tt
.extern_pid
);
438 int thread_pid_tt(struct task_struct
*task
)
440 return(task
->thread
.mode
.tt
.extern_pid
);
443 int is_valid_pid(int pid
)
445 struct task_struct
*task
;
447 read_lock(&tasklist_lock
);
448 for_each_process(task
){
449 if(task
->thread
.mode
.tt
.extern_pid
== pid
){
450 read_unlock(&tasklist_lock
);
454 read_unlock(&tasklist_lock
);