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