blob: 8548f126d6287c0af594469644dd4109cce9069e [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"
17#include "kern_constants.h"
18#include "mem.h"
19#include "os.h"
20#include "process.h"
21#include "proc_mm.h"
22#include "ptrace_user.h"
23#include "registers.h"
24#include "skas.h"
25#include "skas_ptrace.h"
26#include "user.h"
27#include "sysdep/stub.h"
Gennady Sharapovabaf6972006-01-18 17:42:46 -080028
29int is_skas_winch(int pid, int fd, void *data)
30{
Jeff Dike512b6fb2007-10-16 01:27:11 -070031 if (pid != getpgrp())
Jeff Dikeba180fd2007-10-16 01:27:00 -070032 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -080033
Jeff Dike42a359e2007-07-15 23:38:55 -070034 register_winch_irq(-1, fd, -1, data, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070035 return 1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -080036}
37
Jeff Dikef30c2c92007-05-06 14:51:29 -070038static int ptrace_dump_regs(int pid)
39{
40 unsigned long regs[MAX_REG_NR];
41 int i;
42
Jeff Dikeba180fd2007-10-16 01:27:00 -070043 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
Jeff Dikef30c2c92007-05-06 14:51:29 -070044 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -070045
46 printk(UM_KERN_ERR "Stub registers -\n");
47 for (i = 0; i < ARRAY_SIZE(regs); i++)
48 printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]);
Jeff Dikef30c2c92007-05-06 14:51:29 -070049
50 return 0;
51}
52
Jeff Dike16dd07b2007-05-06 14:51:48 -070053/*
54 * Signals that are OK to receive in the stub - we'll just continue it.
55 * SIGWINCH will happen when UML is inside a detached screen.
56 */
57#define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH))
58
59/* Signals that the stub will finish with - anything else is an error */
60#define STUB_DONE_MASK ((1 << SIGUSR1) | (1 << SIGTRAP))
61
62void wait_stub_done(int pid)
Gennady Sharapovabaf6972006-01-18 17:42:46 -080063{
64 int n, status, err;
65
Jeff Dikeba180fd2007-10-16 01:27:00 -070066 while (1) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -080067 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -070068 if ((n < 0) || !WIFSTOPPED(status))
Jeff Dike16dd07b2007-05-06 14:51:48 -070069 goto bad_wait;
Gennady Sharapovabaf6972006-01-18 17:42:46 -080070
Jeff Dikeba180fd2007-10-16 01:27:00 -070071 if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
Jeff Dike16dd07b2007-05-06 14:51:48 -070072 break;
73
74 err = ptrace(PTRACE_CONT, pid, 0, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -070075 if (err)
Jeff Dike16dd07b2007-05-06 14:51:48 -070076 panic("wait_stub_done : continue failed, errno = %d\n",
77 errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -080078 }
Jeff Dike16dd07b2007-05-06 14:51:48 -070079
Jeff Dikeba180fd2007-10-16 01:27:00 -070080 if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
Jeff Dike16dd07b2007-05-06 14:51:48 -070081 return;
82
83bad_wait:
84 err = ptrace_dump_regs(pid);
Jeff Dikeba180fd2007-10-16 01:27:00 -070085 if (err)
86 printk(UM_KERN_ERR "Failed to get registers from stub, "
87 "errno = %d\n", -err);
Jeff Dike16dd07b2007-05-06 14:51:48 -070088 panic("wait_stub_done : failed to wait for SIGUSR1/SIGTRAP, pid = %d, "
89 "n = %d, errno = %d, status = 0x%x\n", pid, n, errno, status);
Gennady Sharapovabaf6972006-01-18 17:42:46 -080090}
91
92extern unsigned long current_stub_stack(void);
93
94void get_skas_faultinfo(int pid, struct faultinfo * fi)
95{
96 int err;
97
Jeff Dikeba180fd2007-10-16 01:27:00 -070098 if (ptrace_faultinfo) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -080099 err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700100 if (err)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800101 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
102 "errno = %d\n", errno);
103
104 /* Special handling for i386, which has different structs */
105 if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo))
106 memset((char *)fi + sizeof(struct ptrace_faultinfo), 0,
107 sizeof(struct faultinfo) -
108 sizeof(struct ptrace_faultinfo));
109 }
110 else {
Jeff Dike16dd07b2007-05-06 14:51:48 -0700111 err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700112 if (err)
Jeff Dike16dd07b2007-05-06 14:51:48 -0700113 panic("Failed to continue stub, pid = %d, errno = %d\n",
114 pid, errno);
115 wait_stub_done(pid);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800116
Jeff Dikeba180fd2007-10-16 01:27:00 -0700117 /*
118 * faultinfo is prepared by the stub-segv-handler at start of
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800119 * the stub stack page. We just have to copy it.
120 */
121 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
122 }
123}
124
Jeff Dike77bf4402007-10-16 01:26:58 -0700125static void handle_segv(int pid, struct uml_pt_regs * regs)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800126{
Jeff Dike77bf4402007-10-16 01:26:58 -0700127 get_skas_faultinfo(pid, &regs->faultinfo);
128 segv(regs->faultinfo, 0, 1, NULL);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800129}
130
Jeff Dikeba180fd2007-10-16 01:27:00 -0700131/*
132 * To use the same value of using_sysemu as the caller, ask it that value
133 * (in local_using_sysemu
134 */
135static void handle_trap(int pid, struct uml_pt_regs *regs,
136 int local_using_sysemu)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800137{
138 int err, status;
139
140 /* Mark this as a syscall */
Jeff Dike18baddd2007-10-16 01:27:07 -0700141 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800142
143 if (!local_using_sysemu)
144 {
145 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
146 __NR_getpid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700147 if (err < 0)
148 panic("handle_trap - nullifying syscall failed, "
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800149 "errno = %d\n", errno);
150
Jeff Dikeba180fd2007-10-16 01:27:00 -0700151 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
152 if (err < 0)
153 panic("handle_trap - continuing to end of syscall "
154 "failed, errno = %d\n", errno);
155
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800156 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700157 if ((err < 0) || !WIFSTOPPED(status) ||
158 (WSTOPSIG(status) != SIGTRAP + 0x80)) {
Jeff Dikef30c2c92007-05-06 14:51:29 -0700159 err = ptrace_dump_regs(pid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700160 if (err)
161 printk(UM_KERN_ERR "Failed to get registers "
162 "from process, errno = %d\n", -err);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800163 panic("handle_trap - failed to wait at end of syscall, "
164 "errno = %d, status = %d\n", errno, status);
Jeff Dikef30c2c92007-05-06 14:51:29 -0700165 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800166 }
167
168 handle_syscall(regs);
169}
170
171extern int __syscall_stub_start;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800172
173static int userspace_tramp(void *stack)
174{
175 void *addr;
Jeff Dike537ae942006-09-25 23:33:05 -0700176 int err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800177
178 ptrace(PTRACE_TRACEME, 0, 0, 0);
179
Jeff Dikee64bd132006-07-10 04:45:07 -0700180 init_new_thread_signals();
Jeff Dikea2f018b2007-10-16 01:27:22 -0700181 err = set_interval();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700182 if (err)
Jeff Dike537ae942006-09-25 23:33:05 -0700183 panic("userspace_tramp - setting timer failed, errno = %d\n",
184 err);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800185
Jeff Dikeba180fd2007-10-16 01:27:00 -0700186 if (!proc_mm) {
187 /*
188 * This has a pte, but it can't be mapped in with the usual
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800189 * tlb_flush mechanism because this is part of that mechanism
190 */
Jeff Dike09ee0112006-01-18 17:42:48 -0800191 int fd;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700192 unsigned long long offset;
Jeff Dike09ee0112006-01-18 17:42:48 -0800193 fd = phys_mapping(to_phys(&__syscall_stub_start), &offset);
Jeff Dike1ffb9162007-05-06 14:51:22 -0700194 addr = mmap64((void *) UML_CONFIG_STUB_CODE, UM_KERN_PAGE_SIZE,
Jeff Dike09ee0112006-01-18 17:42:48 -0800195 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700196 if (addr == MAP_FAILED) {
197 printk(UM_KERN_ERR "mapping mmap stub failed, "
198 "errno = %d\n", errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800199 exit(1);
200 }
201
Jeff Dikeba180fd2007-10-16 01:27:00 -0700202 if (stack != NULL) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800203 fd = phys_mapping(to_phys(stack), &offset);
Jeff Dike1ffb9162007-05-06 14:51:22 -0700204 addr = mmap((void *) UML_CONFIG_STUB_DATA,
205 UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800206 MAP_FIXED | MAP_SHARED, fd, offset);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700207 if (addr == MAP_FAILED) {
208 printk(UM_KERN_ERR "mapping segfault stack "
209 "failed, errno = %d\n", errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800210 exit(1);
211 }
212 }
213 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700214 if (!ptrace_faultinfo && (stack != NULL)) {
Jeff Dike4b84c692006-09-25 23:33:04 -0700215 struct sigaction sa;
216
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800217 unsigned long v = UML_CONFIG_STUB_CODE +
218 (unsigned long) stub_segv_handler -
219 (unsigned long) &__syscall_stub_start;
220
Jeff Dike1ffb9162007-05-06 14:51:22 -0700221 set_sigstack((void *) UML_CONFIG_STUB_DATA, UM_KERN_PAGE_SIZE);
Jeff Dike4b84c692006-09-25 23:33:04 -0700222 sigemptyset(&sa.sa_mask);
223 sigaddset(&sa.sa_mask, SIGIO);
224 sigaddset(&sa.sa_mask, SIGWINCH);
Jeff Dike4b84c692006-09-25 23:33:04 -0700225 sigaddset(&sa.sa_mask, SIGVTALRM);
226 sigaddset(&sa.sa_mask, SIGUSR1);
227 sa.sa_flags = SA_ONSTACK;
228 sa.sa_handler = (void *) v;
229 sa.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700230 if (sigaction(SIGSEGV, &sa, NULL) < 0)
Jeff Dike4b84c692006-09-25 23:33:04 -0700231 panic("userspace_tramp - setting SIGSEGV handler "
232 "failed - errno = %d\n", errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800233 }
234
Jeff Dike512b6fb2007-10-16 01:27:11 -0700235 kill(os_getpid(), SIGSTOP);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700236 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800237}
238
239/* Each element set once, and only accessed by a single processor anyway */
240#undef NR_CPUS
241#define NR_CPUS 1
242int userspace_pid[NR_CPUS];
243
244int start_userspace(unsigned long stub_stack)
245{
246 void *stack;
247 unsigned long sp;
248 int pid, status, n, flags;
249
Jeff Dikec539ab72007-06-16 10:16:09 -0700250 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
251 PROT_READ | PROT_WRITE | PROT_EXEC,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800252 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700253 if (stack == MAP_FAILED)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800254 panic("start_userspace : mmap failed, errno = %d", errno);
Jeff Dikec539ab72007-06-16 10:16:09 -0700255 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800256
257 flags = CLONE_FILES | SIGCHLD;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700258 if (proc_mm)
259 flags |= CLONE_VM;
260
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800261 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700262 if (pid < 0)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800263 panic("start_userspace : clone failed, errno = %d", errno);
264
265 do {
266 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700267 if (n < 0)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800268 panic("start_userspace : wait failed, errno = %d",
269 errno);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700270 } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800271
Jeff Dikeba180fd2007-10-16 01:27:00 -0700272 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800273 panic("start_userspace : expected SIGSTOP, got status = %d",
274 status);
275
Jeff Dikeba180fd2007-10-16 01:27:00 -0700276 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
277 (void *) PTRACE_O_TRACESYSGOOD) < 0)
278 panic("start_userspace : PTRACE_OLDSETOPTIONS failed, "
279 "errno = %d\n", errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800280
Jeff Dikeba180fd2007-10-16 01:27:00 -0700281 if (munmap(stack, UM_KERN_PAGE_SIZE) < 0)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800282 panic("start_userspace : munmap failed, errno = %d\n", errno);
283
Jeff Dikeba180fd2007-10-16 01:27:00 -0700284 return pid;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800285}
286
Jeff Dike77bf4402007-10-16 01:26:58 -0700287void userspace(struct uml_pt_regs *regs)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800288{
Jeff Diked2753a6d2007-10-16 01:27:25 -0700289 struct itimerval timer;
290 unsigned long long nsecs, now;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800291 int err, status, op, pid = userspace_pid[0];
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700292 /* To prevent races if using_sysemu changes under us.*/
293 int local_using_sysemu;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800294
Jeff Diked2753a6d2007-10-16 01:27:25 -0700295 if (getitimer(ITIMER_VIRTUAL, &timer))
296 printk("Failed to get itimer, errno = %d\n", errno);
Jeff Dike1a805212007-10-16 01:27:28 -0700297 nsecs = timer.it_value.tv_sec * UM_NSEC_PER_SEC +
298 timer.it_value.tv_usec * UM_NSEC_PER_USEC;
Jeff Diked2753a6d2007-10-16 01:27:25 -0700299 nsecs += os_nsecs();
300
Jeff Dikeba180fd2007-10-16 01:27:00 -0700301 while (1) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800302 restore_registers(pid, regs);
303
304 /* Now we set local_using_sysemu to be used for one loop */
305 local_using_sysemu = get_using_sysemu();
306
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700307 op = SELECT_PTRACE_OPERATION(local_using_sysemu,
308 singlestepping(NULL));
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800309
310 err = ptrace(op, pid, 0, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700311 if (err)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800312 panic("userspace - could not resume userspace process, "
313 "pid=%d, ptrace operation = %d, errno = %d\n",
Paolo 'Blaisorblade' Giarrusso802e3072006-04-10 22:53:32 -0700314 pid, op, errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800315
316 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700317 if (err < 0)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800318 panic("userspace - waitpid failed, errno = %d\n",
319 errno);
320
Jeff Dike77bf4402007-10-16 01:26:58 -0700321 regs->is_user = 1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800322 save_registers(pid, regs);
323 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
324
Jeff Dikeba180fd2007-10-16 01:27:00 -0700325 if (WIFSTOPPED(status)) {
Jeff Dike16dd07b2007-05-06 14:51:48 -0700326 int sig = WSTOPSIG(status);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700327 switch(sig) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800328 case SIGSEGV:
Jeff Dikeba180fd2007-10-16 01:27:00 -0700329 if (PTRACE_FULL_FAULTINFO ||
330 !ptrace_faultinfo) {
331 get_skas_faultinfo(pid,
332 &regs->faultinfo);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700333 (*sig_info[SIGSEGV])(SIGSEGV, regs);
334 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800335 else handle_segv(pid, regs);
336 break;
337 case SIGTRAP + 0x80:
338 handle_trap(pid, regs, local_using_sysemu);
339 break;
340 case SIGTRAP:
341 relay_signal(SIGTRAP, regs);
342 break;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800343 case SIGVTALRM:
Jeff Diked2753a6d2007-10-16 01:27:25 -0700344 now = os_nsecs();
345 if(now < nsecs)
346 break;
347 block_signals();
348 (*sig_info[sig])(sig, regs);
349 unblock_signals();
Jeff Dike1a805212007-10-16 01:27:28 -0700350 nsecs = timer.it_value.tv_sec *
351 UM_NSEC_PER_SEC +
352 timer.it_value.tv_usec *
353 UM_NSEC_PER_USEC;
Jeff Diked2753a6d2007-10-16 01:27:25 -0700354 nsecs += os_nsecs();
355 break;
356 case SIGIO:
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800357 case SIGILL:
358 case SIGBUS:
359 case SIGFPE:
360 case SIGWINCH:
Jeff Dike16dd07b2007-05-06 14:51:48 -0700361 block_signals();
362 (*sig_info[sig])(sig, regs);
363 unblock_signals();
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800364 break;
365 default:
Jeff Dikeba180fd2007-10-16 01:27:00 -0700366 printk(UM_KERN_ERR "userspace - child stopped "
367 "with signal %d\n", sig);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800368 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800369 pid = userspace_pid[0];
370 interrupt_end();
371
372 /* Avoid -ERESTARTSYS handling in host */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700373 if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
Jeff Dike18baddd2007-10-16 01:27:07 -0700374 PT_SYSCALL_NR(regs->gp) = -1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800375 }
376 }
377}
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800378
Jeff Dike16dd07b2007-05-06 14:51:48 -0700379static unsigned long thread_regs[MAX_REG_NR];
Jeff Dike16dd07b2007-05-06 14:51:48 -0700380
381static int __init init_thread_regs(void)
382{
Jeff Dike42daba32007-10-16 01:26:47 -0700383 get_safe_registers(thread_regs);
Jeff Dike16dd07b2007-05-06 14:51:48 -0700384 /* Set parent's instruction pointer to start of clone-stub */
385 thread_regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
386 (unsigned long) stub_clone_handler -
387 (unsigned long) &__syscall_stub_start;
Jeff Dikec539ab72007-06-16 10:16:09 -0700388 thread_regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + UM_KERN_PAGE_SIZE -
Jeff Dike16dd07b2007-05-06 14:51:48 -0700389 sizeof(void *);
390#ifdef __SIGNAL_FRAMESIZE
391 thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
392#endif
393 return 0;
394}
395
396__initcall(init_thread_regs);
397
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800398int copy_context_skas0(unsigned long new_stack, int pid)
399{
Jeff Dike1a805212007-10-16 01:27:28 -0700400 struct timeval tv = { .tv_sec = 0, .tv_usec = UM_USEC_PER_SEC / UM_HZ };
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800401 int err;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800402 unsigned long current_stack = current_stub_stack();
403 struct stub_data *data = (struct stub_data *) current_stack;
404 struct stub_data *child_data = (struct stub_data *) new_stack;
Jeff Dike0a7675a2007-10-16 01:27:05 -0700405 unsigned long long new_offset;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800406 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
407
Jeff Dikeba180fd2007-10-16 01:27:00 -0700408 /*
409 * prepare offset and fd of child's stack as argument for parent's
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800410 * and child's mmap2 calls
411 */
412 *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset),
413 .fd = new_fd,
414 .timer = ((struct itimerval)
Jeff Diked2753a6d2007-10-16 01:27:25 -0700415 { .it_value = tv,
416 .it_interval = tv }) });
417
Jeff Dike16dd07b2007-05-06 14:51:48 -0700418 err = ptrace_setregs(pid, thread_regs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700419 if (err < 0)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800420 panic("copy_context_skas0 : PTRACE_SETREGS failed, "
Jeff Dike2ace87b2006-05-01 12:16:00 -0700421 "pid = %d, errno = %d\n", pid, -err);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800422
423 /* set a well known return code for detection of child write failure */
424 child_data->err = 12345678;
425
Jeff Dikeba180fd2007-10-16 01:27:00 -0700426 /*
427 * Wait, until parent has finished its work: read child's pid from
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800428 * parent's stack, and check, if bad result.
429 */
Jeff Dike16dd07b2007-05-06 14:51:48 -0700430 err = ptrace(PTRACE_CONT, pid, 0, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700431 if (err)
Jeff Dike16dd07b2007-05-06 14:51:48 -0700432 panic("Failed to continue new process, pid = %d, "
433 "errno = %d\n", pid, errno);
434 wait_stub_done(pid);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800435
436 pid = data->err;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700437 if (pid < 0)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800438 panic("copy_context_skas0 - stub-parent reports error %d\n",
Jeff Dike2ace87b2006-05-01 12:16:00 -0700439 -pid);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800440
Jeff Dikeba180fd2007-10-16 01:27:00 -0700441 /*
442 * Wait, until child has finished too: read child's result from
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800443 * child's stack and check it.
444 */
Jeff Dike16dd07b2007-05-06 14:51:48 -0700445 wait_stub_done(pid);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800446 if (child_data->err != UML_CONFIG_STUB_DATA)
Paolo 'Blaisorblade' Giarrusso802e3072006-04-10 22:53:32 -0700447 panic("copy_context_skas0 - stub-child reports error %ld\n",
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800448 child_data->err);
449
450 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
451 (void *)PTRACE_O_TRACESYSGOOD) < 0)
452 panic("copy_context_skas0 : PTRACE_OLDSETOPTIONS failed, "
453 "errno = %d\n", errno);
454
455 return pid;
456}
457
458/*
459 * This is used only, if stub pages are needed, while proc_mm is
Jeff Dikeef0470c2007-05-06 14:51:33 -0700460 * available. Opening /proc/mm creates a new mm_context, which lacks
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800461 * the stub-pages. Thus, we map them using /proc/mm-fd
462 */
463void map_stub_pages(int fd, unsigned long code,
464 unsigned long data, unsigned long stack)
465{
466 struct proc_mm_op mmop;
467 int n;
Jeff Dike0a7675a2007-10-16 01:27:05 -0700468 unsigned long long code_offset;
Jeff Dike09ee0112006-01-18 17:42:48 -0800469 int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start),
470 &code_offset);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800471
472 mmop = ((struct proc_mm_op) { .op = MM_MMAP,
473 .u =
474 { .mmap =
475 { .addr = code,
Jeff Dikec539ab72007-06-16 10:16:09 -0700476 .len = UM_KERN_PAGE_SIZE,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800477 .prot = PROT_EXEC,
478 .flags = MAP_FIXED | MAP_PRIVATE,
Jeff Dike09ee0112006-01-18 17:42:48 -0800479 .fd = code_fd,
480 .offset = code_offset
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800481 } } });
Jeff Dikea61f3342007-05-06 14:51:35 -0700482 CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700483 if (n != sizeof(mmop)) {
Jeff Dikea61f3342007-05-06 14:51:35 -0700484 n = errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700485 printk(UM_KERN_ERR "mmap args - addr = 0x%lx, fd = %d, "
486 "offset = %llx\n", code, code_fd,
487 (unsigned long long) code_offset);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800488 panic("map_stub_pages : /proc/mm map for code failed, "
Jeff Dikea61f3342007-05-06 14:51:35 -0700489 "err = %d\n", n);
Jeff Dikeb4cf95c2007-03-06 01:42:19 -0800490 }
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800491
Jeff Dikeba180fd2007-10-16 01:27:00 -0700492 if (stack) {
Jeff Dike0a7675a2007-10-16 01:27:05 -0700493 unsigned long long map_offset;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800494 int map_fd = phys_mapping(to_phys((void *)stack), &map_offset);
495 mmop = ((struct proc_mm_op)
496 { .op = MM_MMAP,
497 .u =
498 { .mmap =
499 { .addr = data,
Jeff Dikec539ab72007-06-16 10:16:09 -0700500 .len = UM_KERN_PAGE_SIZE,
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800501 .prot = PROT_READ | PROT_WRITE,
502 .flags = MAP_FIXED | MAP_SHARED,
503 .fd = map_fd,
504 .offset = map_offset
505 } } });
Jeff Dikea61f3342007-05-06 14:51:35 -0700506 CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop)));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700507 if (n != sizeof(mmop))
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800508 panic("map_stub_pages : /proc/mm map for data failed, "
Jeff Dikea61f3342007-05-06 14:51:35 -0700509 "err = %d\n", errno);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800510 }
511}
512
Jeff Dike3c917352006-09-27 01:50:40 -0700513void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800514{
Jeff Dike3c917352006-09-27 01:50:40 -0700515 (*buf)[0].JB_IP = (unsigned long) handler;
Jeff Dikee1a79c42007-05-10 22:22:31 -0700516 (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
517 sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800518}
519
Jeff Dikee2216fe2006-02-07 12:58:43 -0800520#define INIT_JMP_NEW_THREAD 0
Jeff Dike3c917352006-09-27 01:50:40 -0700521#define INIT_JMP_CALLBACK 1
522#define INIT_JMP_HALT 2
523#define INIT_JMP_REBOOT 3
Jeff Dikee2216fe2006-02-07 12:58:43 -0800524
Jeff Dike3c917352006-09-27 01:50:40 -0700525void switch_threads(jmp_buf *me, jmp_buf *you)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800526{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700527 if (UML_SETJMP(me) == 0)
Jeff Dike3c917352006-09-27 01:50:40 -0700528 UML_LONGJMP(you, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800529}
530
Jeff Dikead28e022006-04-18 22:21:41 -0700531static jmp_buf initial_jmpbuf;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800532
533/* XXX Make these percpu */
534static void (*cb_proc)(void *arg);
535static void *cb_arg;
Jeff Dikead28e022006-04-18 22:21:41 -0700536static jmp_buf *cb_back;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800537
Jeff Dike3c917352006-09-27 01:50:40 -0700538int start_idle_thread(void *stack, jmp_buf *switch_buf)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800539{
Jeff Dikea5df0d12006-07-14 00:24:02 -0700540 int n;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800541
542 set_handler(SIGWINCH, (__sighandler_t) sig_handler,
Jeff Dike61b63c52007-10-16 01:27:27 -0700543 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGVTALRM, -1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800544
Jeff Dike77f6af72007-05-06 14:51:40 -0700545 /*
546 * Can't use UML_SETJMP or UML_LONGJMP here because they save
547 * and restore signals, with the possible side-effect of
548 * trying to handle any signals which came when they were
549 * blocked, which can't be done on this stack.
550 * Signals must be blocked when jumping back here and restored
551 * after returning to the jumper.
552 */
553 n = setjmp(initial_jmpbuf);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700554 switch(n) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800555 case INIT_JMP_NEW_THREAD:
Jeff Dike3c917352006-09-27 01:50:40 -0700556 (*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler;
557 (*switch_buf)[0].JB_SP = (unsigned long) stack +
Jeff Dikee1a79c42007-05-10 22:22:31 -0700558 UM_THREAD_SIZE - sizeof(void *);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800559 break;
560 case INIT_JMP_CALLBACK:
561 (*cb_proc)(cb_arg);
Jeff Dike77f6af72007-05-06 14:51:40 -0700562 longjmp(*cb_back, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800563 break;
564 case INIT_JMP_HALT:
565 kmalloc_ok = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700566 return 0;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800567 case INIT_JMP_REBOOT:
568 kmalloc_ok = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700569 return 1;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800570 default:
571 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n);
572 }
Jeff Dike77f6af72007-05-06 14:51:40 -0700573 longjmp(*switch_buf, 1);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800574}
575
576void initial_thread_cb_skas(void (*proc)(void *), void *arg)
577{
Jeff Dikead28e022006-04-18 22:21:41 -0700578 jmp_buf here;
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800579
580 cb_proc = proc;
581 cb_arg = arg;
582 cb_back = &here;
583
584 block_signals();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700585 if (UML_SETJMP(&here) == 0)
Jeff Dikead28e022006-04-18 22:21:41 -0700586 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800587 unblock_signals();
588
589 cb_proc = NULL;
590 cb_arg = NULL;
591 cb_back = NULL;
592}
593
594void halt_skas(void)
595{
596 block_signals();
Jeff Dikead28e022006-04-18 22:21:41 -0700597 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800598}
599
600void reboot_skas(void)
601{
602 block_signals();
Jeff Dikead28e022006-04-18 22:21:41 -0700603 UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800604}
605
Jeff Dike77bf4402007-10-16 01:26:58 -0700606void __switch_mm(struct mm_id *mm_idp)
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800607{
608 int err;
609
Jeff Dike77bf4402007-10-16 01:26:58 -0700610 /* FIXME: need cpu pid in __switch_mm */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700611 if (proc_mm) {
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800612 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
613 mm_idp->u.mm_fd);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700614 if (err)
Jeff Dike77bf4402007-10-16 01:26:58 -0700615 panic("__switch_mm - PTRACE_SWITCH_MM failed, "
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800616 "errno = %d\n", errno);
617 }
618 else userspace_pid[0] = mm_idp->u.pid;
619}