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