2 * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
12 #include "ptrace_user.h"
17 #include <sys/syscall.h>
18 #include <asm/types.h>
20 #include "sysdep/ptrace.h"
21 #include "user_util.h"
22 #include "kern_util.h"
24 #include "stub-data.h"
26 #include "sysdep/sigcontext.h"
27 #include "sysdep/stub.h"
30 #include "skas_ptrace.h"
31 #include "chan_user.h"
32 #include "registers.h"
34 #include "uml-config.h"
38 int is_skas_winch(int pid, int fd, void *data)
40 if(pid != os_getpgrp())
43 register_winch_irq(-1, fd, -1, data);
47 void wait_stub_done(int pid, int sig, char * fname)
53 err = ptrace(PTRACE_CONT, pid, 0, sig);
55 panic("%s : continue failed, errno = %d\n",
60 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
61 } while((n >= 0) && WIFSTOPPED(status) &&
62 ((WSTOPSIG(status) == SIGVTALRM) ||
63 /* running UML inside a detached screen can cause
66 (WSTOPSIG(status) == SIGWINCH)));
68 if((n < 0) || !WIFSTOPPED(status) ||
69 (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGTRAP)){
70 unsigned long regs[HOST_FRAME_SIZE];
72 if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
73 printk("Failed to get registers from stub, "
74 "errno = %d\n", errno);
78 printk("Stub registers -\n");
79 for(i = 0; i < HOST_FRAME_SIZE; i++)
80 printk("\t%d - %lx\n", i, regs[i]);
82 panic("%s : failed to wait for SIGUSR1/SIGTRAP, "
83 "pid = %d, n = %d, errno = %d, status = 0x%x\n",
84 fname, pid, n, errno, status);
88 extern unsigned long current_stub_stack(void);
90 void get_skas_faultinfo(int pid, struct faultinfo * fi)
95 err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
97 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
98 "errno = %d\n", errno);
100 /* Special handling for i386, which has different structs */
101 if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo))
102 memset((char *)fi + sizeof(struct ptrace_faultinfo), 0,
103 sizeof(struct faultinfo) -
104 sizeof(struct ptrace_faultinfo));
107 wait_stub_done(pid, SIGSEGV, "get_skas_faultinfo");
109 /* faultinfo is prepared by the stub-segv-handler at start of
110 * the stub stack page. We just have to copy it.
112 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
116 static void handle_segv(int pid, union uml_pt_regs * regs)
118 get_skas_faultinfo(pid, ®s->skas.faultinfo);
119 segv(regs->skas.faultinfo, 0, 1, NULL);
122 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
123 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
127 /* Mark this as a syscall */
128 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
130 if (!local_using_sysemu)
132 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
135 panic("handle_trap - nullifying syscall failed errno = %d\n",
138 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
140 panic("handle_trap - continuing to end of syscall failed, "
141 "errno = %d\n", errno);
143 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
144 if((err < 0) || !WIFSTOPPED(status) ||
145 (WSTOPSIG(status) != SIGTRAP + 0x80))
146 panic("handle_trap - failed to wait at end of syscall, "
147 "errno = %d, status = %d\n", errno, status);
150 handle_syscall(regs);
153 extern int __syscall_stub_start;
155 static int userspace_tramp(void *stack)
160 ptrace(PTRACE_TRACEME, 0, 0, 0);
162 init_new_thread_signals();
163 err = set_interval(1);
165 panic("userspace_tramp - setting timer failed, errno = %d\n",
169 /* This has a pte, but it can't be mapped in with the usual
170 * tlb_flush mechanism because this is part of that mechanism
174 fd = phys_mapping(to_phys(&__syscall_stub_start), &offset);
175 addr = mmap64((void *) UML_CONFIG_STUB_CODE, page_size(),
176 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
177 if(addr == MAP_FAILED){
178 printk("mapping mmap stub failed, errno = %d\n",
184 fd = phys_mapping(to_phys(stack), &offset);
185 addr = mmap((void *) UML_CONFIG_STUB_DATA, page_size(),
186 PROT_READ | PROT_WRITE,
187 MAP_FIXED | MAP_SHARED, fd, offset);
188 if(addr == MAP_FAILED){
189 printk("mapping segfault stack failed, "
190 "errno = %d\n", errno);
195 if(!ptrace_faultinfo && (stack != NULL)){
198 unsigned long v = UML_CONFIG_STUB_CODE +
199 (unsigned long) stub_segv_handler -
200 (unsigned long) &__syscall_stub_start;
202 set_sigstack((void *) UML_CONFIG_STUB_DATA, page_size());
203 sigemptyset(&sa.sa_mask);
204 sigaddset(&sa.sa_mask, SIGIO);
205 sigaddset(&sa.sa_mask, SIGWINCH);
206 sigaddset(&sa.sa_mask, SIGALRM);
207 sigaddset(&sa.sa_mask, SIGVTALRM);
208 sigaddset(&sa.sa_mask, SIGUSR1);
209 sa.sa_flags = SA_ONSTACK;
210 sa.sa_handler = (void *) v;
211 sa.sa_restorer = NULL;
212 if(sigaction(SIGSEGV, &sa, NULL) < 0)
213 panic("userspace_tramp - setting SIGSEGV handler "
214 "failed - errno = %d\n", errno);
217 os_stop_process(os_getpid());
221 /* Each element set once, and only accessed by a single processor anyway */
224 int userspace_pid[NR_CPUS];
226 int start_userspace(unsigned long stub_stack)
230 int pid, status, n, flags;
232 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
233 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
234 if(stack == MAP_FAILED)
235 panic("start_userspace : mmap failed, errno = %d", errno);
236 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
238 flags = CLONE_FILES | SIGCHLD;
239 if(proc_mm) flags |= CLONE_VM;
240 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
242 panic("start_userspace : clone failed, errno = %d", errno);
245 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
247 panic("start_userspace : wait failed, errno = %d",
249 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
251 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
252 panic("start_userspace : expected SIGSTOP, got status = %d",
255 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
256 panic("start_userspace : PTRACE_OLDSETOPTIONS failed, errno=%d\n",
259 if(munmap(stack, PAGE_SIZE) < 0)
260 panic("start_userspace : munmap failed, errno = %d\n", errno);
265 void userspace(union uml_pt_regs *regs)
267 int err, status, op, pid = userspace_pid[0];
268 int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
271 restore_registers(pid, regs);
273 /* Now we set local_using_sysemu to be used for one loop */
274 local_using_sysemu = get_using_sysemu();
276 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
278 err = ptrace(op, pid, 0, 0);
280 panic("userspace - could not resume userspace process, "
281 "pid=%d, ptrace operation = %d, errno = %d\n",
284 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
286 panic("userspace - waitpid failed, errno = %d\n",
289 regs->skas.is_user = 1;
290 save_registers(pid, regs);
291 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
293 if(WIFSTOPPED(status)){
294 switch(WSTOPSIG(status)){
296 if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo)
297 user_signal(SIGSEGV, regs, pid);
298 else handle_segv(pid, regs);
301 handle_trap(pid, regs, local_using_sysemu);
304 relay_signal(SIGTRAP, regs);
312 user_signal(WSTOPSIG(status), regs, pid);
315 printk("userspace - child stopped with signal "
316 "%d\n", WSTOPSIG(status));
318 pid = userspace_pid[0];
321 /* Avoid -ERESTARTSYS handling in host */
322 if(PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
323 PT_SYSCALL_NR(regs->skas.regs) = -1;
328 int copy_context_skas0(unsigned long new_stack, int pid)
331 unsigned long regs[HOST_FRAME_SIZE];
332 unsigned long fp_regs[HOST_FP_SIZE];
333 unsigned long current_stack = current_stub_stack();
334 struct stub_data *data = (struct stub_data *) current_stack;
335 struct stub_data *child_data = (struct stub_data *) new_stack;
337 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
339 /* prepare offset and fd of child's stack as argument for parent's
340 * and child's mmap2 calls
342 *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset),
344 .timer = ((struct itimerval)
345 { { 0, 1000000 / hz() },
346 { 0, 1000000 / hz() }})});
347 get_safe_registers(regs, fp_regs);
349 /* Set parent's instruction pointer to start of clone-stub */
350 regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
351 (unsigned long) stub_clone_handler -
352 (unsigned long) &__syscall_stub_start;
353 regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + PAGE_SIZE -
355 #ifdef __SIGNAL_FRAMESIZE
356 regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
358 err = ptrace_setregs(pid, regs);
360 panic("copy_context_skas0 : PTRACE_SETREGS failed, "
361 "pid = %d, errno = %d\n", pid, -err);
363 err = ptrace_setfpregs(pid, fp_regs);
365 panic("copy_context_skas0 : PTRACE_SETFPREGS failed, "
366 "pid = %d, errno = %d\n", pid, -err);
368 /* set a well known return code for detection of child write failure */
369 child_data->err = 12345678;
371 /* Wait, until parent has finished its work: read child's pid from
372 * parent's stack, and check, if bad result.
374 wait_stub_done(pid, 0, "copy_context_skas0");
378 panic("copy_context_skas0 - stub-parent reports error %d\n",
381 /* Wait, until child has finished too: read child's result from
382 * child's stack and check it.
384 wait_stub_done(pid, -1, "copy_context_skas0");
385 if (child_data->err != UML_CONFIG_STUB_DATA)
386 panic("copy_context_skas0 - stub-child reports error %ld\n",
389 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
390 (void *)PTRACE_O_TRACESYSGOOD) < 0)
391 panic("copy_context_skas0 : PTRACE_OLDSETOPTIONS failed, "
392 "errno = %d\n", errno);
398 * This is used only, if stub pages are needed, while proc_mm is
399 * availabl. Opening /proc/mm creates a new mm_context, which lacks
400 * the stub-pages. Thus, we map them using /proc/mm-fd
402 void map_stub_pages(int fd, unsigned long code,
403 unsigned long data, unsigned long stack)
405 struct proc_mm_op mmop;
408 int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start),
411 mmop = ((struct proc_mm_op) { .op = MM_MMAP,
417 .flags = MAP_FIXED | MAP_PRIVATE,
419 .offset = code_offset
421 n = os_write_file(fd, &mmop, sizeof(mmop));
422 if(n != sizeof(mmop))
423 panic("map_stub_pages : /proc/mm map for code failed, "
428 int map_fd = phys_mapping(to_phys((void *)stack), &map_offset);
429 mmop = ((struct proc_mm_op)
435 .prot = PROT_READ | PROT_WRITE,
436 .flags = MAP_FIXED | MAP_SHARED,
440 n = os_write_file(fd, &mmop, sizeof(mmop));
441 if(n != sizeof(mmop))
442 panic("map_stub_pages : /proc/mm map for data failed, "
447 void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
449 (*buf)[0].JB_IP = (unsigned long) handler;
450 (*buf)[0].JB_SP = (unsigned long) stack +
451 (PAGE_SIZE << UML_CONFIG_KERNEL_STACK_ORDER) - sizeof(void *);
454 #define INIT_JMP_NEW_THREAD 0
455 #define INIT_JMP_CALLBACK 1
456 #define INIT_JMP_HALT 2
457 #define INIT_JMP_REBOOT 3
459 void switch_threads(jmp_buf *me, jmp_buf *you)
461 if(UML_SETJMP(me) == 0)
465 static jmp_buf initial_jmpbuf;
467 /* XXX Make these percpu */
468 static void (*cb_proc)(void *arg);
470 static jmp_buf *cb_back;
472 int start_idle_thread(void *stack, jmp_buf *switch_buf)
476 set_handler(SIGWINCH, (__sighandler_t) sig_handler,
477 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
480 n = UML_SETJMP(&initial_jmpbuf);
482 case INIT_JMP_NEW_THREAD:
483 (*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler;
484 (*switch_buf)[0].JB_SP = (unsigned long) stack +
485 (PAGE_SIZE << UML_CONFIG_KERNEL_STACK_ORDER) -
488 case INIT_JMP_CALLBACK:
490 UML_LONGJMP(cb_back, 1);
495 case INIT_JMP_REBOOT:
499 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n);
501 UML_LONGJMP(switch_buf, 1);
504 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
513 if(UML_SETJMP(&here) == 0)
514 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
525 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
528 void reboot_skas(void)
531 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
534 void switch_mm_skas(struct mm_id *mm_idp)
538 #warning need cpu pid in switch_mm_skas
540 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
543 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, "
544 "errno = %d\n", errno);
546 else userspace_pid[0] = mm_idp->u.pid;