1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4 * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
15 #include <asm/unistd.h>
16 #include <as-layout.h>
18 #include <kern_util.h>
21 #include <ptrace_user.h>
22 #include <registers.h>
24 #include <sysdep/stub.h>
25 #include <linux/threads.h>
27 int is_skas_winch(int pid, int fd, void *data)
29 return pid == getpgrp();
32 static const char *ptrace_reg_name(int idx)
34 #define R(n) case HOST_##n: return #n
57 #elif defined(__i386__)
80 static int ptrace_dump_regs(int pid)
82 unsigned long regs[MAX_REG_NR];
85 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
88 printk(UM_KERN_ERR "Stub registers -\n");
89 for (i = 0; i < ARRAY_SIZE(regs); i++) {
90 const char *regname = ptrace_reg_name(i);
92 printk(UM_KERN_ERR "\t%s\t(%2d): %lx\n", regname, i, regs[i]);
99 * Signals that are OK to receive in the stub - we'll just continue it.
100 * SIGWINCH will happen when UML is inside a detached screen.
102 #define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
104 /* Signals that the stub will finish with - anything else is an error */
105 #define STUB_DONE_MASK (1 << SIGTRAP)
107 void wait_stub_done(int pid)
112 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
113 if ((n < 0) || !WIFSTOPPED(status))
116 if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
119 err = ptrace(PTRACE_CONT, pid, 0, 0);
121 printk(UM_KERN_ERR "%s : continue failed, errno = %d\n",
127 if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
131 err = ptrace_dump_regs(pid);
133 printk(UM_KERN_ERR "Failed to get registers from stub, errno = %d\n",
135 printk(UM_KERN_ERR "%s : failed to wait for SIGTRAP, pid = %d, n = %d, errno = %d, status = 0x%x\n",
136 __func__, pid, n, errno, status);
140 extern unsigned long current_stub_stack(void);
142 static void get_skas_faultinfo(int pid, struct faultinfo *fi, unsigned long *aux_fp_regs)
146 err = get_fp_registers(pid, aux_fp_regs);
148 printk(UM_KERN_ERR "save_fp_registers returned %d\n",
152 err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
154 printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
155 "errno = %d\n", pid, errno);
161 * faultinfo is prepared by the stub_segv_handler at start of
162 * the stub stack page. We just have to copy it.
164 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
166 err = put_fp_registers(pid, aux_fp_regs);
168 printk(UM_KERN_ERR "put_fp_registers returned %d\n",
174 static void handle_segv(int pid, struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
176 get_skas_faultinfo(pid, ®s->faultinfo, aux_fp_regs);
177 segv(regs->faultinfo, 0, 1, NULL);
181 * To use the same value of using_sysemu as the caller, ask it that value
182 * (in local_using_sysemu
184 static void handle_trap(int pid, struct uml_pt_regs *regs,
185 int local_using_sysemu)
189 if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
192 if (!local_using_sysemu)
194 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
197 printk(UM_KERN_ERR "%s - nullifying syscall failed, errno = %d\n",
202 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
204 printk(UM_KERN_ERR "%s - continuing to end of syscall failed, errno = %d\n",
209 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
210 if ((err < 0) || !WIFSTOPPED(status) ||
211 (WSTOPSIG(status) != SIGTRAP + 0x80)) {
212 err = ptrace_dump_regs(pid);
214 printk(UM_KERN_ERR "Failed to get registers from process, errno = %d\n",
216 printk(UM_KERN_ERR "%s - failed to wait at end of syscall, errno = %d, status = %d\n",
217 __func__, errno, status);
222 handle_syscall(regs);
225 extern char __syscall_stub_start[];
228 * userspace_tramp() - userspace trampoline
229 * @stack: pointer to the new userspace stack page, can be NULL, if? FIXME:
231 * The userspace trampoline is used to setup a new userspace process in start_userspace() after it was clone()'ed.
232 * This function will run on a temporary stack page.
233 * It ptrace()'es itself, then
234 * Two pages are mapped into the userspace address space:
235 * - STUB_CODE (with EXEC), which contains the skas stub code
236 * - STUB_DATA (with R/W), which contains a data page that is used to transfer certain data between the UML userspace process and the UML kernel.
237 * Also for the userspace process a SIGSEGV handler is installed to catch pagefaults in the userspace process.
238 * And last the process stops itself to give control to the UML kernel for this userspace process.
240 * Return: Always zero, otherwise the current userspace process is ended with non null exit() call
242 static int userspace_tramp(void *stack)
246 unsigned long long offset;
248 ptrace(PTRACE_TRACEME, 0, 0, 0);
250 signal(SIGTERM, SIG_DFL);
251 signal(SIGWINCH, SIG_IGN);
253 fd = phys_mapping(uml_to_phys(__syscall_stub_start), &offset);
254 addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
255 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
256 if (addr == MAP_FAILED) {
257 printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, errno = %d\n",
263 fd = phys_mapping(uml_to_phys(stack), &offset);
264 addr = mmap((void *) STUB_DATA,
265 STUB_DATA_PAGES * UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
266 MAP_FIXED | MAP_SHARED, fd, offset);
267 if (addr == MAP_FAILED) {
268 printk(UM_KERN_ERR "mapping segfault stack at 0x%lx failed, errno = %d\n",
276 unsigned long v = STUB_CODE +
277 (unsigned long) stub_segv_handler -
278 (unsigned long) __syscall_stub_start;
280 set_sigstack((void *) STUB_DATA, STUB_DATA_PAGES * UM_KERN_PAGE_SIZE);
281 sigemptyset(&sa.sa_mask);
282 sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
283 sa.sa_sigaction = (void *) v;
284 sa.sa_restorer = NULL;
285 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
286 printk(UM_KERN_ERR "%s - setting SIGSEGV handler failed - errno = %d\n",
292 kill(os_getpid(), SIGSTOP);
296 int userspace_pid[NR_CPUS];
297 int kill_userspace_mm[NR_CPUS];
300 * start_userspace() - prepare a new userspace process
301 * @stub_stack: pointer to the stub stack. Can be NULL, if? FIXME:
303 * Setups a new temporary stack page that is used while userspace_tramp() runs
304 * Clones the kernel process into a new userspace process, with FDs only.
306 * Return: When positive: the process id of the new userspace process,
307 * when negative: an error number.
308 * FIXME: can PIDs become negative?!
310 int start_userspace(unsigned long stub_stack)
314 int pid, status, n, flags, err;
316 /* setup a temporary stack page */
317 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
318 PROT_READ | PROT_WRITE | PROT_EXEC,
319 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
320 if (stack == MAP_FAILED) {
322 printk(UM_KERN_ERR "%s : mmap failed, errno = %d\n",
327 /* set stack pointer to the end of the stack page, so it can grow downwards */
328 sp = (unsigned long)stack + UM_KERN_PAGE_SIZE;
330 flags = CLONE_FILES | SIGCHLD;
332 /* clone into new userspace process */
333 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
336 printk(UM_KERN_ERR "%s : clone failed, errno = %d\n",
342 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
345 printk(UM_KERN_ERR "%s : wait failed, errno = %d\n",
349 } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
351 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
353 printk(UM_KERN_ERR "%s : expected SIGSTOP, got status = %d\n",
358 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
359 (void *) PTRACE_O_TRACESYSGOOD) < 0) {
361 printk(UM_KERN_ERR "%s : PTRACE_OLDSETOPTIONS failed, errno = %d\n",
366 if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
368 printk(UM_KERN_ERR "%s : munmap failed, errno = %d\n",
376 os_kill_ptraced_process(pid, 1);
380 void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
382 int err, status, op, pid = userspace_pid[0];
383 /* To prevent races if using_sysemu changes under us.*/
384 int local_using_sysemu;
387 /* Handle any immediate reschedules or signals */
391 if (kill_userspace_mm[0])
395 * This can legitimately fail if the process loads a
396 * bogus value into a segment register. It will
397 * segfault and PTRACE_GETREGS will read that value
398 * out of the process. However, PTRACE_SETREGS will
399 * fail. In this case, there is nothing to do but
400 * just kill the process.
402 if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) {
403 printk(UM_KERN_ERR "%s - ptrace set regs failed, errno = %d\n",
408 if (put_fp_registers(pid, regs->fp)) {
409 printk(UM_KERN_ERR "%s - ptrace set fp regs failed, errno = %d\n",
414 /* Now we set local_using_sysemu to be used for one loop */
415 local_using_sysemu = get_using_sysemu();
417 op = SELECT_PTRACE_OPERATION(local_using_sysemu,
418 singlestepping(NULL));
420 if (ptrace(op, pid, 0, 0)) {
421 printk(UM_KERN_ERR "%s - ptrace continue failed, op = %d, errno = %d\n",
422 __func__, op, errno);
426 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
428 printk(UM_KERN_ERR "%s - wait failed, errno = %d\n",
434 if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
435 printk(UM_KERN_ERR "%s - PTRACE_GETREGS failed, errno = %d\n",
440 if (get_fp_registers(pid, regs->fp)) {
441 printk(UM_KERN_ERR "%s - get_fp_registers failed, errno = %d\n",
446 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
448 if (WIFSTOPPED(status)) {
449 int sig = WSTOPSIG(status);
451 /* These signal handlers need the si argument.
452 * The SIGIO and SIGALARM handlers which constitute the
453 * majority of invocations, do not use it.
462 ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
468 if (PTRACE_FULL_FAULTINFO) {
469 get_skas_faultinfo(pid,
470 ®s->faultinfo, aux_fp_regs);
471 (*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
474 else handle_segv(pid, regs, aux_fp_regs);
477 handle_trap(pid, regs, local_using_sysemu);
480 relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
489 block_signals_trace();
490 (*sig_info[sig])(sig, (struct siginfo *)&si, regs);
491 unblock_signals_trace();
494 printk(UM_KERN_ERR "%s - child stopped with signal %d\n",
498 pid = userspace_pid[0];
501 /* Avoid -ERESTARTSYS handling in host */
502 if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
503 PT_SYSCALL_NR(regs->gp) = -1;
508 static unsigned long thread_regs[MAX_REG_NR];
509 static unsigned long thread_fp_regs[FP_SIZE];
511 static int __init init_thread_regs(void)
513 get_safe_registers(thread_regs, thread_fp_regs);
514 /* Set parent's instruction pointer to start of clone-stub */
515 thread_regs[REGS_IP_INDEX] = STUB_CODE +
516 (unsigned long) stub_clone_handler -
517 (unsigned long) __syscall_stub_start;
518 thread_regs[REGS_SP_INDEX] = STUB_DATA + STUB_DATA_PAGES * UM_KERN_PAGE_SIZE -
520 #ifdef __SIGNAL_FRAMESIZE
521 thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
526 __initcall(init_thread_regs);
528 int copy_context_skas0(unsigned long new_stack, int pid)
531 unsigned long current_stack = current_stub_stack();
532 struct stub_data *data = (struct stub_data *) current_stack;
533 struct stub_data *child_data = (struct stub_data *) new_stack;
534 unsigned long long new_offset;
535 int new_fd = phys_mapping(uml_to_phys((void *)new_stack), &new_offset);
538 * prepare offset and fd of child's stack as argument for parent's
539 * and child's mmap2 calls
541 *data = ((struct stub_data) {
542 .offset = MMAP_OFFSET(new_offset),
544 .parent_err = -ESRCH,
548 *child_data = ((struct stub_data) {
552 err = ptrace_setregs(pid, thread_regs);
555 printk(UM_KERN_ERR "%s : PTRACE_SETREGS failed, pid = %d, errno = %d\n",
556 __func__, pid, -err);
560 err = put_fp_registers(pid, thread_fp_regs);
562 printk(UM_KERN_ERR "%s : put_fp_registers failed, pid = %d, err = %d\n",
568 * Wait, until parent has finished its work: read child's pid from
569 * parent's stack, and check, if bad result.
571 err = ptrace(PTRACE_CONT, pid, 0, 0);
574 printk(UM_KERN_ERR "Failed to continue new process, pid = %d, errno = %d\n",
581 pid = data->parent_err;
583 printk(UM_KERN_ERR "%s - stub-parent reports error %d\n",
589 * Wait, until child has finished too: read child's result from
590 * child's stack and check it.
593 if (child_data->child_err != STUB_DATA) {
594 printk(UM_KERN_ERR "%s - stub-child %d reports error %ld\n",
595 __func__, pid, data->child_err);
596 err = data->child_err;
600 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
601 (void *)PTRACE_O_TRACESYSGOOD) < 0) {
603 printk(UM_KERN_ERR "%s : PTRACE_OLDSETOPTIONS failed, errno = %d\n",
611 os_kill_ptraced_process(pid, 1);
615 void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
617 (*buf)[0].JB_IP = (unsigned long) handler;
618 (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
622 #define INIT_JMP_NEW_THREAD 0
623 #define INIT_JMP_CALLBACK 1
624 #define INIT_JMP_HALT 2
625 #define INIT_JMP_REBOOT 3
627 void switch_threads(jmp_buf *me, jmp_buf *you)
629 if (UML_SETJMP(me) == 0)
633 static jmp_buf initial_jmpbuf;
635 /* XXX Make these percpu */
636 static void (*cb_proc)(void *arg);
638 static jmp_buf *cb_back;
640 int start_idle_thread(void *stack, jmp_buf *switch_buf)
644 set_handler(SIGWINCH);
647 * Can't use UML_SETJMP or UML_LONGJMP here because they save
648 * and restore signals, with the possible side-effect of
649 * trying to handle any signals which came when they were
650 * blocked, which can't be done on this stack.
651 * Signals must be blocked when jumping back here and restored
652 * after returning to the jumper.
654 n = setjmp(initial_jmpbuf);
656 case INIT_JMP_NEW_THREAD:
657 (*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
658 (*switch_buf)[0].JB_SP = (unsigned long) stack +
659 UM_THREAD_SIZE - sizeof(void *);
661 case INIT_JMP_CALLBACK:
663 longjmp(*cb_back, 1);
668 case INIT_JMP_REBOOT:
672 printk(UM_KERN_ERR "Bad sigsetjmp return in %s - %d\n",
676 longjmp(*switch_buf, 1);
679 printk(UM_KERN_ERR "impossible long jump!");
684 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
692 block_signals_trace();
693 if (UML_SETJMP(&here) == 0)
694 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
695 unblock_signals_trace();
704 block_signals_trace();
705 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
708 static bool noreboot;
710 static int __init noreboot_cmd_param(char *str, int *add)
716 __uml_setup("noreboot", noreboot_cmd_param,
718 " Rather than rebooting, exit always, akin to QEMU's -no-reboot option.\n"
719 " This is useful if you're using CONFIG_PANIC_TIMEOUT in order to catch\n"
722 void reboot_skas(void)
724 block_signals_trace();
725 UML_LONGJMP(&initial_jmpbuf, noreboot ? INIT_JMP_HALT : INIT_JMP_REBOOT);
728 void __switch_mm(struct mm_id *mm_idp)
730 userspace_pid[0] = mm_idp->u.pid;
731 kill_userspace_mm[0] = mm_idp->kill;