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