Pull asus_acpi-0.30 into release branch
[linux-2.6] / 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         arch_switch_to_skas(current->thread.prev_sched, current);
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         os_usr1_signal(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         os_usr1_signal(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
95         /* XXX: if interrupt_end() calls schedule, this call to
96          * arch_switch_to_skas isn't needed. We could want to apply this to
97          * improve performance. -bb */
98         arch_switch_to_skas(current->thread.prev_sched, current);
99
100         current->thread.prev_sched = NULL;
101
102 /* Handle any immediate reschedules or signals */
103         interrupt_end();
104
105         userspace(&current->thread.regs.regs);
106 }
107
108 int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp,
109                      unsigned long stack_top, struct task_struct * p,
110                      struct pt_regs *regs)
111 {
112         void (*handler)(int);
113
114         if(current->thread.forking){
115                 memcpy(&p->thread.regs.regs.skas, &regs->regs.skas,
116                        sizeof(p->thread.regs.regs.skas));
117                 REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.skas.regs, 0);
118                 if(sp != 0) REGS_SP(p->thread.regs.regs.skas.regs) = sp;
119
120                 handler = fork_handler;
121
122                 arch_copy_thread(&current->thread.arch, &p->thread.arch);
123         }
124         else {
125                 init_thread_registers(&p->thread.regs.regs);
126                 p->thread.request.u.thread = current->thread.request.u.thread;
127                 handler = new_thread_handler;
128         }
129
130         new_thread(task_stack_page(p), &p->thread.mode.skas.switch_buf,
131                    &p->thread.mode.skas.fork_buf, handler);
132         return(0);
133 }
134
135 int new_mm(unsigned long stack)
136 {
137         int fd;
138
139         fd = os_open_file("/proc/mm", of_cloexec(of_write(OPENFLAGS())), 0);
140         if(fd < 0)
141                 return(fd);
142
143         if(skas_needs_stub)
144                 map_stub_pages(fd, CONFIG_STUB_CODE, CONFIG_STUB_DATA, stack);
145
146         return(fd);
147 }
148
149 void init_idle_skas(void)
150 {
151         cpu_tasks[current_thread->cpu].pid = os_getpid();
152         default_idle();
153 }
154
155 extern void start_kernel(void);
156
157 static int start_kernel_proc(void *unused)
158 {
159         int pid;
160
161         block_signals();
162         pid = os_getpid();
163
164         cpu_tasks[0].pid = pid;
165         cpu_tasks[0].task = current;
166 #ifdef CONFIG_SMP
167         cpu_online_map = cpumask_of_cpu(0);
168 #endif
169         start_kernel();
170         return(0);
171 }
172
173 extern int userspace_pid[];
174
175 int start_uml_skas(void)
176 {
177         if(proc_mm)
178                 userspace_pid[0] = start_userspace(0);
179
180         init_new_thread_signals(1);
181
182         init_task.thread.request.u.thread.proc = start_kernel_proc;
183         init_task.thread.request.u.thread.arg = NULL;
184         return(start_idle_thread(task_stack_page(&init_task),
185                                  &init_task.thread.mode.skas.switch_buf,
186                                  &init_task.thread.mode.skas.fork_buf));
187 }
188
189 int external_pid_skas(struct task_struct *task)
190 {
191 #warning Need to look up userspace_pid by cpu
192         return(userspace_pid[0]);
193 }
194
195 int thread_pid_skas(struct task_struct *task)
196 {
197 #warning Need to look up userspace_pid by cpu
198         return(userspace_pid[0]);
199 }
200
201 void kill_off_processes_skas(void)
202 {
203         if(proc_mm)
204 #warning need to loop over userspace_pids in kill_off_processes_skas
205                 os_kill_ptraced_process(userspace_pid[0], 1);
206         else {
207                 struct task_struct *p;
208                 int pid, me;
209
210                 me = os_getpid();
211                 for_each_process(p){
212                         if(p->mm == NULL)
213                                 continue;
214
215                         pid = p->mm->context.skas.id.u.pid;
216                         os_kill_ptraced_process(pid, 1);
217                 }
218         }
219 }
220
221 unsigned long current_stub_stack(void)
222 {
223         if(current->mm == NULL)
224                 return(0);
225
226         return(current->mm->context.skas.id.stack);
227 }