2 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
3 * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
14 #include <asm/unistd.h>
15 #include <as-layout.h>
17 #include <kern_util.h>
20 #include <ptrace_user.h>
21 #include <registers.h>
23 #include <sysdep/stub.h>
25 int is_skas_winch(int pid, int fd, void *data)
27 return pid == getpgrp();
30 static int ptrace_dump_regs(int pid)
32 unsigned long regs[MAX_REG_NR];
35 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
38 printk(UM_KERN_ERR "Stub registers -\n");
39 for (i = 0; i < ARRAY_SIZE(regs); i++)
40 printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]);
46 * Signals that are OK to receive in the stub - we'll just continue it.
47 * SIGWINCH will happen when UML is inside a detached screen.
49 #define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
51 /* Signals that the stub will finish with - anything else is an error */
52 #define STUB_DONE_MASK (1 << SIGTRAP)
54 void wait_stub_done(int pid)
59 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
60 if ((n < 0) || !WIFSTOPPED(status))
63 if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
66 err = ptrace(PTRACE_CONT, pid, 0, 0);
68 printk(UM_KERN_ERR "wait_stub_done : continue failed, "
69 "errno = %d\n", errno);
74 if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
78 err = ptrace_dump_regs(pid);
80 printk(UM_KERN_ERR "Failed to get registers from stub, "
81 "errno = %d\n", -err);
82 printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
83 "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
88 extern unsigned long current_stub_stack(void);
90 static void get_skas_faultinfo(int pid, struct faultinfo *fi, unsigned long *aux_fp_regs)
94 err = get_fp_registers(pid, aux_fp_regs);
96 printk(UM_KERN_ERR "save_fp_registers returned %d\n",
100 err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
102 printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
103 "errno = %d\n", pid, errno);
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));
114 err = put_fp_registers(pid, aux_fp_regs);
116 printk(UM_KERN_ERR "put_fp_registers returned %d\n",
122 static void handle_segv(int pid, struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
124 get_skas_faultinfo(pid, ®s->faultinfo, aux_fp_regs);
125 segv(regs->faultinfo, 0, 1, NULL);
129 * To use the same value of using_sysemu as the caller, ask it that value
130 * (in local_using_sysemu
132 static void handle_trap(int pid, struct uml_pt_regs *regs,
133 int local_using_sysemu)
137 if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
140 if (!local_using_sysemu)
142 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
145 printk(UM_KERN_ERR "handle_trap - nullifying syscall "
146 "failed, errno = %d\n", errno);
150 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
152 printk(UM_KERN_ERR "handle_trap - continuing to end of "
153 "syscall failed, errno = %d\n", errno);
157 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
158 if ((err < 0) || !WIFSTOPPED(status) ||
159 (WSTOPSIG(status) != SIGTRAP + 0x80)) {
160 err = ptrace_dump_regs(pid);
162 printk(UM_KERN_ERR "Failed to get registers "
163 "from process, errno = %d\n", -err);
164 printk(UM_KERN_ERR "handle_trap - failed to wait at "
165 "end of syscall, errno = %d, status = %d\n",
171 handle_syscall(regs);
174 extern char __syscall_stub_start[];
176 static int userspace_tramp(void *stack)
180 unsigned long long offset;
182 ptrace(PTRACE_TRACEME, 0, 0, 0);
184 signal(SIGTERM, SIG_DFL);
185 signal(SIGWINCH, SIG_IGN);
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
191 fd = phys_mapping(to_phys(__syscall_stub_start), &offset);
192 addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
193 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
194 if (addr == MAP_FAILED) {
195 printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
196 "errno = %d\n", STUB_CODE, errno);
201 fd = phys_mapping(to_phys(stack), &offset);
202 addr = mmap((void *) STUB_DATA,
203 UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
204 MAP_FIXED | MAP_SHARED, fd, offset);
205 if (addr == MAP_FAILED) {
206 printk(UM_KERN_ERR "mapping segfault stack "
207 "at 0x%lx failed, errno = %d\n",
215 unsigned long v = STUB_CODE +
216 (unsigned long) stub_segv_handler -
217 (unsigned long) __syscall_stub_start;
219 set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
220 sigemptyset(&sa.sa_mask);
221 sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
222 sa.sa_sigaction = (void *) v;
223 sa.sa_restorer = NULL;
224 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
225 printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
226 "handler failed - errno = %d\n", errno);
231 kill(os_getpid(), SIGSTOP);
235 /* Each element set once, and only accessed by a single processor anyway */
238 int userspace_pid[NR_CPUS];
240 int start_userspace(unsigned long stub_stack)
244 int pid, status, n, flags, err;
246 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
247 PROT_READ | PROT_WRITE | PROT_EXEC,
248 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
249 if (stack == MAP_FAILED) {
251 printk(UM_KERN_ERR "start_userspace : mmap failed, "
252 "errno = %d\n", errno);
256 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
258 flags = CLONE_FILES | SIGCHLD;
260 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
263 printk(UM_KERN_ERR "start_userspace : clone failed, "
264 "errno = %d\n", errno);
269 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
272 printk(UM_KERN_ERR "start_userspace : wait failed, "
273 "errno = %d\n", errno);
276 } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
278 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
280 printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
281 "status = %d\n", status);
285 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
286 (void *) PTRACE_O_TRACESYSGOOD) < 0) {
288 printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
289 "failed, errno = %d\n", errno);
293 if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
295 printk(UM_KERN_ERR "start_userspace : munmap failed, "
296 "errno = %d\n", errno);
303 os_kill_ptraced_process(pid, 1);
307 void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
309 int err, status, op, pid = userspace_pid[0];
310 /* To prevent races if using_sysemu changes under us.*/
311 int local_using_sysemu;
314 /* Handle any immediate reschedules or signals */
320 * This can legitimately fail if the process loads a
321 * bogus value into a segment register. It will
322 * segfault and PTRACE_GETREGS will read that value
323 * out of the process. However, PTRACE_SETREGS will
324 * fail. In this case, there is nothing to do but
325 * just kill the process.
327 if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp))
330 if (put_fp_registers(pid, regs->fp))
333 /* Now we set local_using_sysemu to be used for one loop */
334 local_using_sysemu = get_using_sysemu();
336 op = SELECT_PTRACE_OPERATION(local_using_sysemu,
337 singlestepping(NULL));
339 if (ptrace(op, pid, 0, 0)) {
340 printk(UM_KERN_ERR "userspace - ptrace continue "
341 "failed, op = %d, errno = %d\n", op, errno);
345 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
347 printk(UM_KERN_ERR "userspace - wait failed, "
348 "errno = %d\n", errno);
353 if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
354 printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
355 "errno = %d\n", errno);
359 if (get_fp_registers(pid, regs->fp)) {
360 printk(UM_KERN_ERR "userspace - get_fp_registers failed, "
361 "errno = %d\n", errno);
365 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
367 if (WIFSTOPPED(status)) {
368 int sig = WSTOPSIG(status);
370 ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
374 if (PTRACE_FULL_FAULTINFO) {
375 get_skas_faultinfo(pid,
376 ®s->faultinfo, aux_fp_regs);
377 (*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
380 else handle_segv(pid, regs, aux_fp_regs);
383 handle_trap(pid, regs, local_using_sysemu);
386 relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
396 (*sig_info[sig])(sig, (struct siginfo *)&si, regs);
400 printk(UM_KERN_ERR "userspace - child stopped "
401 "with signal %d\n", sig);
404 pid = userspace_pid[0];
407 /* Avoid -ERESTARTSYS handling in host */
408 if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
409 PT_SYSCALL_NR(regs->gp) = -1;
414 static unsigned long thread_regs[MAX_REG_NR];
415 static unsigned long thread_fp_regs[FP_SIZE];
417 static int __init init_thread_regs(void)
419 get_safe_registers(thread_regs, thread_fp_regs);
420 /* Set parent's instruction pointer to start of clone-stub */
421 thread_regs[REGS_IP_INDEX] = STUB_CODE +
422 (unsigned long) stub_clone_handler -
423 (unsigned long) __syscall_stub_start;
424 thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
426 #ifdef __SIGNAL_FRAMESIZE
427 thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
432 __initcall(init_thread_regs);
434 int copy_context_skas0(unsigned long new_stack, int pid)
437 unsigned long current_stack = current_stub_stack();
438 struct stub_data *data = (struct stub_data *) current_stack;
439 struct stub_data *child_data = (struct stub_data *) new_stack;
440 unsigned long long new_offset;
441 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
444 * prepare offset and fd of child's stack as argument for parent's
445 * and child's mmap2 calls
447 *data = ((struct stub_data) {
448 .offset = MMAP_OFFSET(new_offset),
452 err = ptrace_setregs(pid, thread_regs);
455 printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
456 "failed, pid = %d, errno = %d\n", pid, -err);
460 err = put_fp_registers(pid, thread_fp_regs);
462 printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
463 "failed, pid = %d, err = %d\n", pid, err);
467 /* set a well known return code for detection of child write failure */
468 child_data->err = 12345678;
471 * Wait, until parent has finished its work: read child's pid from
472 * parent's stack, and check, if bad result.
474 err = ptrace(PTRACE_CONT, pid, 0, 0);
477 printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
478 "errno = %d\n", pid, errno);
486 printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
492 * Wait, until child has finished too: read child's result from
493 * child's stack and check it.
496 if (child_data->err != STUB_DATA) {
497 printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports "
498 "error %ld\n", child_data->err);
499 err = child_data->err;
503 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
504 (void *)PTRACE_O_TRACESYSGOOD) < 0) {
506 printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
507 "failed, errno = %d\n", errno);
514 os_kill_ptraced_process(pid, 1);
518 void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
520 (*buf)[0].JB_IP = (unsigned long) handler;
521 (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
525 #define INIT_JMP_NEW_THREAD 0
526 #define INIT_JMP_CALLBACK 1
527 #define INIT_JMP_HALT 2
528 #define INIT_JMP_REBOOT 3
530 void switch_threads(jmp_buf *me, jmp_buf *you)
532 if (UML_SETJMP(me) == 0)
536 static jmp_buf initial_jmpbuf;
538 /* XXX Make these percpu */
539 static void (*cb_proc)(void *arg);
541 static jmp_buf *cb_back;
543 int start_idle_thread(void *stack, jmp_buf *switch_buf)
547 set_handler(SIGWINCH);
550 * Can't use UML_SETJMP or UML_LONGJMP here because they save
551 * and restore signals, with the possible side-effect of
552 * trying to handle any signals which came when they were
553 * blocked, which can't be done on this stack.
554 * Signals must be blocked when jumping back here and restored
555 * after returning to the jumper.
557 n = setjmp(initial_jmpbuf);
559 case INIT_JMP_NEW_THREAD:
560 (*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
561 (*switch_buf)[0].JB_SP = (unsigned long) stack +
562 UM_THREAD_SIZE - sizeof(void *);
564 case INIT_JMP_CALLBACK:
566 longjmp(*cb_back, 1);
571 case INIT_JMP_REBOOT:
575 printk(UM_KERN_ERR "Bad sigsetjmp return in "
576 "start_idle_thread - %d\n", n);
579 longjmp(*switch_buf, 1);
582 printk(UM_KERN_ERR "impossible long jump!");
587 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
596 if (UML_SETJMP(&here) == 0)
597 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
608 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
611 void reboot_skas(void)
614 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
617 void __switch_mm(struct mm_id *mm_idp)
619 userspace_pid[0] = mm_idp->u.pid;