Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 1 | /* |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 7 | #include <unistd.h> |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 8 | #include <sched.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <string.h> |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 11 | #include <sys/mman.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 12 | #include <sys/ptrace.h> |
| 13 | #include <sys/wait.h> |
| 14 | #include <asm/unistd.h> |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 15 | #include "as-layout.h" |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 16 | #include "chan_user.h" |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 17 | #include "kern_util.h" |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 18 | #include "mem.h" |
| 19 | #include "os.h" |
| 20 | #include "process.h" |
| 21 | #include "proc_mm.h" |
| 22 | #include "ptrace_user.h" |
| 23 | #include "registers.h" |
| 24 | #include "skas.h" |
| 25 | #include "skas_ptrace.h" |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 26 | #include "sysdep/stub.h" |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 27 | |
| 28 | int is_skas_winch(int pid, int fd, void *data) |
| 29 | { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 30 | if (pid != getpgrp()) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 31 | return 0; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 32 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 33 | register_winch_irq(-1, fd, -1, data, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 34 | return 1; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 37 | static int ptrace_dump_regs(int pid) |
| 38 | { |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 39 | unsigned long regs[MAX_REG_NR]; |
| 40 | int i; |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 41 | |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 42 | if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) |
| 43 | return -errno; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 44 | |
| 45 | printk(UM_KERN_ERR "Stub registers -\n"); |
| 46 | for (i = 0; i < ARRAY_SIZE(regs); i++) |
| 47 | printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]); |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 48 | |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 49 | return 0; |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Signals that are OK to receive in the stub - we'll just continue it. |
| 54 | * SIGWINCH will happen when UML is inside a detached screen. |
| 55 | */ |
Jeff Dike | 3d5ede6 | 2008-06-05 22:46:13 -0700 | [diff] [blame] | 56 | #define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH)) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 57 | |
| 58 | /* Signals that the stub will finish with - anything else is an error */ |
Jeff Dike | ee3d9bd | 2008-02-04 22:30:56 -0800 | [diff] [blame] | 59 | #define STUB_DONE_MASK (1 << SIGTRAP) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 60 | |
| 61 | void wait_stub_done(int pid) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 62 | { |
| 63 | int n, status, err; |
| 64 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 65 | while (1) { |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 66 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 67 | if ((n < 0) || !WIFSTOPPED(status)) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 68 | goto bad_wait; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 69 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 70 | if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 71 | break; |
| 72 | |
| 73 | err = ptrace(PTRACE_CONT, pid, 0, 0); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 74 | if (err) { |
| 75 | printk(UM_KERN_ERR "wait_stub_done : continue failed, " |
| 76 | "errno = %d\n", errno); |
| 77 | fatal_sigsegv(); |
| 78 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 79 | } |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 80 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 81 | if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 82 | return; |
| 83 | |
| 84 | bad_wait: |
| 85 | err = ptrace_dump_regs(pid); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 86 | if (err) |
| 87 | printk(UM_KERN_ERR "Failed to get registers from stub, " |
| 88 | "errno = %d\n", -err); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 89 | printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, " |
| 90 | "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno, |
| 91 | status); |
| 92 | fatal_sigsegv(); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | extern unsigned long current_stub_stack(void); |
| 96 | |
WANG Cong | 99764fa | 2008-07-23 21:28:49 -0700 | [diff] [blame] | 97 | static void get_skas_faultinfo(int pid, struct faultinfo *fi) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 98 | { |
| 99 | int err; |
| 100 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 101 | if (ptrace_faultinfo) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 102 | err = ptrace(PTRACE_FAULTINFO, pid, 0, fi); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 103 | if (err) { |
| 104 | printk(UM_KERN_ERR "get_skas_faultinfo - " |
| 105 | "PTRACE_FAULTINFO failed, errno = %d\n", errno); |
| 106 | fatal_sigsegv(); |
| 107 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 108 | |
| 109 | /* Special handling for i386, which has different structs */ |
| 110 | if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo)) |
| 111 | memset((char *)fi + sizeof(struct ptrace_faultinfo), 0, |
| 112 | sizeof(struct faultinfo) - |
| 113 | sizeof(struct ptrace_faultinfo)); |
| 114 | } |
| 115 | else { |
Jeff Dike | 2f56deb | 2008-02-23 15:23:49 -0800 | [diff] [blame] | 116 | unsigned long fpregs[FP_SIZE]; |
| 117 | |
| 118 | err = get_fp_registers(pid, fpregs); |
| 119 | if (err < 0) { |
| 120 | printk(UM_KERN_ERR "save_fp_registers returned %d\n", |
| 121 | err); |
| 122 | fatal_sigsegv(); |
| 123 | } |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 124 | err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 125 | if (err) { |
| 126 | printk(UM_KERN_ERR "Failed to continue stub, pid = %d, " |
| 127 | "errno = %d\n", pid, errno); |
| 128 | fatal_sigsegv(); |
| 129 | } |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 130 | wait_stub_done(pid); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 131 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 132 | /* |
| 133 | * faultinfo is prepared by the stub-segv-handler at start of |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 134 | * the stub stack page. We just have to copy it. |
| 135 | */ |
| 136 | memcpy(fi, (void *)current_stub_stack(), sizeof(*fi)); |
Jeff Dike | 2f56deb | 2008-02-23 15:23:49 -0800 | [diff] [blame] | 137 | |
| 138 | err = put_fp_registers(pid, fpregs); |
| 139 | if (err < 0) { |
| 140 | printk(UM_KERN_ERR "put_fp_registers returned %d\n", |
| 141 | err); |
| 142 | fatal_sigsegv(); |
| 143 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 147 | static void handle_segv(int pid, struct uml_pt_regs * regs) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 148 | { |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 149 | get_skas_faultinfo(pid, ®s->faultinfo); |
| 150 | segv(regs->faultinfo, 0, 1, NULL); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 153 | /* |
| 154 | * To use the same value of using_sysemu as the caller, ask it that value |
| 155 | * (in local_using_sysemu |
| 156 | */ |
| 157 | static void handle_trap(int pid, struct uml_pt_regs *regs, |
| 158 | int local_using_sysemu) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 159 | { |
| 160 | int err, status; |
| 161 | |
Jeff Dike | e06173b | 2008-02-04 22:31:12 -0800 | [diff] [blame] | 162 | if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END)) |
| 163 | fatal_sigsegv(); |
| 164 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 165 | /* Mark this as a syscall */ |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 166 | UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 167 | |
| 168 | if (!local_using_sysemu) |
| 169 | { |
| 170 | err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, |
| 171 | __NR_getpid); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 172 | if (err < 0) { |
| 173 | printk(UM_KERN_ERR "handle_trap - nullifying syscall " |
| 174 | "failed, errno = %d\n", errno); |
| 175 | fatal_sigsegv(); |
| 176 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 177 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 178 | err = ptrace(PTRACE_SYSCALL, pid, 0, 0); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 179 | if (err < 0) { |
| 180 | printk(UM_KERN_ERR "handle_trap - continuing to end of " |
| 181 | "syscall failed, errno = %d\n", errno); |
| 182 | fatal_sigsegv(); |
| 183 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 184 | |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 185 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 186 | if ((err < 0) || !WIFSTOPPED(status) || |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 187 | (WSTOPSIG(status) != SIGTRAP + 0x80)) { |
| 188 | err = ptrace_dump_regs(pid); |
| 189 | if (err) |
| 190 | printk(UM_KERN_ERR "Failed to get registers " |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 191 | "from process, errno = %d\n", -err); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 192 | printk(UM_KERN_ERR "handle_trap - failed to wait at " |
| 193 | "end of syscall, errno = %d, status = %d\n", |
| 194 | errno, status); |
| 195 | fatal_sigsegv(); |
| 196 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | handle_syscall(regs); |
| 200 | } |
| 201 | |
| 202 | extern int __syscall_stub_start; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 203 | |
| 204 | static int userspace_tramp(void *stack) |
| 205 | { |
| 206 | void *addr; |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 207 | int err; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 208 | |
| 209 | ptrace(PTRACE_TRACEME, 0, 0, 0); |
| 210 | |
Lepton Wu | a24864a | 2007-10-16 01:27:35 -0700 | [diff] [blame] | 211 | signal(SIGTERM, SIG_DFL); |
Jeff Dike | ee3d9bd | 2008-02-04 22:30:56 -0800 | [diff] [blame] | 212 | signal(SIGWINCH, SIG_IGN); |
Jeff Dike | a2f018b | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 213 | err = set_interval(); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 214 | if (err) { |
| 215 | printk(UM_KERN_ERR "userspace_tramp - setting timer failed, " |
| 216 | "errno = %d\n", err); |
| 217 | exit(1); |
| 218 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 219 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 220 | if (!proc_mm) { |
| 221 | /* |
| 222 | * This has a pte, but it can't be mapped in with the usual |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 223 | * tlb_flush mechanism because this is part of that mechanism |
| 224 | */ |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 225 | int fd; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 226 | unsigned long long offset; |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 227 | fd = phys_mapping(to_phys(&__syscall_stub_start), &offset); |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 228 | addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE, |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 229 | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 230 | if (addr == MAP_FAILED) { |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 231 | printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, " |
| 232 | "errno = %d\n", STUB_CODE, errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 233 | exit(1); |
| 234 | } |
| 235 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 236 | if (stack != NULL) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 237 | fd = phys_mapping(to_phys(stack), &offset); |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 238 | addr = mmap((void *) STUB_DATA, |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 239 | UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE, |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 240 | MAP_FIXED | MAP_SHARED, fd, offset); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 241 | if (addr == MAP_FAILED) { |
| 242 | printk(UM_KERN_ERR "mapping segfault stack " |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 243 | "at 0x%lx failed, errno = %d\n", |
| 244 | STUB_DATA, errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 245 | exit(1); |
| 246 | } |
| 247 | } |
| 248 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 249 | if (!ptrace_faultinfo && (stack != NULL)) { |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 250 | struct sigaction sa; |
| 251 | |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 252 | unsigned long v = STUB_CODE + |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 253 | (unsigned long) stub_segv_handler - |
| 254 | (unsigned long) &__syscall_stub_start; |
| 255 | |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 256 | set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE); |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 257 | sigemptyset(&sa.sa_mask); |
Al Viro | 9b25fcb | 2011-08-18 20:04:09 +0100 | [diff] [blame] | 258 | sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO; |
| 259 | sa.sa_sigaction = (void *) v; |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 260 | sa.sa_restorer = NULL; |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 261 | if (sigaction(SIGSEGV, &sa, NULL) < 0) { |
| 262 | printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV " |
| 263 | "handler failed - errno = %d\n", errno); |
| 264 | exit(1); |
| 265 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 268 | kill(os_getpid(), SIGSTOP); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 269 | return 0; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* Each element set once, and only accessed by a single processor anyway */ |
| 273 | #undef NR_CPUS |
| 274 | #define NR_CPUS 1 |
| 275 | int userspace_pid[NR_CPUS]; |
| 276 | |
| 277 | int start_userspace(unsigned long stub_stack) |
| 278 | { |
| 279 | void *stack; |
| 280 | unsigned long sp; |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 281 | int pid, status, n, flags, err; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 282 | |
Jeff Dike | c539ab7 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 283 | stack = mmap(NULL, UM_KERN_PAGE_SIZE, |
| 284 | PROT_READ | PROT_WRITE | PROT_EXEC, |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 285 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 286 | if (stack == MAP_FAILED) { |
| 287 | err = -errno; |
| 288 | printk(UM_KERN_ERR "start_userspace : mmap failed, " |
Jeff Dike | b549883 | 2008-02-04 22:31:21 -0800 | [diff] [blame] | 289 | "errno = %d\n", errno); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 290 | return err; |
| 291 | } |
| 292 | |
Jeff Dike | c539ab7 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 293 | sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 294 | |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 295 | flags = CLONE_FILES; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 296 | if (proc_mm) |
| 297 | flags |= CLONE_VM; |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 298 | else |
| 299 | flags |= SIGCHLD; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 300 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 301 | pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 302 | if (pid < 0) { |
| 303 | err = -errno; |
| 304 | printk(UM_KERN_ERR "start_userspace : clone failed, " |
Jeff Dike | b549883 | 2008-02-04 22:31:21 -0800 | [diff] [blame] | 305 | "errno = %d\n", errno); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 306 | return err; |
| 307 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 308 | |
| 309 | do { |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 310 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL)); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 311 | if (n < 0) { |
| 312 | err = -errno; |
| 313 | printk(UM_KERN_ERR "start_userspace : wait failed, " |
Jeff Dike | b549883 | 2008-02-04 22:31:21 -0800 | [diff] [blame] | 314 | "errno = %d\n", errno); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 315 | goto out_kill; |
| 316 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 317 | } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM)); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 318 | |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 319 | if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) { |
| 320 | err = -EINVAL; |
| 321 | printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got " |
Jeff Dike | b549883 | 2008-02-04 22:31:21 -0800 | [diff] [blame] | 322 | "status = %d\n", status); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 323 | goto out_kill; |
| 324 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 325 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 326 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 327 | (void *) PTRACE_O_TRACESYSGOOD) < 0) { |
| 328 | err = -errno; |
| 329 | printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS " |
| 330 | "failed, errno = %d\n", errno); |
| 331 | goto out_kill; |
| 332 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 333 | |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 334 | if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) { |
| 335 | err = -errno; |
| 336 | printk(UM_KERN_ERR "start_userspace : munmap failed, " |
| 337 | "errno = %d\n", errno); |
| 338 | goto out_kill; |
| 339 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 340 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 341 | return pid; |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 342 | |
| 343 | out_kill: |
| 344 | os_kill_ptraced_process(pid, 1); |
| 345 | return err; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 348 | void userspace(struct uml_pt_regs *regs) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 349 | { |
Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 350 | struct itimerval timer; |
| 351 | unsigned long long nsecs, now; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 352 | int err, status, op, pid = userspace_pid[0]; |
Jeff Dike | 2ea5bc5 | 2007-05-10 22:22:32 -0700 | [diff] [blame] | 353 | /* To prevent races if using_sysemu changes under us.*/ |
| 354 | int local_using_sysemu; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 355 | |
Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 356 | if (getitimer(ITIMER_VIRTUAL, &timer)) |
Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 357 | printk(UM_KERN_ERR "Failed to get itimer, errno = %d\n", errno); |
Jeff Dike | 1a80521 | 2007-10-16 01:27:28 -0700 | [diff] [blame] | 358 | nsecs = timer.it_value.tv_sec * UM_NSEC_PER_SEC + |
| 359 | timer.it_value.tv_usec * UM_NSEC_PER_USEC; |
Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 360 | nsecs += os_nsecs(); |
| 361 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 362 | while (1) { |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 363 | /* |
| 364 | * This can legitimately fail if the process loads a |
| 365 | * bogus value into a segment register. It will |
| 366 | * segfault and PTRACE_GETREGS will read that value |
| 367 | * out of the process. However, PTRACE_SETREGS will |
| 368 | * fail. In this case, there is nothing to do but |
| 369 | * just kill the process. |
| 370 | */ |
Jeff Dike | d25f2e1 | 2008-02-04 22:30:57 -0800 | [diff] [blame] | 371 | if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 372 | fatal_sigsegv(); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 373 | |
Ingo van Lil | fbfe9c8 | 2011-09-14 16:21:23 -0700 | [diff] [blame] | 374 | if (put_fp_registers(pid, regs->fp)) |
| 375 | fatal_sigsegv(); |
| 376 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 377 | /* Now we set local_using_sysemu to be used for one loop */ |
| 378 | local_using_sysemu = get_using_sysemu(); |
| 379 | |
Jeff Dike | 2ea5bc5 | 2007-05-10 22:22:32 -0700 | [diff] [blame] | 380 | op = SELECT_PTRACE_OPERATION(local_using_sysemu, |
| 381 | singlestepping(NULL)); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 382 | |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 383 | if (ptrace(op, pid, 0, 0)) { |
| 384 | printk(UM_KERN_ERR "userspace - ptrace continue " |
| 385 | "failed, op = %d, errno = %d\n", op, errno); |
| 386 | fatal_sigsegv(); |
| 387 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 388 | |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 389 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL)); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 390 | if (err < 0) { |
| 391 | printk(UM_KERN_ERR "userspace - wait failed, " |
| 392 | "errno = %d\n", errno); |
| 393 | fatal_sigsegv(); |
| 394 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 395 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 396 | regs->is_user = 1; |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 397 | if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) { |
| 398 | printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, " |
| 399 | "errno = %d\n", errno); |
| 400 | fatal_sigsegv(); |
| 401 | } |
Jeff Dike | d25f2e1 | 2008-02-04 22:30:57 -0800 | [diff] [blame] | 402 | |
Ingo van Lil | fbfe9c8 | 2011-09-14 16:21:23 -0700 | [diff] [blame] | 403 | if (get_fp_registers(pid, regs->fp)) { |
| 404 | printk(UM_KERN_ERR "userspace - get_fp_registers failed, " |
| 405 | "errno = %d\n", errno); |
| 406 | fatal_sigsegv(); |
| 407 | } |
| 408 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 409 | UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */ |
| 410 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 411 | if (WIFSTOPPED(status)) { |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 412 | int sig = WSTOPSIG(status); |
Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 413 | switch (sig) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 414 | case SIGSEGV: |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 415 | if (PTRACE_FULL_FAULTINFO || |
| 416 | !ptrace_faultinfo) { |
| 417 | get_skas_faultinfo(pid, |
| 418 | ®s->faultinfo); |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 419 | (*sig_info[SIGSEGV])(SIGSEGV, regs); |
| 420 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 421 | else handle_segv(pid, regs); |
| 422 | break; |
| 423 | case SIGTRAP + 0x80: |
| 424 | handle_trap(pid, regs, local_using_sysemu); |
| 425 | break; |
| 426 | case SIGTRAP: |
| 427 | relay_signal(SIGTRAP, regs); |
| 428 | break; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 429 | case SIGVTALRM: |
Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 430 | now = os_nsecs(); |
Jeff Dike | d25f2e1 | 2008-02-04 22:30:57 -0800 | [diff] [blame] | 431 | if (now < nsecs) |
Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 432 | break; |
| 433 | block_signals(); |
| 434 | (*sig_info[sig])(sig, regs); |
| 435 | unblock_signals(); |
Jeff Dike | 1a80521 | 2007-10-16 01:27:28 -0700 | [diff] [blame] | 436 | nsecs = timer.it_value.tv_sec * |
| 437 | UM_NSEC_PER_SEC + |
| 438 | timer.it_value.tv_usec * |
| 439 | UM_NSEC_PER_USEC; |
Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 440 | nsecs += os_nsecs(); |
| 441 | break; |
| 442 | case SIGIO: |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 443 | case SIGILL: |
| 444 | case SIGBUS: |
| 445 | case SIGFPE: |
| 446 | case SIGWINCH: |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 447 | block_signals(); |
| 448 | (*sig_info[sig])(sig, regs); |
| 449 | unblock_signals(); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 450 | break; |
| 451 | default: |
Jeff Dike | 96cee30 | 2008-05-12 14:01:48 -0700 | [diff] [blame] | 452 | printk(UM_KERN_ERR "userspace - child stopped " |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 453 | "with signal %d\n", sig); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 454 | fatal_sigsegv(); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 455 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 456 | pid = userspace_pid[0]; |
| 457 | interrupt_end(); |
| 458 | |
| 459 | /* Avoid -ERESTARTSYS handling in host */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 460 | if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET) |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 461 | PT_SYSCALL_NR(regs->gp) = -1; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 465 | |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 466 | static unsigned long thread_regs[MAX_REG_NR]; |
Ingo van Lil | fbfe9c8 | 2011-09-14 16:21:23 -0700 | [diff] [blame] | 467 | static unsigned long thread_fp_regs[FP_SIZE]; |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 468 | |
| 469 | static int __init init_thread_regs(void) |
| 470 | { |
Ingo van Lil | fbfe9c8 | 2011-09-14 16:21:23 -0700 | [diff] [blame] | 471 | get_safe_registers(thread_regs, thread_fp_regs); |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 472 | /* Set parent's instruction pointer to start of clone-stub */ |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 473 | thread_regs[REGS_IP_INDEX] = STUB_CODE + |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 474 | (unsigned long) stub_clone_handler - |
| 475 | (unsigned long) &__syscall_stub_start; |
Jeff Dike | 54ae36f | 2007-10-16 01:27:33 -0700 | [diff] [blame] | 476 | thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE - |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 477 | sizeof(void *); |
| 478 | #ifdef __SIGNAL_FRAMESIZE |
| 479 | thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE; |
| 480 | #endif |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | __initcall(init_thread_regs); |
| 485 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 486 | int copy_context_skas0(unsigned long new_stack, int pid) |
| 487 | { |
Jeff Dike | 1a80521 | 2007-10-16 01:27:28 -0700 | [diff] [blame] | 488 | struct timeval tv = { .tv_sec = 0, .tv_usec = UM_USEC_PER_SEC / UM_HZ }; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 489 | int err; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 490 | unsigned long current_stack = current_stub_stack(); |
| 491 | struct stub_data *data = (struct stub_data *) current_stack; |
| 492 | struct stub_data *child_data = (struct stub_data *) new_stack; |
Jeff Dike | 0a7675a | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 493 | unsigned long long new_offset; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 494 | int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset); |
| 495 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 496 | /* |
| 497 | * prepare offset and fd of child's stack as argument for parent's |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 498 | * and child's mmap2 calls |
| 499 | */ |
| 500 | *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset), |
| 501 | .fd = new_fd, |
| 502 | .timer = ((struct itimerval) |
Jeff Dike | d2753a6d | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 503 | { .it_value = tv, |
| 504 | .it_interval = tv }) }); |
| 505 | |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 506 | err = ptrace_setregs(pid, thread_regs); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 507 | if (err < 0) { |
| 508 | err = -errno; |
| 509 | printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS " |
| 510 | "failed, pid = %d, errno = %d\n", pid, -err); |
| 511 | return err; |
| 512 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 513 | |
Ingo van Lil | fbfe9c8 | 2011-09-14 16:21:23 -0700 | [diff] [blame] | 514 | err = put_fp_registers(pid, thread_fp_regs); |
| 515 | if (err < 0) { |
| 516 | printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers " |
| 517 | "failed, pid = %d, err = %d\n", pid, err); |
| 518 | return err; |
| 519 | } |
| 520 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 521 | /* set a well known return code for detection of child write failure */ |
| 522 | child_data->err = 12345678; |
| 523 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 524 | /* |
| 525 | * Wait, until parent has finished its work: read child's pid from |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 526 | * parent's stack, and check, if bad result. |
| 527 | */ |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 528 | err = ptrace(PTRACE_CONT, pid, 0, 0); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 529 | if (err) { |
| 530 | err = -errno; |
| 531 | printk(UM_KERN_ERR "Failed to continue new process, pid = %d, " |
| 532 | "errno = %d\n", pid, errno); |
| 533 | return err; |
| 534 | } |
| 535 | |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 536 | wait_stub_done(pid); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 537 | |
| 538 | pid = data->err; |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 539 | if (pid < 0) { |
| 540 | printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports " |
| 541 | "error %d\n", -pid); |
| 542 | return pid; |
| 543 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 544 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 545 | /* |
| 546 | * Wait, until child has finished too: read child's result from |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 547 | * child's stack and check it. |
| 548 | */ |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 549 | wait_stub_done(pid); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 550 | if (child_data->err != STUB_DATA) { |
| 551 | printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports " |
| 552 | "error %ld\n", child_data->err); |
| 553 | err = child_data->err; |
| 554 | goto out_kill; |
| 555 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 556 | |
| 557 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 558 | (void *)PTRACE_O_TRACESYSGOOD) < 0) { |
| 559 | err = -errno; |
| 560 | printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS " |
| 561 | "failed, errno = %d\n", errno); |
| 562 | goto out_kill; |
| 563 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 564 | |
| 565 | return pid; |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 566 | |
| 567 | out_kill: |
| 568 | os_kill_ptraced_process(pid, 1); |
| 569 | return err; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /* |
| 573 | * This is used only, if stub pages are needed, while proc_mm is |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 574 | * available. Opening /proc/mm creates a new mm_context, which lacks |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 575 | * the stub-pages. Thus, we map them using /proc/mm-fd |
| 576 | */ |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 577 | int map_stub_pages(int fd, unsigned long code, unsigned long data, |
| 578 | unsigned long stack) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 579 | { |
| 580 | struct proc_mm_op mmop; |
| 581 | int n; |
Jeff Dike | 0a7675a | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 582 | unsigned long long code_offset; |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 583 | int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start), |
| 584 | &code_offset); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 585 | |
| 586 | mmop = ((struct proc_mm_op) { .op = MM_MMAP, |
| 587 | .u = |
| 588 | { .mmap = |
| 589 | { .addr = code, |
Jeff Dike | c539ab7 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 590 | .len = UM_KERN_PAGE_SIZE, |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 591 | .prot = PROT_EXEC, |
| 592 | .flags = MAP_FIXED | MAP_PRIVATE, |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 593 | .fd = code_fd, |
| 594 | .offset = code_offset |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 595 | } } }); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 596 | CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop))); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 597 | if (n != sizeof(mmop)) { |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 598 | n = errno; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 599 | printk(UM_KERN_ERR "mmap args - addr = 0x%lx, fd = %d, " |
| 600 | "offset = %llx\n", code, code_fd, |
| 601 | (unsigned long long) code_offset); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 602 | printk(UM_KERN_ERR "map_stub_pages : /proc/mm map for code " |
| 603 | "failed, err = %d\n", n); |
| 604 | return -n; |
Jeff Dike | b4cf95c | 2007-03-06 01:42:19 -0800 | [diff] [blame] | 605 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 606 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 607 | if (stack) { |
Jeff Dike | 0a7675a | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 608 | unsigned long long map_offset; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 609 | int map_fd = phys_mapping(to_phys((void *)stack), &map_offset); |
| 610 | mmop = ((struct proc_mm_op) |
| 611 | { .op = MM_MMAP, |
| 612 | .u = |
| 613 | { .mmap = |
| 614 | { .addr = data, |
Jeff Dike | c539ab7 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 615 | .len = UM_KERN_PAGE_SIZE, |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 616 | .prot = PROT_READ | PROT_WRITE, |
| 617 | .flags = MAP_FIXED | MAP_SHARED, |
| 618 | .fd = map_fd, |
| 619 | .offset = map_offset |
| 620 | } } }); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 621 | CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop))); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 622 | if (n != sizeof(mmop)) { |
| 623 | n = errno; |
| 624 | printk(UM_KERN_ERR "map_stub_pages : /proc/mm map for " |
| 625 | "data failed, err = %d\n", n); |
| 626 | return -n; |
| 627 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 628 | } |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 629 | |
| 630 | return 0; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 633 | void new_thread(void *stack, jmp_buf *buf, void (*handler)(void)) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 634 | { |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 635 | (*buf)[0].JB_IP = (unsigned long) handler; |
Jeff Dike | e1a79c4 | 2007-05-10 22:22:31 -0700 | [diff] [blame] | 636 | (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE - |
| 637 | sizeof(void *); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Jeff Dike | e2216fe | 2006-02-07 12:58:43 -0800 | [diff] [blame] | 640 | #define INIT_JMP_NEW_THREAD 0 |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 641 | #define INIT_JMP_CALLBACK 1 |
| 642 | #define INIT_JMP_HALT 2 |
| 643 | #define INIT_JMP_REBOOT 3 |
Jeff Dike | e2216fe | 2006-02-07 12:58:43 -0800 | [diff] [blame] | 644 | |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 645 | void switch_threads(jmp_buf *me, jmp_buf *you) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 646 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 647 | if (UML_SETJMP(me) == 0) |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 648 | UML_LONGJMP(you, 1); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 651 | static jmp_buf initial_jmpbuf; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 652 | |
| 653 | /* XXX Make these percpu */ |
| 654 | static void (*cb_proc)(void *arg); |
| 655 | static void *cb_arg; |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 656 | static jmp_buf *cb_back; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 657 | |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 658 | int start_idle_thread(void *stack, jmp_buf *switch_buf) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 659 | { |
Jeff Dike | a5df0d1 | 2006-07-14 00:24:02 -0700 | [diff] [blame] | 660 | int n; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 661 | |
Al Viro | e87df98 | 2011-08-18 20:04:29 +0100 | [diff] [blame^] | 662 | set_handler(SIGWINCH, (__sighandler_t) sig_handler); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 663 | |
Jeff Dike | 77f6af7 | 2007-05-06 14:51:40 -0700 | [diff] [blame] | 664 | /* |
| 665 | * Can't use UML_SETJMP or UML_LONGJMP here because they save |
| 666 | * and restore signals, with the possible side-effect of |
| 667 | * trying to handle any signals which came when they were |
| 668 | * blocked, which can't be done on this stack. |
| 669 | * Signals must be blocked when jumping back here and restored |
| 670 | * after returning to the jumper. |
| 671 | */ |
| 672 | n = setjmp(initial_jmpbuf); |
Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 673 | switch (n) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 674 | case INIT_JMP_NEW_THREAD: |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 675 | (*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler; |
| 676 | (*switch_buf)[0].JB_SP = (unsigned long) stack + |
Jeff Dike | e1a79c4 | 2007-05-10 22:22:31 -0700 | [diff] [blame] | 677 | UM_THREAD_SIZE - sizeof(void *); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 678 | break; |
| 679 | case INIT_JMP_CALLBACK: |
| 680 | (*cb_proc)(cb_arg); |
Jeff Dike | 77f6af7 | 2007-05-06 14:51:40 -0700 | [diff] [blame] | 681 | longjmp(*cb_back, 1); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 682 | break; |
| 683 | case INIT_JMP_HALT: |
| 684 | kmalloc_ok = 0; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 685 | return 0; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 686 | case INIT_JMP_REBOOT: |
| 687 | kmalloc_ok = 0; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 688 | return 1; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 689 | default: |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 690 | printk(UM_KERN_ERR "Bad sigsetjmp return in " |
| 691 | "start_idle_thread - %d\n", n); |
| 692 | fatal_sigsegv(); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 693 | } |
Jeff Dike | 77f6af7 | 2007-05-06 14:51:40 -0700 | [diff] [blame] | 694 | longjmp(*switch_buf, 1); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | void initial_thread_cb_skas(void (*proc)(void *), void *arg) |
| 698 | { |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 699 | jmp_buf here; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 700 | |
| 701 | cb_proc = proc; |
| 702 | cb_arg = arg; |
| 703 | cb_back = &here; |
| 704 | |
| 705 | block_signals(); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 706 | if (UML_SETJMP(&here) == 0) |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 707 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 708 | unblock_signals(); |
| 709 | |
| 710 | cb_proc = NULL; |
| 711 | cb_arg = NULL; |
| 712 | cb_back = NULL; |
| 713 | } |
| 714 | |
| 715 | void halt_skas(void) |
| 716 | { |
| 717 | block_signals(); |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 718 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void reboot_skas(void) |
| 722 | { |
| 723 | block_signals(); |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 724 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 725 | } |
| 726 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 727 | void __switch_mm(struct mm_id *mm_idp) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 728 | { |
| 729 | int err; |
| 730 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 731 | /* FIXME: need cpu pid in __switch_mm */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 732 | if (proc_mm) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 733 | err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, |
| 734 | mm_idp->u.mm_fd); |
Jeff Dike | 3e6f2ac | 2008-02-04 22:30:58 -0800 | [diff] [blame] | 735 | if (err) { |
| 736 | printk(UM_KERN_ERR "__switch_mm - PTRACE_SWITCH_MM " |
| 737 | "failed, errno = %d\n", errno); |
| 738 | fatal_sigsegv(); |
| 739 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 740 | } |
| 741 | else userspace_pid[0] = mm_idp->u.pid; |
| 742 | } |