Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / arch / um / kernel / process_kern.c
blob1588e175b4db066f491d8b50a354502d23e4e0ab
1 /*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
6 #include "linux/config.h"
7 #include "linux/kernel.h"
8 #include "linux/sched.h"
9 #include "linux/interrupt.h"
10 #include "linux/mm.h"
11 #include "linux/slab.h"
12 #include "linux/utsname.h"
13 #include "linux/fs.h"
14 #include "linux/utime.h"
15 #include "linux/smp_lock.h"
16 #include "linux/module.h"
17 #include "linux/init.h"
18 #include "linux/capability.h"
19 #include "asm/unistd.h"
20 #include "asm/mman.h"
21 #include "asm/segment.h"
22 #include "asm/stat.h"
23 #include "asm/pgtable.h"
24 #include "asm/processor.h"
25 #include "asm/tlbflush.h"
26 #include "asm/spinlock.h"
27 #include "asm/uaccess.h"
28 #include "asm/user.h"
29 #include "user_util.h"
30 #include "kern_util.h"
31 #include "kern.h"
32 #include "signal_kern.h"
33 #include "signal_user.h"
34 #include "init.h"
35 #include "irq_user.h"
36 #include "mem_user.h"
37 #include "time_user.h"
38 #include "tlb.h"
39 #include "frame_kern.h"
40 #include "sigcontext.h"
41 #include "2_5compat.h"
42 #include "os.h"
44 /* This is a per-cpu array. A processor only modifies its entry and it only
45 * cares about its entry, so it's OK if another processor is modifying its
46 * entry.
48 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
50 struct task_struct *get_task(int pid, int require)
52 struct task_struct *task, *ret;
54 ret = NULL;
55 read_lock(&tasklist_lock);
56 for_each_process(task){
57 if(task->pid == pid){
58 ret = task;
59 break;
62 read_unlock(&tasklist_lock);
63 if(require && (ret == NULL)) panic("get_task couldn't find a task\n");
64 return(ret);
67 int is_valid_pid(int pid)
69 struct task_struct *task;
71 read_lock(&tasklist_lock);
72 for_each_process(task){
73 if(task->thread.extern_pid == pid){
74 read_unlock(&tasklist_lock);
75 return(1);
78 read_unlock(&tasklist_lock);
79 return(0);
82 int external_pid(void *t)
84 struct task_struct *task = t ? t : current;
86 return(task->thread.extern_pid);
89 int pid_to_processor_id(int pid)
91 int i;
93 for(i = 0; i < ncpus; i++){
94 if(cpu_tasks[i].pid == pid) return(i);
96 return(-1);
99 void free_stack(unsigned long stack, int order)
101 free_pages(stack, order);
104 void set_init_pid(int pid)
106 int err;
108 init_task.thread.extern_pid = pid;
109 err = os_pipe(init_task.thread.switch_pipe, 1, 1);
110 if(err) panic("Can't create switch pipe for init_task, errno = %d",
111 err);
114 int set_user_mode(void *t)
116 struct task_struct *task;
118 task = t ? t : current;
119 if(task->thread.tracing) return(1);
120 task->thread.request.op = OP_TRACE_ON;
121 os_usr1_process(os_getpid());
122 return(0);
125 void set_tracing(void *task, int tracing)
127 ((struct task_struct *) task)->thread.tracing = tracing;
130 int is_tracing(void *t)
132 return (((struct task_struct *) t)->thread.tracing);
135 unsigned long alloc_stack(int order, int atomic)
137 unsigned long page;
138 int flags = GFP_KERNEL;
140 if(atomic) flags |= GFP_ATOMIC;
141 if((page = __get_free_pages(flags, order)) == 0)
142 return(0);
143 stack_protections(page);
144 return(page);
147 extern void schedule_tail(struct task_struct *prev);
149 static void new_thread_handler(int sig)
151 int (*fn)(void *);
152 void *arg;
154 fn = current->thread.request.u.thread.proc;
155 arg = current->thread.request.u.thread.arg;
156 current->thread.regs.regs.sc = (void *) (&sig + 1);
157 suspend_new_thread(current->thread.switch_pipe[0]);
159 block_signals();
160 #ifdef CONFIG_SMP
161 schedule_tail(NULL);
162 #endif
163 enable_timer();
164 free_page(current->thread.temp_stack);
165 set_cmdline("(kernel thread)");
166 force_flush_all();
168 current->thread.prev_sched = NULL;
169 change_sig(SIGUSR1, 1);
170 change_sig(SIGVTALRM, 1);
171 change_sig(SIGPROF, 1);
172 unblock_signals();
173 if(!run_kernel_thread(fn, arg, &current->thread.jmp))
174 do_exit(0);
177 static int new_thread_proc(void *stack)
179 change_sig(SIGIO, 0);
180 change_sig(SIGVTALRM, 0);
181 change_sig(SIGPROF, 0);
182 init_new_thread(stack, new_thread_handler);
183 os_usr1_process(os_getpid());
184 return(0);
187 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
189 struct task_struct *p;
191 current->thread.request.u.thread.proc = fn;
192 current->thread.request.u.thread.arg = arg;
193 p = do_fork(CLONE_VM | flags, 0, NULL, 0, NULL);
194 if(IS_ERR(p)) panic("do_fork failed in kernel_thread");
195 return(p->pid);
198 void switch_mm(struct mm_struct *prev, struct mm_struct *next,
199 struct task_struct *tsk, unsigned cpu)
201 if (prev != next)
202 clear_bit(cpu, &prev->cpu_vm_mask);
203 set_bit(cpu, &next->cpu_vm_mask);
206 void set_current(void *t)
208 struct task_struct *task = t;
210 cpu_tasks[task->thread_info->cpu] = ((struct cpu_task)
211 { task->thread.extern_pid, task });
214 void *switch_to(void *prev, void *next, void *last)
216 struct task_struct *from, *to;
217 unsigned long flags;
218 int vtalrm, alrm, prof, err, cpu;
219 char c;
220 /* jailing and SMP are incompatible, so this doesn't need to be
221 * made per-cpu
223 static int reading;
225 from = prev;
226 to = next;
228 to->thread.prev_sched = from;
230 cpu = from->thread_info->cpu;
231 if(cpu == 0)
232 forward_interrupts(to->thread.extern_pid);
233 #ifdef CONFIG_SMP
234 forward_ipi(cpu_data[cpu].ipi_pipe[0], to->thread.extern_pid);
235 #endif
236 local_irq_save(flags);
238 vtalrm = change_sig(SIGVTALRM, 0);
239 alrm = change_sig(SIGALRM, 0);
240 prof = change_sig(SIGPROF, 0);
242 forward_pending_sigio(to->thread.extern_pid);
244 c = 0;
245 set_current(to);
247 reading = 0;
248 err = os_write_file(to->thread.switch_pipe[1], &c, sizeof(c));
249 if(err != sizeof(c))
250 panic("write of switch_pipe failed, errno = %d", -err);
252 reading = 1;
253 if((from->state == TASK_ZOMBIE) || (from->state == TASK_DEAD))
254 os_kill_process(os_getpid());
256 err = os_read_file(from->thread.switch_pipe[0], &c, sizeof(c));
257 if(err != sizeof(c))
258 panic("read of switch_pipe failed, errno = %d", -err);
260 /* This works around a nasty race with 'jail'. If we are switching
261 * between two threads of a threaded app and the incoming process
262 * runs before the outgoing process reaches the read, and it makes
263 * it all the way out to userspace, then it will have write-protected
264 * the outgoing process stack. Then, when the outgoing process
265 * returns from the write, it will segfault because it can no longer
266 * write its own stack. So, in order to avoid that, the incoming
267 * thread sits in a loop yielding until 'reading' is set. This
268 * isn't entirely safe, since there may be a reschedule from a timer
269 * happening between setting 'reading' and sleeping in read. But,
270 * it should get a whole quantum in which to reach the read and sleep,
271 * which should be enough.
274 if(jail){
275 while(!reading) sched_yield();
278 change_sig(SIGVTALRM, vtalrm);
279 change_sig(SIGALRM, alrm);
280 change_sig(SIGPROF, prof);
282 arch_switch();
284 flush_tlb_all();
285 local_irq_restore(flags);
287 return(current->thread.prev_sched);
290 void interrupt_end(void)
292 if(need_resched()) schedule();
293 if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal(0);
296 void release_thread(struct task_struct *task)
298 os_kill_process(task->thread.extern_pid);
301 void exit_thread(void)
303 close(current->thread.switch_pipe[0]);
304 close(current->thread.switch_pipe[1]);
305 unprotect_stack((unsigned long) current->thread_info);
308 /* Signal masking - signals are blocked at the start of fork_tramp. They
309 * are re-enabled when finish_fork_handler is entered by fork_tramp hitting
310 * itself with a SIGUSR1. set_user_mode has to be run with SIGUSR1 off,
311 * so it is blocked before it's called. They are re-enabled on sigreturn
312 * despite the fact that they were blocked when the SIGUSR1 was issued because
313 * copy_thread copies the parent's signcontext, including the signal mask
314 * onto the signal frame.
317 void finish_fork_handler(int sig)
319 current->thread.regs.regs.sc = (void *) (&sig + 1);
320 suspend_new_thread(current->thread.switch_pipe[0]);
322 #ifdef CONFIG_SMP
323 schedule_tail(NULL);
324 #endif
325 enable_timer();
326 change_sig(SIGVTALRM, 1);
327 force_flush_all();
328 if(current->mm != current->parent->mm)
329 protect(uml_reserved, high_physmem - uml_reserved, 1, 1, 0, 1);
330 task_protections((unsigned long) current->thread_info);
332 current->thread.prev_sched = NULL;
334 free_page(current->thread.temp_stack);
335 change_sig(SIGUSR1, 0);
336 set_user_mode(current);
339 void *get_current(void)
341 return(current);
344 /* This sigusr1 business works around a bug in gcc's -pg support.
345 * Normally a procedure's mcount call comes after esp has been copied to
346 * ebp and the new frame is constructed. With procedures with no locals,
347 * the mcount comes before, as the first thing that the procedure does.
348 * When that procedure is main for a thread, ebp comes in as NULL. So,
349 * when mcount dereferences it, it segfaults. So, UML works around this
350 * by adding a non-optimizable local to the various trampolines, fork_tramp
351 * and outer_tramp below, and exec_tramp.
354 static int sigusr1 = SIGUSR1;
356 int fork_tramp(void *stack)
358 int sig = sigusr1;
360 change_sig(SIGIO, 0);
361 change_sig(SIGVTALRM, 0);
362 change_sig(SIGPROF, 0);
363 init_new_thread(stack, finish_fork_handler);
365 kill(os_getpid(), sig);
366 return(0);
369 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
370 unsigned long stack_top, struct task_struct * p,
371 struct pt_regs *regs)
373 int new_pid, err;
374 unsigned long stack;
375 int (*tramp)(void *);
377 p->thread = (struct thread_struct) INIT_THREAD;
378 p->thread.kernel_stack =
379 (unsigned long) p->thread_info + 2 * PAGE_SIZE;
381 if(current->thread.forking)
382 tramp = fork_tramp;
383 else {
384 tramp = new_thread_proc;
385 p->thread.request.u.thread = current->thread.request.u.thread;
388 err = os_pipe(p->thread.switch_pipe, 1, 1);
389 if(err){
390 printk("copy_thread : pipe failed, errno = %d\n", -err);
391 return(err);
394 stack = alloc_stack(0, 0);
395 if(stack == 0){
396 printk(KERN_ERR "copy_thread : failed to allocate "
397 "temporary stack\n");
398 return(-ENOMEM);
401 clone_flags &= CLONE_VM;
402 p->thread.temp_stack = stack;
403 new_pid = start_fork_tramp((void *) p->thread.kernel_stack, stack,
404 clone_flags, tramp);
405 if(new_pid < 0){
406 printk(KERN_ERR "copy_thread : clone failed - errno = %d\n",
407 -new_pid);
408 return(new_pid);
411 if(current->thread.forking){
412 sc_to_sc(p->thread.regs.regs.sc, current->thread.regs.regs.sc);
413 PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
414 if(sp != 0) PT_REGS_SP(&p->thread.regs) = sp;
416 p->thread.extern_pid = new_pid;
418 current->thread.request.op = OP_FORK;
419 current->thread.request.u.fork.pid = new_pid;
420 os_usr1_process(os_getpid());
421 return(0);
424 void tracing_reboot(void)
426 current->thread.request.op = OP_REBOOT;
427 os_usr1_process(os_getpid());
430 void tracing_halt(void)
432 current->thread.request.op = OP_HALT;
433 os_usr1_process(os_getpid());
436 void tracing_cb(void (*proc)(void *), void *arg)
438 if(os_getpid() == tracing_pid){
439 (*proc)(arg);
441 else {
442 current->thread.request.op = OP_CB;
443 current->thread.request.u.cb.proc = proc;
444 current->thread.request.u.cb.arg = arg;
445 os_usr1_process(os_getpid());
449 int do_proc_op(void *t, int proc_id)
451 struct task_struct *task;
452 struct thread_struct *thread;
453 int op, pid;
455 task = t;
456 thread = &task->thread;
457 op = thread->request.op;
458 switch(op){
459 case OP_NONE:
460 case OP_TRACE_ON:
461 break;
462 case OP_EXEC:
463 pid = thread->request.u.exec.pid;
464 do_exec(thread->extern_pid, pid);
465 thread->extern_pid = pid;
466 cpu_tasks[task->thread_info->cpu].pid = pid;
467 break;
468 case OP_FORK:
469 attach_process(thread->request.u.fork.pid);
470 break;
471 case OP_CB:
472 (*thread->request.u.cb.proc)(thread->request.u.cb.arg);
473 break;
474 case OP_REBOOT:
475 case OP_HALT:
476 break;
477 default:
478 tracer_panic("Bad op in do_proc_op");
479 break;
481 thread->request.op = OP_NONE;
482 return(op);
485 unsigned long stack_sp(unsigned long page)
487 return(page + PAGE_SIZE - sizeof(void *));
490 int current_pid(void)
492 return(current->pid);
495 void default_idle(void)
497 idle_timer();
499 atomic_inc(&init_mm.mm_count);
500 current->mm = &init_mm;
501 current->active_mm = &init_mm;
503 while(1){
504 /* endless idle loop with no priority at all */
505 SET_PRI(current);
508 * although we are an idle CPU, we do not want to
509 * get into the scheduler unnecessarily.
511 irq_stat[smp_processor_id()].idle_timestamp = jiffies;
512 if(need_resched())
513 schedule();
515 idle_sleep(10);
519 void cpu_idle(void)
521 default_idle();
524 int page_size(void)
526 return(PAGE_SIZE);
529 int page_mask(void)
531 return(PAGE_MASK);
534 unsigned long um_virt_to_phys(void *t, unsigned long addr)
536 struct task_struct *task;
537 pgd_t *pgd;
538 pmd_t *pmd;
539 pte_t *pte;
541 task = t;
542 if(task->mm == NULL) return(0xffffffff);
543 pgd = pgd_offset(task->mm, addr);
544 pmd = pmd_offset(pgd, addr);
545 if(!pmd_present(*pmd)) return(0xffffffff);
546 pte = pte_offset_kernel(pmd, addr);
547 if(!pte_present(*pte)) return(0xffffffff);
548 return((pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
551 char *current_cmd(void)
553 #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
554 return("(Unknown)");
555 #else
556 unsigned long addr = um_virt_to_phys(current, current->mm->arg_start);
557 return addr == 0xffffffff? "(Unknown)": __va(addr);
558 #endif
561 void force_sigbus(void)
563 printk(KERN_ERR "Killing pid %d because of a lack of memory\n",
564 current->pid);
565 lock_kernel();
566 sigaddset(&current->pending.signal, SIGBUS);
567 recalc_sigpending();
568 current->flags |= PF_SIGNALED;
569 do_exit(SIGBUS | 0x80);
572 void dump_thread(struct pt_regs *regs, struct user *u)
576 void enable_hlt(void)
578 panic("enable_hlt");
581 void disable_hlt(void)
583 panic("disable_hlt");
586 extern int signal_frame_size;
588 void *um_kmalloc(int size)
590 return(kmalloc(size, GFP_KERNEL));
593 void *um_kmalloc_atomic(int size)
595 return(kmalloc(size, GFP_ATOMIC));
598 unsigned long get_fault_addr(void)
600 return((unsigned long) current->thread.fault_addr);
603 EXPORT_SYMBOL(get_fault_addr);
605 void clear_singlestep(void *t)
607 struct task_struct *task = (struct task_struct *) t;
609 task->ptrace &= ~PT_DTRACE;
612 int singlestepping(void *t)
614 struct task_struct *task = (struct task_struct *) t;
616 if(task->thread.singlestep_syscall)
617 return(0);
618 return(task->ptrace & PT_DTRACE);
621 void not_implemented(void)
623 printk(KERN_DEBUG "Something isn't implemented in here\n");
626 EXPORT_SYMBOL(not_implemented);
628 int user_context(unsigned long sp)
630 return((sp & (PAGE_MASK << 1)) != current->thread.kernel_stack);
633 extern void remove_umid_dir(void);
634 __uml_exitcall(remove_umid_dir);
636 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
638 void do_uml_exitcalls(void)
640 exitcall_t *call;
642 call = &__uml_exitcall_end;
643 while (--call >= &__uml_exitcall_begin)
644 (*call)();
647 void *round_up(unsigned long addr)
649 return(ROUND_UP(addr));
652 void *round_down(unsigned long addr)
654 return(ROUND_DOWN(addr));
657 char *uml_strdup(char *string)
659 char *new;
661 new = kmalloc(strlen(string) + 1, GFP_KERNEL);
662 if(new == NULL) return(NULL);
663 strcpy(new, string);
664 return(new);
667 /* Changed by jail_setup, which is a setup */
668 int jail = 0;
670 int __init jail_setup(char *line, int *add)
672 int ok = 1;
674 if(jail) return(0);
675 #ifdef CONFIG_SMP
676 printf("'jail' may not used used in a kernel with CONFIG_SMP "
677 "enabled\n");
678 ok = 0;
679 #endif
680 #ifdef CONFIG_HOSTFS
681 printf("'jail' may not used used in a kernel with CONFIG_HOSTFS "
682 "enabled\n");
683 ok = 0;
684 #endif
685 #ifdef CONFIG_MODULES
686 printf("'jail' may not used used in a kernel with CONFIG_MODULES "
687 "enabled\n");
688 ok = 0;
689 #endif
690 if(!ok) exit(1);
692 /* CAP_SYS_RAWIO controls the ability to open /dev/mem and /dev/kmem.
693 * Removing it from the bounding set eliminates the ability of anything
694 * to acquire it, and thus read or write kernel memory.
696 cap_lower(cap_bset, CAP_SYS_RAWIO);
697 jail = 1;
698 return(0);
701 __uml_setup("jail", jail_setup,
702 "jail\n"
703 " Enables the protection of kernel memory from processes.\n\n"
706 static void mprotect_kernel_mem(int w)
708 unsigned long start, end;
710 if(!jail || (current == &init_task)) return;
712 start = (unsigned long) current->thread_info + PAGE_SIZE;
713 end = (unsigned long) current->thread_info + PAGE_SIZE * 4;
714 protect(uml_reserved, start - uml_reserved, 1, w, 1, 1);
715 protect(end, high_physmem - end, 1, w, 1, 1);
717 start = (unsigned long) ROUND_DOWN(&_stext);
718 end = (unsigned long) ROUND_UP(&_etext);
719 protect(start, end - start, 1, w, 1, 1);
721 start = (unsigned long) ROUND_DOWN(&_unprotected_end);
722 end = (unsigned long) ROUND_UP(&_edata);
723 protect(start, end - start, 1, w, 1, 1);
725 start = (unsigned long) ROUND_DOWN(&__bss_start);
726 end = (unsigned long) ROUND_UP(brk_start);
727 protect(start, end - start, 1, w, 1, 1);
729 mprotect_kernel_vm(w);
732 /* No SMP problems since jailing and SMP are incompatible */
733 void unprotect_kernel_mem(void)
735 mprotect_kernel_mem(1);
738 void protect_kernel_mem(void)
740 mprotect_kernel_mem(0);
743 void *get_init_task(void)
745 return(&init_thread_union.thread_info.task);
748 int copy_to_user_proc(void *to, void *from, int size)
750 return(copy_to_user(to, from, size));
753 int copy_from_user_proc(void *to, void *from, int size)
755 return(copy_from_user(to, from, size));
758 int clear_user_proc(void *buf, int size)
760 return(clear_user(buf, size));
763 void set_thread_sc(void *sc)
765 current->thread.regs.regs.sc = sc;
768 int smp_sigio_handler(void)
770 #ifdef CONFIG_SMP
771 int cpu = current->thread_info->cpu;
772 IPI_handler(cpu);
773 if(cpu != 0)
774 return(1);
775 #endif
776 return(0);
779 int um_in_interrupt(void)
781 return(in_interrupt());
784 int cpu(void)
786 return(current->thread_info->cpu);
790 * Overrides for Emacs so that we follow Linus's tabbing style.
791 * Emacs will notice this stuff at the end of the file and automatically
792 * adjust the settings for this buffer only. This must remain at the end
793 * of the file.
794 * ---------------------------------------------------------------------------
795 * Local variables:
796 * c-file-style: "linux"
797 * End: