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