13c3bb155dea6bca1a903e8c39f80de8ca061b2b
[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 != 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 < 1){
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                 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                 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, 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         if (local_using_sysemu)
150                 err = ptrace(PTRACE_SYSEMU, pid, 0, 0);
151         else
152                 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
153         if(err)
154                 panic("userspace - PTRACE_SYSCALL failed, errno = %d\n", 
155                        errno);
156         while(1){
157                 err = waitpid(pid, &status, WUNTRACED);
158                 if(err < 0)
159                         panic("userspace - waitpid failed, errno = %d\n", 
160                               errno);
161
162                 regs->skas.is_user = 1;
163                 save_registers(regs);
164
165                 if(WIFSTOPPED(status)){
166                         switch(WSTOPSIG(status)){
167                         case SIGSEGV:
168                                 handle_segv(pid);
169                                 break;
170                         case SIGTRAP:
171                                 handle_trap(pid, regs, local_using_sysemu);
172                                 break;
173                         case SIGIO:
174                         case SIGVTALRM:
175                         case SIGILL:
176                         case SIGBUS:
177                         case SIGFPE:
178                         case SIGWINCH:
179                                 user_signal(WSTOPSIG(status), regs);
180                                 break;
181                         default:
182                                 printk("userspace - child stopped with signal "
183                                        "%d\n", WSTOPSIG(status));
184                         }
185                         interrupt_end();
186                 }
187
188                 restore_registers(regs);
189
190                 /*Now we ended the syscall, so re-read local_using_sysemu.*/
191                 local_using_sysemu = get_using_sysemu();
192
193                 if (local_using_sysemu)
194                         op = singlestepping_skas() ? PTRACE_SINGLESTEP :
195                                 PTRACE_SYSEMU;
196                 else
197                         op = singlestepping_skas() ? PTRACE_SINGLESTEP :
198                                 PTRACE_SYSCALL;
199
200                 err = ptrace(op, pid, 0, 0);
201                 if(err)
202                         panic("userspace - PTRACE_SYSCALL failed, "
203                               "errno = %d\n", errno);
204         }
205 }
206
207 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
208                 void (*handler)(int))
209 {
210         unsigned long flags;
211         jmp_buf switch_buf, fork_buf;
212
213         *switch_buf_ptr = &switch_buf;
214         *fork_buf_ptr = &fork_buf;
215
216         /* Somewhat subtle - siglongjmp restores the signal mask before doing
217          * the longjmp.  This means that when jumping from one stack to another
218          * when the target stack has interrupts enabled, an interrupt may occur
219          * on the source stack.  This is bad when starting up a process because
220          * it's not supposed to get timer ticks until it has been scheduled.
221          * So, we disable interrupts around the sigsetjmp to ensure that 
222          * they can't happen until we get back here where they are safe.
223          */
224         flags = get_signals();
225         block_signals();
226         if(sigsetjmp(fork_buf, 1) == 0)
227                 new_thread_proc(stack, handler);
228         set_signals(flags);
229
230         remove_sigstack();
231 }
232
233 void thread_wait(void *sw, void *fb)
234 {
235         jmp_buf buf, **switch_buf = sw, *fork_buf;
236
237         *switch_buf = &buf;
238         fork_buf = fb;
239         if(sigsetjmp(buf, 1) == 0)
240                 siglongjmp(*fork_buf, 1);
241 }
242
243 static int move_registers(int pid, int int_op, int fp_op, 
244                           union uml_pt_regs *regs, unsigned long *fp_regs)
245 {
246         if(ptrace(int_op, pid, 0, regs->skas.regs) < 0)
247                 return(-errno);
248         if(ptrace(fp_op, pid, 0, fp_regs) < 0)
249                 return(-errno);
250         return(0);
251 }
252
253 void save_registers(union uml_pt_regs *regs)
254 {
255         unsigned long *fp_regs;
256         int err, fp_op;
257
258         if(have_fpx_regs){
259                 fp_op = PTRACE_GETFPXREGS;
260                 fp_regs = regs->skas.xfp;
261         }
262         else {
263                 fp_op = PTRACE_GETFPREGS;
264                 fp_regs = regs->skas.fp;
265         }
266
267         err = move_registers(userspace_pid[0], PTRACE_GETREGS, fp_op, regs, 
268                              fp_regs);
269         if(err)
270                 panic("save_registers - saving registers failed, errno = %d\n",
271                       -err);
272 }
273
274 void restore_registers(union uml_pt_regs *regs)
275 {
276         unsigned long *fp_regs;
277         int err, fp_op;
278
279         if(have_fpx_regs){
280                 fp_op = PTRACE_SETFPXREGS;
281                 fp_regs = regs->skas.xfp;
282         }
283         else {
284                 fp_op = PTRACE_SETFPREGS;
285                 fp_regs = regs->skas.fp;
286         }
287
288         err = move_registers(userspace_pid[0], PTRACE_SETREGS, fp_op, regs, 
289                              fp_regs);
290         if(err)
291                 panic("restore_registers - saving registers failed, "
292                       "errno = %d\n", -err);
293 }
294
295 void switch_threads(void *me, void *next)
296 {
297         jmp_buf my_buf, **me_ptr = me, *next_buf = next;
298         
299         *me_ptr = &my_buf;
300         if(sigsetjmp(my_buf, 1) == 0)
301                 siglongjmp(*next_buf, 1);
302 }
303
304 static jmp_buf initial_jmpbuf;
305
306 /* XXX Make these percpu */
307 static void (*cb_proc)(void *arg);
308 static void *cb_arg;
309 static jmp_buf *cb_back;
310
311 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
312 {
313         jmp_buf **switch_buf = switch_buf_ptr;
314         int n;
315
316         *fork_buf_ptr = &initial_jmpbuf;
317         n = sigsetjmp(initial_jmpbuf, 1);
318         if(n == 0)
319                 new_thread_proc((void *) stack, new_thread_handler);
320         else if(n == 1)
321                 remove_sigstack();
322         else if(n == 2){
323                 (*cb_proc)(cb_arg);
324                 siglongjmp(*cb_back, 1);
325         }
326         else if(n == 3){
327                 kmalloc_ok = 0;
328                 return(0);
329         }
330         else if(n == 4){
331                 kmalloc_ok = 0;
332                 return(1);
333         }
334         siglongjmp(**switch_buf, 1);
335 }
336
337 void remove_sigstack(void)
338 {
339         stack_t stack = ((stack_t) { .ss_flags  = SS_DISABLE,
340                                      .ss_sp     = NULL,
341                                      .ss_size   = 0 });
342
343         if(sigaltstack(&stack, NULL) != 0)
344                 panic("disabling signal stack failed, errno = %d\n", errno);
345 }
346
347 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
348 {
349         jmp_buf here;
350
351         cb_proc = proc;
352         cb_arg = arg;
353         cb_back = &here;
354
355         block_signals();
356         if(sigsetjmp(here, 1) == 0)
357                 siglongjmp(initial_jmpbuf, 2);
358         unblock_signals();
359
360         cb_proc = NULL;
361         cb_arg = NULL;
362         cb_back = NULL;
363 }
364
365 void halt_skas(void)
366 {
367         block_signals();
368         siglongjmp(initial_jmpbuf, 3);
369 }
370
371 void reboot_skas(void)
372 {
373         block_signals();
374         siglongjmp(initial_jmpbuf, 4);
375 }
376
377 int new_mm(int from)
378 {
379         struct proc_mm_op copy;
380         int n, fd = os_open_file("/proc/mm", 
381                                  of_cloexec(of_write(OPENFLAGS())), 0);
382
383         if(fd < 0)
384                 return(fd);
385
386         if(from != -1){
387                 copy = ((struct proc_mm_op) { .op       = MM_COPY_SEGMENTS,
388                                               .u        = 
389                                               { .copy_segments  = from } } );
390                 n = os_write_file(fd, &copy, sizeof(copy));
391                 if(n != sizeof(copy)) 
392                         printk("new_mm : /proc/mm copy_segments failed, "
393                                "err = %d\n", -n);
394         }
395
396         return(fd);
397 }
398
399 void switch_mm_skas(int mm_fd)
400 {
401         int err;
402
403 #warning need cpu pid in switch_mm_skas
404         err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
405         if(err)
406                 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
407                       errno);
408 }
409
410 void kill_off_processes_skas(void)
411 {
412 #warning need to loop over userspace_pids in kill_off_processes_skas
413         os_kill_process(userspace_pid[0], 1);
414 }
415
416 void init_registers(int pid)
417 {
418         int err;
419
420         if(ptrace(PTRACE_GETREGS, pid, 0, exec_regs) < 0)
421                 panic("check_ptrace : PTRACE_GETREGS failed, errno = %d", 
422                       errno);
423
424         err = ptrace(PTRACE_GETFPXREGS, pid, 0, exec_fpx_regs);
425         if(!err)
426                 return;
427
428         have_fpx_regs = 0;
429         if(errno != EIO)
430                 panic("check_ptrace : PTRACE_GETFPXREGS failed, errno = %d", 
431                       errno);
432
433         err = ptrace(PTRACE_GETFPREGS, pid, 0, exec_fp_regs);
434         if(err)
435                 panic("check_ptrace : PTRACE_GETFPREGS failed, errno = %d", 
436                       errno);
437 }
438
439 /*
440  * Overrides for Emacs so that we follow Linus's tabbing style.
441  * Emacs will notice this stuff at the end of the file and automatically
442  * adjust the settings for this buffer only.  This must remain at the end
443  * of the file.
444  * ---------------------------------------------------------------------------
445  * Local variables:
446  * c-file-style: "linux"
447  * End:
448  */