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