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 "signal_user.h"
17 #include "kern_util.h"
18 #include "user_util.h"
21 #include "sigcontext.h"
22 #include "time_user.h"
29 void *switch_to_tt(void *prev, void *next, void *last)
31 struct task_struct *from, *to;
33 int err, vtalrm, alrm, prof, cpu;
35 /* jailing and SMP are incompatible, so this doesn't need to be
43 to->thread.prev_sched = from;
45 cpu = from->thread_info->cpu;
47 forward_interrupts(to->thread.mode.tt.extern_pid);
49 forward_ipi(cpu_data[cpu].ipi_pipe[0], to->thread.mode.tt.extern_pid);
51 local_irq_save(flags);
53 vtalrm = change_sig(SIGVTALRM, 0);
54 alrm = change_sig(SIGALRM, 0);
55 prof = change_sig(SIGPROF, 0);
57 forward_pending_sigio(to->thread.mode.tt.extern_pid);
63 err = os_write_file(to->thread.mode.tt.switch_pipe[1], &c, sizeof(c));
65 panic("write of switch_pipe failed, err = %d", -err);
68 if((from->state == TASK_ZOMBIE) || (from->state == TASK_DEAD))
69 os_kill_process(os_getpid(), 0);
71 err = os_read_file(from->thread.mode.tt.switch_pipe[0], &c, sizeof(c));
73 panic("read of switch_pipe failed, errno = %d", -err);
75 /* This works around a nasty race with 'jail'. If we are switching
76 * between two threads of a threaded app and the incoming process
77 * runs before the outgoing process reaches the read, and it makes
78 * it all the way out to userspace, then it will have write-protected
79 * the outgoing process stack. Then, when the outgoing process
80 * returns from the write, it will segfault because it can no longer
81 * write its own stack. So, in order to avoid that, the incoming
82 * thread sits in a loop yielding until 'reading' is set. This
83 * isn't entirely safe, since there may be a reschedule from a timer
84 * happening between setting 'reading' and sleeping in read. But,
85 * it should get a whole quantum in which to reach the read and sleep,
86 * which should be enough.
90 while(!reading) sched_yield();
93 change_sig(SIGVTALRM, vtalrm);
94 change_sig(SIGALRM, alrm);
95 change_sig(SIGPROF, prof);
100 local_irq_restore(flags);
102 return(current->thread.prev_sched);
105 void release_thread_tt(struct task_struct *task)
107 int pid = task->thread.mode.tt.extern_pid;
109 if(os_getpid() != pid)
110 os_kill_process(pid, 0);
113 void exit_thread_tt(void)
115 os_close_file(current->thread.mode.tt.switch_pipe[0]);
116 os_close_file(current->thread.mode.tt.switch_pipe[1]);
119 void schedule_tail(task_t *prev);
121 static void new_thread_handler(int sig)
123 unsigned long disable;
127 fn = current->thread.request.u.thread.proc;
128 arg = current->thread.request.u.thread.arg;
130 UPT_SC(¤t->thread.regs.regs) = (void *) (&sig + 1);
131 disable = (1 << (SIGVTALRM - 1)) | (1 << (SIGALRM - 1)) |
132 (1 << (SIGIO - 1)) | (1 << (SIGPROF - 1));
133 SC_SIGMASK(UPT_SC(¤t->thread.regs.regs)) &= ~disable;
135 suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
138 if(current->thread.prev_sched != NULL)
139 schedule_tail(current->thread.prev_sched);
140 current->thread.prev_sched = NULL;
142 init_new_thread_signals(1);
144 free_page(current->thread.temp_stack);
145 set_cmdline("(kernel thread)");
147 change_sig(SIGUSR1, 1);
148 change_sig(SIGVTALRM, 1);
149 change_sig(SIGPROF, 1);
151 if(!run_kernel_thread(fn, arg, ¤t->thread.exec_buf))
155 static int new_thread_proc(void *stack)
157 /* local_irq_disable is needed to block out signals until this thread is
158 * properly scheduled. Otherwise, the tracing thread will get mighty
159 * upset about any signals that arrive before that.
160 * This has the complication that it sets the saved signal mask in
161 * the sigcontext to block signals. This gets restored when this
162 * thread (or a descendant, since they get a copy of this sigcontext)
163 * returns to userspace.
164 * So, this is compensated for elsewhere.
165 * XXX There is still a small window until local_irq_disable() actually
166 * finishes where signals are possible - shouldn't be a problem in
167 * practice since SIGIO hasn't been forwarded here yet, and the
168 * local_irq_disable should finish before a SIGVTALRM has time to be
173 init_new_thread_stack(stack, new_thread_handler);
174 os_usr1_process(os_getpid());
178 /* Signal masking - signals are blocked at the start of fork_tramp. They
179 * are re-enabled when finish_fork_handler is entered by fork_tramp hitting
180 * itself with a SIGUSR1. set_user_mode has to be run with SIGUSR1 off,
181 * so it is blocked before it's called. They are re-enabled on sigreturn
182 * despite the fact that they were blocked when the SIGUSR1 was issued because
183 * copy_thread copies the parent's sigcontext, including the signal mask
184 * onto the signal frame.
187 void finish_fork_handler(int sig)
189 UPT_SC(¤t->thread.regs.regs) = (void *) (&sig + 1);
190 suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
193 if(current->thread.prev_sched != NULL)
194 schedule_tail(current->thread.prev_sched);
195 current->thread.prev_sched = NULL;
198 change_sig(SIGVTALRM, 1);
200 if(current->mm != current->parent->mm)
201 protect_memory(uml_reserved, high_physmem - uml_reserved, 1,
203 task_protections((unsigned long) current_thread);
205 free_page(current->thread.temp_stack);
207 change_sig(SIGUSR1, 0);
208 set_user_mode(current);
211 int fork_tramp(void *stack)
215 init_new_thread_stack(stack, finish_fork_handler);
217 os_usr1_process(os_getpid());
221 int copy_thread_tt(int nr, unsigned long clone_flags, unsigned long sp,
222 unsigned long stack_top, struct task_struct * p,
223 struct pt_regs *regs)
225 int (*tramp)(void *);
229 if(current->thread.forking)
232 tramp = new_thread_proc;
233 p->thread.request.u.thread = current->thread.request.u.thread;
236 err = os_pipe(p->thread.mode.tt.switch_pipe, 1, 1);
238 printk("copy_thread : pipe failed, err = %d\n", -err);
242 stack = alloc_stack(0, 0);
244 printk(KERN_ERR "copy_thread : failed to allocate "
245 "temporary stack\n");
249 clone_flags &= CLONE_VM;
250 p->thread.temp_stack = stack;
251 new_pid = start_fork_tramp((void *) p->thread.kernel_stack, stack,
254 printk(KERN_ERR "copy_thread : clone failed - errno = %d\n",
259 if(current->thread.forking){
260 sc_to_sc(UPT_SC(&p->thread.regs.regs),
261 UPT_SC(¤t->thread.regs.regs));
262 SC_SET_SYSCALL_RETURN(UPT_SC(&p->thread.regs.regs), 0);
263 if(sp != 0) SC_SP(UPT_SC(&p->thread.regs.regs)) = sp;
265 p->thread.mode.tt.extern_pid = new_pid;
267 current->thread.request.op = OP_FORK;
268 current->thread.request.u.fork.pid = new_pid;
269 os_usr1_process(os_getpid());
275 current->thread.request.op = OP_REBOOT;
276 os_usr1_process(os_getpid());
281 current->thread.request.op = OP_HALT;
282 os_usr1_process(os_getpid());
285 void kill_off_processes_tt(void)
287 struct task_struct *p;
292 if(p->thread.mode.tt.extern_pid != me)
293 os_kill_process(p->thread.mode.tt.extern_pid, 0);
295 if(init_task.thread.mode.tt.extern_pid != me)
296 os_kill_process(init_task.thread.mode.tt.extern_pid, 0);
299 void initial_thread_cb_tt(void (*proc)(void *), void *arg)
301 if(os_getpid() == tracing_pid){
305 current->thread.request.op = OP_CB;
306 current->thread.request.u.cb.proc = proc;
307 current->thread.request.u.cb.arg = arg;
308 os_usr1_process(os_getpid());
312 int do_proc_op(void *t, int proc_id)
314 struct task_struct *task;
315 struct thread_struct *thread;
319 thread = &task->thread;
320 op = thread->request.op;
326 pid = thread->request.u.exec.pid;
327 do_exec(thread->mode.tt.extern_pid, pid);
328 thread->mode.tt.extern_pid = pid;
329 cpu_tasks[task->thread_info->cpu].pid = pid;
332 attach_process(thread->request.u.fork.pid);
335 (*thread->request.u.cb.proc)(thread->request.u.cb.arg);
341 tracer_panic("Bad op in do_proc_op");
344 thread->request.op = OP_NONE;
348 void init_idle_tt(void)
353 /* Changed by jail_setup, which is a setup */
356 int __init jail_setup(char *line, int *add)
362 printf("'jail' may not used used in a kernel with CONFIG_SMP "
367 printf("'jail' may not used used in a kernel with CONFIG_HOSTFS "
371 #ifdef CONFIG_MODULES
372 printf("'jail' may not used used in a kernel with CONFIG_MODULES "
378 /* CAP_SYS_RAWIO controls the ability to open /dev/mem and /dev/kmem.
379 * Removing it from the bounding set eliminates the ability of anything
380 * to acquire it, and thus read or write kernel memory.
382 cap_lower(cap_bset, CAP_SYS_RAWIO);
387 __uml_setup("jail", jail_setup,
389 " Enables the protection of kernel memory from processes.\n\n"
392 static void mprotect_kernel_mem(int w)
394 unsigned long start, end;
397 if(!jail || (current == &init_task)) return;
399 pages = (1 << CONFIG_KERNEL_STACK_ORDER);
401 start = (unsigned long) current_thread + PAGE_SIZE;
402 end = (unsigned long) current_thread + PAGE_SIZE * pages;
403 protect_memory(uml_reserved, start - uml_reserved, 1, w, 1, 1);
404 protect_memory(end, high_physmem - end, 1, w, 1, 1);
406 start = (unsigned long) UML_ROUND_DOWN(&_stext);
407 end = (unsigned long) UML_ROUND_UP(&_etext);
408 protect_memory(start, end - start, 1, w, 1, 1);
410 start = (unsigned long) UML_ROUND_DOWN(&_unprotected_end);
411 end = (unsigned long) UML_ROUND_UP(&_edata);
412 protect_memory(start, end - start, 1, w, 1, 1);
414 start = (unsigned long) UML_ROUND_DOWN(&__bss_start);
415 end = (unsigned long) UML_ROUND_UP(brk_start);
416 protect_memory(start, end - start, 1, w, 1, 1);
418 mprotect_kernel_vm(w);
421 void unprotect_kernel_mem(void)
423 mprotect_kernel_mem(1);
426 void protect_kernel_mem(void)
428 mprotect_kernel_mem(0);
431 extern void start_kernel(void);
433 static int start_kernel_proc(void *unused)
440 cpu_tasks[0].pid = pid;
441 cpu_tasks[0].task = current;
443 cpu_online_map = cpumask_of_cpu(0);
445 if(debug) os_stop_process(pid);
450 void set_tracing(void *task, int tracing)
452 ((struct task_struct *) task)->thread.mode.tt.tracing = tracing;
455 int is_tracing(void *t)
457 return (((struct task_struct *) t)->thread.mode.tt.tracing);
460 int set_user_mode(void *t)
462 struct task_struct *task;
464 task = t ? t : current;
465 if(task->thread.mode.tt.tracing)
467 task->thread.request.op = OP_TRACE_ON;
468 os_usr1_process(os_getpid());
472 void set_init_pid(int pid)
476 init_task.thread.mode.tt.extern_pid = pid;
477 err = os_pipe(init_task.thread.mode.tt.switch_pipe, 1, 1);
479 panic("Can't create switch pipe for init_task, errno = %d",
483 int singlestepping_tt(void *t)
485 struct task_struct *task = t;
487 if(task->thread.mode.tt.singlestep_syscall)
489 return(task->ptrace & PT_DTRACE);
492 void clear_singlestep(void *t)
494 struct task_struct *task = t;
496 task->ptrace &= ~PT_DTRACE;
499 int start_uml_tt(void)
504 pages = (1 << CONFIG_KERNEL_STACK_ORDER) - 2;
505 sp = (void *) init_task.thread.kernel_stack + pages * PAGE_SIZE -
506 sizeof(unsigned long);
507 return(tracer(start_kernel_proc, sp));
510 int external_pid_tt(struct task_struct *task)
512 return(task->thread.mode.tt.extern_pid);
515 int thread_pid_tt(struct task_struct *task)
517 return(task->thread.mode.tt.extern_pid);
520 int is_valid_pid(int pid)
522 struct task_struct *task;
524 read_lock(&tasklist_lock);
525 for_each_process(task){
526 if(task->thread.mode.tt.extern_pid == pid){
527 read_unlock(&tasklist_lock);
531 read_unlock(&tasklist_lock);
536 * Overrides for Emacs so that we follow Linus's tabbing style.
537 * Emacs will notice this stuff at the end of the file and automatically
538 * adjust the settings for this buffer only. This must remain at the end
540 * ---------------------------------------------------------------------------
542 * c-file-style: "linux"