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