Merge master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / arch / um / kernel / skas / 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/slab.h"
8 #include "linux/ptrace.h"
9 #include "linux/proc_fs.h"
10 #include "linux/file.h"
11 #include "linux/errno.h"
12 #include "linux/init.h"
13 #include "asm/uaccess.h"
14 #include "asm/atomic.h"
15 #include "kern_util.h"
16 #include "time_user.h"
17 #include "skas.h"
18 #include "os.h"
19 #include "user_util.h"
20 #include "tlb.h"
21 #include "kern.h"
22 #include "mode.h"
23 #include "proc_mm.h"
24 #include "registers.h"
25
26 void switch_to_skas(void *prev, void *next)
27 {
28         struct task_struct *from, *to;
29
30         from = prev;
31         to = next;
32
33         /* XXX need to check runqueues[cpu].idle */
34         if(current->pid == 0)
35                 switch_timers(0);
36
37         switch_threads(&from->thread.mode.skas.switch_buf, 
38                        to->thread.mode.skas.switch_buf);
39
40         if(current->pid == 0)
41                 switch_timers(1);
42 }
43
44 extern void schedule_tail(struct task_struct *prev);
45
46 void new_thread_handler(int sig)
47 {
48         int (*fn)(void *), n;
49         void *arg;
50
51         fn = current->thread.request.u.thread.proc;
52         arg = current->thread.request.u.thread.arg;
53         change_sig(SIGUSR1, 1);
54         thread_wait(&current->thread.mode.skas.switch_buf, 
55                     current->thread.mode.skas.fork_buf);
56
57         if(current->thread.prev_sched != NULL)
58                 schedule_tail(current->thread.prev_sched);
59         current->thread.prev_sched = NULL;
60
61         /* The return value is 1 if the kernel thread execs a process,
62          * 0 if it just exits
63          */
64         n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
65         if(n == 1){
66                 /* Handle any immediate reschedules or signals */
67                 interrupt_end();
68                 userspace(&current->thread.regs.regs);
69         }
70         else do_exit(0);
71 }
72
73 void new_thread_proc(void *stack, void (*handler)(int sig))
74 {
75         init_new_thread_stack(stack, handler);
76         os_usr1_process(os_getpid());
77 }
78
79 void release_thread_skas(struct task_struct *task)
80 {
81 }
82
83 void fork_handler(int sig)
84 {
85         change_sig(SIGUSR1, 1);
86         thread_wait(&current->thread.mode.skas.switch_buf, 
87                     current->thread.mode.skas.fork_buf);
88         
89         force_flush_all();
90         if(current->thread.prev_sched == NULL)
91                 panic("blech");
92
93         schedule_tail(current->thread.prev_sched);
94         current->thread.prev_sched = NULL;
95
96         /* Handle any immediate reschedules or signals */
97         interrupt_end();
98         userspace(&current->thread.regs.regs);
99 }
100
101 int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp,
102                      unsigned long stack_top, struct task_struct * p, 
103                      struct pt_regs *regs)
104 {
105         void (*handler)(int);
106
107         if(current->thread.forking){
108                 memcpy(&p->thread.regs.regs.skas, &regs->regs.skas,
109                        sizeof(p->thread.regs.regs.skas));
110                 REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.skas.regs, 0);
111                 if(sp != 0) REGS_SP(p->thread.regs.regs.skas.regs) = sp;
112
113                 handler = fork_handler;
114         }
115         else {
116                 init_thread_registers(&p->thread.regs.regs);
117                 p->thread.request.u.thread = current->thread.request.u.thread;
118                 handler = new_thread_handler;
119         }
120
121         new_thread(p->thread_info, &p->thread.mode.skas.switch_buf,
122                    &p->thread.mode.skas.fork_buf, handler);
123         return(0);
124 }
125
126 extern void map_stub_pages(int fd, unsigned long code,
127                            unsigned long data, unsigned long stack);
128 int new_mm(int from, unsigned long stack)
129 {
130         struct proc_mm_op copy;
131         int n, fd;
132
133         fd = os_open_file("/proc/mm", of_cloexec(of_write(OPENFLAGS())), 0);
134         if(fd < 0)
135                 return(fd);
136
137         if(from != -1){
138                 copy = ((struct proc_mm_op) { .op       = MM_COPY_SEGMENTS,
139                                               .u        =
140                                               { .copy_segments  = from } } );
141                 n = os_write_file(fd, &copy, sizeof(copy));
142                 if(n != sizeof(copy))
143                         printk("new_mm : /proc/mm copy_segments failed, "
144                                "err = %d\n", -n);
145         }
146
147         if(skas_needs_stub)
148                 map_stub_pages(fd, CONFIG_STUB_CODE, CONFIG_STUB_DATA, stack);
149
150         return(fd);
151 }
152
153 void init_idle_skas(void)
154 {
155         cpu_tasks[current_thread->cpu].pid = os_getpid();
156         default_idle();
157 }
158
159 extern void start_kernel(void);
160
161 static int start_kernel_proc(void *unused)
162 {
163         int pid;
164
165         block_signals();
166         pid = os_getpid();
167
168         cpu_tasks[0].pid = pid;
169         cpu_tasks[0].task = current;
170 #ifdef CONFIG_SMP
171         cpu_online_map = cpumask_of_cpu(0);
172 #endif
173         start_kernel();
174         return(0);
175 }
176
177 extern int userspace_pid[];
178
179 int start_uml_skas(void)
180 {
181         if(proc_mm)
182                 userspace_pid[0] = start_userspace(0);
183
184         init_new_thread_signals(1);
185
186         init_task.thread.request.u.thread.proc = start_kernel_proc;
187         init_task.thread.request.u.thread.arg = NULL;
188         return(start_idle_thread(init_task.thread_info,
189                                  &init_task.thread.mode.skas.switch_buf,
190                                  &init_task.thread.mode.skas.fork_buf));
191 }
192
193 int external_pid_skas(struct task_struct *task)
194 {
195 #warning Need to look up userspace_pid by cpu
196         return(userspace_pid[0]);
197 }
198
199 int thread_pid_skas(struct task_struct *task)
200 {
201 #warning Need to look up userspace_pid by cpu
202         return(userspace_pid[0]);
203 }
204
205 void kill_off_processes_skas(void)
206 {
207         if(proc_mm)
208 #warning need to loop over userspace_pids in kill_off_processes_skas
209                 os_kill_ptraced_process(userspace_pid[0], 1);
210         else {
211                 struct task_struct *p;
212                 int pid, me;
213
214                 me = os_getpid();
215                 for_each_process(p){
216                         if(p->mm == NULL)
217                                 continue;
218
219                         pid = p->mm->context.skas.id.u.pid;
220                         os_kill_ptraced_process(pid, 1);
221                 }
222         }
223 }
224
225 unsigned long current_stub_stack(void)
226 {
227         if(current->mm == NULL)
228                 return(0);
229
230         return(current->mm->context.skas.id.stack);
231 }