Commit | Line | Data |
---|---|---|
abaf6977 | 1 | /* |
ba180fd4 | 2 | * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
abaf6977 GS |
3 | * Licensed under the GPL |
4 | */ | |
5 | ||
6 | #include <stdlib.h> | |
abaf6977 | 7 | #include <unistd.h> |
abaf6977 | 8 | #include <sched.h> |
ba180fd4 JD |
9 | #include <errno.h> |
10 | #include <string.h> | |
abaf6977 | 11 | #include <sys/mman.h> |
ba180fd4 JD |
12 | #include <sys/ptrace.h> |
13 | #include <sys/wait.h> | |
14 | #include <asm/unistd.h> | |
15 | #include "as-layout.h" | |
abaf6977 | 16 | #include "chan_user.h" |
edea1385 | 17 | #include "kern_util.h" |
abaf6977 | 18 | #include "mem.h" |
ba180fd4 | 19 | #include "os.h" |
abaf6977 | 20 | #include "process.h" |
ba180fd4 JD |
21 | #include "proc_mm.h" |
22 | #include "ptrace_user.h" | |
23 | #include "registers.h" | |
24 | #include "skas.h" | |
25 | #include "skas_ptrace.h" | |
ba180fd4 | 26 | #include "sysdep/stub.h" |
abaf6977 GS |
27 | |
28 | int is_skas_winch(int pid, int fd, void *data) | |
29 | { | |
512b6fb1 | 30 | if (pid != getpgrp()) |
ba180fd4 | 31 | return 0; |
abaf6977 | 32 | |
42a359e3 | 33 | register_winch_irq(-1, fd, -1, data, 0); |
ba180fd4 | 34 | return 1; |
abaf6977 GS |
35 | } |
36 | ||
f30c2c98 JD |
37 | static int ptrace_dump_regs(int pid) |
38 | { | |
3e6f2ac4 JD |
39 | unsigned long regs[MAX_REG_NR]; |
40 | int i; | |
f30c2c98 | 41 | |
3e6f2ac4 JD |
42 | if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) |
43 | return -errno; | |
ba180fd4 JD |
44 | |
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]); | |
f30c2c98 | 48 | |
3e6f2ac4 | 49 | return 0; |
f30c2c98 JD |
50 | } |
51 | ||
16dd07bc JD |
52 | /* |
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 | */ | |
3d5ede6f | 56 | #define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH)) |
16dd07bc JD |
57 | |
58 | /* Signals that the stub will finish with - anything else is an error */ | |
ee3d9bd4 | 59 | #define STUB_DONE_MASK (1 << SIGTRAP) |
16dd07bc JD |
60 | |
61 | void wait_stub_done(int pid) | |
abaf6977 GS |
62 | { |
63 | int n, status, err; | |
64 | ||
ba180fd4 | 65 | while (1) { |
4dbed85a | 66 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL)); |
ba180fd4 | 67 | if ((n < 0) || !WIFSTOPPED(status)) |
16dd07bc JD |
68 | goto bad_wait; |
69 | ||
ba180fd4 | 70 | if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0) |
16dd07bc JD |
71 | break; |
72 | ||
73 | err = ptrace(PTRACE_CONT, pid, 0, 0); | |
3e6f2ac4 JD |
74 | if (err) { |
75 | printk(UM_KERN_ERR "wait_stub_done : continue failed, " | |
76 | "errno = %d\n", errno); | |
77 | fatal_sigsegv(); | |
78 | } | |
abaf6977 | 79 | } |
16dd07bc | 80 | |
ba180fd4 | 81 | if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0) |
16dd07bc JD |
82 | return; |
83 | ||
84 | bad_wait: | |
85 | err = ptrace_dump_regs(pid); | |
ba180fd4 JD |
86 | if (err) |
87 | printk(UM_KERN_ERR "Failed to get registers from stub, " | |
88 | "errno = %d\n", -err); | |
3e6f2ac4 JD |
89 | 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(); | |
abaf6977 GS |
93 | } |
94 | ||
95 | extern unsigned long current_stub_stack(void); | |
96 | ||
99764fa4 | 97 | static void get_skas_faultinfo(int pid, struct faultinfo *fi) |
abaf6977 GS |
98 | { |
99 | int err; | |
100 | ||
ba180fd4 | 101 | if (ptrace_faultinfo) { |
abaf6977 | 102 | err = ptrace(PTRACE_FAULTINFO, pid, 0, fi); |
3e6f2ac4 JD |
103 | if (err) { |
104 | printk(UM_KERN_ERR "get_skas_faultinfo - " | |
105 | "PTRACE_FAULTINFO failed, errno = %d\n", errno); | |
106 | fatal_sigsegv(); | |
107 | } | |
abaf6977 GS |
108 | |
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 { | |
2f56debd JD |
116 | 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 | } | |
16dd07bc | 124 | err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV); |
3e6f2ac4 JD |
125 | if (err) { |
126 | printk(UM_KERN_ERR "Failed to continue stub, pid = %d, " | |
127 | "errno = %d\n", pid, errno); | |
128 | fatal_sigsegv(); | |
129 | } | |
16dd07bc | 130 | wait_stub_done(pid); |
abaf6977 | 131 | |
ba180fd4 JD |
132 | /* |
133 | * faultinfo is prepared by the stub-segv-handler at start of | |
abaf6977 GS |
134 | * the stub stack page. We just have to copy it. |
135 | */ | |
136 | memcpy(fi, (void *)current_stub_stack(), sizeof(*fi)); | |
2f56debd JD |
137 | |
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 | } | |
abaf6977 GS |
144 | } |
145 | } | |
146 | ||
77bf4400 | 147 | static void handle_segv(int pid, struct uml_pt_regs * regs) |
abaf6977 | 148 | { |
77bf4400 JD |
149 | get_skas_faultinfo(pid, ®s->faultinfo); |
150 | segv(regs->faultinfo, 0, 1, NULL); | |
abaf6977 GS |
151 | } |
152 | ||
ba180fd4 JD |
153 | /* |
154 | * To use the same value of using_sysemu as the caller, ask it that value | |
155 | * (in local_using_sysemu | |
156 | */ | |
157 | static void handle_trap(int pid, struct uml_pt_regs *regs, | |
158 | int local_using_sysemu) | |
abaf6977 GS |
159 | { |
160 | int err, status; | |
161 | ||
e06173bd JD |
162 | if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END)) |
163 | fatal_sigsegv(); | |
164 | ||
abaf6977 | 165 | /* Mark this as a syscall */ |
18baddda | 166 | UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp); |
abaf6977 GS |
167 | |
168 | if (!local_using_sysemu) | |
169 | { | |
170 | err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, | |
171 | __NR_getpid); | |
3e6f2ac4 JD |
172 | if (err < 0) { |
173 | printk(UM_KERN_ERR "handle_trap - nullifying syscall " | |
174 | "failed, errno = %d\n", errno); | |
175 | fatal_sigsegv(); | |
176 | } | |
abaf6977 GS |
177 | |
178 | err = ptrace(PTRACE_SYSCALL, pid, 0, 0); | |
3e6f2ac4 JD |
179 | 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 | } | |
abaf6977 | 184 | |
4dbed85a | 185 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL)); |
ba180fd4 | 186 | if ((err < 0) || !WIFSTOPPED(status) || |
3e6f2ac4 JD |
187 | (WSTOPSIG(status) != SIGTRAP + 0x80)) { |
188 | err = ptrace_dump_regs(pid); | |
189 | if (err) | |
190 | printk(UM_KERN_ERR "Failed to get registers " | |
ba180fd4 | 191 | "from process, errno = %d\n", -err); |
3e6f2ac4 JD |
192 | 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 | } | |
abaf6977 GS |
197 | } |
198 | ||
199 | handle_syscall(regs); | |
200 | } | |
201 | ||
202 | extern int __syscall_stub_start; | |
abaf6977 GS |
203 | |
204 | static int userspace_tramp(void *stack) | |
205 | { | |
206 | void *addr; | |
537ae946 | 207 | int err; |
abaf6977 GS |
208 | |
209 | ptrace(PTRACE_TRACEME, 0, 0, 0); | |
210 | ||
a24864a1 | 211 | signal(SIGTERM, SIG_DFL); |
ee3d9bd4 | 212 | signal(SIGWINCH, SIG_IGN); |
a2f018bf | 213 | err = set_interval(); |
3e6f2ac4 JD |
214 | if (err) { |
215 | printk(UM_KERN_ERR "userspace_tramp - setting timer failed, " | |
216 | "errno = %d\n", err); | |
217 | exit(1); | |
218 | } | |
abaf6977 | 219 | |
ba180fd4 JD |
220 | if (!proc_mm) { |
221 | /* | |
222 | * This has a pte, but it can't be mapped in with the usual | |
abaf6977 GS |
223 | * tlb_flush mechanism because this is part of that mechanism |
224 | */ | |
09ee011e | 225 | int fd; |
ba180fd4 | 226 | unsigned long long offset; |
09ee011e | 227 | fd = phys_mapping(to_phys(&__syscall_stub_start), &offset); |
54ae36f2 | 228 | addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE, |
09ee011e | 229 | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset); |
ba180fd4 | 230 | if (addr == MAP_FAILED) { |
54ae36f2 JD |
231 | printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, " |
232 | "errno = %d\n", STUB_CODE, errno); | |
abaf6977 GS |
233 | exit(1); |
234 | } | |
235 | ||
ba180fd4 | 236 | if (stack != NULL) { |
abaf6977 | 237 | fd = phys_mapping(to_phys(stack), &offset); |
54ae36f2 | 238 | addr = mmap((void *) STUB_DATA, |
1ffb9164 | 239 | UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE, |
abaf6977 | 240 | MAP_FIXED | MAP_SHARED, fd, offset); |
ba180fd4 JD |
241 | if (addr == MAP_FAILED) { |
242 | printk(UM_KERN_ERR "mapping segfault stack " | |
54ae36f2 JD |
243 | "at 0x%lx failed, errno = %d\n", |
244 | STUB_DATA, errno); | |
abaf6977 GS |
245 | exit(1); |
246 | } | |
247 | } | |
248 | } | |
ba180fd4 | 249 | if (!ptrace_faultinfo && (stack != NULL)) { |
4b84c69b JD |
250 | struct sigaction sa; |
251 | ||
54ae36f2 | 252 | unsigned long v = STUB_CODE + |
abaf6977 GS |
253 | (unsigned long) stub_segv_handler - |
254 | (unsigned long) &__syscall_stub_start; | |
255 | ||
54ae36f2 | 256 | set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE); |
4b84c69b | 257 | sigemptyset(&sa.sa_mask); |
ee3d9bd4 | 258 | sa.sa_flags = SA_ONSTACK | SA_NODEFER; |
4b84c69b JD |
259 | sa.sa_handler = (void *) v; |
260 | sa.sa_restorer = NULL; | |
3e6f2ac4 JD |
261 | 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 | } | |
abaf6977 GS |
266 | } |
267 | ||
512b6fb1 | 268 | kill(os_getpid(), SIGSTOP); |
ba180fd4 | 269 | return 0; |
abaf6977 GS |
270 | } |
271 | ||
272 | /* Each element set once, and only accessed by a single processor anyway */ | |
273 | #undef NR_CPUS | |
274 | #define NR_CPUS 1 | |
275 | int userspace_pid[NR_CPUS]; | |
276 | ||
277 | int start_userspace(unsigned long stub_stack) | |
278 | { | |
279 | void *stack; | |
280 | unsigned long sp; | |
3e6f2ac4 | 281 | int pid, status, n, flags, err; |
abaf6977 | 282 | |
c539ab73 JD |
283 | stack = mmap(NULL, UM_KERN_PAGE_SIZE, |
284 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
abaf6977 | 285 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3e6f2ac4 JD |
286 | if (stack == MAP_FAILED) { |
287 | err = -errno; | |
288 | printk(UM_KERN_ERR "start_userspace : mmap failed, " | |
b5498832 | 289 | "errno = %d\n", errno); |
3e6f2ac4 JD |
290 | return err; |
291 | } | |
292 | ||
c539ab73 | 293 | sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *); |
abaf6977 | 294 | |
4dbed85a | 295 | flags = CLONE_FILES; |
ba180fd4 JD |
296 | if (proc_mm) |
297 | flags |= CLONE_VM; | |
4dbed85a SG |
298 | else |
299 | flags |= SIGCHLD; | |
ba180fd4 | 300 | |
abaf6977 | 301 | pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack); |
3e6f2ac4 JD |
302 | if (pid < 0) { |
303 | err = -errno; | |
304 | printk(UM_KERN_ERR "start_userspace : clone failed, " | |
b5498832 | 305 | "errno = %d\n", errno); |
3e6f2ac4 JD |
306 | return err; |
307 | } | |
abaf6977 GS |
308 | |
309 | do { | |
4dbed85a | 310 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL)); |
3e6f2ac4 JD |
311 | if (n < 0) { |
312 | err = -errno; | |
313 | printk(UM_KERN_ERR "start_userspace : wait failed, " | |
b5498832 | 314 | "errno = %d\n", errno); |
3e6f2ac4 JD |
315 | goto out_kill; |
316 | } | |
ba180fd4 | 317 | } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM)); |
abaf6977 | 318 | |
3e6f2ac4 JD |
319 | if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) { |
320 | err = -EINVAL; | |
321 | printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got " | |
b5498832 | 322 | "status = %d\n", status); |
3e6f2ac4 JD |
323 | goto out_kill; |
324 | } | |
abaf6977 | 325 | |
ba180fd4 | 326 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, |
3e6f2ac4 JD |
327 | (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 | } | |
abaf6977 | 333 | |
3e6f2ac4 JD |
334 | 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 | } | |
abaf6977 | 340 | |
ba180fd4 | 341 | return pid; |
3e6f2ac4 JD |
342 | |
343 | out_kill: | |
344 | os_kill_ptraced_process(pid, 1); | |
345 | return err; | |
abaf6977 GS |
346 | } |
347 | ||
77bf4400 | 348 | void userspace(struct uml_pt_regs *regs) |
abaf6977 | 349 | { |
d2753a6d JD |
350 | struct itimerval timer; |
351 | unsigned long long nsecs, now; | |
abaf6977 | 352 | int err, status, op, pid = userspace_pid[0]; |
2ea5bc5e JD |
353 | /* To prevent races if using_sysemu changes under us.*/ |
354 | int local_using_sysemu; | |
abaf6977 | 355 | |
d2753a6d | 356 | if (getitimer(ITIMER_VIRTUAL, &timer)) |
5134d8fe | 357 | printk(UM_KERN_ERR "Failed to get itimer, errno = %d\n", errno); |
1a805219 JD |
358 | nsecs = timer.it_value.tv_sec * UM_NSEC_PER_SEC + |
359 | timer.it_value.tv_usec * UM_NSEC_PER_USEC; | |
d2753a6d JD |
360 | nsecs += os_nsecs(); |
361 | ||
ba180fd4 | 362 | while (1) { |
3e6f2ac4 JD |
363 | /* |
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 | */ | |
d25f2e12 | 371 | if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) |
3e6f2ac4 | 372 | fatal_sigsegv(); |
abaf6977 | 373 | |
fbfe9c84 IL |
374 | if (put_fp_registers(pid, regs->fp)) |
375 | fatal_sigsegv(); | |
376 | ||
abaf6977 GS |
377 | /* Now we set local_using_sysemu to be used for one loop */ |
378 | local_using_sysemu = get_using_sysemu(); | |
379 | ||
2ea5bc5e JD |
380 | op = SELECT_PTRACE_OPERATION(local_using_sysemu, |
381 | singlestepping(NULL)); | |
abaf6977 | 382 | |
3e6f2ac4 JD |
383 | 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 | } | |
abaf6977 | 388 | |
4dbed85a | 389 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL)); |
3e6f2ac4 JD |
390 | if (err < 0) { |
391 | printk(UM_KERN_ERR "userspace - wait failed, " | |
392 | "errno = %d\n", errno); | |
393 | fatal_sigsegv(); | |
394 | } | |
abaf6977 | 395 | |
77bf4400 | 396 | regs->is_user = 1; |
3e6f2ac4 JD |
397 | 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 | } | |
d25f2e12 | 402 | |
fbfe9c84 IL |
403 | 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 | ||
abaf6977 GS |
409 | UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */ |
410 | ||
ba180fd4 | 411 | if (WIFSTOPPED(status)) { |
16dd07bc | 412 | int sig = WSTOPSIG(status); |
5134d8fe | 413 | switch (sig) { |
abaf6977 | 414 | case SIGSEGV: |
ba180fd4 JD |
415 | if (PTRACE_FULL_FAULTINFO || |
416 | !ptrace_faultinfo) { | |
417 | get_skas_faultinfo(pid, | |
418 | ®s->faultinfo); | |
16dd07bc JD |
419 | (*sig_info[SIGSEGV])(SIGSEGV, regs); |
420 | } | |
abaf6977 GS |
421 | 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; | |
abaf6977 | 429 | case SIGVTALRM: |
d2753a6d | 430 | now = os_nsecs(); |
d25f2e12 | 431 | if (now < nsecs) |
d2753a6d JD |
432 | break; |
433 | block_signals(); | |
434 | (*sig_info[sig])(sig, regs); | |
435 | unblock_signals(); | |
1a805219 JD |
436 | nsecs = timer.it_value.tv_sec * |
437 | UM_NSEC_PER_SEC + | |
438 | timer.it_value.tv_usec * | |
439 | UM_NSEC_PER_USEC; | |
d2753a6d JD |
440 | nsecs += os_nsecs(); |
441 | break; | |
442 | case SIGIO: | |
abaf6977 GS |
443 | case SIGILL: |
444 | case SIGBUS: | |
445 | case SIGFPE: | |
446 | case SIGWINCH: | |
16dd07bc JD |
447 | block_signals(); |
448 | (*sig_info[sig])(sig, regs); | |
449 | unblock_signals(); | |
abaf6977 GS |
450 | break; |
451 | default: | |
96cee304 | 452 | printk(UM_KERN_ERR "userspace - child stopped " |
ba180fd4 | 453 | "with signal %d\n", sig); |
3e6f2ac4 | 454 | fatal_sigsegv(); |
abaf6977 | 455 | } |
abaf6977 GS |
456 | pid = userspace_pid[0]; |
457 | interrupt_end(); | |
458 | ||
459 | /* Avoid -ERESTARTSYS handling in host */ | |
ba180fd4 | 460 | if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET) |
18baddda | 461 | PT_SYSCALL_NR(regs->gp) = -1; |
abaf6977 GS |
462 | } |
463 | } | |
464 | } | |
abaf6977 | 465 | |
16dd07bc | 466 | static unsigned long thread_regs[MAX_REG_NR]; |
fbfe9c84 | 467 | static unsigned long thread_fp_regs[FP_SIZE]; |
16dd07bc JD |
468 | |
469 | static int __init init_thread_regs(void) | |
470 | { | |
fbfe9c84 | 471 | get_safe_registers(thread_regs, thread_fp_regs); |
16dd07bc | 472 | /* Set parent's instruction pointer to start of clone-stub */ |
54ae36f2 | 473 | thread_regs[REGS_IP_INDEX] = STUB_CODE + |
16dd07bc JD |
474 | (unsigned long) stub_clone_handler - |
475 | (unsigned long) &__syscall_stub_start; | |
54ae36f2 | 476 | thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE - |
16dd07bc JD |
477 | 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 | ||
abaf6977 GS |
486 | int copy_context_skas0(unsigned long new_stack, int pid) |
487 | { | |
1a805219 | 488 | struct timeval tv = { .tv_sec = 0, .tv_usec = UM_USEC_PER_SEC / UM_HZ }; |
abaf6977 | 489 | int err; |
abaf6977 GS |
490 | 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; | |
0a7675aa | 493 | unsigned long long new_offset; |
abaf6977 GS |
494 | int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset); |
495 | ||
ba180fd4 JD |
496 | /* |
497 | * prepare offset and fd of child's stack as argument for parent's | |
abaf6977 GS |
498 | * and child's mmap2 calls |
499 | */ | |
500 | *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset), | |
501 | .fd = new_fd, | |
502 | .timer = ((struct itimerval) | |
d2753a6d JD |
503 | { .it_value = tv, |
504 | .it_interval = tv }) }); | |
505 | ||
16dd07bc | 506 | err = ptrace_setregs(pid, thread_regs); |
3e6f2ac4 JD |
507 | 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 | } | |
abaf6977 | 513 | |
fbfe9c84 IL |
514 | 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 | ||
abaf6977 GS |
521 | /* set a well known return code for detection of child write failure */ |
522 | child_data->err = 12345678; | |
523 | ||
ba180fd4 JD |
524 | /* |
525 | * Wait, until parent has finished its work: read child's pid from | |
abaf6977 GS |
526 | * parent's stack, and check, if bad result. |
527 | */ | |
16dd07bc | 528 | err = ptrace(PTRACE_CONT, pid, 0, 0); |
3e6f2ac4 JD |
529 | 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 | ||
16dd07bc | 536 | wait_stub_done(pid); |
abaf6977 GS |
537 | |
538 | pid = data->err; | |
3e6f2ac4 JD |
539 | if (pid < 0) { |
540 | printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports " | |
541 | "error %d\n", -pid); | |
542 | return pid; | |
543 | } | |
abaf6977 | 544 | |
ba180fd4 JD |
545 | /* |
546 | * Wait, until child has finished too: read child's result from | |
abaf6977 GS |
547 | * child's stack and check it. |
548 | */ | |
16dd07bc | 549 | wait_stub_done(pid); |
3e6f2ac4 JD |
550 | 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 | } | |
abaf6977 GS |
556 | |
557 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, | |
3e6f2ac4 JD |
558 | (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 | } | |
abaf6977 GS |
564 | |
565 | return pid; | |
3e6f2ac4 JD |
566 | |
567 | out_kill: | |
568 | os_kill_ptraced_process(pid, 1); | |
569 | return err; | |
abaf6977 GS |
570 | } |
571 | ||
572 | /* | |
573 | * This is used only, if stub pages are needed, while proc_mm is | |
ef0470c0 | 574 | * available. Opening /proc/mm creates a new mm_context, which lacks |
abaf6977 GS |
575 | * the stub-pages. Thus, we map them using /proc/mm-fd |
576 | */ | |
3e6f2ac4 JD |
577 | int map_stub_pages(int fd, unsigned long code, unsigned long data, |
578 | unsigned long stack) | |
abaf6977 GS |
579 | { |
580 | struct proc_mm_op mmop; | |
581 | int n; | |
0a7675aa | 582 | unsigned long long code_offset; |
09ee011e JD |
583 | int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start), |
584 | &code_offset); | |
abaf6977 GS |
585 | |
586 | mmop = ((struct proc_mm_op) { .op = MM_MMAP, | |
587 | .u = | |
588 | { .mmap = | |
589 | { .addr = code, | |
c539ab73 | 590 | .len = UM_KERN_PAGE_SIZE, |
abaf6977 GS |
591 | .prot = PROT_EXEC, |
592 | .flags = MAP_FIXED | MAP_PRIVATE, | |
09ee011e JD |
593 | .fd = code_fd, |
594 | .offset = code_offset | |
abaf6977 | 595 | } } }); |
a61f334f | 596 | CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop))); |
ba180fd4 | 597 | if (n != sizeof(mmop)) { |
a61f334f | 598 | n = errno; |
ba180fd4 JD |
599 | printk(UM_KERN_ERR "mmap args - addr = 0x%lx, fd = %d, " |
600 | "offset = %llx\n", code, code_fd, | |
601 | (unsigned long long) code_offset); | |
3e6f2ac4 JD |
602 | printk(UM_KERN_ERR "map_stub_pages : /proc/mm map for code " |
603 | "failed, err = %d\n", n); | |
604 | return -n; | |
b4cf95c6 | 605 | } |
abaf6977 | 606 | |
ba180fd4 | 607 | if (stack) { |
0a7675aa | 608 | unsigned long long map_offset; |
abaf6977 GS |
609 | 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, | |
c539ab73 | 615 | .len = UM_KERN_PAGE_SIZE, |
abaf6977 GS |
616 | .prot = PROT_READ | PROT_WRITE, |
617 | .flags = MAP_FIXED | MAP_SHARED, | |
618 | .fd = map_fd, | |
619 | .offset = map_offset | |
620 | } } }); | |
a61f334f | 621 | CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop))); |
3e6f2ac4 JD |
622 | 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 | } | |
abaf6977 | 628 | } |
3e6f2ac4 JD |
629 | |
630 | return 0; | |
abaf6977 GS |
631 | } |
632 | ||
3c917350 | 633 | void new_thread(void *stack, jmp_buf *buf, void (*handler)(void)) |
abaf6977 | 634 | { |
3c917350 | 635 | (*buf)[0].JB_IP = (unsigned long) handler; |
e1a79c40 JD |
636 | (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE - |
637 | sizeof(void *); | |
abaf6977 GS |
638 | } |
639 | ||
e2216feb | 640 | #define INIT_JMP_NEW_THREAD 0 |
3c917350 JD |
641 | #define INIT_JMP_CALLBACK 1 |
642 | #define INIT_JMP_HALT 2 | |
643 | #define INIT_JMP_REBOOT 3 | |
abaf6977 | 644 | |
3c917350 | 645 | void switch_threads(jmp_buf *me, jmp_buf *you) |
abaf6977 | 646 | { |
ba180fd4 | 647 | if (UML_SETJMP(me) == 0) |
3c917350 | 648 | UML_LONGJMP(you, 1); |
abaf6977 GS |
649 | } |
650 | ||
ad28e029 | 651 | static jmp_buf initial_jmpbuf; |
abaf6977 GS |
652 | |
653 | /* XXX Make these percpu */ | |
654 | static void (*cb_proc)(void *arg); | |
655 | static void *cb_arg; | |
ad28e029 | 656 | static jmp_buf *cb_back; |
abaf6977 | 657 | |
3c917350 | 658 | int start_idle_thread(void *stack, jmp_buf *switch_buf) |
abaf6977 | 659 | { |
a5df0d1a | 660 | int n; |
abaf6977 GS |
661 | |
662 | set_handler(SIGWINCH, (__sighandler_t) sig_handler, | |
61b63c55 | 663 | SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGVTALRM, -1); |
abaf6977 | 664 | |
77f6af77 JD |
665 | /* |
666 | * Can't use UML_SETJMP or UML_LONGJMP here because they save | |
667 | * and restore signals, with the possible side-effect of | |
668 | * trying to handle any signals which came when they were | |
669 | * blocked, which can't be done on this stack. | |
670 | * Signals must be blocked when jumping back here and restored | |
671 | * after returning to the jumper. | |
672 | */ | |
673 | n = setjmp(initial_jmpbuf); | |
5134d8fe | 674 | switch (n) { |
abaf6977 | 675 | case INIT_JMP_NEW_THREAD: |
3c917350 JD |
676 | (*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler; |
677 | (*switch_buf)[0].JB_SP = (unsigned long) stack + | |
e1a79c40 | 678 | UM_THREAD_SIZE - sizeof(void *); |
abaf6977 GS |
679 | break; |
680 | case INIT_JMP_CALLBACK: | |
681 | (*cb_proc)(cb_arg); | |
77f6af77 | 682 | longjmp(*cb_back, 1); |
abaf6977 GS |
683 | break; |
684 | case INIT_JMP_HALT: | |
685 | kmalloc_ok = 0; | |
ba180fd4 | 686 | return 0; |
abaf6977 GS |
687 | case INIT_JMP_REBOOT: |
688 | kmalloc_ok = 0; | |
ba180fd4 | 689 | return 1; |
abaf6977 | 690 | default: |
3e6f2ac4 JD |
691 | printk(UM_KERN_ERR "Bad sigsetjmp return in " |
692 | "start_idle_thread - %d\n", n); | |
693 | fatal_sigsegv(); | |
abaf6977 | 694 | } |
77f6af77 | 695 | longjmp(*switch_buf, 1); |
abaf6977 GS |
696 | } |
697 | ||
698 | void initial_thread_cb_skas(void (*proc)(void *), void *arg) | |
699 | { | |
ad28e029 | 700 | jmp_buf here; |
abaf6977 GS |
701 | |
702 | cb_proc = proc; | |
703 | cb_arg = arg; | |
704 | cb_back = &here; | |
705 | ||
706 | block_signals(); | |
ba180fd4 | 707 | if (UML_SETJMP(&here) == 0) |
ad28e029 | 708 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK); |
abaf6977 GS |
709 | unblock_signals(); |
710 | ||
711 | cb_proc = NULL; | |
712 | cb_arg = NULL; | |
713 | cb_back = NULL; | |
714 | } | |
715 | ||
716 | void halt_skas(void) | |
717 | { | |
718 | block_signals(); | |
ad28e029 | 719 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT); |
abaf6977 GS |
720 | } |
721 | ||
722 | void reboot_skas(void) | |
723 | { | |
724 | block_signals(); | |
ad28e029 | 725 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT); |
abaf6977 GS |
726 | } |
727 | ||
77bf4400 | 728 | void __switch_mm(struct mm_id *mm_idp) |
abaf6977 GS |
729 | { |
730 | int err; | |
731 | ||
77bf4400 | 732 | /* FIXME: need cpu pid in __switch_mm */ |
ba180fd4 | 733 | if (proc_mm) { |
abaf6977 GS |
734 | err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, |
735 | mm_idp->u.mm_fd); | |
3e6f2ac4 JD |
736 | if (err) { |
737 | printk(UM_KERN_ERR "__switch_mm - PTRACE_SWITCH_MM " | |
738 | "failed, errno = %d\n", errno); | |
739 | fatal_sigsegv(); | |
740 | } | |
abaf6977 GS |
741 | } |
742 | else userspace_pid[0] = mm_idp->u.pid; | |
743 | } |