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