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