blob: b856c66ebd3a2be7c2a61027eb8ea616fe4cb1b9 [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
Richard Weinberger1d80f0c2015-10-25 19:54:33 +0100175int get_syscall(struct uml_pt_regs *regs)
176{
177 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp);
178
179 return UPT_SYSCALL_NR(regs);
180}
181
Nicolas Iooss5f329432014-10-12 13:02:13 +0200182extern char __syscall_stub_start[];
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800183
184static int userspace_tramp(void *stack)
185{
186 void *addr;
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000187 int fd;
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100188 unsigned long long offset;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800189
190 ptrace(PTRACE_TRACEME, 0, 0, 0);
191
Lepton Wua24864a2007-10-16 01:27:35 -0700192 signal(SIGTERM, SIG_DFL);
Jeff Dikeee3d9bd2008-02-04 22:30:56 -0800193 signal(SIGWINCH, SIG_IGN);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800194
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100195 /*
196 * This has a pte, but it can't be mapped in with the usual
197 * tlb_flush mechanism because this is part of that mechanism
198 */
Nicolas Iooss5f329432014-10-12 13:02:13 +0200199 fd = phys_mapping(to_phys(__syscall_stub_start), &offset);
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100200 addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
201 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
202 if (addr == MAP_FAILED) {
203 printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
204 "errno = %d\n", STUB_CODE, errno);
205 exit(1);
206 }
207
208 if (stack != NULL) {
209 fd = phys_mapping(to_phys(stack), &offset);
210 addr = mmap((void *) STUB_DATA,
211 UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
212 MAP_FIXED | MAP_SHARED, fd, offset);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700213 if (addr == MAP_FAILED) {
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100214 printk(UM_KERN_ERR "mapping segfault stack "
215 "at 0x%lx failed, errno = %d\n",
216 STUB_DATA, errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800217 exit(1);
218 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800219 }
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100220 if (stack != NULL) {
Jeff Dike4b84c692006-09-25 23:33:04 -0700221 struct sigaction sa;
222
Jeff Dike54ae36f2007-10-16 01:27:33 -0700223 unsigned long v = STUB_CODE +
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800224 (unsigned long) stub_segv_handler -
Nicolas Iooss5f329432014-10-12 13:02:13 +0200225 (unsigned long) __syscall_stub_start;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800226
Jeff Dike54ae36f2007-10-16 01:27:33 -0700227 set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
Jeff Dike4b84c692006-09-25 23:33:04 -0700228 sigemptyset(&sa.sa_mask);
Al Viro9b25fcb2011-08-18 20:04:09 +0100229 sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
230 sa.sa_sigaction = (void *) v;
Jeff Dike4b84c692006-09-25 23:33:04 -0700231 sa.sa_restorer = NULL;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800232 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
233 printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
234 "handler failed - errno = %d\n", errno);
235 exit(1);
236 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800237 }
238
Jeff Dike512b6fb2007-10-16 01:27:11 -0700239 kill(os_getpid(), SIGSTOP);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700240 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800241}
242
243/* Each element set once, and only accessed by a single processor anyway */
244#undef NR_CPUS
245#define NR_CPUS 1
246int userspace_pid[NR_CPUS];
247
248int start_userspace(unsigned long stub_stack)
249{
250 void *stack;
251 unsigned long sp;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800252 int pid, status, n, flags, err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800253
Jeff Dikec539ab72007-06-16 10:16:09 -0700254 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
255 PROT_READ | PROT_WRITE | PROT_EXEC,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800256 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800257 if (stack == MAP_FAILED) {
258 err = -errno;
259 printk(UM_KERN_ERR "start_userspace : mmap failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800260 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800261 return err;
262 }
263
Jeff Dikec539ab72007-06-16 10:16:09 -0700264 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800265
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100266 flags = CLONE_FILES | SIGCHLD;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700267
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800268 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800269 if (pid < 0) {
270 err = -errno;
271 printk(UM_KERN_ERR "start_userspace : clone failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800272 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800273 return err;
274 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800275
276 do {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800277 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800278 if (n < 0) {
279 err = -errno;
280 printk(UM_KERN_ERR "start_userspace : wait failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800281 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800282 goto out_kill;
283 }
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000284 } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800285
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800286 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
287 err = -EINVAL;
288 printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
Jeff Dikeb5498832008-02-04 22:31:21 -0800289 "status = %d\n", status);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800290 goto out_kill;
291 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800292
Jeff Dikeba180fd2007-10-16 01:27:00 -0700293 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800294 (void *) PTRACE_O_TRACESYSGOOD) < 0) {
295 err = -errno;
296 printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
297 "failed, errno = %d\n", errno);
298 goto out_kill;
299 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800300
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800301 if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
302 err = -errno;
303 printk(UM_KERN_ERR "start_userspace : munmap failed, "
304 "errno = %d\n", errno);
305 goto out_kill;
306 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800307
Jeff Dikeba180fd2007-10-16 01:27:00 -0700308 return pid;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800309
310 out_kill:
311 os_kill_ptraced_process(pid, 1);
312 return err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800313}
314
Jeff Dike77bf4402007-10-16 01:26:58 -0700315void userspace(struct uml_pt_regs *regs)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800316{
317 int err, status, op, pid = userspace_pid[0];
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700318 /* To prevent races if using_sysemu changes under us.*/
319 int local_using_sysemu;
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200320 siginfo_t si;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800321
Al Virob8a42092012-05-23 00:25:15 -0400322 /* Handle any immediate reschedules or signals */
323 interrupt_end();
324
Jeff Dikeba180fd2007-10-16 01:27:00 -0700325 while (1) {
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000326
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800327 /*
328 * This can legitimately fail if the process loads a
329 * bogus value into a segment register. It will
330 * segfault and PTRACE_GETREGS will read that value
331 * out of the process. However, PTRACE_SETREGS will
332 * fail. In this case, there is nothing to do but
333 * just kill the process.
334 */
Jeff Diked25f2e12008-02-04 22:30:57 -0800335 if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp))
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800336 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800337
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700338 if (put_fp_registers(pid, regs->fp))
339 fatal_sigsegv();
340
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800341 /* Now we set local_using_sysemu to be used for one loop */
342 local_using_sysemu = get_using_sysemu();
343
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700344 op = SELECT_PTRACE_OPERATION(local_using_sysemu,
345 singlestepping(NULL));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800346
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800347 if (ptrace(op, pid, 0, 0)) {
348 printk(UM_KERN_ERR "userspace - ptrace continue "
349 "failed, op = %d, errno = %d\n", op, errno);
350 fatal_sigsegv();
351 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800352
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800353 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800354 if (err < 0) {
355 printk(UM_KERN_ERR "userspace - wait failed, "
356 "errno = %d\n", errno);
357 fatal_sigsegv();
358 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800359
Jeff Dike77bf4402007-10-16 01:26:58 -0700360 regs->is_user = 1;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800361 if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
362 printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
363 "errno = %d\n", errno);
364 fatal_sigsegv();
365 }
Jeff Diked25f2e12008-02-04 22:30:57 -0800366
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700367 if (get_fp_registers(pid, regs->fp)) {
368 printk(UM_KERN_ERR "userspace - get_fp_registers failed, "
369 "errno = %d\n", errno);
370 fatal_sigsegv();
371 }
372
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800373 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
374
Jeff Dikeba180fd2007-10-16 01:27:00 -0700375 if (WIFSTOPPED(status)) {
Jeff Dike16dd07b2007-05-06 14:51:48 -0700376 int sig = WSTOPSIG(status);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200377
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200378 ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200379
Jeff Dike5134d8f2008-02-08 04:22:08 -0800380 switch (sig) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800381 case SIGSEGV:
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100382 if (PTRACE_FULL_FAULTINFO) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700383 get_skas_faultinfo(pid,
384 &regs->faultinfo);
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200385 (*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200386 regs);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700387 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800388 else handle_segv(pid, regs);
389 break;
390 case SIGTRAP + 0x80:
391 handle_trap(pid, regs, local_using_sysemu);
392 break;
393 case SIGTRAP:
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200394 relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800395 break;
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000396 case SIGALRM:
Jeff Diked2753a6d2007-10-16 01:27:25 -0700397 break;
398 case SIGIO:
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800399 case SIGILL:
400 case SIGBUS:
401 case SIGFPE:
402 case SIGWINCH:
Jeff Dike16dd07b2007-05-06 14:51:48 -0700403 block_signals();
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200404 (*sig_info[sig])(sig, (struct siginfo *)&si, regs);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700405 unblock_signals();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800406 break;
407 default:
Jeff Dike96cee302008-05-12 14:01:48 -0700408 printk(UM_KERN_ERR "userspace - child stopped "
Jeff Dikeba180fd2007-10-16 01:27:00 -0700409 "with signal %d\n", sig);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800410 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800411 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800412 pid = userspace_pid[0];
413 interrupt_end();
414
415 /* Avoid -ERESTARTSYS handling in host */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700416 if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
Jeff Dike18baddd2007-10-16 01:27:07 -0700417 PT_SYSCALL_NR(regs->gp) = -1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800418 }
419 }
420}
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800421
Jeff Dike16dd07b2007-05-06 14:51:48 -0700422static unsigned long thread_regs[MAX_REG_NR];
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700423static unsigned long thread_fp_regs[FP_SIZE];
Jeff Dike16dd07b2007-05-06 14:51:48 -0700424
425static int __init init_thread_regs(void)
426{
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700427 get_safe_registers(thread_regs, thread_fp_regs);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700428 /* Set parent's instruction pointer to start of clone-stub */
Jeff Dike54ae36f2007-10-16 01:27:33 -0700429 thread_regs[REGS_IP_INDEX] = STUB_CODE +
Jeff Dike16dd07b2007-05-06 14:51:48 -0700430 (unsigned long) stub_clone_handler -
Nicolas Iooss5f329432014-10-12 13:02:13 +0200431 (unsigned long) __syscall_stub_start;
Jeff Dike54ae36f2007-10-16 01:27:33 -0700432 thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
Jeff Dike16dd07b2007-05-06 14:51:48 -0700433 sizeof(void *);
434#ifdef __SIGNAL_FRAMESIZE
435 thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
436#endif
437 return 0;
438}
439
440__initcall(init_thread_regs);
441
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800442int copy_context_skas0(unsigned long new_stack, int pid)
443{
444 int err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800445 unsigned long current_stack = current_stub_stack();
446 struct stub_data *data = (struct stub_data *) current_stack;
447 struct stub_data *child_data = (struct stub_data *) new_stack;
Jeff Dike0a7675a2007-10-16 01:27:05 -0700448 unsigned long long new_offset;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800449 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
450
Jeff Dikeba180fd2007-10-16 01:27:00 -0700451 /*
452 * prepare offset and fd of child's stack as argument for parent's
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800453 * and child's mmap2 calls
454 */
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000455 *data = ((struct stub_data) {
456 .offset = MMAP_OFFSET(new_offset),
457 .fd = new_fd
458 });
Jeff Diked2753a6d2007-10-16 01:27:25 -0700459
Jeff Dike16dd07b2007-05-06 14:51:48 -0700460 err = ptrace_setregs(pid, thread_regs);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800461 if (err < 0) {
462 err = -errno;
463 printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
464 "failed, pid = %d, errno = %d\n", pid, -err);
465 return err;
466 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800467
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700468 err = put_fp_registers(pid, thread_fp_regs);
469 if (err < 0) {
470 printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
471 "failed, pid = %d, err = %d\n", pid, err);
472 return err;
473 }
474
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800475 /* set a well known return code for detection of child write failure */
476 child_data->err = 12345678;
477
Jeff Dikeba180fd2007-10-16 01:27:00 -0700478 /*
479 * Wait, until parent has finished its work: read child's pid from
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800480 * parent's stack, and check, if bad result.
481 */
Jeff Dike16dd07b2007-05-06 14:51:48 -0700482 err = ptrace(PTRACE_CONT, pid, 0, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800483 if (err) {
484 err = -errno;
485 printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
486 "errno = %d\n", pid, errno);
487 return err;
488 }
489
Jeff Dike16dd07b2007-05-06 14:51:48 -0700490 wait_stub_done(pid);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800491
492 pid = data->err;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800493 if (pid < 0) {
494 printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
495 "error %d\n", -pid);
496 return pid;
497 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800498
Jeff Dikeba180fd2007-10-16 01:27:00 -0700499 /*
500 * Wait, until child has finished too: read child's result from
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800501 * child's stack and check it.
502 */
Jeff Dike16dd07b2007-05-06 14:51:48 -0700503 wait_stub_done(pid);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800504 if (child_data->err != STUB_DATA) {
505 printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports "
506 "error %ld\n", child_data->err);
507 err = child_data->err;
508 goto out_kill;
509 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800510
511 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800512 (void *)PTRACE_O_TRACESYSGOOD) < 0) {
513 err = -errno;
514 printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
515 "failed, errno = %d\n", errno);
516 goto out_kill;
517 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800518
519 return pid;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800520
521 out_kill:
522 os_kill_ptraced_process(pid, 1);
523 return err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800524}
525
Jeff Dike3c917352006-09-27 01:50:40 -0700526void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800527{
Jeff Dike3c917352006-09-27 01:50:40 -0700528 (*buf)[0].JB_IP = (unsigned long) handler;
Jeff Dikee1a79c42007-05-10 22:22:31 -0700529 (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
530 sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800531}
532
Jeff Dikee2216fe2006-02-07 12:58:43 -0800533#define INIT_JMP_NEW_THREAD 0
Jeff Dike3c917352006-09-27 01:50:40 -0700534#define INIT_JMP_CALLBACK 1
535#define INIT_JMP_HALT 2
536#define INIT_JMP_REBOOT 3
Jeff Dikee2216fe2006-02-07 12:58:43 -0800537
Jeff Dike3c917352006-09-27 01:50:40 -0700538void switch_threads(jmp_buf *me, jmp_buf *you)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800539{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700540 if (UML_SETJMP(me) == 0)
Jeff Dike3c917352006-09-27 01:50:40 -0700541 UML_LONGJMP(you, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800542}
543
Jeff Dikead28e022006-04-18 22:21:41 -0700544static jmp_buf initial_jmpbuf;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800545
546/* XXX Make these percpu */
547static void (*cb_proc)(void *arg);
548static void *cb_arg;
Jeff Dikead28e022006-04-18 22:21:41 -0700549static jmp_buf *cb_back;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800550
Jeff Dike3c917352006-09-27 01:50:40 -0700551int start_idle_thread(void *stack, jmp_buf *switch_buf)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800552{
Jeff Dikea5df0d12006-07-14 00:24:02 -0700553 int n;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800554
Al Viro00361682011-08-18 20:04:39 +0100555 set_handler(SIGWINCH);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800556
Jeff Dike77f6af72007-05-06 14:51:40 -0700557 /*
558 * Can't use UML_SETJMP or UML_LONGJMP here because they save
559 * and restore signals, with the possible side-effect of
560 * trying to handle any signals which came when they were
561 * blocked, which can't be done on this stack.
562 * Signals must be blocked when jumping back here and restored
563 * after returning to the jumper.
564 */
565 n = setjmp(initial_jmpbuf);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800566 switch (n) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800567 case INIT_JMP_NEW_THREAD:
Thomas Meyer33bbc302015-03-28 09:59:46 +0100568 (*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
Jeff Dike3c917352006-09-27 01:50:40 -0700569 (*switch_buf)[0].JB_SP = (unsigned long) stack +
Jeff Dikee1a79c42007-05-10 22:22:31 -0700570 UM_THREAD_SIZE - sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800571 break;
572 case INIT_JMP_CALLBACK:
573 (*cb_proc)(cb_arg);
Jeff Dike77f6af72007-05-06 14:51:40 -0700574 longjmp(*cb_back, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800575 break;
576 case INIT_JMP_HALT:
577 kmalloc_ok = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700578 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800579 case INIT_JMP_REBOOT:
580 kmalloc_ok = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700581 return 1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800582 default:
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800583 printk(UM_KERN_ERR "Bad sigsetjmp return in "
584 "start_idle_thread - %d\n", n);
585 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800586 }
Jeff Dike77f6af72007-05-06 14:51:40 -0700587 longjmp(*switch_buf, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800588}
589
590void initial_thread_cb_skas(void (*proc)(void *), void *arg)
591{
Jeff Dikead28e022006-04-18 22:21:41 -0700592 jmp_buf here;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800593
594 cb_proc = proc;
595 cb_arg = arg;
596 cb_back = &here;
597
598 block_signals();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700599 if (UML_SETJMP(&here) == 0)
Jeff Dikead28e022006-04-18 22:21:41 -0700600 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800601 unblock_signals();
602
603 cb_proc = NULL;
604 cb_arg = NULL;
605 cb_back = NULL;
606}
607
608void halt_skas(void)
609{
610 block_signals();
Jeff Dikead28e022006-04-18 22:21:41 -0700611 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800612}
613
614void reboot_skas(void)
615{
616 block_signals();
Jeff Dikead28e022006-04-18 22:21:41 -0700617 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800618}
619
Jeff Dike77bf4402007-10-16 01:26:58 -0700620void __switch_mm(struct mm_id *mm_idp)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800621{
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100622 userspace_pid[0] = mm_idp->u.pid;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800623}