blob: 31743f1d1195e0257fcef7df198849a452fa319a [file] [log] [blame]
Gennady Sharapovabaf6972006-01-18 17:42:46 -08001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Gennady Sharapovabaf6972006-01-18 17:42:46 -08003 * Licensed under the GPL
4 */
5
6#include <stdlib.h>
Gennady Sharapovabaf6972006-01-18 17:42:46 -08007#include <unistd.h>
Gennady Sharapovabaf6972006-01-18 17:42:46 -08008#include <sched.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07009#include <errno.h>
10#include <string.h>
Gennady Sharapovabaf6972006-01-18 17:42:46 -080011#include <sys/mman.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070012#include <sys/ptrace.h>
13#include <sys/wait.h>
14#include <asm/unistd.h>
Jeff Dike16dd07b2007-05-06 14:51:48 -070015#include "as-layout.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070016#include "chan_user.h"
Jeff Dikeedea1382008-02-04 22:30:46 -080017#include "kern_util.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070018#include "mem.h"
19#include "os.h"
20#include "process.h"
21#include "proc_mm.h"
22#include "ptrace_user.h"
23#include "registers.h"
24#include "skas.h"
25#include "skas_ptrace.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070026#include "sysdep/stub.h"
Gennady Sharapovabaf6972006-01-18 17:42:46 -080027
28int is_skas_winch(int pid, int fd, void *data)
29{
Jeff Dike512b6fb2007-10-16 01:27:11 -070030 if (pid != getpgrp())
Jeff Dikeba180fd2007-10-16 01:27:00 -070031 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -080032
Jeff Dike42a359e2007-07-15 23:38:55 -070033 register_winch_irq(-1, fd, -1, data, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070034 return 1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -080035}
36
Jeff Dikef30c2c92007-05-06 14:51:29 -070037static int ptrace_dump_regs(int pid)
38{
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080039 unsigned long regs[MAX_REG_NR];
40 int i;
Jeff Dikef30c2c92007-05-06 14:51:29 -070041
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080042 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
43 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -070044
45 printk(UM_KERN_ERR "Stub registers -\n");
46 for (i = 0; i < ARRAY_SIZE(regs); i++)
47 printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]);
Jeff Dikef30c2c92007-05-06 14:51:29 -070048
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080049 return 0;
Jeff Dikef30c2c92007-05-06 14:51:29 -070050}
51
Jeff Dike16dd07b2007-05-06 14:51:48 -070052/*
53 * Signals that are OK to receive in the stub - we'll just continue it.
54 * SIGWINCH will happen when UML is inside a detached screen.
55 */
Jeff Dike3d5ede62008-06-05 22:46:13 -070056#define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH))
Jeff Dike16dd07b2007-05-06 14:51:48 -070057
58/* Signals that the stub will finish with - anything else is an error */
Jeff Dikeee3d9bd2008-02-04 22:30:56 -080059#define STUB_DONE_MASK (1 << SIGTRAP)
Jeff Dike16dd07b2007-05-06 14:51:48 -070060
61void wait_stub_done(int pid)
Gennady Sharapovabaf6972006-01-18 17:42:46 -080062{
63 int n, status, err;
64
Jeff Dikeba180fd2007-10-16 01:27:00 -070065 while (1) {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080066 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dikeba180fd2007-10-16 01:27:00 -070067 if ((n < 0) || !WIFSTOPPED(status))
Jeff Dike16dd07b2007-05-06 14:51:48 -070068 goto bad_wait;
Gennady Sharapovabaf6972006-01-18 17:42:46 -080069
Jeff Dikeba180fd2007-10-16 01:27:00 -070070 if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
Jeff Dike16dd07b2007-05-06 14:51:48 -070071 break;
72
73 err = ptrace(PTRACE_CONT, pid, 0, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080074 if (err) {
75 printk(UM_KERN_ERR "wait_stub_done : continue failed, "
76 "errno = %d\n", errno);
77 fatal_sigsegv();
78 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -080079 }
Jeff Dike16dd07b2007-05-06 14:51:48 -070080
Jeff Dikeba180fd2007-10-16 01:27:00 -070081 if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
Jeff Dike16dd07b2007-05-06 14:51:48 -070082 return;
83
84bad_wait:
85 err = ptrace_dump_regs(pid);
Jeff Dikeba180fd2007-10-16 01:27:00 -070086 if (err)
87 printk(UM_KERN_ERR "Failed to get registers from stub, "
88 "errno = %d\n", -err);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -080089 printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
90 "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
91 status);
92 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -080093}
94
95extern unsigned long current_stub_stack(void);
96
WANG Cong99764fa2008-07-23 21:28:49 -070097static void get_skas_faultinfo(int pid, struct faultinfo *fi)
Gennady Sharapovabaf6972006-01-18 17:42:46 -080098{
99 int err;
100
Jeff Dikeba180fd2007-10-16 01:27:00 -0700101 if (ptrace_faultinfo) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800102 err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800103 if (err) {
104 printk(UM_KERN_ERR "get_skas_faultinfo - "
105 "PTRACE_FAULTINFO failed, errno = %d\n", errno);
106 fatal_sigsegv();
107 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800108
109 /* Special handling for i386, which has different structs */
110 if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo))
111 memset((char *)fi + sizeof(struct ptrace_faultinfo), 0,
112 sizeof(struct faultinfo) -
113 sizeof(struct ptrace_faultinfo));
114 }
115 else {
Jeff Dike2f56deb2008-02-23 15:23:49 -0800116 unsigned long fpregs[FP_SIZE];
117
118 err = get_fp_registers(pid, fpregs);
119 if (err < 0) {
120 printk(UM_KERN_ERR "save_fp_registers returned %d\n",
121 err);
122 fatal_sigsegv();
123 }
Jeff Dike16dd07b2007-05-06 14:51:48 -0700124 err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800125 if (err) {
126 printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
127 "errno = %d\n", pid, errno);
128 fatal_sigsegv();
129 }
Jeff Dike16dd07b2007-05-06 14:51:48 -0700130 wait_stub_done(pid);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800131
Jeff Dikeba180fd2007-10-16 01:27:00 -0700132 /*
133 * faultinfo is prepared by the stub-segv-handler at start of
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800134 * the stub stack page. We just have to copy it.
135 */
136 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
Jeff Dike2f56deb2008-02-23 15:23:49 -0800137
138 err = put_fp_registers(pid, fpregs);
139 if (err < 0) {
140 printk(UM_KERN_ERR "put_fp_registers returned %d\n",
141 err);
142 fatal_sigsegv();
143 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800144 }
145}
146
Jeff Dike77bf4402007-10-16 01:26:58 -0700147static void handle_segv(int pid, struct uml_pt_regs * regs)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800148{
Jeff Dike77bf4402007-10-16 01:26:58 -0700149 get_skas_faultinfo(pid, &regs->faultinfo);
150 segv(regs->faultinfo, 0, 1, NULL);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800151}
152
Jeff Dikeba180fd2007-10-16 01:27:00 -0700153/*
154 * To use the same value of using_sysemu as the caller, ask it that value
155 * (in local_using_sysemu
156 */
157static void handle_trap(int pid, struct uml_pt_regs *regs,
158 int local_using_sysemu)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800159{
160 int err, status;
161
Jeff Dikee06173b2008-02-04 22:31:12 -0800162 if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
163 fatal_sigsegv();
164
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800165 /* Mark this as a syscall */
Jeff Dike18baddd2007-10-16 01:27:07 -0700166 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800167
168 if (!local_using_sysemu)
169 {
170 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
171 __NR_getpid);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800172 if (err < 0) {
173 printk(UM_KERN_ERR "handle_trap - nullifying syscall "
174 "failed, errno = %d\n", errno);
175 fatal_sigsegv();
176 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800177
Jeff Dikeba180fd2007-10-16 01:27:00 -0700178 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800179 if (err < 0) {
180 printk(UM_KERN_ERR "handle_trap - continuing to end of "
181 "syscall failed, errno = %d\n", errno);
182 fatal_sigsegv();
183 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700184
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800185 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700186 if ((err < 0) || !WIFSTOPPED(status) ||
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800187 (WSTOPSIG(status) != SIGTRAP + 0x80)) {
188 err = ptrace_dump_regs(pid);
189 if (err)
190 printk(UM_KERN_ERR "Failed to get registers "
Jeff Dikeba180fd2007-10-16 01:27:00 -0700191 "from process, errno = %d\n", -err);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800192 printk(UM_KERN_ERR "handle_trap - failed to wait at "
193 "end of syscall, errno = %d, status = %d\n",
194 errno, status);
195 fatal_sigsegv();
196 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800197 }
198
199 handle_syscall(regs);
200}
201
202extern int __syscall_stub_start;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800203
204static int userspace_tramp(void *stack)
205{
206 void *addr;
Jeff Dike537ae942006-09-25 23:33:05 -0700207 int err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800208
209 ptrace(PTRACE_TRACEME, 0, 0, 0);
210
Lepton Wua24864a2007-10-16 01:27:35 -0700211 signal(SIGTERM, SIG_DFL);
Jeff Dikeee3d9bd2008-02-04 22:30:56 -0800212 signal(SIGWINCH, SIG_IGN);
Jeff Dikea2f018b2007-10-16 01:27:22 -0700213 err = set_interval();
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800214 if (err) {
215 printk(UM_KERN_ERR "userspace_tramp - setting timer failed, "
216 "errno = %d\n", err);
217 exit(1);
218 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800219
Jeff Dikeba180fd2007-10-16 01:27:00 -0700220 if (!proc_mm) {
221 /*
222 * This has a pte, but it can't be mapped in with the usual
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800223 * tlb_flush mechanism because this is part of that mechanism
224 */
Jeff Dike09ee0112006-01-18 17:42:48 -0800225 int fd;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700226 unsigned long long offset;
Jeff Dike09ee0112006-01-18 17:42:48 -0800227 fd = phys_mapping(to_phys(&__syscall_stub_start), &offset);
Jeff Dike54ae36f2007-10-16 01:27:33 -0700228 addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
Jeff Dike09ee0112006-01-18 17:42:48 -0800229 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700230 if (addr == MAP_FAILED) {
Jeff Dike54ae36f2007-10-16 01:27:33 -0700231 printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
232 "errno = %d\n", STUB_CODE, errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800233 exit(1);
234 }
235
Jeff Dikeba180fd2007-10-16 01:27:00 -0700236 if (stack != NULL) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800237 fd = phys_mapping(to_phys(stack), &offset);
Jeff Dike54ae36f2007-10-16 01:27:33 -0700238 addr = mmap((void *) STUB_DATA,
Jeff Dike1ffb9162007-05-06 14:51:22 -0700239 UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800240 MAP_FIXED | MAP_SHARED, fd, offset);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700241 if (addr == MAP_FAILED) {
242 printk(UM_KERN_ERR "mapping segfault stack "
Jeff Dike54ae36f2007-10-16 01:27:33 -0700243 "at 0x%lx failed, errno = %d\n",
244 STUB_DATA, errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800245 exit(1);
246 }
247 }
248 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700249 if (!ptrace_faultinfo && (stack != NULL)) {
Jeff Dike4b84c692006-09-25 23:33:04 -0700250 struct sigaction sa;
251
Jeff Dike54ae36f2007-10-16 01:27:33 -0700252 unsigned long v = STUB_CODE +
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800253 (unsigned long) stub_segv_handler -
254 (unsigned long) &__syscall_stub_start;
255
Jeff Dike54ae36f2007-10-16 01:27:33 -0700256 set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
Jeff Dike4b84c692006-09-25 23:33:04 -0700257 sigemptyset(&sa.sa_mask);
Al Viro9b25fcb2011-08-18 20:04:09 +0100258 sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
259 sa.sa_sigaction = (void *) v;
Jeff Dike4b84c692006-09-25 23:33:04 -0700260 sa.sa_restorer = NULL;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800261 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
262 printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
263 "handler failed - errno = %d\n", errno);
264 exit(1);
265 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800266 }
267
Jeff Dike512b6fb2007-10-16 01:27:11 -0700268 kill(os_getpid(), SIGSTOP);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700269 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800270}
271
272/* Each element set once, and only accessed by a single processor anyway */
273#undef NR_CPUS
274#define NR_CPUS 1
275int userspace_pid[NR_CPUS];
276
277int start_userspace(unsigned long stub_stack)
278{
279 void *stack;
280 unsigned long sp;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800281 int pid, status, n, flags, err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800282
Jeff Dikec539ab72007-06-16 10:16:09 -0700283 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
284 PROT_READ | PROT_WRITE | PROT_EXEC,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800285 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800286 if (stack == MAP_FAILED) {
287 err = -errno;
288 printk(UM_KERN_ERR "start_userspace : mmap failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800289 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800290 return err;
291 }
292
Jeff Dikec539ab72007-06-16 10:16:09 -0700293 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800294
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800295 flags = CLONE_FILES;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700296 if (proc_mm)
297 flags |= CLONE_VM;
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800298 else
299 flags |= SIGCHLD;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700300
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800301 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800302 if (pid < 0) {
303 err = -errno;
304 printk(UM_KERN_ERR "start_userspace : clone failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800305 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800306 return err;
307 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800308
309 do {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800310 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800311 if (n < 0) {
312 err = -errno;
313 printk(UM_KERN_ERR "start_userspace : wait failed, "
Jeff Dikeb5498832008-02-04 22:31:21 -0800314 "errno = %d\n", errno);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800315 goto out_kill;
316 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700317 } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800318
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800319 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
320 err = -EINVAL;
321 printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
Jeff Dikeb5498832008-02-04 22:31:21 -0800322 "status = %d\n", status);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800323 goto out_kill;
324 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800325
Jeff Dikeba180fd2007-10-16 01:27:00 -0700326 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800327 (void *) PTRACE_O_TRACESYSGOOD) < 0) {
328 err = -errno;
329 printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
330 "failed, errno = %d\n", errno);
331 goto out_kill;
332 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800333
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800334 if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
335 err = -errno;
336 printk(UM_KERN_ERR "start_userspace : munmap failed, "
337 "errno = %d\n", errno);
338 goto out_kill;
339 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800340
Jeff Dikeba180fd2007-10-16 01:27:00 -0700341 return pid;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800342
343 out_kill:
344 os_kill_ptraced_process(pid, 1);
345 return err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800346}
347
Jeff Dike77bf4402007-10-16 01:26:58 -0700348void userspace(struct uml_pt_regs *regs)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800349{
Jeff Diked2753a6d2007-10-16 01:27:25 -0700350 struct itimerval timer;
351 unsigned long long nsecs, now;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800352 int err, status, op, pid = userspace_pid[0];
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700353 /* To prevent races if using_sysemu changes under us.*/
354 int local_using_sysemu;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800355
Jeff Diked2753a6d2007-10-16 01:27:25 -0700356 if (getitimer(ITIMER_VIRTUAL, &timer))
Jeff Dike5134d8f2008-02-08 04:22:08 -0800357 printk(UM_KERN_ERR "Failed to get itimer, errno = %d\n", errno);
Jeff Dike1a805212007-10-16 01:27:28 -0700358 nsecs = timer.it_value.tv_sec * UM_NSEC_PER_SEC +
359 timer.it_value.tv_usec * UM_NSEC_PER_USEC;
Jeff Diked2753a6d2007-10-16 01:27:25 -0700360 nsecs += os_nsecs();
361
Jeff Dikeba180fd2007-10-16 01:27:00 -0700362 while (1) {
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800363 /*
364 * This can legitimately fail if the process loads a
365 * bogus value into a segment register. It will
366 * segfault and PTRACE_GETREGS will read that value
367 * out of the process. However, PTRACE_SETREGS will
368 * fail. In this case, there is nothing to do but
369 * just kill the process.
370 */
Jeff Diked25f2e12008-02-04 22:30:57 -0800371 if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp))
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800372 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800373
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700374 if (put_fp_registers(pid, regs->fp))
375 fatal_sigsegv();
376
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800377 /* Now we set local_using_sysemu to be used for one loop */
378 local_using_sysemu = get_using_sysemu();
379
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700380 op = SELECT_PTRACE_OPERATION(local_using_sysemu,
381 singlestepping(NULL));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800382
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800383 if (ptrace(op, pid, 0, 0)) {
384 printk(UM_KERN_ERR "userspace - ptrace continue "
385 "failed, op = %d, errno = %d\n", op, errno);
386 fatal_sigsegv();
387 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800388
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800389 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800390 if (err < 0) {
391 printk(UM_KERN_ERR "userspace - wait failed, "
392 "errno = %d\n", errno);
393 fatal_sigsegv();
394 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800395
Jeff Dike77bf4402007-10-16 01:26:58 -0700396 regs->is_user = 1;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800397 if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
398 printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
399 "errno = %d\n", errno);
400 fatal_sigsegv();
401 }
Jeff Diked25f2e12008-02-04 22:30:57 -0800402
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700403 if (get_fp_registers(pid, regs->fp)) {
404 printk(UM_KERN_ERR "userspace - get_fp_registers failed, "
405 "errno = %d\n", errno);
406 fatal_sigsegv();
407 }
408
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800409 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
410
Jeff Dikeba180fd2007-10-16 01:27:00 -0700411 if (WIFSTOPPED(status)) {
Jeff Dike16dd07b2007-05-06 14:51:48 -0700412 int sig = WSTOPSIG(status);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800413 switch (sig) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800414 case SIGSEGV:
Jeff Dikeba180fd2007-10-16 01:27:00 -0700415 if (PTRACE_FULL_FAULTINFO ||
416 !ptrace_faultinfo) {
417 get_skas_faultinfo(pid,
418 &regs->faultinfo);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700419 (*sig_info[SIGSEGV])(SIGSEGV, regs);
420 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800421 else handle_segv(pid, regs);
422 break;
423 case SIGTRAP + 0x80:
424 handle_trap(pid, regs, local_using_sysemu);
425 break;
426 case SIGTRAP:
427 relay_signal(SIGTRAP, regs);
428 break;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800429 case SIGVTALRM:
Jeff Diked2753a6d2007-10-16 01:27:25 -0700430 now = os_nsecs();
Jeff Diked25f2e12008-02-04 22:30:57 -0800431 if (now < nsecs)
Jeff Diked2753a6d2007-10-16 01:27:25 -0700432 break;
433 block_signals();
434 (*sig_info[sig])(sig, regs);
435 unblock_signals();
Jeff Dike1a805212007-10-16 01:27:28 -0700436 nsecs = timer.it_value.tv_sec *
437 UM_NSEC_PER_SEC +
438 timer.it_value.tv_usec *
439 UM_NSEC_PER_USEC;
Jeff Diked2753a6d2007-10-16 01:27:25 -0700440 nsecs += os_nsecs();
441 break;
442 case SIGIO:
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800443 case SIGILL:
444 case SIGBUS:
445 case SIGFPE:
446 case SIGWINCH:
Jeff Dike16dd07b2007-05-06 14:51:48 -0700447 block_signals();
448 (*sig_info[sig])(sig, regs);
449 unblock_signals();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800450 break;
451 default:
Jeff Dike96cee302008-05-12 14:01:48 -0700452 printk(UM_KERN_ERR "userspace - child stopped "
Jeff Dikeba180fd2007-10-16 01:27:00 -0700453 "with signal %d\n", sig);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800454 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800455 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800456 pid = userspace_pid[0];
457 interrupt_end();
458
459 /* Avoid -ERESTARTSYS handling in host */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700460 if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
Jeff Dike18baddd2007-10-16 01:27:07 -0700461 PT_SYSCALL_NR(regs->gp) = -1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800462 }
463 }
464}
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800465
Jeff Dike16dd07b2007-05-06 14:51:48 -0700466static unsigned long thread_regs[MAX_REG_NR];
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700467static unsigned long thread_fp_regs[FP_SIZE];
Jeff Dike16dd07b2007-05-06 14:51:48 -0700468
469static int __init init_thread_regs(void)
470{
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700471 get_safe_registers(thread_regs, thread_fp_regs);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700472 /* Set parent's instruction pointer to start of clone-stub */
Jeff Dike54ae36f2007-10-16 01:27:33 -0700473 thread_regs[REGS_IP_INDEX] = STUB_CODE +
Jeff Dike16dd07b2007-05-06 14:51:48 -0700474 (unsigned long) stub_clone_handler -
475 (unsigned long) &__syscall_stub_start;
Jeff Dike54ae36f2007-10-16 01:27:33 -0700476 thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
Jeff Dike16dd07b2007-05-06 14:51:48 -0700477 sizeof(void *);
478#ifdef __SIGNAL_FRAMESIZE
479 thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
480#endif
481 return 0;
482}
483
484__initcall(init_thread_regs);
485
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800486int copy_context_skas0(unsigned long new_stack, int pid)
487{
Jeff Dike1a805212007-10-16 01:27:28 -0700488 struct timeval tv = { .tv_sec = 0, .tv_usec = UM_USEC_PER_SEC / UM_HZ };
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800489 int err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800490 unsigned long current_stack = current_stub_stack();
491 struct stub_data *data = (struct stub_data *) current_stack;
492 struct stub_data *child_data = (struct stub_data *) new_stack;
Jeff Dike0a7675a2007-10-16 01:27:05 -0700493 unsigned long long new_offset;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800494 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
495
Jeff Dikeba180fd2007-10-16 01:27:00 -0700496 /*
497 * prepare offset and fd of child's stack as argument for parent's
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800498 * and child's mmap2 calls
499 */
500 *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset),
501 .fd = new_fd,
502 .timer = ((struct itimerval)
Jeff Diked2753a6d2007-10-16 01:27:25 -0700503 { .it_value = tv,
504 .it_interval = tv }) });
505
Jeff Dike16dd07b2007-05-06 14:51:48 -0700506 err = ptrace_setregs(pid, thread_regs);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800507 if (err < 0) {
508 err = -errno;
509 printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
510 "failed, pid = %d, errno = %d\n", pid, -err);
511 return err;
512 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800513
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700514 err = put_fp_registers(pid, thread_fp_regs);
515 if (err < 0) {
516 printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
517 "failed, pid = %d, err = %d\n", pid, err);
518 return err;
519 }
520
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800521 /* set a well known return code for detection of child write failure */
522 child_data->err = 12345678;
523
Jeff Dikeba180fd2007-10-16 01:27:00 -0700524 /*
525 * Wait, until parent has finished its work: read child's pid from
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800526 * parent's stack, and check, if bad result.
527 */
Jeff Dike16dd07b2007-05-06 14:51:48 -0700528 err = ptrace(PTRACE_CONT, pid, 0, 0);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800529 if (err) {
530 err = -errno;
531 printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
532 "errno = %d\n", pid, errno);
533 return err;
534 }
535
Jeff Dike16dd07b2007-05-06 14:51:48 -0700536 wait_stub_done(pid);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800537
538 pid = data->err;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800539 if (pid < 0) {
540 printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
541 "error %d\n", -pid);
542 return pid;
543 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800544
Jeff Dikeba180fd2007-10-16 01:27:00 -0700545 /*
546 * Wait, until child has finished too: read child's result from
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800547 * child's stack and check it.
548 */
Jeff Dike16dd07b2007-05-06 14:51:48 -0700549 wait_stub_done(pid);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800550 if (child_data->err != STUB_DATA) {
551 printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports "
552 "error %ld\n", child_data->err);
553 err = child_data->err;
554 goto out_kill;
555 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800556
557 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800558 (void *)PTRACE_O_TRACESYSGOOD) < 0) {
559 err = -errno;
560 printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
561 "failed, errno = %d\n", errno);
562 goto out_kill;
563 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800564
565 return pid;
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800566
567 out_kill:
568 os_kill_ptraced_process(pid, 1);
569 return err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800570}
571
572/*
573 * This is used only, if stub pages are needed, while proc_mm is
Jeff Dikeef0470c2007-05-06 14:51:33 -0700574 * available. Opening /proc/mm creates a new mm_context, which lacks
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800575 * the stub-pages. Thus, we map them using /proc/mm-fd
576 */
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800577int map_stub_pages(int fd, unsigned long code, unsigned long data,
578 unsigned long stack)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800579{
580 struct proc_mm_op mmop;
581 int n;
Jeff Dike0a7675a2007-10-16 01:27:05 -0700582 unsigned long long code_offset;
Jeff Dike09ee0112006-01-18 17:42:48 -0800583 int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start),
584 &code_offset);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800585
586 mmop = ((struct proc_mm_op) { .op = MM_MMAP,
587 .u =
588 { .mmap =
589 { .addr = code,
Jeff Dikec539ab72007-06-16 10:16:09 -0700590 .len = UM_KERN_PAGE_SIZE,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800591 .prot = PROT_EXEC,
592 .flags = MAP_FIXED | MAP_PRIVATE,
Jeff Dike09ee0112006-01-18 17:42:48 -0800593 .fd = code_fd,
594 .offset = code_offset
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800595 } } });
Jeff Dikea61f3342007-05-06 14:51:35 -0700596 CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700597 if (n != sizeof(mmop)) {
Jeff Dikea61f3342007-05-06 14:51:35 -0700598 n = errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700599 printk(UM_KERN_ERR "mmap args - addr = 0x%lx, fd = %d, "
600 "offset = %llx\n", code, code_fd,
601 (unsigned long long) code_offset);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800602 printk(UM_KERN_ERR "map_stub_pages : /proc/mm map for code "
603 "failed, err = %d\n", n);
604 return -n;
Jeff Dikeb4cf95c2007-03-06 01:42:19 -0800605 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800606
Jeff Dikeba180fd2007-10-16 01:27:00 -0700607 if (stack) {
Jeff Dike0a7675a2007-10-16 01:27:05 -0700608 unsigned long long map_offset;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800609 int map_fd = phys_mapping(to_phys((void *)stack), &map_offset);
610 mmop = ((struct proc_mm_op)
611 { .op = MM_MMAP,
612 .u =
613 { .mmap =
614 { .addr = data,
Jeff Dikec539ab72007-06-16 10:16:09 -0700615 .len = UM_KERN_PAGE_SIZE,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800616 .prot = PROT_READ | PROT_WRITE,
617 .flags = MAP_FIXED | MAP_SHARED,
618 .fd = map_fd,
619 .offset = map_offset
620 } } });
Jeff Dikea61f3342007-05-06 14:51:35 -0700621 CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800622 if (n != sizeof(mmop)) {
623 n = errno;
624 printk(UM_KERN_ERR "map_stub_pages : /proc/mm map for "
625 "data failed, err = %d\n", n);
626 return -n;
627 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800628 }
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800629
630 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800631}
632
Jeff Dike3c917352006-09-27 01:50:40 -0700633void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800634{
Jeff Dike3c917352006-09-27 01:50:40 -0700635 (*buf)[0].JB_IP = (unsigned long) handler;
Jeff Dikee1a79c42007-05-10 22:22:31 -0700636 (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
637 sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800638}
639
Jeff Dikee2216fe2006-02-07 12:58:43 -0800640#define INIT_JMP_NEW_THREAD 0
Jeff Dike3c917352006-09-27 01:50:40 -0700641#define INIT_JMP_CALLBACK 1
642#define INIT_JMP_HALT 2
643#define INIT_JMP_REBOOT 3
Jeff Dikee2216fe2006-02-07 12:58:43 -0800644
Jeff Dike3c917352006-09-27 01:50:40 -0700645void switch_threads(jmp_buf *me, jmp_buf *you)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800646{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700647 if (UML_SETJMP(me) == 0)
Jeff Dike3c917352006-09-27 01:50:40 -0700648 UML_LONGJMP(you, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800649}
650
Jeff Dikead28e022006-04-18 22:21:41 -0700651static jmp_buf initial_jmpbuf;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800652
653/* XXX Make these percpu */
654static void (*cb_proc)(void *arg);
655static void *cb_arg;
Jeff Dikead28e022006-04-18 22:21:41 -0700656static jmp_buf *cb_back;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800657
Jeff Dike3c917352006-09-27 01:50:40 -0700658int start_idle_thread(void *stack, jmp_buf *switch_buf)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800659{
Jeff Dikea5df0d12006-07-14 00:24:02 -0700660 int n;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800661
Al Viroe87df982011-08-18 20:04:29 +0100662 set_handler(SIGWINCH, (__sighandler_t) sig_handler);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800663
Jeff Dike77f6af72007-05-06 14:51:40 -0700664 /*
665 * Can't use UML_SETJMP or UML_LONGJMP here because they save
666 * and restore signals, with the possible side-effect of
667 * trying to handle any signals which came when they were
668 * blocked, which can't be done on this stack.
669 * Signals must be blocked when jumping back here and restored
670 * after returning to the jumper.
671 */
672 n = setjmp(initial_jmpbuf);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800673 switch (n) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800674 case INIT_JMP_NEW_THREAD:
Jeff Dike3c917352006-09-27 01:50:40 -0700675 (*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler;
676 (*switch_buf)[0].JB_SP = (unsigned long) stack +
Jeff Dikee1a79c42007-05-10 22:22:31 -0700677 UM_THREAD_SIZE - sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800678 break;
679 case INIT_JMP_CALLBACK:
680 (*cb_proc)(cb_arg);
Jeff Dike77f6af72007-05-06 14:51:40 -0700681 longjmp(*cb_back, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800682 break;
683 case INIT_JMP_HALT:
684 kmalloc_ok = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700685 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800686 case INIT_JMP_REBOOT:
687 kmalloc_ok = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700688 return 1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800689 default:
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800690 printk(UM_KERN_ERR "Bad sigsetjmp return in "
691 "start_idle_thread - %d\n", n);
692 fatal_sigsegv();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800693 }
Jeff Dike77f6af72007-05-06 14:51:40 -0700694 longjmp(*switch_buf, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800695}
696
697void initial_thread_cb_skas(void (*proc)(void *), void *arg)
698{
Jeff Dikead28e022006-04-18 22:21:41 -0700699 jmp_buf here;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800700
701 cb_proc = proc;
702 cb_arg = arg;
703 cb_back = &here;
704
705 block_signals();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700706 if (UML_SETJMP(&here) == 0)
Jeff Dikead28e022006-04-18 22:21:41 -0700707 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800708 unblock_signals();
709
710 cb_proc = NULL;
711 cb_arg = NULL;
712 cb_back = NULL;
713}
714
715void halt_skas(void)
716{
717 block_signals();
Jeff Dikead28e022006-04-18 22:21:41 -0700718 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800719}
720
721void reboot_skas(void)
722{
723 block_signals();
Jeff Dikead28e022006-04-18 22:21:41 -0700724 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800725}
726
Jeff Dike77bf4402007-10-16 01:26:58 -0700727void __switch_mm(struct mm_id *mm_idp)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800728{
729 int err;
730
Jeff Dike77bf4402007-10-16 01:26:58 -0700731 /* FIXME: need cpu pid in __switch_mm */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700732 if (proc_mm) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800733 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
734 mm_idp->u.mm_fd);
Jeff Dike3e6f2ac2008-02-04 22:30:58 -0800735 if (err) {
736 printk(UM_KERN_ERR "__switch_mm - PTRACE_SWITCH_MM "
737 "failed, errno = %d\n", errno);
738 fatal_sigsegv();
739 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800740 }
741 else userspace_pid[0] = mm_idp->u.pid;
742}