linux 2.6.16.38 w/ vs2.0.3-rc1
[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 "kern_util.h"
17 #include "user_util.h"
18 #include "os.h"
19 #include "kern.h"
20 #include "sigcontext.h"
21 #include "mem_user.h"
22 #include "tlb.h"
23 #include "mode.h"
24 #include "mode_kern.h"
25 #include "init.h"
26 #include "tt.h"
27
28 void switch_to_tt(void *prev, void *next)
29 {
30         struct task_struct *from, *to, *prev_sched;
31         unsigned long flags;
32         int err, vtalrm, alrm, prof, cpu;
33         char c;
34
35         from = prev;
36         to = next;
37
38         cpu = task_thread_info(from)->cpu;
39         if(cpu == 0)
40                 forward_interrupts(to->thread.mode.tt.extern_pid);
41 #ifdef CONFIG_SMP
42         forward_ipi(cpu_data[cpu].ipi_pipe[0], to->thread.mode.tt.extern_pid);
43 #endif
44         local_irq_save(flags);
45
46         vtalrm = change_sig(SIGVTALRM, 0);
47         alrm = change_sig(SIGALRM, 0);
48         prof = change_sig(SIGPROF, 0);
49
50         forward_pending_sigio(to->thread.mode.tt.extern_pid);
51
52         c = 0;
53
54         err = os_write_file(to->thread.mode.tt.switch_pipe[1], &c, sizeof(c));
55         if(err != sizeof(c))
56                 panic("write of switch_pipe failed, err = %d", -err);
57
58         if(from->thread.mode.tt.switch_pipe[0] == -1)
59                 os_kill_process(os_getpid(), 0);
60
61         err = os_read_file(from->thread.mode.tt.switch_pipe[0], &c, sizeof(c));
62         if(err != sizeof(c))
63                 panic("read of switch_pipe failed, errno = %d", -err);
64
65         /* If the process that we have just scheduled away from has exited,
66          * then it needs to be killed here.  The reason is that, even though
67          * it will kill itself when it next runs, that may be too late.  Its
68          * stack will be freed, possibly before then, and if that happens,
69          * we have a use-after-free situation.  So, it gets killed here
70          * in case it has not already killed itself.
71          */
72         prev_sched = current->thread.prev_sched;
73         if(prev_sched->thread.mode.tt.switch_pipe[0] == -1)
74                 os_kill_process(prev_sched->thread.mode.tt.extern_pid, 1);
75
76         change_sig(SIGVTALRM, vtalrm);
77         change_sig(SIGALRM, alrm);
78         change_sig(SIGPROF, prof);
79
80         arch_switch();
81
82         flush_tlb_all();
83         local_irq_restore(flags);
84 }
85
86 void release_thread_tt(struct task_struct *task)
87 {
88         int pid = task->thread.mode.tt.extern_pid;
89
90         /*
91          * We first have to kill the other process, before
92          * closing its switch_pipe. Else it might wake up
93          * and receive "EOF" before we could kill it.
94          */
95         if(os_getpid() != pid)
96                 os_kill_process(pid, 0);
97
98         os_close_file(task->thread.mode.tt.switch_pipe[0]);
99         os_close_file(task->thread.mode.tt.switch_pipe[1]);
100         /* use switch_pipe as flag: thread is released */
101         task->thread.mode.tt.switch_pipe[0] = -1;
102 }
103
104 void suspend_new_thread(int fd)
105 {
106         int err;
107         char c;
108
109         os_stop_process(os_getpid());
110         err = os_read_file(fd, &c, sizeof(c));
111         if(err != sizeof(c))
112                 panic("read failed in suspend_new_thread, err = %d", -err);
113 }
114
115 void schedule_tail(task_t *prev);
116
117 static void new_thread_handler(int sig)
118 {
119         unsigned long disable;
120         int (*fn)(void *);
121         void *arg;
122
123         fn = current->thread.request.u.thread.proc;
124         arg = current->thread.request.u.thread.arg;
125
126         UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
127         disable = (1 << (SIGVTALRM - 1)) | (1 << (SIGALRM - 1)) |
128                 (1 << (SIGIO - 1)) | (1 << (SIGPROF - 1));
129         SC_SIGMASK(UPT_SC(&current->thread.regs.regs)) &= ~disable;
130
131         suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
132
133         force_flush_all();
134         if(current->thread.prev_sched != NULL)
135                 schedule_tail(current->thread.prev_sched);
136         current->thread.prev_sched = NULL;
137
138         init_new_thread_signals(1);
139         enable_timer();
140         free_page(current->thread.temp_stack);
141         set_cmdline("(kernel thread)");
142
143         change_sig(SIGUSR1, 1);
144         change_sig(SIGVTALRM, 1);
145         change_sig(SIGPROF, 1);
146         local_irq_enable();
147         if(!run_kernel_thread(fn, arg, &current->thread.exec_buf))
148                 do_exit(0);
149
150         /* XXX No set_user_mode here because a newly execed process will
151          * immediately segfault on its non-existent IP, coming straight back
152          * to the signal handler, which will call set_user_mode on its way
153          * out.  This should probably change since it's confusing.
154          */
155 }
156
157 static int new_thread_proc(void *stack)
158 {
159         /* local_irq_disable is needed to block out signals until this thread is
160          * properly scheduled.  Otherwise, the tracing thread will get mighty
161          * upset about any signals that arrive before that.
162          * This has the complication that it sets the saved signal mask in
163          * the sigcontext to block signals.  This gets restored when this
164          * thread (or a descendant, since they get a copy of this sigcontext)
165          * returns to userspace.
166          * So, this is compensated for elsewhere.
167          * XXX There is still a small window until local_irq_disable() actually
168          * finishes where signals are possible - shouldn't be a problem in
169          * practice since SIGIO hasn't been forwarded here yet, and the
170          * local_irq_disable should finish before a SIGVTALRM has time to be
171          * delivered.
172          */
173
174         local_irq_disable();
175         init_new_thread_stack(stack, new_thread_handler);
176         os_usr1_process(os_getpid());
177         change_sig(SIGUSR1, 1);
178         return(0);
179 }
180
181 /* Signal masking - signals are blocked at the start of fork_tramp.  They
182  * are re-enabled when finish_fork_handler is entered by fork_tramp hitting
183  * itself with a SIGUSR1.  set_user_mode has to be run with SIGUSR1 off,
184  * so it is blocked before it's called.  They are re-enabled on sigreturn
185  * despite the fact that they were blocked when the SIGUSR1 was issued because
186  * copy_thread copies the parent's sigcontext, including the signal mask
187  * onto the signal frame.
188  */
189
190 void finish_fork_handler(int sig)
191 {
192         UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
193         suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
194
195         force_flush_all();
196         if(current->thread.prev_sched != NULL)
197                 schedule_tail(current->thread.prev_sched);
198         current->thread.prev_sched = NULL;
199
200         enable_timer();
201         change_sig(SIGVTALRM, 1);
202         local_irq_enable();
203         if(current->mm != current->parent->mm)
204                 protect_memory(uml_reserved, high_physmem - uml_reserved, 1, 
205                                1, 0, 1);
206         task_protections((unsigned long) current_thread);
207
208         free_page(current->thread.temp_stack);
209         local_irq_disable();
210         change_sig(SIGUSR1, 0);
211         set_user_mode(current);
212 }
213
214 int fork_tramp(void *stack)
215 {
216         local_irq_disable();
217         arch_init_thread();
218         init_new_thread_stack(stack, finish_fork_handler);
219
220         os_usr1_process(os_getpid());
221         change_sig(SIGUSR1, 1);
222         return(0);
223 }
224
225 int copy_thread_tt(int nr, unsigned long clone_flags, unsigned long sp,
226                    unsigned long stack_top, struct task_struct * p, 
227                    struct pt_regs *regs)
228 {
229         int (*tramp)(void *);
230         int new_pid, err;
231         unsigned long stack;
232         
233         if(current->thread.forking)
234                 tramp = fork_tramp;
235         else {
236                 tramp = new_thread_proc;
237                 p->thread.request.u.thread = current->thread.request.u.thread;
238         }
239
240         err = os_pipe(p->thread.mode.tt.switch_pipe, 1, 1);
241         if(err < 0){
242                 printk("copy_thread : pipe failed, err = %d\n", -err);
243                 return(err);
244         }
245
246         stack = alloc_stack(0, 0);
247         if(stack == 0){
248                 printk(KERN_ERR "copy_thread : failed to allocate "
249                        "temporary stack\n");
250                 return(-ENOMEM);
251         }
252
253         clone_flags &= CLONE_VM;
254         p->thread.temp_stack = stack;
255         new_pid = start_fork_tramp(task_stack_page(p), stack, clone_flags, tramp);
256         if(new_pid < 0){
257                 printk(KERN_ERR "copy_thread : clone failed - errno = %d\n", 
258                        -new_pid);
259                 return(new_pid);
260         }
261
262         if(current->thread.forking){
263                 sc_to_sc(UPT_SC(&p->thread.regs.regs), UPT_SC(&regs->regs));
264                 SC_SET_SYSCALL_RETURN(UPT_SC(&p->thread.regs.regs), 0);
265                 if(sp != 0)
266                         SC_SP(UPT_SC(&p->thread.regs.regs)) = sp;
267         }
268         p->thread.mode.tt.extern_pid = new_pid;
269
270         current->thread.request.op = OP_FORK;
271         current->thread.request.u.fork.pid = new_pid;
272         os_usr1_process(os_getpid());
273
274         /* Enable the signal and then disable it to ensure that it is handled
275          * here, and nowhere else.
276          */
277         change_sig(SIGUSR1, 1);
278
279         change_sig(SIGUSR1, 0);
280         err = 0;
281         return(err);
282 }
283
284 void reboot_tt(void)
285 {
286         current->thread.request.op = OP_REBOOT;
287         os_usr1_process(os_getpid());
288         change_sig(SIGUSR1, 1);
289 }
290
291 void halt_tt(void)
292 {
293         current->thread.request.op = OP_HALT;
294         os_usr1_process(os_getpid());
295         change_sig(SIGUSR1, 1);
296 }
297
298 void kill_off_processes_tt(void)
299 {
300         struct task_struct *p;
301         int me;
302
303         me = os_getpid();
304         for_each_process(p){
305                 if(p->thread.mode.tt.extern_pid != me) 
306                         os_kill_process(p->thread.mode.tt.extern_pid, 0);
307         }
308         if(init_task.thread.mode.tt.extern_pid != me) 
309                 os_kill_process(init_task.thread.mode.tt.extern_pid, 0);
310 }
311
312 void initial_thread_cb_tt(void (*proc)(void *), void *arg)
313 {
314         if(os_getpid() == tracing_pid){
315                 (*proc)(arg);
316         }
317         else {
318                 current->thread.request.op = OP_CB;
319                 current->thread.request.u.cb.proc = proc;
320                 current->thread.request.u.cb.arg = arg;
321                 os_usr1_process(os_getpid());
322                 change_sig(SIGUSR1, 1);
323
324                 change_sig(SIGUSR1, 0);
325         }
326 }
327
328 int do_proc_op(void *t, int proc_id)
329 {
330         struct task_struct *task;
331         struct thread_struct *thread;
332         int op, pid;
333
334         task = t;
335         thread = &task->thread;
336         op = thread->request.op;
337         switch(op){
338         case OP_NONE:
339         case OP_TRACE_ON:
340                 break;
341         case OP_EXEC:
342                 pid = thread->request.u.exec.pid;
343                 do_exec(thread->mode.tt.extern_pid, pid);
344                 thread->mode.tt.extern_pid = pid;
345                 cpu_tasks[task_thread_info(task)->cpu].pid = pid;
346                 break;
347         case OP_FORK:
348                 attach_process(thread->request.u.fork.pid);
349                 break;
350         case OP_CB:
351                 (*thread->request.u.cb.proc)(thread->request.u.cb.arg);
352                 break;
353         case OP_REBOOT:
354         case OP_HALT:
355                 break;
356         default:
357                 tracer_panic("Bad op in do_proc_op");
358                 break;
359         }
360         thread->request.op = OP_NONE;
361         return(op);
362 }
363
364 void init_idle_tt(void)
365 {
366         default_idle();
367 }
368
369 extern void start_kernel(void);
370
371 static int start_kernel_proc(void *unused)
372 {
373         int pid;
374
375         block_signals();
376         pid = os_getpid();
377
378         cpu_tasks[0].pid = pid;
379         cpu_tasks[0].task = current;
380 #ifdef CONFIG_SMP
381         cpu_online_map = cpumask_of_cpu(0);
382 #endif
383         if(debug) os_stop_process(pid);
384         start_kernel();
385         return(0);
386 }
387
388 void set_tracing(void *task, int tracing)
389 {
390         ((struct task_struct *) task)->thread.mode.tt.tracing = tracing;
391 }
392
393 int is_tracing(void *t)
394 {
395         return (((struct task_struct *) t)->thread.mode.tt.tracing);
396 }
397
398 int set_user_mode(void *t)
399 {
400         struct task_struct *task;
401
402         task = t ? t : current;
403         if(task->thread.mode.tt.tracing) 
404                 return(1);
405         task->thread.request.op = OP_TRACE_ON;
406         os_usr1_process(os_getpid());
407         return(0);
408 }
409
410 void set_init_pid(int pid)
411 {
412         int err;
413
414         init_task.thread.mode.tt.extern_pid = pid;
415         err = os_pipe(init_task.thread.mode.tt.switch_pipe, 1, 1);
416         if(err)
417                 panic("Can't create switch pipe for init_task, errno = %d",
418                       -err);
419 }
420
421 int start_uml_tt(void)
422 {
423         void *sp;
424         int pages;
425
426         pages = (1 << CONFIG_KERNEL_STACK_ORDER);
427         sp = task_stack_page(&init_task) +
428                 pages * PAGE_SIZE - sizeof(unsigned long);
429         return(tracer(start_kernel_proc, sp));
430 }
431
432 int external_pid_tt(struct task_struct *task)
433 {
434         return(task->thread.mode.tt.extern_pid);
435 }
436
437 int thread_pid_tt(struct task_struct *task)
438 {
439         return(task->thread.mode.tt.extern_pid);
440 }
441
442 int is_valid_pid(int pid)
443 {
444         struct task_struct *task;
445
446         read_lock(&tasklist_lock);
447         for_each_process(task){
448                 if(task->thread.mode.tt.extern_pid == pid){
449                         read_unlock(&tasklist_lock);
450                         return(1);
451                 }
452         }
453         read_unlock(&tasklist_lock);
454         return(0);
455 }