This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / um / kernel / tt / process_kern.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
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"
15 #include "irq_user.h"
16 #include "signal_user.h"
17 #include "kern_util.h"
18 #include "user_util.h"
19 #include "os.h"
20 #include "kern.h"
21 #include "sigcontext.h"
22 #include "time_user.h"
23 #include "mem_user.h"
24 #include "tlb.h"
25 #include "mode.h"
26 #include "init.h"
27 #include "tt.h"
28
29 void *switch_to_tt(void *prev, void *next, void *last)
30 {
31         struct task_struct *from, *to;
32         unsigned long flags;
33         int err, vtalrm, alrm, prof, cpu;
34         char c;
35         /* jailing and SMP are incompatible, so this doesn't need to be 
36          * made per-cpu 
37          */
38         static int reading;
39
40         from = prev;
41         to = next;
42
43         to->thread.prev_sched = from;
44
45         cpu = from->thread_info->cpu;
46         if(cpu == 0)
47                 forward_interrupts(to->thread.mode.tt.extern_pid);
48 #ifdef CONFIG_SMP
49         forward_ipi(cpu_data[cpu].ipi_pipe[0], to->thread.mode.tt.extern_pid);
50 #endif
51         local_irq_save(flags);
52
53         vtalrm = change_sig(SIGVTALRM, 0);
54         alrm = change_sig(SIGALRM, 0);
55         prof = change_sig(SIGPROF, 0);
56
57         forward_pending_sigio(to->thread.mode.tt.extern_pid);
58
59         c = 0;
60         set_current(to);
61
62         reading = 0;
63         err = os_write_file(to->thread.mode.tt.switch_pipe[1], &c, sizeof(c));
64         if(err != sizeof(c))
65                 panic("write of switch_pipe failed, err = %d", -err);
66
67         reading = 1;
68         if((from->state == TASK_ZOMBIE) || (from->state == TASK_DEAD))
69                 os_kill_process(os_getpid(), 0);
70
71         err = os_read_file(from->thread.mode.tt.switch_pipe[0], &c, sizeof(c));
72         if(err != sizeof(c))
73                 panic("read of switch_pipe failed, errno = %d", -err);
74
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.
87          */
88
89         if(jail){
90                 while(!reading) sched_yield();
91         }
92
93         change_sig(SIGVTALRM, vtalrm);
94         change_sig(SIGALRM, alrm);
95         change_sig(SIGPROF, prof);
96
97         arch_switch();
98
99         flush_tlb_all();
100         local_irq_restore(flags);
101
102         return(current->thread.prev_sched);
103 }
104
105 void release_thread_tt(struct task_struct *task)
106 {
107         int pid = task->thread.mode.tt.extern_pid;
108
109         if(os_getpid() != pid)
110                 os_kill_process(pid, 0);
111 }
112
113 void exit_thread_tt(void)
114 {
115         os_close_file(current->thread.mode.tt.switch_pipe[0]);
116         os_close_file(current->thread.mode.tt.switch_pipe[1]);
117 }
118
119 void schedule_tail(task_t *prev);
120
121 static void new_thread_handler(int sig)
122 {
123         unsigned long disable;
124         int (*fn)(void *);
125         void *arg;
126
127         fn = current->thread.request.u.thread.proc;
128         arg = current->thread.request.u.thread.arg;
129
130         UPT_SC(&current->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(&current->thread.regs.regs)) &= ~disable;
134
135         suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
136
137         force_flush_all();
138         if(current->thread.prev_sched != NULL)
139                 schedule_tail(current->thread.prev_sched);
140         current->thread.prev_sched = NULL;
141
142         init_new_thread_signals(1);
143         enable_timer();
144         free_page(current->thread.temp_stack);
145         set_cmdline("(kernel thread)");
146
147         change_sig(SIGUSR1, 1);
148         change_sig(SIGVTALRM, 1);
149         change_sig(SIGPROF, 1);
150         local_irq_enable();
151         if(!run_kernel_thread(fn, arg, &current->thread.exec_buf))
152                 do_exit(0);
153 }
154
155 static int new_thread_proc(void *stack)
156 {
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 
169          * delivered.
170          */
171
172         local_irq_disable();
173         init_new_thread_stack(stack, new_thread_handler);
174         os_usr1_process(os_getpid());
175         return(0);
176 }
177
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.
185  */
186
187 void finish_fork_handler(int sig)
188 {
189         UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
190         suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
191
192         force_flush_all();
193         if(current->thread.prev_sched != NULL)
194                 schedule_tail(current->thread.prev_sched);
195         current->thread.prev_sched = NULL;
196
197         enable_timer();
198         change_sig(SIGVTALRM, 1);
199         local_irq_enable();
200         if(current->mm != current->parent->mm)
201                 protect_memory(uml_reserved, high_physmem - uml_reserved, 1, 
202                                1, 0, 1);
203         task_protections((unsigned long) current_thread);
204
205         free_page(current->thread.temp_stack);
206         local_irq_disable();
207         change_sig(SIGUSR1, 0);
208         set_user_mode(current);
209 }
210
211 int fork_tramp(void *stack)
212 {
213         local_irq_disable();
214         arch_init_thread();
215         init_new_thread_stack(stack, finish_fork_handler);
216
217         os_usr1_process(os_getpid());
218         return(0);
219 }
220
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)
224 {
225         int (*tramp)(void *);
226         int new_pid, err;
227         unsigned long stack;
228         
229         if(current->thread.forking)
230                 tramp = fork_tramp;
231         else {
232                 tramp = new_thread_proc;
233                 p->thread.request.u.thread = current->thread.request.u.thread;
234         }
235
236         err = os_pipe(p->thread.mode.tt.switch_pipe, 1, 1);
237         if(err < 0){
238                 printk("copy_thread : pipe failed, err = %d\n", -err);
239                 return(err);
240         }
241
242         stack = alloc_stack(0, 0);
243         if(stack == 0){
244                 printk(KERN_ERR "copy_thread : failed to allocate "
245                        "temporary stack\n");
246                 return(-ENOMEM);
247         }
248
249         clone_flags &= CLONE_VM;
250         p->thread.temp_stack = stack;
251         new_pid = start_fork_tramp((void *) p->thread.kernel_stack, stack,
252                                    clone_flags, tramp);
253         if(new_pid < 0){
254                 printk(KERN_ERR "copy_thread : clone failed - errno = %d\n", 
255                        -new_pid);
256                 return(new_pid);
257         }
258
259         if(current->thread.forking){
260                 sc_to_sc(UPT_SC(&p->thread.regs.regs), 
261                          UPT_SC(&current->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;
264         }
265         p->thread.mode.tt.extern_pid = new_pid;
266
267         current->thread.request.op = OP_FORK;
268         current->thread.request.u.fork.pid = new_pid;
269         os_usr1_process(os_getpid());
270         return(0);
271 }
272
273 void reboot_tt(void)
274 {
275         current->thread.request.op = OP_REBOOT;
276         os_usr1_process(os_getpid());
277 }
278
279 void halt_tt(void)
280 {
281         current->thread.request.op = OP_HALT;
282         os_usr1_process(os_getpid());
283 }
284
285 void kill_off_processes_tt(void)
286 {
287         struct task_struct *p;
288         int me;
289
290         me = os_getpid();
291         for_each_process(p){
292                 if(p->thread.mode.tt.extern_pid != me) 
293                         os_kill_process(p->thread.mode.tt.extern_pid, 0);
294         }
295         if(init_task.thread.mode.tt.extern_pid != me) 
296                 os_kill_process(init_task.thread.mode.tt.extern_pid, 0);
297 }
298
299 void initial_thread_cb_tt(void (*proc)(void *), void *arg)
300 {
301         if(os_getpid() == tracing_pid){
302                 (*proc)(arg);
303         }
304         else {
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());
309         }
310 }
311
312 int do_proc_op(void *t, int proc_id)
313 {
314         struct task_struct *task;
315         struct thread_struct *thread;
316         int op, pid;
317
318         task = t;
319         thread = &task->thread;
320         op = thread->request.op;
321         switch(op){
322         case OP_NONE:
323         case OP_TRACE_ON:
324                 break;
325         case OP_EXEC:
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;
330                 break;
331         case OP_FORK:
332                 attach_process(thread->request.u.fork.pid);
333                 break;
334         case OP_CB:
335                 (*thread->request.u.cb.proc)(thread->request.u.cb.arg);
336                 break;
337         case OP_REBOOT:
338         case OP_HALT:
339                 break;
340         default:
341                 tracer_panic("Bad op in do_proc_op");
342                 break;
343         }
344         thread->request.op = OP_NONE;
345         return(op);
346 }
347
348 void init_idle_tt(void)
349 {
350         default_idle();
351 }
352
353 /* Changed by jail_setup, which is a setup */
354 int jail = 0;
355
356 int __init jail_setup(char *line, int *add)
357 {
358         int ok = 1;
359
360         if(jail) return(0);
361 #ifdef CONFIG_SMP
362         printf("'jail' may not used used in a kernel with CONFIG_SMP "
363                "enabled\n");
364         ok = 0;
365 #endif
366 #ifdef CONFIG_HOSTFS
367         printf("'jail' may not used used in a kernel with CONFIG_HOSTFS "
368                "enabled\n");
369         ok = 0;
370 #endif
371 #ifdef CONFIG_MODULES
372         printf("'jail' may not used used in a kernel with CONFIG_MODULES "
373                "enabled\n");
374         ok = 0;
375 #endif  
376         if(!ok) exit(1);
377
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.
381          */
382         cap_lower(cap_bset, CAP_SYS_RAWIO);
383         jail = 1;
384         return(0);
385 }
386
387 __uml_setup("jail", jail_setup,
388 "jail\n"
389 "    Enables the protection of kernel memory from processes.\n\n"
390 );
391
392 static void mprotect_kernel_mem(int w)
393 {
394         unsigned long start, end;
395         int pages;
396
397         if(!jail || (current == &init_task)) return;
398
399         pages = (1 << CONFIG_KERNEL_STACK_ORDER);
400
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);
405
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);
409
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);
413
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);
417
418         mprotect_kernel_vm(w);
419 }
420
421 void unprotect_kernel_mem(void)
422 {
423         mprotect_kernel_mem(1);
424 }
425
426 void protect_kernel_mem(void)
427 {
428         mprotect_kernel_mem(0);
429 }
430
431 extern void start_kernel(void);
432
433 static int start_kernel_proc(void *unused)
434 {
435         int pid;
436
437         block_signals();
438         pid = os_getpid();
439
440         cpu_tasks[0].pid = pid;
441         cpu_tasks[0].task = current;
442 #ifdef CONFIG_SMP
443         cpu_online_map = cpumask_of_cpu(0);
444 #endif
445         if(debug) os_stop_process(pid);
446         start_kernel();
447         return(0);
448 }
449
450 void set_tracing(void *task, int tracing)
451 {
452         ((struct task_struct *) task)->thread.mode.tt.tracing = tracing;
453 }
454
455 int is_tracing(void *t)
456 {
457         return (((struct task_struct *) t)->thread.mode.tt.tracing);
458 }
459
460 int set_user_mode(void *t)
461 {
462         struct task_struct *task;
463
464         task = t ? t : current;
465         if(task->thread.mode.tt.tracing) 
466                 return(1);
467         task->thread.request.op = OP_TRACE_ON;
468         os_usr1_process(os_getpid());
469         return(0);
470 }
471
472 void set_init_pid(int pid)
473 {
474         int err;
475
476         init_task.thread.mode.tt.extern_pid = pid;
477         err = os_pipe(init_task.thread.mode.tt.switch_pipe, 1, 1);
478         if(err) 
479                 panic("Can't create switch pipe for init_task, errno = %d", 
480                       -err);
481 }
482
483 int singlestepping_tt(void *t)
484 {
485         struct task_struct *task = t;
486
487         if(task->thread.mode.tt.singlestep_syscall)
488                 return(0);
489         return(task->ptrace & PT_DTRACE);
490 }
491
492 void clear_singlestep(void *t)
493 {
494         struct task_struct *task = t;
495
496         task->ptrace &= ~PT_DTRACE;
497 }
498
499 int start_uml_tt(void)
500 {
501         void *sp;
502         int pages;
503
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));
508 }
509
510 int external_pid_tt(struct task_struct *task)
511 {
512         return(task->thread.mode.tt.extern_pid);
513 }
514
515 int thread_pid_tt(struct task_struct *task)
516 {
517         return(task->thread.mode.tt.extern_pid);
518 }
519
520 int is_valid_pid(int pid)
521 {
522         struct task_struct *task;
523
524         read_lock(&tasklist_lock);
525         for_each_process(task){
526                 if(task->thread.mode.tt.extern_pid == pid){
527                         read_unlock(&tasklist_lock);
528                         return(1);
529                 }
530         }
531         read_unlock(&tasklist_lock);
532         return(0);
533 }
534
535 /*
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
539  * of the file.
540  * ---------------------------------------------------------------------------
541  * Local variables:
542  * c-file-style: "linux"
543  * End:
544  */