Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-for-linus-2.6
[linux-2.6] / arch / um / kernel / skas / process.c
1 /* 
2  * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <signal.h>
11 #include <setjmp.h>
12 #include <sched.h>
13 #include <sys/wait.h>
14 #include <sys/mman.h>
15 #include <sys/user.h>
16 #include <sys/time.h>
17 #include <asm/unistd.h>
18 #include <asm/types.h>
19 #include "user.h"
20 #include "ptrace_user.h"
21 #include "time_user.h"
22 #include "sysdep/ptrace.h"
23 #include "user_util.h"
24 #include "kern_util.h"
25 #include "skas.h"
26 #include "stub-data.h"
27 #include "mm_id.h"
28 #include "sysdep/sigcontext.h"
29 #include "sysdep/stub.h"
30 #include "os.h"
31 #include "proc_mm.h"
32 #include "skas_ptrace.h"
33 #include "chan_user.h"
34 #include "signal_user.h"
35 #include "registers.h"
36 #include "mem.h"
37 #include "uml-config.h"
38 #include "process.h"
39
40 int is_skas_winch(int pid, int fd, void *data)
41 {
42         if(pid != os_getpgrp())
43                 return(0);
44
45         register_winch_irq(-1, fd, -1, data);
46         return(1);
47 }
48
49 void wait_stub_done(int pid, int sig, char * fname)
50 {
51         int n, status, err;
52
53         do {
54                 if ( sig != -1 ) {
55                         err = ptrace(PTRACE_CONT, pid, 0, sig);
56                         if(err)
57                                 panic("%s : continue failed, errno = %d\n",
58                                       fname, errno);
59                 }
60                 sig = 0;
61
62                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
63         } while((n >= 0) && WIFSTOPPED(status) &&
64                 (WSTOPSIG(status) == SIGVTALRM));
65
66         if((n < 0) || !WIFSTOPPED(status) ||
67            (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status != SIGTRAP))){
68                 panic("%s : failed to wait for SIGUSR1/SIGTRAP, "
69                       "pid = %d, n = %d, errno = %d, status = 0x%x\n",
70                       fname, pid, n, errno, status);
71         }
72 }
73
74 void get_skas_faultinfo(int pid, struct faultinfo * fi)
75 {
76         int err;
77
78         if(ptrace_faultinfo){
79                 err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
80                 if(err)
81                         panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
82                               "errno = %d\n", errno);
83
84                 /* Special handling for i386, which has different structs */
85                 if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo))
86                         memset((char *)fi + sizeof(struct ptrace_faultinfo), 0,
87                                sizeof(struct faultinfo) -
88                                sizeof(struct ptrace_faultinfo));
89         }
90         else {
91                 wait_stub_done(pid, SIGSEGV, "get_skas_faultinfo");
92
93                 /* faultinfo is prepared by the stub-segv-handler at start of
94                  * the stub stack page. We just have to copy it.
95                  */
96                 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
97         }
98 }
99
100 static void handle_segv(int pid, union uml_pt_regs * regs)
101 {
102         get_skas_faultinfo(pid, &regs->skas.faultinfo);
103         segv(regs->skas.faultinfo, 0, 1, NULL);
104 }
105
106 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
107 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
108 {
109         int err, status;
110
111         /* Mark this as a syscall */
112         UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
113
114         if (!local_using_sysemu)
115         {
116                 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
117                 if(err < 0)
118                         panic("handle_trap - nullifying syscall failed errno = %d\n",
119                               errno);
120
121                 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
122                 if(err < 0)
123                         panic("handle_trap - continuing to end of syscall failed, "
124                               "errno = %d\n", errno);
125
126                 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
127                 if((err < 0) || !WIFSTOPPED(status) ||
128                    (WSTOPSIG(status) != SIGTRAP + 0x80))
129                         panic("handle_trap - failed to wait at end of syscall, "
130                               "errno = %d, status = %d\n", errno, status);
131         }
132
133         handle_syscall(regs);
134 }
135
136 extern int __syscall_stub_start;
137
138 static int userspace_tramp(void *stack)
139 {
140         void *addr;
141
142         ptrace(PTRACE_TRACEME, 0, 0, 0);
143
144         init_new_thread_signals(1);
145         enable_timer();
146
147         if(!proc_mm){
148                 /* This has a pte, but it can't be mapped in with the usual
149                  * tlb_flush mechanism because this is part of that mechanism
150                  */
151                 int fd;
152                 __u64 offset;
153
154                 fd = phys_mapping(to_phys(&__syscall_stub_start), &offset);
155                 addr = mmap64((void *) UML_CONFIG_STUB_CODE, page_size(),
156                               PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
157                 if(addr == MAP_FAILED){
158                         printk("mapping mmap stub failed, errno = %d\n",
159                                errno);
160                         exit(1);
161                 }
162
163                 if(stack != NULL){
164                         fd = phys_mapping(to_phys(stack), &offset);
165                         addr = mmap((void *) UML_CONFIG_STUB_DATA, page_size(),
166                                     PROT_READ | PROT_WRITE,
167                                     MAP_FIXED | MAP_SHARED, fd, offset);
168                         if(addr == MAP_FAILED){
169                                 printk("mapping segfault stack failed, "
170                                        "errno = %d\n", errno);
171                                 exit(1);
172                         }
173                 }
174         }
175         if(!ptrace_faultinfo && (stack != NULL)){
176                 unsigned long v = UML_CONFIG_STUB_CODE +
177                                   (unsigned long) stub_segv_handler -
178                                   (unsigned long) &__syscall_stub_start;
179
180                 set_sigstack((void *) UML_CONFIG_STUB_DATA, page_size());
181                 set_handler(SIGSEGV, (void *) v, SA_ONSTACK,
182                             SIGIO, SIGWINCH, SIGALRM, SIGVTALRM,
183                             SIGUSR1, -1);
184         }
185
186         os_stop_process(os_getpid());
187         return(0);
188 }
189
190 /* Each element set once, and only accessed by a single processor anyway */
191 #undef NR_CPUS
192 #define NR_CPUS 1
193 int userspace_pid[NR_CPUS];
194
195 int start_userspace(unsigned long stub_stack)
196 {
197         void *stack;
198         unsigned long sp;
199         int pid, status, n, flags;
200
201         stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
202                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
203         if(stack == MAP_FAILED)
204                 panic("start_userspace : mmap failed, errno = %d", errno);
205         sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
206
207         flags = CLONE_FILES | SIGCHLD;
208         if(proc_mm) flags |= CLONE_VM;
209         pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
210         if(pid < 0)
211                 panic("start_userspace : clone failed, errno = %d", errno);
212
213         do {
214                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
215                 if(n < 0)
216                         panic("start_userspace : wait failed, errno = %d", 
217                               errno);
218         } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
219
220         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
221                 panic("start_userspace : expected SIGSTOP, got status = %d",
222                       status);
223
224         if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
225                 panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
226                       errno);
227
228         if(munmap(stack, PAGE_SIZE) < 0)
229                 panic("start_userspace : munmap failed, errno = %d\n", errno);
230
231         return(pid);
232 }
233
234 void userspace(union uml_pt_regs *regs)
235 {
236         int err, status, op, pid = userspace_pid[0];
237         int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
238
239         while(1){
240                 restore_registers(pid, regs);
241
242                 /* Now we set local_using_sysemu to be used for one loop */
243                 local_using_sysemu = get_using_sysemu();
244
245                 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
246
247                 err = ptrace(op, pid, 0, 0);
248                 if(err)
249                         panic("userspace - could not resume userspace process, "
250                               "pid=%d, ptrace operation = %d, errno = %d\n",
251                               op, errno);
252
253                 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
254                 if(err < 0)
255                         panic("userspace - waitpid failed, errno = %d\n", 
256                               errno);
257
258                 regs->skas.is_user = 1;
259                 save_registers(pid, regs);
260                 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
261
262                 if(WIFSTOPPED(status)){
263                         switch(WSTOPSIG(status)){
264                         case SIGSEGV:
265                                 if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo)
266                                         user_signal(SIGSEGV, regs, pid);
267                                 else handle_segv(pid, regs);
268                                 break;
269                         case SIGTRAP + 0x80:
270                                 handle_trap(pid, regs, local_using_sysemu);
271                                 break;
272                         case SIGTRAP:
273                                 relay_signal(SIGTRAP, regs);
274                                 break;
275                         case SIGIO:
276                         case SIGVTALRM:
277                         case SIGILL:
278                         case SIGBUS:
279                         case SIGFPE:
280                         case SIGWINCH:
281                                 user_signal(WSTOPSIG(status), regs, pid);
282                                 break;
283                         default:
284                                 printk("userspace - child stopped with signal "
285                                        "%d\n", WSTOPSIG(status));
286                         }
287                         pid = userspace_pid[0];
288                         interrupt_end();
289
290                         /* Avoid -ERESTARTSYS handling in host */
291                         PT_SYSCALL_NR(regs->skas.regs) = -1;
292                 }
293         }
294 }
295 #define INIT_JMP_NEW_THREAD 0
296 #define INIT_JMP_REMOVE_SIGSTACK 1
297 #define INIT_JMP_CALLBACK 2
298 #define INIT_JMP_HALT 3
299 #define INIT_JMP_REBOOT 4
300
301
302 int copy_context_skas0(unsigned long new_stack, int pid)
303 {
304         int err;
305         unsigned long regs[MAX_REG_NR];
306         unsigned long current_stack = current_stub_stack();
307         struct stub_data *data = (struct stub_data *) current_stack;
308         struct stub_data *child_data = (struct stub_data *) new_stack;
309         __u64 new_offset;
310         int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
311
312         /* prepare offset and fd of child's stack as argument for parent's
313          * and child's mmap2 calls
314          */
315         *data = ((struct stub_data) { .offset   = MMAP_OFFSET(new_offset),
316                                       .fd       = new_fd,
317                                       .timer    = ((struct itimerval)
318                                                    { { 0, 1000000 / hz() },
319                                                      { 0, 1000000 / hz() }})});
320         get_safe_registers(regs);
321
322         /* Set parent's instruction pointer to start of clone-stub */
323         regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
324                                 (unsigned long) stub_clone_handler -
325                                 (unsigned long) &__syscall_stub_start;
326         regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + PAGE_SIZE -
327                 sizeof(void *);
328         err = ptrace_setregs(pid, regs);
329         if(err < 0)
330                 panic("copy_context_skas0 : PTRACE_SETREGS failed, "
331                       "pid = %d, errno = %d\n", pid, errno);
332
333         /* set a well known return code for detection of child write failure */
334         child_data->err = 12345678;
335
336         /* Wait, until parent has finished its work: read child's pid from
337          * parent's stack, and check, if bad result.
338          */
339         wait_stub_done(pid, 0, "copy_context_skas0");
340
341         pid = data->err;
342         if(pid < 0)
343                 panic("copy_context_skas0 - stub-parent reports error %d\n",
344                       pid);
345
346         /* Wait, until child has finished too: read child's result from
347          * child's stack and check it.
348          */
349         wait_stub_done(pid, -1, "copy_context_skas0");
350         if (child_data->err != UML_CONFIG_STUB_DATA)
351                 panic("copy_context_skas0 - stub-child reports error %d\n",
352                       child_data->err);
353
354         if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
355                    (void *)PTRACE_O_TRACESYSGOOD) < 0)
356                 panic("copy_context_skas0 : PTRACE_SETOPTIONS failed, "
357                       "errno = %d\n", errno);
358
359         return pid;
360 }
361
362 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
363                 void (*handler)(int))
364 {
365         unsigned long flags;
366         sigjmp_buf switch_buf, fork_buf;
367
368         *switch_buf_ptr = &switch_buf;
369         *fork_buf_ptr = &fork_buf;
370
371         /* Somewhat subtle - siglongjmp restores the signal mask before doing
372          * the longjmp.  This means that when jumping from one stack to another
373          * when the target stack has interrupts enabled, an interrupt may occur
374          * on the source stack.  This is bad when starting up a process because
375          * it's not supposed to get timer ticks until it has been scheduled.
376          * So, we disable interrupts around the sigsetjmp to ensure that
377          * they can't happen until we get back here where they are safe.
378          */
379         flags = get_signals();
380         block_signals();
381         if(sigsetjmp(fork_buf, 1) == 0)
382                 new_thread_proc(stack, handler);
383
384         remove_sigstack();
385
386         set_signals(flags);
387 }
388
389 void thread_wait(void *sw, void *fb)
390 {
391         sigjmp_buf buf, **switch_buf = sw, *fork_buf;
392
393         *switch_buf = &buf;
394         fork_buf = fb;
395         if(sigsetjmp(buf, 1) == 0)
396                 siglongjmp(*fork_buf, INIT_JMP_REMOVE_SIGSTACK);
397 }
398
399 void switch_threads(void *me, void *next)
400 {
401         sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
402         
403         *me_ptr = &my_buf;
404         if(sigsetjmp(my_buf, 1) == 0)
405                 siglongjmp(*next_buf, 1);
406 }
407
408 static sigjmp_buf initial_jmpbuf;
409
410 /* XXX Make these percpu */
411 static void (*cb_proc)(void *arg);
412 static void *cb_arg;
413 static sigjmp_buf *cb_back;
414
415 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
416 {
417         sigjmp_buf **switch_buf = switch_buf_ptr;
418         int n;
419
420         set_handler(SIGWINCH, (__sighandler_t) sig_handler,
421                     SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
422                     SIGVTALRM, -1);
423
424         *fork_buf_ptr = &initial_jmpbuf;
425         n = sigsetjmp(initial_jmpbuf, 1);
426         switch(n){
427         case INIT_JMP_NEW_THREAD:
428                 new_thread_proc((void *) stack, new_thread_handler);
429                 break;
430         case INIT_JMP_REMOVE_SIGSTACK:
431                 remove_sigstack();
432                 break;
433         case INIT_JMP_CALLBACK:
434                 (*cb_proc)(cb_arg);
435                 siglongjmp(*cb_back, 1);
436                 break;
437         case INIT_JMP_HALT:
438                 kmalloc_ok = 0;
439                 return(0);
440         case INIT_JMP_REBOOT:
441                 kmalloc_ok = 0;
442                 return(1);
443         default:
444                 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n);
445         }
446         siglongjmp(**switch_buf, 1);
447 }
448
449 void remove_sigstack(void)
450 {
451         stack_t stack = ((stack_t) { .ss_flags  = SS_DISABLE,
452                                      .ss_sp     = NULL,
453                                      .ss_size   = 0 });
454
455         if(sigaltstack(&stack, NULL) != 0)
456                 panic("disabling signal stack failed, errno = %d\n", errno);
457 }
458
459 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
460 {
461         sigjmp_buf here;
462
463         cb_proc = proc;
464         cb_arg = arg;
465         cb_back = &here;
466
467         block_signals();
468         if(sigsetjmp(here, 1) == 0)
469                 siglongjmp(initial_jmpbuf, INIT_JMP_CALLBACK);
470         unblock_signals();
471
472         cb_proc = NULL;
473         cb_arg = NULL;
474         cb_back = NULL;
475 }
476
477 void halt_skas(void)
478 {
479         block_signals();
480         siglongjmp(initial_jmpbuf, INIT_JMP_HALT);
481 }
482
483 void reboot_skas(void)
484 {
485         block_signals();
486         siglongjmp(initial_jmpbuf, INIT_JMP_REBOOT);
487 }
488
489 void switch_mm_skas(struct mm_id *mm_idp)
490 {
491         int err;
492
493 #warning need cpu pid in switch_mm_skas
494         if(proc_mm){
495                 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
496                              mm_idp->u.mm_fd);
497                 if(err)
498                         panic("switch_mm_skas - PTRACE_SWITCH_MM failed, "
499                               "errno = %d\n", errno);
500         }
501         else userspace_pid[0] = mm_idp->u.pid;
502 }
503
504 /*
505  * Overrides for Emacs so that we follow Linus's tabbing style.
506  * Emacs will notice this stuff at the end of the file and automatically
507  * adjust the settings for this buffer only.  This must remain at the end
508  * of the file.
509  * ---------------------------------------------------------------------------
510  * Local variables:
511  * c-file-style: "linux"
512  * End:
513  */