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