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/wait.h> |
13 | #include <asm/unistd.h> | |
37185b33 AV |
14 | #include <as-layout.h> |
15 | #include <init.h> | |
16 | #include <kern_util.h> | |
17 | #include <mem.h> | |
18 | #include <os.h> | |
37185b33 AV |
19 | #include <ptrace_user.h> |
20 | #include <registers.h> | |
21 | #include <skas.h> | |
37185b33 | 22 | #include <sysdep/stub.h> |
abaf6977 GS |
23 | |
24 | int is_skas_winch(int pid, int fd, void *data) | |
25 | { | |
17e05209 | 26 | return pid == getpgrp(); |
abaf6977 GS |
27 | } |
28 | ||
f30c2c98 JD |
29 | static int ptrace_dump_regs(int pid) |
30 | { | |
3e6f2ac4 JD |
31 | unsigned long regs[MAX_REG_NR]; |
32 | int i; | |
f30c2c98 | 33 | |
3e6f2ac4 JD |
34 | if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) |
35 | return -errno; | |
ba180fd4 JD |
36 | |
37 | printk(UM_KERN_ERR "Stub registers -\n"); | |
38 | for (i = 0; i < ARRAY_SIZE(regs); i++) | |
39 | printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]); | |
f30c2c98 | 40 | |
3e6f2ac4 | 41 | return 0; |
f30c2c98 JD |
42 | } |
43 | ||
16dd07bc JD |
44 | /* |
45 | * Signals that are OK to receive in the stub - we'll just continue it. | |
46 | * SIGWINCH will happen when UML is inside a detached screen. | |
47 | */ | |
3d5ede6f | 48 | #define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH)) |
16dd07bc JD |
49 | |
50 | /* Signals that the stub will finish with - anything else is an error */ | |
ee3d9bd4 | 51 | #define STUB_DONE_MASK (1 << SIGTRAP) |
16dd07bc JD |
52 | |
53 | void wait_stub_done(int pid) | |
abaf6977 | 54 | { |
ae5db6d1 | 55 | int n, status, err; |
abaf6977 | 56 | |
ba180fd4 | 57 | while (1) { |
4dbed85a | 58 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL)); |
ba180fd4 | 59 | if ((n < 0) || !WIFSTOPPED(status)) |
16dd07bc JD |
60 | goto bad_wait; |
61 | ||
ba180fd4 | 62 | if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0) |
16dd07bc JD |
63 | break; |
64 | ||
65 | err = ptrace(PTRACE_CONT, pid, 0, 0); | |
3e6f2ac4 JD |
66 | if (err) { |
67 | printk(UM_KERN_ERR "wait_stub_done : continue failed, " | |
68 | "errno = %d\n", errno); | |
69 | fatal_sigsegv(); | |
70 | } | |
abaf6977 | 71 | } |
16dd07bc | 72 | |
ba180fd4 | 73 | if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0) |
16dd07bc JD |
74 | return; |
75 | ||
76 | bad_wait: | |
77 | err = ptrace_dump_regs(pid); | |
ba180fd4 JD |
78 | if (err) |
79 | printk(UM_KERN_ERR "Failed to get registers from stub, " | |
80 | "errno = %d\n", -err); | |
3e6f2ac4 JD |
81 | printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, " |
82 | "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno, | |
83 | status); | |
ae5db6d1 | 84 | fatal_sigsegv(); |
abaf6977 GS |
85 | } |
86 | ||
87 | extern unsigned long current_stub_stack(void); | |
88 | ||
99764fa4 | 89 | static void get_skas_faultinfo(int pid, struct faultinfo *fi) |
abaf6977 GS |
90 | { |
91 | int err; | |
d0b5e15f | 92 | unsigned long fpregs[FP_SIZE]; |
abaf6977 | 93 | |
d0b5e15f RW |
94 | err = get_fp_registers(pid, fpregs); |
95 | if (err < 0) { | |
96 | printk(UM_KERN_ERR "save_fp_registers returned %d\n", | |
97 | err); | |
98 | fatal_sigsegv(); | |
abaf6977 | 99 | } |
d0b5e15f RW |
100 | err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV); |
101 | if (err) { | |
102 | printk(UM_KERN_ERR "Failed to continue stub, pid = %d, " | |
103 | "errno = %d\n", pid, errno); | |
104 | fatal_sigsegv(); | |
105 | } | |
106 | wait_stub_done(pid); | |
abaf6977 | 107 | |
d0b5e15f RW |
108 | /* |
109 | * faultinfo is prepared by the stub-segv-handler at start of | |
110 | * the stub stack page. We just have to copy it. | |
111 | */ | |
112 | memcpy(fi, (void *)current_stub_stack(), sizeof(*fi)); | |
2f56debd | 113 | |
d0b5e15f RW |
114 | err = put_fp_registers(pid, fpregs); |
115 | if (err < 0) { | |
116 | printk(UM_KERN_ERR "put_fp_registers returned %d\n", | |
117 | err); | |
118 | fatal_sigsegv(); | |
abaf6977 GS |
119 | } |
120 | } | |
121 | ||
77bf4400 | 122 | static void handle_segv(int pid, struct uml_pt_regs * regs) |
abaf6977 | 123 | { |
77bf4400 JD |
124 | get_skas_faultinfo(pid, ®s->faultinfo); |
125 | segv(regs->faultinfo, 0, 1, NULL); | |
abaf6977 GS |
126 | } |
127 | ||
ba180fd4 JD |
128 | /* |
129 | * To use the same value of using_sysemu as the caller, ask it that value | |
130 | * (in local_using_sysemu | |
131 | */ | |
132 | static void handle_trap(int pid, struct uml_pt_regs *regs, | |
133 | int local_using_sysemu) | |
abaf6977 GS |
134 | { |
135 | int err, status; | |
136 | ||
e06173bd JD |
137 | if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END)) |
138 | fatal_sigsegv(); | |
139 | ||
abaf6977 | 140 | /* Mark this as a syscall */ |
18baddda | 141 | UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp); |
abaf6977 GS |
142 | |
143 | if (!local_using_sysemu) | |
144 | { | |
966e803a | 145 | err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, |
abaf6977 | 146 | __NR_getpid); |
3e6f2ac4 JD |
147 | if (err < 0) { |
148 | printk(UM_KERN_ERR "handle_trap - nullifying syscall " | |
149 | "failed, errno = %d\n", errno); | |
150 | fatal_sigsegv(); | |
151 | } | |
abaf6977 GS |
152 | |
153 | err = ptrace(PTRACE_SYSCALL, pid, 0, 0); | |
3e6f2ac4 JD |
154 | if (err < 0) { |
155 | printk(UM_KERN_ERR "handle_trap - continuing to end of " | |
156 | "syscall failed, errno = %d\n", errno); | |
157 | fatal_sigsegv(); | |
158 | } | |
abaf6977 | 159 | |
4dbed85a | 160 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL)); |
ba180fd4 | 161 | if ((err < 0) || !WIFSTOPPED(status) || |
3e6f2ac4 JD |
162 | (WSTOPSIG(status) != SIGTRAP + 0x80)) { |
163 | err = ptrace_dump_regs(pid); | |
164 | if (err) | |
165 | printk(UM_KERN_ERR "Failed to get registers " | |
ba180fd4 | 166 | "from process, errno = %d\n", -err); |
3e6f2ac4 JD |
167 | printk(UM_KERN_ERR "handle_trap - failed to wait at " |
168 | "end of syscall, errno = %d, status = %d\n", | |
169 | errno, status); | |
170 | fatal_sigsegv(); | |
171 | } | |
abaf6977 GS |
172 | } |
173 | ||
174 | handle_syscall(regs); | |
175 | } | |
176 | ||
177 | extern int __syscall_stub_start; | |
abaf6977 GS |
178 | |
179 | static int userspace_tramp(void *stack) | |
180 | { | |
181 | void *addr; | |
d0b5e15f RW |
182 | int err, fd; |
183 | unsigned long long offset; | |
abaf6977 GS |
184 | |
185 | ptrace(PTRACE_TRACEME, 0, 0, 0); | |
186 | ||
a24864a1 | 187 | signal(SIGTERM, SIG_DFL); |
ee3d9bd4 | 188 | signal(SIGWINCH, SIG_IGN); |
a2f018bf | 189 | err = set_interval(); |
3e6f2ac4 JD |
190 | if (err) { |
191 | printk(UM_KERN_ERR "userspace_tramp - setting timer failed, " | |
192 | "errno = %d\n", err); | |
193 | exit(1); | |
194 | } | |
abaf6977 | 195 | |
d0b5e15f RW |
196 | /* |
197 | * This has a pte, but it can't be mapped in with the usual | |
198 | * tlb_flush mechanism because this is part of that mechanism | |
199 | */ | |
200 | fd = phys_mapping(to_phys(&__syscall_stub_start), &offset); | |
201 | addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE, | |
202 | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset); | |
203 | if (addr == MAP_FAILED) { | |
204 | printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, " | |
205 | "errno = %d\n", STUB_CODE, errno); | |
206 | exit(1); | |
207 | } | |
208 | ||
209 | if (stack != NULL) { | |
210 | fd = phys_mapping(to_phys(stack), &offset); | |
211 | addr = mmap((void *) STUB_DATA, | |
212 | UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE, | |
213 | MAP_FIXED | MAP_SHARED, fd, offset); | |
ba180fd4 | 214 | if (addr == MAP_FAILED) { |
d0b5e15f RW |
215 | printk(UM_KERN_ERR "mapping segfault stack " |
216 | "at 0x%lx failed, errno = %d\n", | |
217 | STUB_DATA, errno); | |
abaf6977 GS |
218 | exit(1); |
219 | } | |
abaf6977 | 220 | } |
d0b5e15f | 221 | if (stack != NULL) { |
4b84c69b JD |
222 | struct sigaction sa; |
223 | ||
54ae36f2 | 224 | unsigned long v = STUB_CODE + |
abaf6977 GS |
225 | (unsigned long) stub_segv_handler - |
226 | (unsigned long) &__syscall_stub_start; | |
227 | ||
54ae36f2 | 228 | set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE); |
4b84c69b | 229 | sigemptyset(&sa.sa_mask); |
9b25fcbd AV |
230 | sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO; |
231 | sa.sa_sigaction = (void *) v; | |
4b84c69b | 232 | sa.sa_restorer = NULL; |
3e6f2ac4 JD |
233 | if (sigaction(SIGSEGV, &sa, NULL) < 0) { |
234 | printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV " | |
235 | "handler failed - errno = %d\n", errno); | |
236 | exit(1); | |
237 | } | |
abaf6977 GS |
238 | } |
239 | ||
512b6fb1 | 240 | kill(os_getpid(), SIGSTOP); |
ba180fd4 | 241 | return 0; |
abaf6977 GS |
242 | } |
243 | ||
244 | /* Each element set once, and only accessed by a single processor anyway */ | |
245 | #undef NR_CPUS | |
246 | #define NR_CPUS 1 | |
247 | int userspace_pid[NR_CPUS]; | |
248 | ||
249 | int start_userspace(unsigned long stub_stack) | |
250 | { | |
251 | void *stack; | |
252 | unsigned long sp; | |
3e6f2ac4 | 253 | int pid, status, n, flags, err; |
abaf6977 | 254 | |
c539ab73 JD |
255 | stack = mmap(NULL, UM_KERN_PAGE_SIZE, |
256 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
abaf6977 | 257 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
3e6f2ac4 JD |
258 | if (stack == MAP_FAILED) { |
259 | err = -errno; | |
260 | printk(UM_KERN_ERR "start_userspace : mmap failed, " | |
b5498832 | 261 | "errno = %d\n", errno); |
3e6f2ac4 JD |
262 | return err; |
263 | } | |
264 | ||
c539ab73 | 265 | sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *); |
abaf6977 | 266 | |
d0b5e15f | 267 | flags = CLONE_FILES | SIGCHLD; |
ba180fd4 | 268 | |
abaf6977 | 269 | pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack); |
3e6f2ac4 JD |
270 | if (pid < 0) { |
271 | err = -errno; | |
272 | printk(UM_KERN_ERR "start_userspace : clone failed, " | |
b5498832 | 273 | "errno = %d\n", errno); |
3e6f2ac4 JD |
274 | return err; |
275 | } | |
abaf6977 GS |
276 | |
277 | do { | |
4dbed85a | 278 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL)); |
3e6f2ac4 JD |
279 | if (n < 0) { |
280 | err = -errno; | |
281 | printk(UM_KERN_ERR "start_userspace : wait failed, " | |
b5498832 | 282 | "errno = %d\n", errno); |
3e6f2ac4 JD |
283 | goto out_kill; |
284 | } | |
ba180fd4 | 285 | } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM)); |
abaf6977 | 286 | |
3e6f2ac4 JD |
287 | if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) { |
288 | err = -EINVAL; | |
289 | printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got " | |
b5498832 | 290 | "status = %d\n", status); |
3e6f2ac4 JD |
291 | goto out_kill; |
292 | } | |
abaf6977 | 293 | |
ba180fd4 | 294 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, |
3e6f2ac4 JD |
295 | (void *) PTRACE_O_TRACESYSGOOD) < 0) { |
296 | err = -errno; | |
297 | printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS " | |
298 | "failed, errno = %d\n", errno); | |
299 | goto out_kill; | |
300 | } | |
abaf6977 | 301 | |
3e6f2ac4 JD |
302 | if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) { |
303 | err = -errno; | |
304 | printk(UM_KERN_ERR "start_userspace : munmap failed, " | |
305 | "errno = %d\n", errno); | |
306 | goto out_kill; | |
307 | } | |
abaf6977 | 308 | |
ba180fd4 | 309 | return pid; |
3e6f2ac4 JD |
310 | |
311 | out_kill: | |
312 | os_kill_ptraced_process(pid, 1); | |
313 | return err; | |
abaf6977 GS |
314 | } |
315 | ||
77bf4400 | 316 | void userspace(struct uml_pt_regs *regs) |
abaf6977 | 317 | { |
d2753a6d JD |
318 | struct itimerval timer; |
319 | unsigned long long nsecs, now; | |
abaf6977 | 320 | int err, status, op, pid = userspace_pid[0]; |
2ea5bc5e JD |
321 | /* To prevent races if using_sysemu changes under us.*/ |
322 | int local_using_sysemu; | |
d3c1cfcd | 323 | siginfo_t si; |
abaf6977 | 324 | |
b8a42095 AV |
325 | /* Handle any immediate reschedules or signals */ |
326 | interrupt_end(); | |
327 | ||
d2753a6d | 328 | if (getitimer(ITIMER_VIRTUAL, &timer)) |
5134d8fe | 329 | printk(UM_KERN_ERR "Failed to get itimer, errno = %d\n", errno); |
1a805219 JD |
330 | nsecs = timer.it_value.tv_sec * UM_NSEC_PER_SEC + |
331 | timer.it_value.tv_usec * UM_NSEC_PER_USEC; | |
d2753a6d JD |
332 | nsecs += os_nsecs(); |
333 | ||
ba180fd4 | 334 | while (1) { |
3e6f2ac4 JD |
335 | /* |
336 | * This can legitimately fail if the process loads a | |
337 | * bogus value into a segment register. It will | |
338 | * segfault and PTRACE_GETREGS will read that value | |
339 | * out of the process. However, PTRACE_SETREGS will | |
340 | * fail. In this case, there is nothing to do but | |
341 | * just kill the process. | |
342 | */ | |
d25f2e12 | 343 | if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) |
3e6f2ac4 | 344 | fatal_sigsegv(); |
abaf6977 | 345 | |
fbfe9c84 IL |
346 | if (put_fp_registers(pid, regs->fp)) |
347 | fatal_sigsegv(); | |
348 | ||
abaf6977 GS |
349 | /* Now we set local_using_sysemu to be used for one loop */ |
350 | local_using_sysemu = get_using_sysemu(); | |
351 | ||
2ea5bc5e JD |
352 | op = SELECT_PTRACE_OPERATION(local_using_sysemu, |
353 | singlestepping(NULL)); | |
abaf6977 | 354 | |
3e6f2ac4 JD |
355 | if (ptrace(op, pid, 0, 0)) { |
356 | printk(UM_KERN_ERR "userspace - ptrace continue " | |
357 | "failed, op = %d, errno = %d\n", op, errno); | |
358 | fatal_sigsegv(); | |
359 | } | |
abaf6977 | 360 | |
4dbed85a | 361 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL)); |
3e6f2ac4 JD |
362 | if (err < 0) { |
363 | printk(UM_KERN_ERR "userspace - wait failed, " | |
364 | "errno = %d\n", errno); | |
365 | fatal_sigsegv(); | |
366 | } | |
abaf6977 | 367 | |
77bf4400 | 368 | regs->is_user = 1; |
3e6f2ac4 JD |
369 | if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) { |
370 | printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, " | |
371 | "errno = %d\n", errno); | |
372 | fatal_sigsegv(); | |
373 | } | |
d25f2e12 | 374 | |
fbfe9c84 IL |
375 | if (get_fp_registers(pid, regs->fp)) { |
376 | printk(UM_KERN_ERR "userspace - get_fp_registers failed, " | |
377 | "errno = %d\n", errno); | |
378 | fatal_sigsegv(); | |
379 | } | |
380 | ||
abaf6977 GS |
381 | UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */ |
382 | ||
ba180fd4 | 383 | if (WIFSTOPPED(status)) { |
16dd07bc | 384 | int sig = WSTOPSIG(status); |
d3c1cfcd | 385 | |
9a8c1359 | 386 | ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si); |
d3c1cfcd | 387 | |
5134d8fe | 388 | switch (sig) { |
abaf6977 | 389 | case SIGSEGV: |
d0b5e15f | 390 | if (PTRACE_FULL_FAULTINFO) { |
ba180fd4 JD |
391 | get_skas_faultinfo(pid, |
392 | ®s->faultinfo); | |
9a8c1359 | 393 | (*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si, |
d3c1cfcd | 394 | regs); |
16dd07bc | 395 | } |
abaf6977 GS |
396 | else handle_segv(pid, regs); |
397 | break; | |
398 | case SIGTRAP + 0x80: | |
399 | handle_trap(pid, regs, local_using_sysemu); | |
400 | break; | |
401 | case SIGTRAP: | |
9a8c1359 | 402 | relay_signal(SIGTRAP, (struct siginfo *)&si, regs); |
abaf6977 | 403 | break; |
abaf6977 | 404 | case SIGVTALRM: |
d2753a6d | 405 | now = os_nsecs(); |
d25f2e12 | 406 | if (now < nsecs) |
d2753a6d JD |
407 | break; |
408 | block_signals(); | |
9a8c1359 | 409 | (*sig_info[sig])(sig, (struct siginfo *)&si, regs); |
d2753a6d | 410 | unblock_signals(); |
1a805219 JD |
411 | nsecs = timer.it_value.tv_sec * |
412 | UM_NSEC_PER_SEC + | |
413 | timer.it_value.tv_usec * | |
414 | UM_NSEC_PER_USEC; | |
d2753a6d JD |
415 | nsecs += os_nsecs(); |
416 | break; | |
417 | case SIGIO: | |
abaf6977 GS |
418 | case SIGILL: |
419 | case SIGBUS: | |
420 | case SIGFPE: | |
421 | case SIGWINCH: | |
16dd07bc | 422 | block_signals(); |
9a8c1359 | 423 | (*sig_info[sig])(sig, (struct siginfo *)&si, regs); |
16dd07bc | 424 | unblock_signals(); |
abaf6977 GS |
425 | break; |
426 | default: | |
96cee304 | 427 | printk(UM_KERN_ERR "userspace - child stopped " |
ba180fd4 | 428 | "with signal %d\n", sig); |
3e6f2ac4 | 429 | fatal_sigsegv(); |
abaf6977 | 430 | } |
abaf6977 GS |
431 | pid = userspace_pid[0]; |
432 | interrupt_end(); | |
433 | ||
434 | /* Avoid -ERESTARTSYS handling in host */ | |
ba180fd4 | 435 | if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET) |
18baddda | 436 | PT_SYSCALL_NR(regs->gp) = -1; |
abaf6977 GS |
437 | } |
438 | } | |
439 | } | |
abaf6977 | 440 | |
16dd07bc | 441 | static unsigned long thread_regs[MAX_REG_NR]; |
fbfe9c84 | 442 | static unsigned long thread_fp_regs[FP_SIZE]; |
16dd07bc JD |
443 | |
444 | static int __init init_thread_regs(void) | |
445 | { | |
fbfe9c84 | 446 | get_safe_registers(thread_regs, thread_fp_regs); |
16dd07bc | 447 | /* Set parent's instruction pointer to start of clone-stub */ |
54ae36f2 | 448 | thread_regs[REGS_IP_INDEX] = STUB_CODE + |
16dd07bc JD |
449 | (unsigned long) stub_clone_handler - |
450 | (unsigned long) &__syscall_stub_start; | |
54ae36f2 | 451 | thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE - |
16dd07bc JD |
452 | sizeof(void *); |
453 | #ifdef __SIGNAL_FRAMESIZE | |
454 | thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE; | |
455 | #endif | |
456 | return 0; | |
457 | } | |
458 | ||
459 | __initcall(init_thread_regs); | |
460 | ||
abaf6977 GS |
461 | int copy_context_skas0(unsigned long new_stack, int pid) |
462 | { | |
1a805219 | 463 | struct timeval tv = { .tv_sec = 0, .tv_usec = UM_USEC_PER_SEC / UM_HZ }; |
abaf6977 | 464 | int err; |
abaf6977 GS |
465 | unsigned long current_stack = current_stub_stack(); |
466 | struct stub_data *data = (struct stub_data *) current_stack; | |
467 | struct stub_data *child_data = (struct stub_data *) new_stack; | |
0a7675aa | 468 | unsigned long long new_offset; |
abaf6977 GS |
469 | int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset); |
470 | ||
ba180fd4 JD |
471 | /* |
472 | * prepare offset and fd of child's stack as argument for parent's | |
abaf6977 GS |
473 | * and child's mmap2 calls |
474 | */ | |
475 | *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset), | |
476 | .fd = new_fd, | |
477 | .timer = ((struct itimerval) | |
d2753a6d JD |
478 | { .it_value = tv, |
479 | .it_interval = tv }) }); | |
480 | ||
16dd07bc | 481 | err = ptrace_setregs(pid, thread_regs); |
3e6f2ac4 JD |
482 | if (err < 0) { |
483 | err = -errno; | |
484 | printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS " | |
485 | "failed, pid = %d, errno = %d\n", pid, -err); | |
486 | return err; | |
487 | } | |
abaf6977 | 488 | |
fbfe9c84 IL |
489 | err = put_fp_registers(pid, thread_fp_regs); |
490 | if (err < 0) { | |
491 | printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers " | |
492 | "failed, pid = %d, err = %d\n", pid, err); | |
493 | return err; | |
494 | } | |
495 | ||
abaf6977 GS |
496 | /* set a well known return code for detection of child write failure */ |
497 | child_data->err = 12345678; | |
498 | ||
ba180fd4 JD |
499 | /* |
500 | * Wait, until parent has finished its work: read child's pid from | |
abaf6977 GS |
501 | * parent's stack, and check, if bad result. |
502 | */ | |
16dd07bc | 503 | err = ptrace(PTRACE_CONT, pid, 0, 0); |
3e6f2ac4 JD |
504 | if (err) { |
505 | err = -errno; | |
506 | printk(UM_KERN_ERR "Failed to continue new process, pid = %d, " | |
507 | "errno = %d\n", pid, errno); | |
508 | return err; | |
509 | } | |
510 | ||
16dd07bc | 511 | wait_stub_done(pid); |
abaf6977 GS |
512 | |
513 | pid = data->err; | |
3e6f2ac4 JD |
514 | if (pid < 0) { |
515 | printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports " | |
516 | "error %d\n", -pid); | |
517 | return pid; | |
518 | } | |
abaf6977 | 519 | |
ba180fd4 JD |
520 | /* |
521 | * Wait, until child has finished too: read child's result from | |
abaf6977 GS |
522 | * child's stack and check it. |
523 | */ | |
16dd07bc | 524 | wait_stub_done(pid); |
3e6f2ac4 JD |
525 | if (child_data->err != STUB_DATA) { |
526 | printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports " | |
527 | "error %ld\n", child_data->err); | |
528 | err = child_data->err; | |
529 | goto out_kill; | |
530 | } | |
abaf6977 GS |
531 | |
532 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, | |
3e6f2ac4 JD |
533 | (void *)PTRACE_O_TRACESYSGOOD) < 0) { |
534 | err = -errno; | |
535 | printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS " | |
536 | "failed, errno = %d\n", errno); | |
537 | goto out_kill; | |
538 | } | |
abaf6977 GS |
539 | |
540 | return pid; | |
3e6f2ac4 JD |
541 | |
542 | out_kill: | |
543 | os_kill_ptraced_process(pid, 1); | |
544 | return err; | |
abaf6977 GS |
545 | } |
546 | ||
3c917350 | 547 | void new_thread(void *stack, jmp_buf *buf, void (*handler)(void)) |
abaf6977 | 548 | { |
3c917350 | 549 | (*buf)[0].JB_IP = (unsigned long) handler; |
e1a79c40 JD |
550 | (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE - |
551 | sizeof(void *); | |
abaf6977 GS |
552 | } |
553 | ||
e2216feb | 554 | #define INIT_JMP_NEW_THREAD 0 |
3c917350 JD |
555 | #define INIT_JMP_CALLBACK 1 |
556 | #define INIT_JMP_HALT 2 | |
557 | #define INIT_JMP_REBOOT 3 | |
abaf6977 | 558 | |
3c917350 | 559 | void switch_threads(jmp_buf *me, jmp_buf *you) |
abaf6977 | 560 | { |
ba180fd4 | 561 | if (UML_SETJMP(me) == 0) |
3c917350 | 562 | UML_LONGJMP(you, 1); |
abaf6977 GS |
563 | } |
564 | ||
ad28e029 | 565 | static jmp_buf initial_jmpbuf; |
abaf6977 GS |
566 | |
567 | /* XXX Make these percpu */ | |
568 | static void (*cb_proc)(void *arg); | |
569 | static void *cb_arg; | |
ad28e029 | 570 | static jmp_buf *cb_back; |
abaf6977 | 571 | |
3c917350 | 572 | int start_idle_thread(void *stack, jmp_buf *switch_buf) |
abaf6977 | 573 | { |
a5df0d1a | 574 | int n; |
abaf6977 | 575 | |
00361683 | 576 | set_handler(SIGWINCH); |
abaf6977 | 577 | |
77f6af77 JD |
578 | /* |
579 | * Can't use UML_SETJMP or UML_LONGJMP here because they save | |
580 | * and restore signals, with the possible side-effect of | |
581 | * trying to handle any signals which came when they were | |
582 | * blocked, which can't be done on this stack. | |
583 | * Signals must be blocked when jumping back here and restored | |
584 | * after returning to the jumper. | |
585 | */ | |
586 | n = setjmp(initial_jmpbuf); | |
5134d8fe | 587 | switch (n) { |
abaf6977 | 588 | case INIT_JMP_NEW_THREAD: |
3c917350 JD |
589 | (*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler; |
590 | (*switch_buf)[0].JB_SP = (unsigned long) stack + | |
e1a79c40 | 591 | UM_THREAD_SIZE - sizeof(void *); |
abaf6977 GS |
592 | break; |
593 | case INIT_JMP_CALLBACK: | |
594 | (*cb_proc)(cb_arg); | |
77f6af77 | 595 | longjmp(*cb_back, 1); |
abaf6977 GS |
596 | break; |
597 | case INIT_JMP_HALT: | |
598 | kmalloc_ok = 0; | |
ba180fd4 | 599 | return 0; |
abaf6977 GS |
600 | case INIT_JMP_REBOOT: |
601 | kmalloc_ok = 0; | |
ba180fd4 | 602 | return 1; |
abaf6977 | 603 | default: |
3e6f2ac4 JD |
604 | printk(UM_KERN_ERR "Bad sigsetjmp return in " |
605 | "start_idle_thread - %d\n", n); | |
606 | fatal_sigsegv(); | |
abaf6977 | 607 | } |
77f6af77 | 608 | longjmp(*switch_buf, 1); |
abaf6977 GS |
609 | } |
610 | ||
611 | void initial_thread_cb_skas(void (*proc)(void *), void *arg) | |
612 | { | |
ad28e029 | 613 | jmp_buf here; |
abaf6977 GS |
614 | |
615 | cb_proc = proc; | |
616 | cb_arg = arg; | |
617 | cb_back = &here; | |
618 | ||
619 | block_signals(); | |
ba180fd4 | 620 | if (UML_SETJMP(&here) == 0) |
ad28e029 | 621 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK); |
abaf6977 GS |
622 | unblock_signals(); |
623 | ||
624 | cb_proc = NULL; | |
625 | cb_arg = NULL; | |
626 | cb_back = NULL; | |
627 | } | |
628 | ||
629 | void halt_skas(void) | |
630 | { | |
631 | block_signals(); | |
ad28e029 | 632 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT); |
abaf6977 GS |
633 | } |
634 | ||
635 | void reboot_skas(void) | |
636 | { | |
637 | block_signals(); | |
ad28e029 | 638 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT); |
abaf6977 GS |
639 | } |
640 | ||
77bf4400 | 641 | void __switch_mm(struct mm_id *mm_idp) |
abaf6977 | 642 | { |
d0b5e15f | 643 | userspace_pid[0] = mm_idp->u.pid; |
abaf6977 | 644 | } |