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