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 "kern_util.h"
23 #include "stub-data.h"
25 #include "sysdep/sigcontext.h"
26 #include "sysdep/stub.h"
29 #include "skas_ptrace.h"
30 #include "chan_user.h"
31 #include "registers.h"
33 #include "uml-config.h"
36 #include "kern_constants.h"
37 #include "as-layout.h"
39 int is_skas_winch(int pid, int fd, void *data)
41 if(pid != os_getpgrp())
44 register_winch_irq(-1, fd, -1, data);
48 static int ptrace_dump_regs(int pid)
50 unsigned long regs[MAX_REG_NR];
53 if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
56 printk("Stub registers -\n");
57 for(i = 0; i < ARRAY_SIZE(regs); i++)
58 printk("\t%d - %lx\n", i, regs[i]);
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.
68 #define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH))
70 /* Signals that the stub will finish with - anything else is an error */
71 #define STUB_DONE_MASK ((1 << SIGUSR1) | (1 << SIGTRAP))
73 void wait_stub_done(int pid)
78 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
79 if((n < 0) || !WIFSTOPPED(status))
82 if(((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
85 err = ptrace(PTRACE_CONT, pid, 0, 0);
87 panic("wait_stub_done : continue failed, errno = %d\n",
91 if(((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
95 err = ptrace_dump_regs(pid);
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);
102 extern unsigned long current_stub_stack(void);
104 void get_skas_faultinfo(int pid, struct faultinfo * fi)
108 if(ptrace_faultinfo){
109 err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
111 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
112 "errno = %d\n", errno);
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));
121 err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
123 panic("Failed to continue stub, pid = %d, errno = %d\n",
127 /* faultinfo is prepared by the stub-segv-handler at start of
128 * the stub stack page. We just have to copy it.
130 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
134 static void handle_segv(int pid, union uml_pt_regs * regs)
136 get_skas_faultinfo(pid, ®s->skas.faultinfo);
137 segv(regs->skas.faultinfo, 0, 1, NULL);
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)
145 /* Mark this as a syscall */
146 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
148 if (!local_using_sysemu)
150 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
153 panic("handle_trap - nullifying syscall failed errno = %d\n",
156 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
158 panic("handle_trap - continuing to end of syscall failed, "
159 "errno = %d\n", errno);
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);
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);
173 handle_syscall(regs);
176 extern int __syscall_stub_start;
178 static int userspace_tramp(void *stack)
183 ptrace(PTRACE_TRACEME, 0, 0, 0);
185 init_new_thread_signals();
186 err = set_interval(1);
188 panic("userspace_tramp - setting timer failed, errno = %d\n",
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
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",
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);
218 if(!ptrace_faultinfo && (stack != NULL)){
221 unsigned long v = UML_CONFIG_STUB_CODE +
222 (unsigned long) stub_segv_handler -
223 (unsigned long) &__syscall_stub_start;
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);
240 os_stop_process(os_getpid());
244 /* Each element set once, and only accessed by a single processor anyway */
247 int userspace_pid[NR_CPUS];
249 int start_userspace(unsigned long stub_stack)
253 int pid, status, n, flags;
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 *);
261 flags = CLONE_FILES | SIGCHLD;
262 if(proc_mm) flags |= CLONE_VM;
263 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
265 panic("start_userspace : clone failed, errno = %d", errno);
268 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
270 panic("start_userspace : wait failed, errno = %d",
272 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
274 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
275 panic("start_userspace : expected SIGSTOP, got status = %d",
278 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
279 panic("start_userspace : PTRACE_OLDSETOPTIONS failed, errno=%d\n",
282 if(munmap(stack, PAGE_SIZE) < 0)
283 panic("start_userspace : munmap failed, errno = %d\n", errno);
288 void userspace(union uml_pt_regs *regs)
290 int err, status, op, pid = userspace_pid[0];
291 int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
294 restore_registers(pid, regs);
296 /* Now we set local_using_sysemu to be used for one loop */
297 local_using_sysemu = get_using_sysemu();
299 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
301 err = ptrace(op, pid, 0, 0);
303 panic("userspace - could not resume userspace process, "
304 "pid=%d, ptrace operation = %d, errno = %d\n",
307 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
309 panic("userspace - waitpid failed, errno = %d\n",
312 regs->skas.is_user = 1;
313 save_registers(pid, regs);
314 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
316 if(WIFSTOPPED(status)){
317 int sig = WSTOPSIG(status);
320 if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo){
321 get_skas_faultinfo(pid, ®s->skas.faultinfo);
322 (*sig_info[SIGSEGV])(SIGSEGV, regs);
324 else handle_segv(pid, regs);
327 handle_trap(pid, regs, local_using_sysemu);
330 relay_signal(SIGTRAP, regs);
339 (*sig_info[sig])(sig, regs);
343 printk("userspace - child stopped with signal "
346 pid = userspace_pid[0];
349 /* Avoid -ERESTARTSYS handling in host */
350 if(PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
351 PT_SYSCALL_NR(regs->skas.regs) = -1;
356 static unsigned long thread_regs[MAX_REG_NR];
357 static unsigned long thread_fp_regs[HOST_FP_SIZE];
359 static int __init init_thread_regs(void)
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 -
368 #ifdef __SIGNAL_FRAMESIZE
369 thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
374 __initcall(init_thread_regs);
376 int copy_context_skas0(unsigned long new_stack, int pid)
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;
383 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
385 /* prepare offset and fd of child's stack as argument for parent's
386 * and child's mmap2 calls
388 *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset),
390 .timer = ((struct itimerval)
391 { { 0, 1000000 / hz() },
392 { 0, 1000000 / hz() }})});
393 err = ptrace_setregs(pid, thread_regs);
395 panic("copy_context_skas0 : PTRACE_SETREGS failed, "
396 "pid = %d, errno = %d\n", pid, -err);
398 err = ptrace_setfpregs(pid, thread_fp_regs);
400 panic("copy_context_skas0 : PTRACE_SETFPREGS failed, "
401 "pid = %d, errno = %d\n", pid, -err);
403 /* set a well known return code for detection of child write failure */
404 child_data->err = 12345678;
406 /* Wait, until parent has finished its work: read child's pid from
407 * parent's stack, and check, if bad result.
409 err = ptrace(PTRACE_CONT, pid, 0, 0);
411 panic("Failed to continue new process, pid = %d, "
412 "errno = %d\n", pid, errno);
417 panic("copy_context_skas0 - stub-parent reports error %d\n",
420 /* Wait, until child has finished too: read child's result from
421 * child's stack and check it.
424 if (child_data->err != UML_CONFIG_STUB_DATA)
425 panic("copy_context_skas0 - stub-child reports error %ld\n",
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);
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
441 void map_stub_pages(int fd, unsigned long code,
442 unsigned long data, unsigned long stack)
444 struct proc_mm_op mmop;
447 int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start),
450 mmop = ((struct proc_mm_op) { .op = MM_MMAP,
456 .flags = MAP_FIXED | MAP_PRIVATE,
458 .offset = code_offset
460 CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
461 if(n != sizeof(mmop)){
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, "
471 int map_fd = phys_mapping(to_phys((void *)stack), &map_offset);
472 mmop = ((struct proc_mm_op)
478 .prot = PROT_READ | PROT_WRITE,
479 .flags = MAP_FIXED | MAP_SHARED,
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);
490 void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
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 *);
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
502 void switch_threads(jmp_buf *me, jmp_buf *you)
504 if(UML_SETJMP(me) == 0)
508 static jmp_buf initial_jmpbuf;
510 /* XXX Make these percpu */
511 static void (*cb_proc)(void *arg);
513 static jmp_buf *cb_back;
515 int start_idle_thread(void *stack, jmp_buf *switch_buf)
519 set_handler(SIGWINCH, (__sighandler_t) sig_handler,
520 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
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.
531 n = setjmp(initial_jmpbuf);
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) -
539 case INIT_JMP_CALLBACK:
541 longjmp(*cb_back, 1);
546 case INIT_JMP_REBOOT:
550 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n);
552 longjmp(*switch_buf, 1);
555 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
564 if(UML_SETJMP(&here) == 0)
565 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
576 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
579 void reboot_skas(void)
582 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
585 void switch_mm_skas(struct mm_id *mm_idp)
589 /* FIXME: need cpu pid in switch_mm_skas */
591 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
594 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, "
595 "errno = %d\n", errno);
597 else userspace_pid[0] = mm_idp->u.pid;