blob: 0a99d4515065a2da6d5494e5306e9dfcfeb37da4 [file] [log] [blame]
Gennady Sharapovabaf6972006-01-18 17:42:46 -08001/*
Anton Ivanov2eb5f312015-11-02 16:16:37 +00002 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
Jeff Dikeba180fd2007-10-16 01:27:00 -07003 * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Gennady Sharapovabaf6972006-01-18 17:42:46 -08004 * Licensed under the GPL
5 */
6
7#include <stdlib.h>
Gennady Sharapovabaf6972006-01-18 17:42:46 -08008#include <unistd.h>
Gennady Sharapovabaf6972006-01-18 17:42:46 -08009#include <sched.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070010#include <errno.h>
11#include <string.h>
Gennady Sharapovabaf6972006-01-18 17:42:46 -080012#include <sys/mman.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070013#include <sys/wait.h>
14#include <asm/unistd.h>
Al Viro37185b32012-10-08 03:27:32 +010015#include <as-layout.h>
16#include <init.h>
17#include <kern_util.h>
18#include <mem.h>
19#include <os.h>
Al Viro37185b32012-10-08 03:27:32 +010020#include <ptrace_user.h>
21#include <registers.h>
22#include <skas.h>
Al Viro37185b32012-10-08 03:27:32 +010023#include <sysdep/stub.h>
Gennady Sharapovabaf6972006-01-18 17:42:46 -080024
25int is_skas_winch(int pid, int fd, void *data)
26{
Al Viro17e05202011-08-18 20:08:19 +010027 return pid == getpgrp();
Gennady Sharapovabaf6972006-01-18 17:42:46 -080028}
29
Jeff Dikef30c2c92007-05-06 14:51:29 -070030static int ptrace_dump_regs(int pid)
31{
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080032 unsigned long regs[MAX_REG_NR];
33 int i;
Jeff Dikef30c2c92007-05-06 14:51:29 -070034
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080035 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
36 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -070037
38 printk(UM_KERN_ERR "Stub registers -\n");
39 for (i = 0; i < ARRAY_SIZE(regs); i++)
40 printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]);
Jeff Dikef30c2c92007-05-06 14:51:29 -070041
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080042 return 0;
Jeff Dikef30c2c92007-05-06 14:51:29 -070043}
44
Jeff Dike16dd07b2007-05-06 14:51:48 -070045/*
46 * Signals that are OK to receive in the stub - we'll just continue it.
47 * SIGWINCH will happen when UML is inside a detached screen.
48 */
Anton Ivanov2eb5f312015-11-02 16:16:37 +000049#define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
Jeff Dike16dd07b2007-05-06 14:51:48 -070050
51/* Signals that the stub will finish with - anything else is an error */
Jeff Dikeee3d9bd2008-02-04 22:30:56 -080052#define STUB_DONE_MASK (1 << SIGTRAP)
Jeff Dike16dd07b2007-05-06 14:51:48 -070053
54void wait_stub_done(int pid)
Gennady Sharapovabaf6972006-01-18 17:42:46 -080055{
Richard Weinbergerae5db6d2014-07-20 12:56:34 +020056 int n, status, err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -080057
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 while (1) {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080059 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dikeba180fd2007-10-16 01:27:00 -070060 if ((n < 0) || !WIFSTOPPED(status))
Jeff Dike16dd07b2007-05-06 14:51:48 -070061 goto bad_wait;
Gennady Sharapovabaf6972006-01-18 17:42:46 -080062
Jeff Dikeba180fd2007-10-16 01:27:00 -070063 if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
Jeff Dike16dd07b2007-05-06 14:51:48 -070064 break;
65
66 err = ptrace(PTRACE_CONT, pid, 0, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080067 if (err) {
68 printk(UM_KERN_ERR "wait_stub_done : continue failed, "
69 "errno = %d\n", errno);
70 fatal_sigsegv();
71 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -080072 }
Jeff Dike16dd07b2007-05-06 14:51:48 -070073
Jeff Dikeba180fd2007-10-16 01:27:00 -070074 if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
Jeff Dike16dd07b2007-05-06 14:51:48 -070075 return;
76
77bad_wait:
78 err = ptrace_dump_regs(pid);
Jeff Dikeba180fd2007-10-16 01:27:00 -070079 if (err)
80 printk(UM_KERN_ERR "Failed to get registers from stub, "
81 "errno = %d\n", -err);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080082 printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
83 "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
84 status);
Richard Weinbergerae5db6d2014-07-20 12:56:34 +020085 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -080086}
87
88extern unsigned long current_stub_stack(void);
89
WANG Cong99764fa2008-07-23 21:28:49 -070090static void get_skas_faultinfo(int pid, struct faultinfo *fi)
Gennady Sharapovabaf6972006-01-18 17:42:46 -080091{
92 int err;
Richard Weinbergerd0b5e152015-03-18 21:31:27 +010093 unsigned long fpregs[FP_SIZE];
Gennady Sharapovabaf6972006-01-18 17:42:46 -080094
Richard Weinbergerd0b5e152015-03-18 21:31:27 +010095 err = get_fp_registers(pid, fpregs);
96 if (err < 0) {
97 printk(UM_KERN_ERR "save_fp_registers returned %d\n",
98 err);
99 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800100 }
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100101 err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
102 if (err) {
103 printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
104 "errno = %d\n", pid, errno);
105 fatal_sigsegv();
106 }
107 wait_stub_done(pid);
Jeff Dike2f56deb2008-02-23 15:23:49 -0800108
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100109 /*
110 * faultinfo is prepared by the stub-segv-handler at start of
111 * the stub stack page. We just have to copy it.
112 */
113 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800114
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100115 err = put_fp_registers(pid, fpregs);
116 if (err < 0) {
117 printk(UM_KERN_ERR "put_fp_registers returned %d\n",
118 err);
119 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800120 }
121}
122
Jeff Dike77bf4402007-10-16 01:26:58 -0700123static void handle_segv(int pid, struct uml_pt_regs * regs)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800124{
Jeff Dike77bf4402007-10-16 01:26:58 -0700125 get_skas_faultinfo(pid, &regs->faultinfo);
126 segv(regs->faultinfo, 0, 1, NULL);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800127}
128
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129/*
130 * To use the same value of using_sysemu as the caller, ask it that value
131 * (in local_using_sysemu
132 */
133static void handle_trap(int pid, struct uml_pt_regs *regs,
134 int local_using_sysemu)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800135{
136 int err, status;
137
Jeff Dikee06173b2008-02-04 22:31:12 -0800138 if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
139 fatal_sigsegv();
140
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800141 if (!local_using_sysemu)
142 {
Al Viro966e8032011-08-18 20:12:19 +0100143 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800144 __NR_getpid);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800145 if (err < 0) {
146 printk(UM_KERN_ERR "handle_trap - nullifying syscall "
147 "failed, errno = %d\n", errno);
148 fatal_sigsegv();
149 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800150
Jeff Dikeba180fd2007-10-16 01:27:00 -0700151 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800152 if (err < 0) {
153 printk(UM_KERN_ERR "handle_trap - continuing to end of "
154 "syscall failed, errno = %d\n", errno);
155 fatal_sigsegv();
156 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700157
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800158 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700159 if ((err < 0) || !WIFSTOPPED(status) ||
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800160 (WSTOPSIG(status) != SIGTRAP + 0x80)) {
161 err = ptrace_dump_regs(pid);
162 if (err)
163 printk(UM_KERN_ERR "Failed to get registers "
Jeff Dikeba180fd2007-10-16 01:27:00 -0700164 "from process, errno = %d\n", -err);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800165 printk(UM_KERN_ERR "handle_trap - failed to wait at "
166 "end of syscall, errno = %d, status = %d\n",
167 errno, status);
168 fatal_sigsegv();
169 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800170 }
171
172 handle_syscall(regs);
173}
174
Nicolas Iooss5f329432014-10-12 13:02:13 +0200175extern char __syscall_stub_start[];
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800176
177static int userspace_tramp(void *stack)
178{
179 void *addr;
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000180 int fd;
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100181 unsigned long long offset;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800182
183 ptrace(PTRACE_TRACEME, 0, 0, 0);
184
Lepton Wua24864a2007-10-16 01:27:35 -0700185 signal(SIGTERM, SIG_DFL);
Jeff Dikeee3d9bd2008-02-04 22:30:56 -0800186 signal(SIGWINCH, SIG_IGN);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800187
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100188 /*
189 * This has a pte, but it can't be mapped in with the usual
190 * tlb_flush mechanism because this is part of that mechanism
191 */
Nicolas Iooss5f329432014-10-12 13:02:13 +0200192 fd = phys_mapping(to_phys(__syscall_stub_start), &offset);
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100193 addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
194 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
195 if (addr == MAP_FAILED) {
196 printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
197 "errno = %d\n", STUB_CODE, errno);
198 exit(1);
199 }
200
201 if (stack != NULL) {
202 fd = phys_mapping(to_phys(stack), &offset);
203 addr = mmap((void *) STUB_DATA,
204 UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
205 MAP_FIXED | MAP_SHARED, fd, offset);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700206 if (addr == MAP_FAILED) {
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100207 printk(UM_KERN_ERR "mapping segfault stack "
208 "at 0x%lx failed, errno = %d\n",
209 STUB_DATA, errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800210 exit(1);
211 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800212 }
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100213 if (stack != NULL) {
Jeff Dike4b84c692006-09-25 23:33:04 -0700214 struct sigaction sa;
215
Jeff Dike54ae36f2007-10-16 01:27:33 -0700216 unsigned long v = STUB_CODE +
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800217 (unsigned long) stub_segv_handler -
Nicolas Iooss5f329432014-10-12 13:02:13 +0200218 (unsigned long) __syscall_stub_start;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800219
Jeff Dike54ae36f2007-10-16 01:27:33 -0700220 set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
Jeff Dike4b84c692006-09-25 23:33:04 -0700221 sigemptyset(&sa.sa_mask);
Al Viro9b25fcb2011-08-18 20:04:09 +0100222 sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
223 sa.sa_sigaction = (void *) v;
Jeff Dike4b84c692006-09-25 23:33:04 -0700224 sa.sa_restorer = NULL;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800225 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
226 printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
227 "handler failed - errno = %d\n", errno);
228 exit(1);
229 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800230 }
231
Jeff Dike512b6fb2007-10-16 01:27:11 -0700232 kill(os_getpid(), SIGSTOP);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700233 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800234}
235
236/* Each element set once, and only accessed by a single processor anyway */
237#undef NR_CPUS
238#define NR_CPUS 1
239int userspace_pid[NR_CPUS];
240
241int start_userspace(unsigned long stub_stack)
242{
243 void *stack;
244 unsigned long sp;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800245 int pid, status, n, flags, err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800246
Jeff Dikec539ab72007-06-16 10:16:09 -0700247 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
248 PROT_READ | PROT_WRITE | PROT_EXEC,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800249 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800250 if (stack == MAP_FAILED) {
251 err = -errno;
252 printk(UM_KERN_ERR "start_userspace : mmap failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800253 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800254 return err;
255 }
256
Jeff Dikec539ab72007-06-16 10:16:09 -0700257 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800258
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100259 flags = CLONE_FILES | SIGCHLD;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700260
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800261 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800262 if (pid < 0) {
263 err = -errno;
264 printk(UM_KERN_ERR "start_userspace : clone failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800265 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800266 return err;
267 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800268
269 do {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800270 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800271 if (n < 0) {
272 err = -errno;
273 printk(UM_KERN_ERR "start_userspace : wait failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800274 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800275 goto out_kill;
276 }
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000277 } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800278
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800279 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
280 err = -EINVAL;
281 printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
Jeff Dikeb5498832008-02-04 22:31:21 -0800282 "status = %d\n", status);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800283 goto out_kill;
284 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800285
Jeff Dikeba180fd2007-10-16 01:27:00 -0700286 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800287 (void *) PTRACE_O_TRACESYSGOOD) < 0) {
288 err = -errno;
289 printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
290 "failed, errno = %d\n", errno);
291 goto out_kill;
292 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800293
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800294 if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
295 err = -errno;
296 printk(UM_KERN_ERR "start_userspace : munmap failed, "
297 "errno = %d\n", errno);
298 goto out_kill;
299 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800300
Jeff Dikeba180fd2007-10-16 01:27:00 -0700301 return pid;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800302
303 out_kill:
304 os_kill_ptraced_process(pid, 1);
305 return err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800306}
307
Jeff Dike77bf4402007-10-16 01:26:58 -0700308void userspace(struct uml_pt_regs *regs)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800309{
310 int err, status, op, pid = userspace_pid[0];
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700311 /* To prevent races if using_sysemu changes under us.*/
312 int local_using_sysemu;
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200313 siginfo_t si;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800314
Al Virob8a42092012-05-23 00:25:15 -0400315 /* Handle any immediate reschedules or signals */
316 interrupt_end();
317
Jeff Dikeba180fd2007-10-16 01:27:00 -0700318 while (1) {
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000319
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800320 /*
321 * This can legitimately fail if the process loads a
322 * bogus value into a segment register. It will
323 * segfault and PTRACE_GETREGS will read that value
324 * out of the process. However, PTRACE_SETREGS will
325 * fail. In this case, there is nothing to do but
326 * just kill the process.
327 */
Jeff Diked25f2e12008-02-04 22:30:57 -0800328 if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp))
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800329 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800330
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700331 if (put_fp_registers(pid, regs->fp))
332 fatal_sigsegv();
333
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800334 /* Now we set local_using_sysemu to be used for one loop */
335 local_using_sysemu = get_using_sysemu();
336
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700337 op = SELECT_PTRACE_OPERATION(local_using_sysemu,
338 singlestepping(NULL));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800339
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800340 if (ptrace(op, pid, 0, 0)) {
341 printk(UM_KERN_ERR "userspace - ptrace continue "
342 "failed, op = %d, errno = %d\n", op, errno);
343 fatal_sigsegv();
344 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800345
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800346 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800347 if (err < 0) {
348 printk(UM_KERN_ERR "userspace - wait failed, "
349 "errno = %d\n", errno);
350 fatal_sigsegv();
351 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800352
Jeff Dike77bf4402007-10-16 01:26:58 -0700353 regs->is_user = 1;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800354 if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
355 printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
356 "errno = %d\n", errno);
357 fatal_sigsegv();
358 }
Jeff Diked25f2e12008-02-04 22:30:57 -0800359
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700360 if (get_fp_registers(pid, regs->fp)) {
361 printk(UM_KERN_ERR "userspace - get_fp_registers failed, "
362 "errno = %d\n", errno);
363 fatal_sigsegv();
364 }
365
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800366 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
367
Jeff Dikeba180fd2007-10-16 01:27:00 -0700368 if (WIFSTOPPED(status)) {
Jeff Dike16dd07b2007-05-06 14:51:48 -0700369 int sig = WSTOPSIG(status);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200370
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200371 ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200372
Jeff Dike5134d8f2008-02-08 04:22:08 -0800373 switch (sig) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800374 case SIGSEGV:
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100375 if (PTRACE_FULL_FAULTINFO) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700376 get_skas_faultinfo(pid,
377 &regs->faultinfo);
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200378 (*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200379 regs);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700380 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800381 else handle_segv(pid, regs);
382 break;
383 case SIGTRAP + 0x80:
384 handle_trap(pid, regs, local_using_sysemu);
385 break;
386 case SIGTRAP:
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200387 relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800388 break;
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000389 case SIGALRM:
Jeff Diked2753a6d2007-10-16 01:27:25 -0700390 break;
391 case SIGIO:
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800392 case SIGILL:
393 case SIGBUS:
394 case SIGFPE:
395 case SIGWINCH:
Jeff Dike16dd07b2007-05-06 14:51:48 -0700396 block_signals();
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200397 (*sig_info[sig])(sig, (struct siginfo *)&si, regs);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700398 unblock_signals();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800399 break;
400 default:
Jeff Dike96cee302008-05-12 14:01:48 -0700401 printk(UM_KERN_ERR "userspace - child stopped "
Jeff Dikeba180fd2007-10-16 01:27:00 -0700402 "with signal %d\n", sig);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800403 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800404 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800405 pid = userspace_pid[0];
406 interrupt_end();
407
408 /* Avoid -ERESTARTSYS handling in host */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700409 if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
Jeff Dike18baddd2007-10-16 01:27:07 -0700410 PT_SYSCALL_NR(regs->gp) = -1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800411 }
412 }
413}
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800414
Jeff Dike16dd07b2007-05-06 14:51:48 -0700415static unsigned long thread_regs[MAX_REG_NR];
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700416static unsigned long thread_fp_regs[FP_SIZE];
Jeff Dike16dd07b2007-05-06 14:51:48 -0700417
418static int __init init_thread_regs(void)
419{
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700420 get_safe_registers(thread_regs, thread_fp_regs);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700421 /* Set parent's instruction pointer to start of clone-stub */
Jeff Dike54ae36f2007-10-16 01:27:33 -0700422 thread_regs[REGS_IP_INDEX] = STUB_CODE +
Jeff Dike16dd07b2007-05-06 14:51:48 -0700423 (unsigned long) stub_clone_handler -
Nicolas Iooss5f329432014-10-12 13:02:13 +0200424 (unsigned long) __syscall_stub_start;
Jeff Dike54ae36f2007-10-16 01:27:33 -0700425 thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
Jeff Dike16dd07b2007-05-06 14:51:48 -0700426 sizeof(void *);
427#ifdef __SIGNAL_FRAMESIZE
428 thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
429#endif
430 return 0;
431}
432
433__initcall(init_thread_regs);
434
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800435int copy_context_skas0(unsigned long new_stack, int pid)
436{
437 int err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800438 unsigned long current_stack = current_stub_stack();
439 struct stub_data *data = (struct stub_data *) current_stack;
440 struct stub_data *child_data = (struct stub_data *) new_stack;
Jeff Dike0a7675a2007-10-16 01:27:05 -0700441 unsigned long long new_offset;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800442 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
443
Jeff Dikeba180fd2007-10-16 01:27:00 -0700444 /*
445 * prepare offset and fd of child's stack as argument for parent's
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800446 * and child's mmap2 calls
447 */
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000448 *data = ((struct stub_data) {
449 .offset = MMAP_OFFSET(new_offset),
450 .fd = new_fd
451 });
Jeff Diked2753a6d2007-10-16 01:27:25 -0700452
Jeff Dike16dd07b2007-05-06 14:51:48 -0700453 err = ptrace_setregs(pid, thread_regs);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800454 if (err < 0) {
455 err = -errno;
456 printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
457 "failed, pid = %d, errno = %d\n", pid, -err);
458 return err;
459 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800460
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700461 err = put_fp_registers(pid, thread_fp_regs);
462 if (err < 0) {
463 printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
464 "failed, pid = %d, err = %d\n", pid, err);
465 return err;
466 }
467
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800468 /* set a well known return code for detection of child write failure */
469 child_data->err = 12345678;
470
Jeff Dikeba180fd2007-10-16 01:27:00 -0700471 /*
472 * Wait, until parent has finished its work: read child's pid from
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800473 * parent's stack, and check, if bad result.
474 */
Jeff Dike16dd07b2007-05-06 14:51:48 -0700475 err = ptrace(PTRACE_CONT, pid, 0, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800476 if (err) {
477 err = -errno;
478 printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
479 "errno = %d\n", pid, errno);
480 return err;
481 }
482
Jeff Dike16dd07b2007-05-06 14:51:48 -0700483 wait_stub_done(pid);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800484
485 pid = data->err;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800486 if (pid < 0) {
487 printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
488 "error %d\n", -pid);
489 return pid;
490 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800491
Jeff Dikeba180fd2007-10-16 01:27:00 -0700492 /*
493 * Wait, until child has finished too: read child's result from
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800494 * child's stack and check it.
495 */
Jeff Dike16dd07b2007-05-06 14:51:48 -0700496 wait_stub_done(pid);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800497 if (child_data->err != STUB_DATA) {
498 printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports "
499 "error %ld\n", child_data->err);
500 err = child_data->err;
501 goto out_kill;
502 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800503
504 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800505 (void *)PTRACE_O_TRACESYSGOOD) < 0) {
506 err = -errno;
507 printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
508 "failed, errno = %d\n", errno);
509 goto out_kill;
510 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800511
512 return pid;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800513
514 out_kill:
515 os_kill_ptraced_process(pid, 1);
516 return err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800517}
518
Jeff Dike3c917352006-09-27 01:50:40 -0700519void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800520{
Jeff Dike3c917352006-09-27 01:50:40 -0700521 (*buf)[0].JB_IP = (unsigned long) handler;
Jeff Dikee1a79c42007-05-10 22:22:31 -0700522 (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
523 sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800524}
525
Jeff Dikee2216fe2006-02-07 12:58:43 -0800526#define INIT_JMP_NEW_THREAD 0
Jeff Dike3c917352006-09-27 01:50:40 -0700527#define INIT_JMP_CALLBACK 1
528#define INIT_JMP_HALT 2
529#define INIT_JMP_REBOOT 3
Jeff Dikee2216fe2006-02-07 12:58:43 -0800530
Jeff Dike3c917352006-09-27 01:50:40 -0700531void switch_threads(jmp_buf *me, jmp_buf *you)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800532{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700533 if (UML_SETJMP(me) == 0)
Jeff Dike3c917352006-09-27 01:50:40 -0700534 UML_LONGJMP(you, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800535}
536
Jeff Dikead28e022006-04-18 22:21:41 -0700537static jmp_buf initial_jmpbuf;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800538
539/* XXX Make these percpu */
540static void (*cb_proc)(void *arg);
541static void *cb_arg;
Jeff Dikead28e022006-04-18 22:21:41 -0700542static jmp_buf *cb_back;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800543
Jeff Dike3c917352006-09-27 01:50:40 -0700544int start_idle_thread(void *stack, jmp_buf *switch_buf)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800545{
Jeff Dikea5df0d12006-07-14 00:24:02 -0700546 int n;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800547
Al Viro00361682011-08-18 20:04:39 +0100548 set_handler(SIGWINCH);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800549
Jeff Dike77f6af72007-05-06 14:51:40 -0700550 /*
551 * Can't use UML_SETJMP or UML_LONGJMP here because they save
552 * and restore signals, with the possible side-effect of
553 * trying to handle any signals which came when they were
554 * blocked, which can't be done on this stack.
555 * Signals must be blocked when jumping back here and restored
556 * after returning to the jumper.
557 */
558 n = setjmp(initial_jmpbuf);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800559 switch (n) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800560 case INIT_JMP_NEW_THREAD:
Thomas Meyer33bbc302015-03-28 09:59:46 +0100561 (*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
Jeff Dike3c917352006-09-27 01:50:40 -0700562 (*switch_buf)[0].JB_SP = (unsigned long) stack +
Jeff Dikee1a79c42007-05-10 22:22:31 -0700563 UM_THREAD_SIZE - sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800564 break;
565 case INIT_JMP_CALLBACK:
566 (*cb_proc)(cb_arg);
Jeff Dike77f6af72007-05-06 14:51:40 -0700567 longjmp(*cb_back, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800568 break;
569 case INIT_JMP_HALT:
570 kmalloc_ok = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700571 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800572 case INIT_JMP_REBOOT:
573 kmalloc_ok = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700574 return 1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800575 default:
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800576 printk(UM_KERN_ERR "Bad sigsetjmp return in "
577 "start_idle_thread - %d\n", n);
578 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800579 }
Jeff Dike77f6af72007-05-06 14:51:40 -0700580 longjmp(*switch_buf, 1);
Richard Weinberger11832f32018-06-15 16:42:56 +0200581
582 /* unreachable */
583 printk(UM_KERN_ERR "impossible long jump!");
584 fatal_sigsegv();
585 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800586}
587
588void initial_thread_cb_skas(void (*proc)(void *), void *arg)
589{
Jeff Dikead28e022006-04-18 22:21:41 -0700590 jmp_buf here;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800591
592 cb_proc = proc;
593 cb_arg = arg;
594 cb_back = &here;
595
596 block_signals();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700597 if (UML_SETJMP(&here) == 0)
Jeff Dikead28e022006-04-18 22:21:41 -0700598 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800599 unblock_signals();
600
601 cb_proc = NULL;
602 cb_arg = NULL;
603 cb_back = NULL;
604}
605
606void halt_skas(void)
607{
608 block_signals();
Jeff Dikead28e022006-04-18 22:21:41 -0700609 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800610}
611
612void reboot_skas(void)
613{
614 block_signals();
Jeff Dikead28e022006-04-18 22:21:41 -0700615 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800616}
617
Jeff Dike77bf4402007-10-16 01:26:58 -0700618void __switch_mm(struct mm_id *mm_idp)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800619{
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100620 userspace_pid[0] = mm_idp->u.pid;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800621}