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, errno = %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 os_kill_process(task->thread.mode.tt.extern_pid, 0);
110 void exit_thread_tt(void)
112 close(current->thread.mode.tt.switch_pipe[0]);
113 close(current->thread.mode.tt.switch_pipe[1]);
116 void schedule_tail(task_t *prev);
118 static void new_thread_handler(int sig)
123 fn = current->thread.request.u.thread.proc;
124 arg = current->thread.request.u.thread.arg;
125 UPT_SC(¤t->thread.regs.regs) = (void *) (&sig + 1);
126 suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
129 init_new_thread_signals(1);
131 schedule_tail(current->thread.prev_sched);
134 free_page(current->thread.temp_stack);
135 set_cmdline("(kernel thread)");
138 current->thread.prev_sched = NULL;
139 change_sig(SIGUSR1, 1);
140 change_sig(SIGVTALRM, 1);
141 change_sig(SIGPROF, 1);
143 if(!run_kernel_thread(fn, arg, ¤t->thread.exec_buf))
147 static int new_thread_proc(void *stack)
149 init_new_thread_stack(stack, new_thread_handler);
150 os_usr1_process(os_getpid());
154 /* Signal masking - signals are blocked at the start of fork_tramp. They
155 * are re-enabled when finish_fork_handler is entered by fork_tramp hitting
156 * itself with a SIGUSR1. set_user_mode has to be run with SIGUSR1 off,
157 * so it is blocked before it's called. They are re-enabled on sigreturn
158 * despite the fact that they were blocked when the SIGUSR1 was issued because
159 * copy_thread copies the parent's signcontext, including the signal mask
160 * onto the signal frame.
163 void finish_fork_handler(int sig)
165 UPT_SC(¤t->thread.regs.regs) = (void *) (&sig + 1);
166 suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
172 change_sig(SIGVTALRM, 1);
175 if(current->mm != current->parent->mm)
176 protect_memory(uml_reserved, high_physmem - uml_reserved, 1,
178 task_protections((unsigned long) current->thread_info);
180 current->thread.prev_sched = NULL;
182 free_page(current->thread.temp_stack);
183 change_sig(SIGUSR1, 0);
184 set_user_mode(current);
187 static int sigusr1 = SIGUSR1;
189 int fork_tramp(void *stack)
194 init_new_thread_stack(stack, finish_fork_handler);
196 kill(os_getpid(), sig);
200 int copy_thread_tt(int nr, unsigned long clone_flags, unsigned long sp,
201 unsigned long stack_top, struct task_struct * p,
202 struct pt_regs *regs)
204 int (*tramp)(void *);
208 if(current->thread.forking)
211 tramp = new_thread_proc;
212 p->thread.request.u.thread = current->thread.request.u.thread;
215 err = os_pipe(p->thread.mode.tt.switch_pipe, 1, 1);
217 printk("copy_thread : pipe failed, errno = %d\n", -err);
221 stack = alloc_stack(0, 0);
223 printk(KERN_ERR "copy_thread : failed to allocate "
224 "temporary stack\n");
228 clone_flags &= CLONE_VM;
229 p->thread.temp_stack = stack;
230 new_pid = start_fork_tramp((void *) p->thread.kernel_stack, stack,
233 printk(KERN_ERR "copy_thread : clone failed - errno = %d\n",
238 if(current->thread.forking){
239 sc_to_sc(UPT_SC(&p->thread.regs.regs),
240 UPT_SC(¤t->thread.regs.regs));
241 SC_SET_SYSCALL_RETURN(UPT_SC(&p->thread.regs.regs), 0);
242 if(sp != 0) SC_SP(UPT_SC(&p->thread.regs.regs)) = sp;
244 p->thread.mode.tt.extern_pid = new_pid;
246 current->thread.request.op = OP_FORK;
247 current->thread.request.u.fork.pid = new_pid;
248 os_usr1_process(os_getpid());
254 current->thread.request.op = OP_REBOOT;
255 os_usr1_process(os_getpid());
260 current->thread.request.op = OP_HALT;
261 os_usr1_process(os_getpid());
264 void kill_off_processes_tt(void)
266 struct task_struct *p;
271 if(p->thread.mode.tt.extern_pid != me)
272 os_kill_process(p->thread.mode.tt.extern_pid, 0);
274 if(init_task.thread.mode.tt.extern_pid != me)
275 os_kill_process(init_task.thread.mode.tt.extern_pid, 0);
278 void initial_thread_cb_tt(void (*proc)(void *), void *arg)
280 if(os_getpid() == tracing_pid){
284 current->thread.request.op = OP_CB;
285 current->thread.request.u.cb.proc = proc;
286 current->thread.request.u.cb.arg = arg;
287 os_usr1_process(os_getpid());
291 int do_proc_op(void *t, int proc_id)
293 struct task_struct *task;
294 struct thread_struct *thread;
298 thread = &task->thread;
299 op = thread->request.op;
305 pid = thread->request.u.exec.pid;
306 do_exec(thread->mode.tt.extern_pid, pid);
307 thread->mode.tt.extern_pid = pid;
308 cpu_tasks[task->thread_info->cpu].pid = pid;
311 attach_process(thread->request.u.fork.pid);
314 (*thread->request.u.cb.proc)(thread->request.u.cb.arg);
320 tracer_panic("Bad op in do_proc_op");
323 thread->request.op = OP_NONE;
327 void init_idle_tt(void)
332 /* Changed by jail_setup, which is a setup */
335 int __init jail_setup(char *line, int *add)
341 printf("'jail' may not used used in a kernel with CONFIG_SMP "
346 printf("'jail' may not used used in a kernel with CONFIG_HOSTFS "
350 #ifdef CONFIG_MODULES
351 printf("'jail' may not used used in a kernel with CONFIG_MODULES "
357 /* CAP_SYS_RAWIO controls the ability to open /dev/mem and /dev/kmem.
358 * Removing it from the bounding set eliminates the ability of anything
359 * to acquire it, and thus read or write kernel memory.
361 cap_lower(cap_bset, CAP_SYS_RAWIO);
366 __uml_setup("jail", jail_setup,
368 " Enables the protection of kernel memory from processes.\n\n"
371 static void mprotect_kernel_mem(int w)
373 unsigned long start, end;
376 if(!jail || (current == &init_task)) return;
378 pages = (1 << CONFIG_KERNEL_STACK_ORDER);
380 start = (unsigned long) current->thread_info + PAGE_SIZE;
381 end = (unsigned long) current + PAGE_SIZE * pages;
382 protect_memory(uml_reserved, start - uml_reserved, 1, w, 1, 1);
383 protect_memory(end, high_physmem - end, 1, w, 1, 1);
385 start = (unsigned long) UML_ROUND_DOWN(&_stext);
386 end = (unsigned long) UML_ROUND_UP(&_etext);
387 protect_memory(start, end - start, 1, w, 1, 1);
389 start = (unsigned long) UML_ROUND_DOWN(&_unprotected_end);
390 end = (unsigned long) UML_ROUND_UP(&_edata);
391 protect_memory(start, end - start, 1, w, 1, 1);
393 start = (unsigned long) UML_ROUND_DOWN(&__bss_start);
394 end = (unsigned long) UML_ROUND_UP(brk_start);
395 protect_memory(start, end - start, 1, w, 1, 1);
397 mprotect_kernel_vm(w);
400 void unprotect_kernel_mem(void)
402 mprotect_kernel_mem(1);
405 void protect_kernel_mem(void)
407 mprotect_kernel_mem(0);
410 extern void start_kernel(void);
412 static int start_kernel_proc(void *unused)
419 cpu_tasks[0].pid = pid;
420 cpu_tasks[0].task = current;
422 cpu_online_map = cpumask_of_cpu(0);
424 if(debug) os_stop_process(pid);
429 void set_tracing(void *task, int tracing)
431 ((struct task_struct *) task)->thread.mode.tt.tracing = tracing;
434 int is_tracing(void *t)
436 return (((struct task_struct *) t)->thread.mode.tt.tracing);
439 int set_user_mode(void *t)
441 struct task_struct *task;
443 task = t ? t : current;
444 if(task->thread.mode.tt.tracing)
446 task->thread.request.op = OP_TRACE_ON;
447 os_usr1_process(os_getpid());
451 void set_init_pid(int pid)
455 init_task.thread.mode.tt.extern_pid = pid;
456 err = os_pipe(init_task.thread.mode.tt.switch_pipe, 1, 1);
457 if(err) panic("Can't create switch pipe for init_task, errno = %d",
461 int singlestepping_tt(void *t)
463 struct task_struct *task = t;
465 if(task->thread.mode.tt.singlestep_syscall)
467 return(task->ptrace & PT_DTRACE);
470 void clear_singlestep(void *t)
472 struct task_struct *task = t;
474 task->ptrace &= ~PT_DTRACE;
477 int start_uml_tt(void)
482 pages = (1 << CONFIG_KERNEL_STACK_ORDER) - 2;
483 sp = (void *) init_task.thread.kernel_stack + pages * PAGE_SIZE -
484 sizeof(unsigned long);
485 return(tracer(start_kernel_proc, sp));
488 int external_pid_tt(struct task_struct *task)
490 return(task->thread.mode.tt.extern_pid);
493 int thread_pid_tt(struct task_struct *task)
495 return(task->thread.mode.tt.extern_pid);
498 int is_valid_pid(int pid)
500 struct task_struct *task;
502 read_lock(&tasklist_lock);
503 for_each_process(task){
504 if(task->thread.mode.tt.extern_pid == pid){
505 read_unlock(&tasklist_lock);
509 read_unlock(&tasklist_lock);
514 * Overrides for Emacs so that we follow Linus's tabbing style.
515 * Emacs will notice this stuff at the end of the file and automatically
516 * adjust the settings for this buffer only. This must remain at the end
518 * ---------------------------------------------------------------------------
520 * c-file-style: "linux"