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