2 * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
12 #include <sys/ptrace.h>
14 #include <asm/unistd.h>
15 #include "as-layout.h"
16 #include "chan_user.h"
17 #include "kern_constants.h"
22 #include "ptrace_user.h"
23 #include "registers.h"
25 #include "skas_ptrace.h"
27 #include "sysdep/stub.h"
29 int is_skas_winch(int pid
, int fd
, void *data
)
34 register_winch_irq(-1, fd
, -1, data
, 0);
38 static int ptrace_dump_regs(int pid
)
40 unsigned long regs
[MAX_REG_NR
];
43 if (ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
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
]);
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.
57 #define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH))
59 /* Signals that the stub will finish with - anything else is an error */
60 #define STUB_DONE_MASK ((1 << SIGUSR1) | (1 << SIGTRAP))
62 void wait_stub_done(int pid
)
67 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
| __WALL
));
68 if ((n
< 0) || !WIFSTOPPED(status
))
71 if (((1 << WSTOPSIG(status
)) & STUB_SIG_MASK
) == 0)
74 err
= ptrace(PTRACE_CONT
, pid
, 0, 0);
76 panic("wait_stub_done : continue failed, errno = %d\n",
80 if (((1 << WSTOPSIG(status
)) & STUB_DONE_MASK
) != 0)
84 err
= ptrace_dump_regs(pid
);
86 printk(UM_KERN_ERR
"Failed to get registers from stub, "
87 "errno = %d\n", -err
);
88 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
);
92 extern unsigned long current_stub_stack(void);
94 void get_skas_faultinfo(int pid
, struct faultinfo
* fi
)
98 if (ptrace_faultinfo
) {
99 err
= ptrace(PTRACE_FAULTINFO
, pid
, 0, fi
);
101 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
102 "errno = %d\n", errno
);
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
));
111 err
= ptrace(PTRACE_CONT
, pid
, 0, SIGSEGV
);
113 panic("Failed to continue stub, pid = %d, errno = %d\n",
118 * faultinfo is prepared by the stub-segv-handler at start of
119 * the stub stack page. We just have to copy it.
121 memcpy(fi
, (void *)current_stub_stack(), sizeof(*fi
));
125 static void handle_segv(int pid
, struct uml_pt_regs
* regs
)
127 get_skas_faultinfo(pid
, ®s
->faultinfo
);
128 segv(regs
->faultinfo
, 0, 1, NULL
);
132 * To use the same value of using_sysemu as the caller, ask it that value
133 * (in local_using_sysemu
135 static void handle_trap(int pid
, struct uml_pt_regs
*regs
,
136 int local_using_sysemu
)
140 /* Mark this as a syscall */
141 UPT_SYSCALL_NR(regs
) = PT_SYSCALL_NR(regs
->gp
);
143 if (!local_using_sysemu
)
145 err
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
148 panic("handle_trap - nullifying syscall failed, "
149 "errno = %d\n", errno
);
151 err
= ptrace(PTRACE_SYSCALL
, pid
, 0, 0);
153 panic("handle_trap - continuing to end of syscall "
154 "failed, errno = %d\n", errno
);
156 CATCH_EINTR(err
= waitpid(pid
, &status
, WUNTRACED
| __WALL
));
157 if ((err
< 0) || !WIFSTOPPED(status
) ||
158 (WSTOPSIG(status
) != SIGTRAP
+ 0x80)) {
159 err
= ptrace_dump_regs(pid
);
161 printk(UM_KERN_ERR
"Failed to get registers "
162 "from process, errno = %d\n", -err
);
163 panic("handle_trap - failed to wait at end of syscall, "
164 "errno = %d, status = %d\n", errno
, status
);
168 handle_syscall(regs
);
171 extern int __syscall_stub_start
;
173 static int userspace_tramp(void *stack
)
178 ptrace(PTRACE_TRACEME
, 0, 0, 0);
180 signal(SIGTERM
, SIG_DFL
);
181 err
= set_interval();
183 panic("userspace_tramp - setting timer failed, errno = %d\n",
188 * This has a pte, but it can't be mapped in with the usual
189 * tlb_flush mechanism because this is part of that mechanism
192 unsigned long long offset
;
193 fd
= phys_mapping(to_phys(&__syscall_stub_start
), &offset
);
194 addr
= mmap64((void *) STUB_CODE
, UM_KERN_PAGE_SIZE
,
195 PROT_EXEC
, MAP_FIXED
| MAP_PRIVATE
, fd
, offset
);
196 if (addr
== MAP_FAILED
) {
197 printk(UM_KERN_ERR
"mapping mmap stub at 0x%lx failed, "
198 "errno = %d\n", STUB_CODE
, errno
);
203 fd
= phys_mapping(to_phys(stack
), &offset
);
204 addr
= mmap((void *) STUB_DATA
,
205 UM_KERN_PAGE_SIZE
, PROT_READ
| PROT_WRITE
,
206 MAP_FIXED
| MAP_SHARED
, fd
, offset
);
207 if (addr
== MAP_FAILED
) {
208 printk(UM_KERN_ERR
"mapping segfault stack "
209 "at 0x%lx failed, errno = %d\n",
215 if (!ptrace_faultinfo
&& (stack
!= NULL
)) {
218 unsigned long v
= STUB_CODE
+
219 (unsigned long) stub_segv_handler
-
220 (unsigned long) &__syscall_stub_start
;
222 set_sigstack((void *) STUB_DATA
, UM_KERN_PAGE_SIZE
);
223 sigemptyset(&sa
.sa_mask
);
224 sigaddset(&sa
.sa_mask
, SIGIO
);
225 sigaddset(&sa
.sa_mask
, SIGWINCH
);
226 sigaddset(&sa
.sa_mask
, SIGVTALRM
);
227 sigaddset(&sa
.sa_mask
, SIGUSR1
);
228 sa
.sa_flags
= SA_ONSTACK
;
229 sa
.sa_handler
= (void *) v
;
230 sa
.sa_restorer
= NULL
;
231 if (sigaction(SIGSEGV
, &sa
, NULL
) < 0)
232 panic("userspace_tramp - setting SIGSEGV handler "
233 "failed - errno = %d\n", errno
);
236 kill(os_getpid(), SIGSTOP
);
240 /* Each element set once, and only accessed by a single processor anyway */
243 int userspace_pid
[NR_CPUS
];
245 int start_userspace(unsigned long stub_stack
)
249 int pid
, status
, n
, flags
;
251 stack
= mmap(NULL
, UM_KERN_PAGE_SIZE
,
252 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
253 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
254 if (stack
== MAP_FAILED
)
255 panic("start_userspace : mmap failed, errno = %d", errno
);
256 sp
= (unsigned long) stack
+ UM_KERN_PAGE_SIZE
- sizeof(void *);
264 pid
= clone(userspace_tramp
, (void *) sp
, flags
, (void *) stub_stack
);
266 panic("start_userspace : clone failed, errno = %d", errno
);
269 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
| __WALL
));
271 panic("start_userspace : wait failed, errno = %d",
273 } while (WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGVTALRM
));
275 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
276 panic("start_userspace : expected SIGSTOP, got status = %d",
279 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, NULL
,
280 (void *) PTRACE_O_TRACESYSGOOD
) < 0)
281 panic("start_userspace : PTRACE_OLDSETOPTIONS failed, "
282 "errno = %d\n", errno
);
284 if (munmap(stack
, UM_KERN_PAGE_SIZE
) < 0)
285 panic("start_userspace : munmap failed, errno = %d\n", errno
);
290 void userspace(struct uml_pt_regs
*regs
)
292 struct itimerval timer
;
293 unsigned long long nsecs
, now
;
294 int err
, status
, op
, pid
= userspace_pid
[0];
295 /* To prevent races if using_sysemu changes under us.*/
296 int local_using_sysemu
;
298 if (getitimer(ITIMER_VIRTUAL
, &timer
))
299 printk("Failed to get itimer, errno = %d\n", errno
);
300 nsecs
= timer
.it_value
.tv_sec
* UM_NSEC_PER_SEC
+
301 timer
.it_value
.tv_usec
* UM_NSEC_PER_USEC
;
305 restore_registers(pid
, regs
);
307 /* Now we set local_using_sysemu to be used for one loop */
308 local_using_sysemu
= get_using_sysemu();
310 op
= SELECT_PTRACE_OPERATION(local_using_sysemu
,
311 singlestepping(NULL
));
313 err
= ptrace(op
, pid
, 0, 0);
315 panic("userspace - could not resume userspace process, "
316 "pid=%d, ptrace operation = %d, errno = %d\n",
319 CATCH_EINTR(err
= waitpid(pid
, &status
, WUNTRACED
| __WALL
));
321 panic("userspace - waitpid failed, errno = %d\n",
325 save_registers(pid
, regs
);
326 UPT_SYSCALL_NR(regs
) = -1; /* Assume: It's not a syscall */
328 if (WIFSTOPPED(status
)) {
329 int sig
= WSTOPSIG(status
);
332 if (PTRACE_FULL_FAULTINFO
||
334 get_skas_faultinfo(pid
,
336 (*sig_info
[SIGSEGV
])(SIGSEGV
, regs
);
338 else handle_segv(pid
, regs
);
341 handle_trap(pid
, regs
, local_using_sysemu
);
344 relay_signal(SIGTRAP
, regs
);
351 (*sig_info
[sig
])(sig
, regs
);
353 nsecs
= timer
.it_value
.tv_sec
*
355 timer
.it_value
.tv_usec
*
365 (*sig_info
[sig
])(sig
, regs
);
369 printk(UM_KERN_ERR
"userspace - child stopped "
370 "with signal %d\n", sig
);
372 pid
= userspace_pid
[0];
375 /* Avoid -ERESTARTSYS handling in host */
376 if (PT_SYSCALL_NR_OFFSET
!= PT_SYSCALL_RET_OFFSET
)
377 PT_SYSCALL_NR(regs
->gp
) = -1;
382 static unsigned long thread_regs
[MAX_REG_NR
];
384 static int __init
init_thread_regs(void)
386 get_safe_registers(thread_regs
);
387 /* Set parent's instruction pointer to start of clone-stub */
388 thread_regs
[REGS_IP_INDEX
] = STUB_CODE
+
389 (unsigned long) stub_clone_handler
-
390 (unsigned long) &__syscall_stub_start
;
391 thread_regs
[REGS_SP_INDEX
] = STUB_DATA
+ UM_KERN_PAGE_SIZE
-
393 #ifdef __SIGNAL_FRAMESIZE
394 thread_regs
[REGS_SP_INDEX
] -= __SIGNAL_FRAMESIZE
;
399 __initcall(init_thread_regs
);
401 int copy_context_skas0(unsigned long new_stack
, int pid
)
403 struct timeval tv
= { .tv_sec
= 0, .tv_usec
= UM_USEC_PER_SEC
/ UM_HZ
};
405 unsigned long current_stack
= current_stub_stack();
406 struct stub_data
*data
= (struct stub_data
*) current_stack
;
407 struct stub_data
*child_data
= (struct stub_data
*) new_stack
;
408 unsigned long long new_offset
;
409 int new_fd
= phys_mapping(to_phys((void *)new_stack
), &new_offset
);
412 * prepare offset and fd of child's stack as argument for parent's
413 * and child's mmap2 calls
415 *data
= ((struct stub_data
) { .offset
= MMAP_OFFSET(new_offset
),
417 .timer
= ((struct itimerval
)
419 .it_interval
= tv
}) });
421 err
= ptrace_setregs(pid
, thread_regs
);
423 panic("copy_context_skas0 : PTRACE_SETREGS failed, "
424 "pid = %d, errno = %d\n", pid
, -err
);
426 /* set a well known return code for detection of child write failure */
427 child_data
->err
= 12345678;
430 * Wait, until parent has finished its work: read child's pid from
431 * parent's stack, and check, if bad result.
433 err
= ptrace(PTRACE_CONT
, pid
, 0, 0);
435 panic("Failed to continue new process, pid = %d, "
436 "errno = %d\n", pid
, errno
);
441 panic("copy_context_skas0 - stub-parent reports error %d\n",
445 * Wait, until child has finished too: read child's result from
446 * child's stack and check it.
449 if (child_data
->err
!= STUB_DATA
)
450 panic("copy_context_skas0 - stub-child reports error %ld\n",
453 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, NULL
,
454 (void *)PTRACE_O_TRACESYSGOOD
) < 0)
455 panic("copy_context_skas0 : PTRACE_OLDSETOPTIONS failed, "
456 "errno = %d\n", errno
);
462 * This is used only, if stub pages are needed, while proc_mm is
463 * available. Opening /proc/mm creates a new mm_context, which lacks
464 * the stub-pages. Thus, we map them using /proc/mm-fd
466 void map_stub_pages(int fd
, unsigned long code
,
467 unsigned long data
, unsigned long stack
)
469 struct proc_mm_op mmop
;
471 unsigned long long code_offset
;
472 int code_fd
= phys_mapping(to_phys((void *) &__syscall_stub_start
),
475 mmop
= ((struct proc_mm_op
) { .op
= MM_MMAP
,
479 .len
= UM_KERN_PAGE_SIZE
,
481 .flags
= MAP_FIXED
| MAP_PRIVATE
,
483 .offset
= code_offset
485 CATCH_EINTR(n
= write(fd
, &mmop
, sizeof(mmop
)));
486 if (n
!= sizeof(mmop
)) {
488 printk(UM_KERN_ERR
"mmap args - addr = 0x%lx, fd = %d, "
489 "offset = %llx\n", code
, code_fd
,
490 (unsigned long long) code_offset
);
491 panic("map_stub_pages : /proc/mm map for code failed, "
496 unsigned long long map_offset
;
497 int map_fd
= phys_mapping(to_phys((void *)stack
), &map_offset
);
498 mmop
= ((struct proc_mm_op
)
503 .len
= UM_KERN_PAGE_SIZE
,
504 .prot
= PROT_READ
| PROT_WRITE
,
505 .flags
= MAP_FIXED
| MAP_SHARED
,
509 CATCH_EINTR(n
= write(fd
, &mmop
, sizeof(mmop
)));
510 if (n
!= sizeof(mmop
))
511 panic("map_stub_pages : /proc/mm map for data failed, "
512 "err = %d\n", errno
);
516 void new_thread(void *stack
, jmp_buf *buf
, void (*handler
)(void))
518 (*buf
)[0].JB_IP
= (unsigned long) handler
;
519 (*buf
)[0].JB_SP
= (unsigned long) stack
+ UM_THREAD_SIZE
-
523 #define INIT_JMP_NEW_THREAD 0
524 #define INIT_JMP_CALLBACK 1
525 #define INIT_JMP_HALT 2
526 #define INIT_JMP_REBOOT 3
528 void switch_threads(jmp_buf *me
, jmp_buf *you
)
530 if (UML_SETJMP(me
) == 0)
534 static jmp_buf initial_jmpbuf
;
536 /* XXX Make these percpu */
537 static void (*cb_proc
)(void *arg
);
539 static jmp_buf *cb_back
;
541 int start_idle_thread(void *stack
, jmp_buf *switch_buf
)
545 set_handler(SIGWINCH
, (__sighandler_t
) sig_handler
,
546 SA_ONSTACK
| SA_RESTART
, SIGUSR1
, SIGIO
, SIGVTALRM
, -1);
549 * Can't use UML_SETJMP or UML_LONGJMP here because they save
550 * and restore signals, with the possible side-effect of
551 * trying to handle any signals which came when they were
552 * blocked, which can't be done on this stack.
553 * Signals must be blocked when jumping back here and restored
554 * after returning to the jumper.
556 n
= setjmp(initial_jmpbuf
);
558 case INIT_JMP_NEW_THREAD
:
559 (*switch_buf
)[0].JB_IP
= (unsigned long) new_thread_handler
;
560 (*switch_buf
)[0].JB_SP
= (unsigned long) stack
+
561 UM_THREAD_SIZE
- sizeof(void *);
563 case INIT_JMP_CALLBACK
:
565 longjmp(*cb_back
, 1);
570 case INIT_JMP_REBOOT
:
574 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n
);
576 longjmp(*switch_buf
, 1);
579 void initial_thread_cb_skas(void (*proc
)(void *), void *arg
)
588 if (UML_SETJMP(&here
) == 0)
589 UML_LONGJMP(&initial_jmpbuf
, INIT_JMP_CALLBACK
);
600 UML_LONGJMP(&initial_jmpbuf
, INIT_JMP_HALT
);
603 void reboot_skas(void)
606 UML_LONGJMP(&initial_jmpbuf
, INIT_JMP_REBOOT
);
609 void __switch_mm(struct mm_id
*mm_idp
)
613 /* FIXME: need cpu pid in __switch_mm */
615 err
= ptrace(PTRACE_SWITCH_MM
, userspace_pid
[0], 0,
618 panic("__switch_mm - PTRACE_SWITCH_MM failed, "
619 "errno = %d\n", errno
);
621 else userspace_pid
[0] = mm_idp
->u
.pid
;