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