[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / arch / um / kernel / tt / process_kern.c
blobdf810ca8fc1204a1c48c1c05e6f3c93d945fbb9c
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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"
29 void *switch_to_tt(void *prev, void *next, void *last)
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
38 static int reading;
40 from = prev;
41 to = next;
43 to->thread.prev_sched = from;
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);
53 vtalrm = change_sig(SIGVTALRM, 0);
54 alrm = change_sig(SIGALRM, 0);
55 prof = change_sig(SIGPROF, 0);
57 forward_pending_sigio(to->thread.mode.tt.extern_pid);
59 c = 0;
60 set_current(to);
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);
67 reading = 1;
68 if(from->thread.mode.tt.switch_pipe[0] == -1)
69 os_kill_process(os_getpid(), 0);
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);
75 /* If the process that we have just scheduled away from has exited,
76 * then it needs to be killed here. The reason is that, even though
77 * it will kill itself when it next runs, that may be too late. Its
78 * stack will be freed, possibly before then, and if that happens,
79 * we have a use-after-free situation. So, it gets killed here
80 * in case it has not already killed itself.
82 prev_sched = current->thread.prev_sched;
83 if(prev_sched->thread.mode.tt.switch_pipe[0] == -1)
84 os_kill_process(prev_sched->thread.mode.tt.extern_pid, 1);
86 change_sig(SIGVTALRM, vtalrm);
87 change_sig(SIGALRM, alrm);
88 change_sig(SIGPROF, prof);
90 arch_switch();
92 flush_tlb_all();
93 local_irq_restore(flags);
95 return(current->thread.prev_sched);
98 void release_thread_tt(struct task_struct *task)
100 int pid = task->thread.mode.tt.extern_pid;
103 * We first have to kill the other process, before
104 * closing its switch_pipe. Else it might wake up
105 * and receive "EOF" before we could kill it.
107 if(os_getpid() != pid)
108 os_kill_process(pid, 0);
110 os_close_file(task->thread.mode.tt.switch_pipe[0]);
111 os_close_file(task->thread.mode.tt.switch_pipe[1]);
112 /* use switch_pipe as flag: thread is released */
113 task->thread.mode.tt.switch_pipe[0] = -1;
116 void suspend_new_thread(int fd)
118 int err;
119 char c;
121 os_stop_process(os_getpid());
122 err = os_read_file(fd, &c, sizeof(c));
123 if(err != sizeof(c))
124 panic("read failed in suspend_new_thread, err = %d", -err);
127 void schedule_tail(task_t *prev);
129 static void new_thread_handler(int sig)
131 unsigned long disable;
132 int (*fn)(void *);
133 void *arg;
135 fn = current->thread.request.u.thread.proc;
136 arg = current->thread.request.u.thread.arg;
138 UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
139 disable = (1 << (SIGVTALRM - 1)) | (1 << (SIGALRM - 1)) |
140 (1 << (SIGIO - 1)) | (1 << (SIGPROF - 1));
141 SC_SIGMASK(UPT_SC(&current->thread.regs.regs)) &= ~disable;
143 suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
145 force_flush_all();
146 if(current->thread.prev_sched != NULL)
147 schedule_tail(current->thread.prev_sched);
148 current->thread.prev_sched = NULL;
150 init_new_thread_signals(1);
151 enable_timer();
152 free_page(current->thread.temp_stack);
153 set_cmdline("(kernel thread)");
155 change_sig(SIGUSR1, 1);
156 change_sig(SIGVTALRM, 1);
157 change_sig(SIGPROF, 1);
158 local_irq_enable();
159 if(!run_kernel_thread(fn, arg, &current->thread.exec_buf))
160 do_exit(0);
162 /* XXX No set_user_mode here because a newly execed process will
163 * immediately segfault on its non-existent IP, coming straight back
164 * to the signal handler, which will call set_user_mode on its way
165 * out. This should probably change since it's confusing.
169 static int new_thread_proc(void *stack)
171 /* local_irq_disable is needed to block out signals until this thread is
172 * properly scheduled. Otherwise, the tracing thread will get mighty
173 * upset about any signals that arrive before that.
174 * This has the complication that it sets the saved signal mask in
175 * the sigcontext to block signals. This gets restored when this
176 * thread (or a descendant, since they get a copy of this sigcontext)
177 * returns to userspace.
178 * So, this is compensated for elsewhere.
179 * XXX There is still a small window until local_irq_disable() actually
180 * finishes where signals are possible - shouldn't be a problem in
181 * practice since SIGIO hasn't been forwarded here yet, and the
182 * local_irq_disable should finish before a SIGVTALRM has time to be
183 * delivered.
186 local_irq_disable();
187 init_new_thread_stack(stack, new_thread_handler);
188 os_usr1_process(os_getpid());
189 change_sig(SIGUSR1, 1);
190 return(0);
193 /* Signal masking - signals are blocked at the start of fork_tramp. They
194 * are re-enabled when finish_fork_handler is entered by fork_tramp hitting
195 * itself with a SIGUSR1. set_user_mode has to be run with SIGUSR1 off,
196 * so it is blocked before it's called. They are re-enabled on sigreturn
197 * despite the fact that they were blocked when the SIGUSR1 was issued because
198 * copy_thread copies the parent's sigcontext, including the signal mask
199 * onto the signal frame.
202 void finish_fork_handler(int sig)
204 UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
205 suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
207 force_flush_all();
208 if(current->thread.prev_sched != NULL)
209 schedule_tail(current->thread.prev_sched);
210 current->thread.prev_sched = NULL;
212 enable_timer();
213 change_sig(SIGVTALRM, 1);
214 local_irq_enable();
215 if(current->mm != current->parent->mm)
216 protect_memory(uml_reserved, high_physmem - uml_reserved, 1,
217 1, 0, 1);
218 task_protections((unsigned long) current_thread);
220 free_page(current->thread.temp_stack);
221 local_irq_disable();
222 change_sig(SIGUSR1, 0);
223 set_user_mode(current);
226 int fork_tramp(void *stack)
228 local_irq_disable();
229 arch_init_thread();
230 init_new_thread_stack(stack, finish_fork_handler);
232 os_usr1_process(os_getpid());
233 change_sig(SIGUSR1, 1);
234 return(0);
237 int copy_thread_tt(int nr, unsigned long clone_flags, unsigned long sp,
238 unsigned long stack_top, struct task_struct * p,
239 struct pt_regs *regs)
241 int (*tramp)(void *);
242 int new_pid, err;
243 unsigned long stack;
245 if(current->thread.forking)
246 tramp = fork_tramp;
247 else {
248 tramp = new_thread_proc;
249 p->thread.request.u.thread = current->thread.request.u.thread;
252 err = os_pipe(p->thread.mode.tt.switch_pipe, 1, 1);
253 if(err < 0){
254 printk("copy_thread : pipe failed, err = %d\n", -err);
255 return(err);
258 stack = alloc_stack(0, 0);
259 if(stack == 0){
260 printk(KERN_ERR "copy_thread : failed to allocate "
261 "temporary stack\n");
262 return(-ENOMEM);
265 clone_flags &= CLONE_VM;
266 p->thread.temp_stack = stack;
267 new_pid = start_fork_tramp(p->thread_info, stack, clone_flags, tramp);
268 if(new_pid < 0){
269 printk(KERN_ERR "copy_thread : clone failed - errno = %d\n",
270 -new_pid);
271 return(new_pid);
274 if(current->thread.forking){
275 sc_to_sc(UPT_SC(&p->thread.regs.regs),
276 UPT_SC(&current->thread.regs.regs));
277 SC_SET_SYSCALL_RETURN(UPT_SC(&p->thread.regs.regs), 0);
278 if(sp != 0) SC_SP(UPT_SC(&p->thread.regs.regs)) = sp;
280 p->thread.mode.tt.extern_pid = new_pid;
282 current->thread.request.op = OP_FORK;
283 current->thread.request.u.fork.pid = new_pid;
284 os_usr1_process(os_getpid());
286 /* Enable the signal and then disable it to ensure that it is handled
287 * here, and nowhere else.
289 change_sig(SIGUSR1, 1);
291 change_sig(SIGUSR1, 0);
292 err = 0;
293 return(err);
296 void reboot_tt(void)
298 current->thread.request.op = OP_REBOOT;
299 os_usr1_process(os_getpid());
300 change_sig(SIGUSR1, 1);
303 void halt_tt(void)
305 current->thread.request.op = OP_HALT;
306 os_usr1_process(os_getpid());
307 change_sig(SIGUSR1, 1);
310 void kill_off_processes_tt(void)
312 struct task_struct *p;
313 int me;
315 me = os_getpid();
316 for_each_process(p){
317 if(p->thread.mode.tt.extern_pid != me)
318 os_kill_process(p->thread.mode.tt.extern_pid, 0);
320 if(init_task.thread.mode.tt.extern_pid != me)
321 os_kill_process(init_task.thread.mode.tt.extern_pid, 0);
324 void initial_thread_cb_tt(void (*proc)(void *), void *arg)
326 if(os_getpid() == tracing_pid){
327 (*proc)(arg);
329 else {
330 current->thread.request.op = OP_CB;
331 current->thread.request.u.cb.proc = proc;
332 current->thread.request.u.cb.arg = arg;
333 os_usr1_process(os_getpid());
334 change_sig(SIGUSR1, 1);
336 change_sig(SIGUSR1, 0);
340 int do_proc_op(void *t, int proc_id)
342 struct task_struct *task;
343 struct thread_struct *thread;
344 int op, pid;
346 task = t;
347 thread = &task->thread;
348 op = thread->request.op;
349 switch(op){
350 case OP_NONE:
351 case OP_TRACE_ON:
352 break;
353 case OP_EXEC:
354 pid = thread->request.u.exec.pid;
355 do_exec(thread->mode.tt.extern_pid, pid);
356 thread->mode.tt.extern_pid = pid;
357 cpu_tasks[task->thread_info->cpu].pid = pid;
358 break;
359 case OP_FORK:
360 attach_process(thread->request.u.fork.pid);
361 break;
362 case OP_CB:
363 (*thread->request.u.cb.proc)(thread->request.u.cb.arg);
364 break;
365 case OP_REBOOT:
366 case OP_HALT:
367 break;
368 default:
369 tracer_panic("Bad op in do_proc_op");
370 break;
372 thread->request.op = OP_NONE;
373 return(op);
376 void init_idle_tt(void)
378 default_idle();
381 extern void start_kernel(void);
383 static int start_kernel_proc(void *unused)
385 int pid;
387 block_signals();
388 pid = os_getpid();
390 cpu_tasks[0].pid = pid;
391 cpu_tasks[0].task = current;
392 #ifdef CONFIG_SMP
393 cpu_online_map = cpumask_of_cpu(0);
394 #endif
395 if(debug) os_stop_process(pid);
396 start_kernel();
397 return(0);
400 void set_tracing(void *task, int tracing)
402 ((struct task_struct *) task)->thread.mode.tt.tracing = tracing;
405 int is_tracing(void *t)
407 return (((struct task_struct *) t)->thread.mode.tt.tracing);
410 int set_user_mode(void *t)
412 struct task_struct *task;
414 task = t ? t : current;
415 if(task->thread.mode.tt.tracing)
416 return(1);
417 task->thread.request.op = OP_TRACE_ON;
418 os_usr1_process(os_getpid());
419 return(0);
422 void set_init_pid(int pid)
424 int err;
426 init_task.thread.mode.tt.extern_pid = pid;
427 err = os_pipe(init_task.thread.mode.tt.switch_pipe, 1, 1);
428 if(err)
429 panic("Can't create switch pipe for init_task, errno = %d",
430 -err);
433 int start_uml_tt(void)
435 void *sp;
436 int pages;
438 pages = (1 << CONFIG_KERNEL_STACK_ORDER);
439 sp = (void *) ((unsigned long) init_task.thread_info) +
440 pages * PAGE_SIZE - sizeof(unsigned long);
441 return(tracer(start_kernel_proc, sp));
444 int external_pid_tt(struct task_struct *task)
446 return(task->thread.mode.tt.extern_pid);
449 int thread_pid_tt(struct task_struct *task)
451 return(task->thread.mode.tt.extern_pid);
454 int is_valid_pid(int pid)
456 struct task_struct *task;
458 read_lock(&tasklist_lock);
459 for_each_process(task){
460 if(task->thread.mode.tt.extern_pid == pid){
461 read_unlock(&tasklist_lock);
462 return(1);
465 read_unlock(&tasklist_lock);
466 return(0);
470 * Overrides for Emacs so that we follow Linus's tabbing style.
471 * Emacs will notice this stuff at the end of the file and automatically
472 * adjust the settings for this buffer only. This must remain at the end
473 * of the file.
474 * ---------------------------------------------------------------------------
475 * Local variables:
476 * c-file-style: "linux"
477 * End: