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