fedora core 2.6.10-1.12-FC2
[linux-2.6.git] / arch / um / kernel / skas / process.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <setjmp.h>
11 #include <sched.h>
12 #include <sys/wait.h>
13 #include <sys/ptrace.h>
14 #include <sys/mman.h>
15 #include <sys/user.h>
16 #include <asm/unistd.h>
17 #include "user.h"
18 #include "ptrace_user.h"
19 #include "time_user.h"
20 #include "sysdep/ptrace.h"
21 #include "user_util.h"
22 #include "kern_util.h"
23 #include "skas.h"
24 #include "sysdep/sigcontext.h"
25 #include "os.h"
26 #include "proc_mm.h"
27 #include "skas_ptrace.h"
28 #include "chan_user.h"
29 #include "signal_user.h"
30
31 int is_skas_winch(int pid, int fd, void *data)
32 {
33         if(pid != os_getpid())
34                 return(0);
35
36         register_winch_irq(-1, fd, -1, data);
37         return(1);
38 }
39
40 /* These are set once at boot time and not changed thereafter */
41
42 unsigned long exec_regs[FRAME_SIZE];
43 unsigned long exec_fp_regs[HOST_FP_SIZE];
44 unsigned long exec_fpx_regs[HOST_XFP_SIZE];
45 int have_fpx_regs = 1;
46
47 static void handle_segv(int pid)
48 {
49         struct ptrace_faultinfo fault;
50         int err;
51
52         err = ptrace(PTRACE_FAULTINFO, pid, 0, &fault);
53         if(err)
54                 panic("handle_segv - PTRACE_FAULTINFO failed, errno = %d\n",
55                       errno);
56
57         segv(fault.addr, 0, FAULT_WRITE(fault.is_write), 1, NULL);
58 }
59
60 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
61 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
62 {
63         int err, syscall_nr, status;
64
65         syscall_nr = PT_SYSCALL_NR(regs->skas.regs);
66         UPT_SYSCALL_NR(regs) = syscall_nr;
67         if(syscall_nr < 0){
68                 relay_signal(SIGTRAP, regs);
69                 return;
70         }
71
72         if (!local_using_sysemu)
73         {
74                 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
75                 if(err < 0)
76                         panic("handle_trap - nullifying syscall failed errno = %d\n",
77                               errno);
78
79                 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
80                 if(err < 0)
81                         panic("handle_trap - continuing to end of syscall failed, "
82                               "errno = %d\n", errno);
83
84                 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
85                 if((err < 0) || !WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
86                         panic("handle_trap - failed to wait at end of syscall, "
87                               "errno = %d, status = %d\n", errno, status);
88         }
89
90         handle_syscall(regs);
91 }
92
93 static int userspace_tramp(void *arg)
94 {
95         init_new_thread_signals(0);
96         enable_timer();
97         ptrace(PTRACE_TRACEME, 0, 0, 0);
98         os_stop_process(os_getpid());
99         return(0);
100 }
101
102 /* Each element set once, and only accessed by a single processor anyway */
103 #define NR_CPUS 1
104 int userspace_pid[NR_CPUS];
105
106 void start_userspace(int cpu)
107 {
108         void *stack;
109         unsigned long sp;
110         int pid, status, n;
111
112         stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
113                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
114         if(stack == MAP_FAILED)
115                 panic("start_userspace : mmap failed, errno = %d", errno);
116         sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
117
118         pid = clone(userspace_tramp, (void *) sp, 
119                     CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
120         if(pid < 0)
121                 panic("start_userspace : clone failed, errno = %d", errno);
122
123         do {
124                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
125                 if(n < 0)
126                         panic("start_userspace : wait failed, errno = %d", 
127                               errno);
128         } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
129
130         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
131                 panic("start_userspace : expected SIGSTOP, got status = %d",
132                       status);
133
134         if(munmap(stack, PAGE_SIZE) < 0)
135                 panic("start_userspace : munmap failed, errno = %d\n", errno);
136
137         userspace_pid[cpu] = pid;
138 }
139
140 void userspace(union uml_pt_regs *regs)
141 {
142         int err, status, op, pt_syscall_parm, pid = userspace_pid[0];
143         int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
144
145         restore_registers(regs);
146                 
147         local_using_sysemu = get_using_sysemu();
148
149         pt_syscall_parm = local_using_sysemu ? PTRACE_SYSEMU : PTRACE_SYSCALL;
150         err = ptrace(pt_syscall_parm, pid, 0, 0);
151
152         if(err)
153                 panic("userspace - PTRACE_%s failed, errno = %d\n",
154                        local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
155         while(1){
156                 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
157                 if(err < 0)
158                         panic("userspace - waitpid failed, errno = %d\n", 
159                               errno);
160
161                 regs->skas.is_user = 1;
162                 save_registers(regs);
163
164                 if(WIFSTOPPED(status)){
165                         switch(WSTOPSIG(status)){
166                         case SIGSEGV:
167                                 handle_segv(pid);
168                                 break;
169                         case SIGTRAP:
170                                 handle_trap(pid, regs, local_using_sysemu);
171                                 break;
172                         case SIGIO:
173                         case SIGVTALRM:
174                         case SIGILL:
175                         case SIGBUS:
176                         case SIGFPE:
177                         case SIGWINCH:
178                                 user_signal(WSTOPSIG(status), regs);
179                                 break;
180                         default:
181                                 printk("userspace - child stopped with signal "
182                                        "%d\n", WSTOPSIG(status));
183                         }
184                         interrupt_end();
185                 }
186
187                 restore_registers(regs);
188
189                 /*Now we ended the syscall, so re-read local_using_sysemu.*/
190                 local_using_sysemu = get_using_sysemu();
191                 pt_syscall_parm = local_using_sysemu ? PTRACE_SYSEMU : PTRACE_SYSCALL;
192
193                 op = singlestepping(NULL) ? PTRACE_SINGLESTEP :
194                         pt_syscall_parm;
195
196                 err = ptrace(op, pid, 0, 0);
197                 if(err)
198                         panic("userspace - PTRACE_%s failed, "
199                               "errno = %d\n",
200                               local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
201         }
202 }
203
204 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
205                 void (*handler)(int))
206 {
207         unsigned long flags;
208         sigjmp_buf switch_buf, fork_buf;
209
210         *switch_buf_ptr = &switch_buf;
211         *fork_buf_ptr = &fork_buf;
212
213         /* Somewhat subtle - siglongjmp restores the signal mask before doing
214          * the longjmp.  This means that when jumping from one stack to another
215          * when the target stack has interrupts enabled, an interrupt may occur
216          * on the source stack.  This is bad when starting up a process because
217          * it's not supposed to get timer ticks until it has been scheduled.
218          * So, we disable interrupts around the sigsetjmp to ensure that
219          * they can't happen until we get back here where they are safe.
220          */
221         flags = get_signals();
222         block_signals();
223         if(sigsetjmp(fork_buf, 1) == 0)
224                 new_thread_proc(stack, handler);
225         set_signals(flags);
226
227         remove_sigstack();
228 }
229
230 void thread_wait(void *sw, void *fb)
231 {
232         sigjmp_buf buf, **switch_buf = sw, *fork_buf;
233
234         *switch_buf = &buf;
235         fork_buf = fb;
236         if(sigsetjmp(buf, 1) == 0)
237                 siglongjmp(*fork_buf, 1);
238 }
239
240 static int move_registers(int pid, int int_op, int fp_op,
241                           union uml_pt_regs *regs, unsigned long *fp_regs)
242 {
243         if(ptrace(int_op, pid, 0, regs->skas.regs) < 0)
244                 return(-errno);
245         if(ptrace(fp_op, pid, 0, fp_regs) < 0)
246                 return(-errno);
247         return(0);
248 }
249
250 void save_registers(union uml_pt_regs *regs)
251 {
252         unsigned long *fp_regs;
253         int err, fp_op;
254
255         if(have_fpx_regs){
256                 fp_op = PTRACE_GETFPXREGS;
257                 fp_regs = regs->skas.xfp;
258         }
259         else {
260                 fp_op = PTRACE_GETFPREGS;
261                 fp_regs = regs->skas.fp;
262         }
263
264         err = move_registers(userspace_pid[0], PTRACE_GETREGS, fp_op, regs,
265                              fp_regs);
266         if(err)
267                 panic("save_registers - saving registers failed, errno = %d\n",
268                       -err);
269 }
270
271 void restore_registers(union uml_pt_regs *regs)
272 {
273         unsigned long *fp_regs;
274         int err, fp_op;
275
276         if(have_fpx_regs){
277                 fp_op = PTRACE_SETFPXREGS;
278                 fp_regs = regs->skas.xfp;
279         }
280         else {
281                 fp_op = PTRACE_SETFPREGS;
282                 fp_regs = regs->skas.fp;
283         }
284
285         err = move_registers(userspace_pid[0], PTRACE_SETREGS, fp_op, regs,
286                              fp_regs);
287         if(err)
288                 panic("restore_registers - saving registers failed, "
289                       "errno = %d\n", -err);
290 }
291
292 void switch_threads(void *me, void *next)
293 {
294         sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
295         
296         *me_ptr = &my_buf;
297         if(sigsetjmp(my_buf, 1) == 0)
298                 siglongjmp(*next_buf, 1);
299 }
300
301 static sigjmp_buf initial_jmpbuf;
302
303 /* XXX Make these percpu */
304 static void (*cb_proc)(void *arg);
305 static void *cb_arg;
306 static sigjmp_buf *cb_back;
307
308 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
309 {
310         sigjmp_buf **switch_buf = switch_buf_ptr;
311         int n;
312
313         *fork_buf_ptr = &initial_jmpbuf;
314         n = sigsetjmp(initial_jmpbuf, 1);
315         if(n == 0)
316                 new_thread_proc((void *) stack, new_thread_handler);
317         else if(n == 1)
318                 remove_sigstack();
319         else if(n == 2){
320                 (*cb_proc)(cb_arg);
321                 siglongjmp(*cb_back, 1);
322         }
323         else if(n == 3){
324                 kmalloc_ok = 0;
325                 return(0);
326         }
327         else if(n == 4){
328                 kmalloc_ok = 0;
329                 return(1);
330         }
331         siglongjmp(**switch_buf, 1);
332 }
333
334 void remove_sigstack(void)
335 {
336         stack_t stack = ((stack_t) { .ss_flags  = SS_DISABLE,
337                                      .ss_sp     = NULL,
338                                      .ss_size   = 0 });
339
340         if(sigaltstack(&stack, NULL) != 0)
341                 panic("disabling signal stack failed, errno = %d\n", errno);
342 }
343
344 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
345 {
346         sigjmp_buf here;
347
348         cb_proc = proc;
349         cb_arg = arg;
350         cb_back = &here;
351
352         block_signals();
353         if(sigsetjmp(here, 1) == 0)
354                 siglongjmp(initial_jmpbuf, 2);
355         unblock_signals();
356
357         cb_proc = NULL;
358         cb_arg = NULL;
359         cb_back = NULL;
360 }
361
362 void halt_skas(void)
363 {
364         block_signals();
365         siglongjmp(initial_jmpbuf, 3);
366 }
367
368 void reboot_skas(void)
369 {
370         block_signals();
371         siglongjmp(initial_jmpbuf, 4);
372 }
373
374 void switch_mm_skas(int mm_fd)
375 {
376         int err;
377
378 #warning need cpu pid in switch_mm_skas
379         err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
380         if(err)
381                 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
382                       errno);
383 }
384
385 void kill_off_processes_skas(void)
386 {
387 #warning need to loop over userspace_pids in kill_off_processes_skas
388         os_kill_ptraced_process(userspace_pid[0], 1);
389 }
390
391 void init_registers(int pid)
392 {
393         int err;
394
395         if(ptrace(PTRACE_GETREGS, pid, 0, exec_regs) < 0)
396                 panic("check_ptrace : PTRACE_GETREGS failed, errno = %d", 
397                       errno);
398
399         err = ptrace(PTRACE_GETFPXREGS, pid, 0, exec_fpx_regs);
400         if(!err)
401                 return;
402
403         have_fpx_regs = 0;
404         if(errno != EIO)
405                 panic("check_ptrace : PTRACE_GETFPXREGS failed, errno = %d", 
406                       errno);
407
408         err = ptrace(PTRACE_GETFPREGS, pid, 0, exec_fp_regs);
409         if(err)
410                 panic("check_ptrace : PTRACE_GETFPREGS failed, errno = %d", 
411                       errno);
412 }
413
414 /*
415  * Overrides for Emacs so that we follow Linus's tabbing style.
416  * Emacs will notice this stuff at the end of the file and automatically
417  * adjust the settings for this buffer only.  This must remain at the end
418  * of the file.
419  * ---------------------------------------------------------------------------
420  * Local variables:
421  * c-file-style: "linux"
422  * End:
423  */