2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
12 #include <sys/ptrace.h>
15 #include <asm/unistd.h>
17 #include "ptrace_user.h"
18 #include "time_user.h"
19 #include "sysdep/ptrace.h"
20 #include "user_util.h"
21 #include "kern_util.h"
23 #include "sysdep/sigcontext.h"
26 #include "skas_ptrace.h"
28 unsigned long exec_regs[FRAME_SIZE];
29 unsigned long exec_fp_regs[HOST_FP_SIZE];
30 unsigned long exec_fpx_regs[HOST_XFP_SIZE];
31 int have_fpx_regs = 1;
33 static void handle_segv(int pid)
35 struct ptrace_faultinfo fault;
38 err = ptrace(PTRACE_FAULTINFO, pid, 0, &fault);
40 panic("handle_segv - PTRACE_FAULTINFO failed, errno = %d\n",
43 segv(fault.addr, 0, FAULT_WRITE(fault.is_write), 1, NULL);
46 static void handle_trap(int pid, union uml_pt_regs *regs)
48 int err, syscall_nr, status;
50 syscall_nr = PT_SYSCALL_NR(regs->skas.regs);
52 relay_signal(SIGTRAP, regs);
55 UPT_SYSCALL_NR(regs) = syscall_nr;
57 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
59 panic("handle_trap - nullifying syscall failed errno = %d\n",
62 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
64 panic("handle_trap - continuing to end of syscall failed, "
65 "errno = %d\n", errno);
67 err = waitpid(pid, &status, WUNTRACED);
68 if((err < 0) || !WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
69 panic("handle_trap - failed to wait at end of syscall, "
70 "errno = %d, status = %d\n", errno, status);
77 static int userspace_tramp(void *arg)
79 init_new_thread_signals(0);
81 ptrace(PTRACE_TRACEME, 0, 0, 0);
82 os_stop_process(os_getpid());
86 void start_userspace(void)
92 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
93 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
94 if(stack == MAP_FAILED)
95 panic("start_userspace : mmap failed, errno = %d", errno);
96 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
98 pid = clone(userspace_tramp, (void *) sp,
99 CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
101 panic("start_userspace : clone failed, errno = %d", errno);
104 n = waitpid(pid, &status, WUNTRACED);
106 panic("start_userspace : wait failed, errno = %d",
108 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
110 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
111 panic("start_userspace : expected SIGSTOP, got status = %d",
114 if(munmap(stack, PAGE_SIZE) < 0)
115 panic("start_userspace : munmap failed, errno = %d\n", errno);
120 void userspace(union uml_pt_regs *regs)
124 restore_registers(regs);
126 err = ptrace(PTRACE_SYSCALL, userspace_pid, 0, 0);
128 panic("userspace - PTRACE_SYSCALL failed, errno = %d\n",
131 err = waitpid(userspace_pid, &status, WUNTRACED);
133 panic("userspace - waitpid failed, errno = %d\n",
136 regs->skas.is_user = 1;
137 save_registers(regs);
139 if(WIFSTOPPED(status)){
140 switch(WSTOPSIG(status)){
142 handle_segv(userspace_pid);
145 handle_trap(userspace_pid, regs);
152 user_signal(WSTOPSIG(status), regs);
155 printk("userspace - child stopped with signal "
156 "%d\n", WSTOPSIG(status));
161 restore_registers(regs);
163 op = singlestepping_skas() ? PTRACE_SINGLESTEP :
165 err = ptrace(op, userspace_pid, 0, 0);
167 panic("userspace - PTRACE_SYSCALL failed, "
168 "errno = %d\n", errno);
172 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
173 void (*handler)(int))
175 jmp_buf switch_buf, fork_buf;
177 *switch_buf_ptr = &switch_buf;
178 *fork_buf_ptr = &fork_buf;
180 if(setjmp(fork_buf) == 0)
181 new_thread_proc(stack, handler);
186 void thread_wait(void *sw, void *fb)
188 jmp_buf buf, **switch_buf = sw, *fork_buf;
193 longjmp(*fork_buf, 1);
196 static int move_registers(int int_op, int fp_op, union uml_pt_regs *regs,
197 unsigned long *fp_regs)
199 if(ptrace(int_op, userspace_pid, 0, regs->skas.regs) < 0)
201 if(ptrace(fp_op, userspace_pid, 0, fp_regs) < 0)
206 void save_registers(union uml_pt_regs *regs)
208 unsigned long *fp_regs;
212 fp_op = PTRACE_GETFPXREGS;
213 fp_regs = regs->skas.xfp;
216 fp_op = PTRACE_GETFPREGS;
217 fp_regs = regs->skas.fp;
220 err = move_registers(PTRACE_GETREGS, fp_op, regs, fp_regs);
222 panic("save_registers - saving registers failed, errno = %d\n",
226 void restore_registers(union uml_pt_regs *regs)
228 unsigned long *fp_regs;
232 fp_op = PTRACE_SETFPXREGS;
233 fp_regs = regs->skas.xfp;
236 fp_op = PTRACE_SETFPREGS;
237 fp_regs = regs->skas.fp;
240 err = move_registers(PTRACE_SETREGS, fp_op, regs, fp_regs);
242 panic("restore_registers - saving registers failed, "
243 "errno = %d\n", err);
246 void switch_threads(void *me, void *next)
248 jmp_buf my_buf, **me_ptr = me, *next_buf = next;
251 if(setjmp(my_buf) == 0)
252 longjmp(*next_buf, 1);
255 static jmp_buf initial_jmpbuf;
257 /* XXX Make these percpu */
258 static void (*cb_proc)(void *arg);
260 static jmp_buf *cb_back;
262 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
264 jmp_buf **switch_buf = switch_buf_ptr;
267 *fork_buf_ptr = &initial_jmpbuf;
268 n = setjmp(initial_jmpbuf);
270 new_thread_proc((void *) stack, new_thread_handler);
275 longjmp(*cb_back, 1);
285 longjmp(**switch_buf, 1);
288 void remove_sigstack(void)
290 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
294 if(sigaltstack(&stack, NULL) != 0)
295 panic("disabling signal stack failed, errno = %d\n", errno);
298 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
307 if(setjmp(here) == 0)
308 longjmp(initial_jmpbuf, 2);
319 longjmp(initial_jmpbuf, 3);
322 void reboot_skas(void)
325 longjmp(initial_jmpbuf, 4);
330 struct proc_mm_op copy;
331 int n, fd = os_open_file("/proc/mm", of_write(OPENFLAGS()), 0);
337 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
339 { .copy_segments = from } } );
340 n = os_write_file(fd, ©, sizeof(copy));
341 if(n != sizeof(copy))
342 printk("new_mm : /proc/mm copy_segments failed, "
343 "errno = %d\n", errno);
348 void switch_mm_skas(int mm_fd)
352 err = ptrace(PTRACE_SWITCH_MM, userspace_pid, 0, mm_fd);
354 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
358 void kill_off_processes_skas(void)
360 os_kill_process(userspace_pid, 1);
363 void init_registers(int pid)
367 if(ptrace(PTRACE_GETREGS, pid, 0, exec_regs) < 0)
368 panic("check_ptrace : PTRACE_GETREGS failed, errno = %d",
371 err = ptrace(PTRACE_GETFPXREGS, pid, 0, exec_fpx_regs);
377 panic("check_ptrace : PTRACE_GETFPXREGS failed, errno = %d",
380 err = ptrace(PTRACE_GETFPREGS, pid, 0, exec_fp_regs);
382 panic("check_ptrace : PTRACE_GETFPREGS failed, errno = %d",
387 * Overrides for Emacs so that we follow Linus's tabbing style.
388 * Emacs will notice this stuff at the end of the file and automatically
389 * adjust the settings for this buffer only. This must remain at the end
391 * ---------------------------------------------------------------------------
393 * c-file-style: "linux"