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