2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <sys/ptrace.h>
16 #include <asm/unistd.h>
18 #include "ptrace_user.h"
19 #include "time_user.h"
20 #include "sysdep/ptrace.h"
21 #include "user_util.h"
22 #include "kern_util.h"
24 #include "sysdep/sigcontext.h"
27 #include "skas_ptrace.h"
28 #include "chan_user.h"
29 #include "signal_user.h"
31 int is_skas_winch(int pid, int fd, void *data)
36 register_winch_irq(-1, fd, -1, data);
40 /* These are set once at boot time and not changed thereafter */
42 unsigned long exec_regs[FRAME_SIZE];
43 unsigned long exec_fp_regs[HOST_FP_SIZE];
44 unsigned long exec_fpx_regs[HOST_XFP_SIZE];
45 int have_fpx_regs = 1;
47 static void handle_segv(int pid)
49 struct ptrace_faultinfo fault;
52 err = ptrace(PTRACE_FAULTINFO, pid, 0, &fault);
54 panic("handle_segv - PTRACE_FAULTINFO failed, errno = %d\n",
57 segv(fault.addr, 0, FAULT_WRITE(fault.is_write), 1, NULL);
60 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
61 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
63 int err, syscall_nr, status;
65 syscall_nr = PT_SYSCALL_NR(regs->skas.regs);
66 UPT_SYSCALL_NR(regs) = syscall_nr;
68 relay_signal(SIGTRAP, regs);
72 if (!local_using_sysemu)
74 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
76 panic("handle_trap - nullifying syscall failed errno = %d\n",
79 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
81 panic("handle_trap - continuing to end of syscall failed, "
82 "errno = %d\n", errno);
84 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
85 if((err < 0) || !WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
86 panic("handle_trap - failed to wait at end of syscall, "
87 "errno = %d, status = %d\n", errno, status);
93 static int userspace_tramp(void *arg)
95 init_new_thread_signals(0);
97 ptrace(PTRACE_TRACEME, 0, 0, 0);
98 os_stop_process(os_getpid());
102 /* Each element set once, and only accessed by a single processor anyway */
104 int userspace_pid[NR_CPUS];
106 void start_userspace(int cpu)
112 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
113 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
114 if(stack == MAP_FAILED)
115 panic("start_userspace : mmap failed, errno = %d", errno);
116 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
118 pid = clone(userspace_tramp, (void *) sp,
119 CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
121 panic("start_userspace : clone failed, errno = %d", errno);
124 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
126 panic("start_userspace : wait failed, errno = %d",
128 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
130 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
131 panic("start_userspace : expected SIGSTOP, got status = %d",
134 if(munmap(stack, PAGE_SIZE) < 0)
135 panic("start_userspace : munmap failed, errno = %d\n", errno);
137 userspace_pid[cpu] = pid;
140 void userspace(union uml_pt_regs *regs)
142 int err, status, op, pid = userspace_pid[0];
143 int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
145 restore_registers(regs);
147 local_using_sysemu = get_using_sysemu();
149 if (local_using_sysemu)
150 err = ptrace(PTRACE_SYSEMU, pid, 0, 0);
152 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
154 panic("userspace - PTRACE_%s failed, errno = %d\n",
155 local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
157 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
159 panic("userspace - waitpid failed, errno = %d\n",
162 regs->skas.is_user = 1;
163 save_registers(regs);
165 if(WIFSTOPPED(status)){
166 switch(WSTOPSIG(status)){
171 handle_trap(pid, regs, local_using_sysemu);
179 user_signal(WSTOPSIG(status), regs);
182 printk("userspace - child stopped with signal "
183 "%d\n", WSTOPSIG(status));
188 restore_registers(regs);
190 /*Now we ended the syscall, so re-read local_using_sysemu.*/
191 local_using_sysemu = get_using_sysemu();
193 if (local_using_sysemu)
194 op = singlestepping_skas() ? PTRACE_SINGLESTEP :
197 op = singlestepping_skas() ? PTRACE_SINGLESTEP :
200 err = ptrace(op, pid, 0, 0);
202 panic("userspace - PTRACE_%s failed, "
204 local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
208 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
209 void (*handler)(int))
212 sigjmp_buf switch_buf, fork_buf;
214 *switch_buf_ptr = &switch_buf;
215 *fork_buf_ptr = &fork_buf;
217 /* Somewhat subtle - siglongjmp restores the signal mask before doing
218 * the longjmp. This means that when jumping from one stack to another
219 * when the target stack has interrupts enabled, an interrupt may occur
220 * on the source stack. This is bad when starting up a process because
221 * it's not supposed to get timer ticks until it has been scheduled.
222 * So, we disable interrupts around the sigsetjmp to ensure that
223 * they can't happen until we get back here where they are safe.
225 flags = get_signals();
227 if(sigsetjmp(fork_buf, 1) == 0)
228 new_thread_proc(stack, handler);
234 void thread_wait(void *sw, void *fb)
236 sigjmp_buf buf, **switch_buf = sw, *fork_buf;
240 if(sigsetjmp(buf, 1) == 0)
241 siglongjmp(*fork_buf, 1);
244 static int move_registers(int pid, int int_op, int fp_op,
245 union uml_pt_regs *regs, unsigned long *fp_regs)
247 if(ptrace(int_op, pid, 0, regs->skas.regs) < 0)
249 if(ptrace(fp_op, pid, 0, fp_regs) < 0)
254 void save_registers(union uml_pt_regs *regs)
256 unsigned long *fp_regs;
260 fp_op = PTRACE_GETFPXREGS;
261 fp_regs = regs->skas.xfp;
264 fp_op = PTRACE_GETFPREGS;
265 fp_regs = regs->skas.fp;
268 err = move_registers(userspace_pid[0], PTRACE_GETREGS, fp_op, regs,
271 panic("save_registers - saving registers failed, errno = %d\n",
275 void restore_registers(union uml_pt_regs *regs)
277 unsigned long *fp_regs;
281 fp_op = PTRACE_SETFPXREGS;
282 fp_regs = regs->skas.xfp;
285 fp_op = PTRACE_SETFPREGS;
286 fp_regs = regs->skas.fp;
289 err = move_registers(userspace_pid[0], PTRACE_SETREGS, fp_op, regs,
292 panic("restore_registers - saving registers failed, "
293 "errno = %d\n", -err);
296 void switch_threads(void *me, void *next)
298 sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
301 if(sigsetjmp(my_buf, 1) == 0)
302 siglongjmp(*next_buf, 1);
305 static sigjmp_buf initial_jmpbuf;
307 /* XXX Make these percpu */
308 static void (*cb_proc)(void *arg);
310 static sigjmp_buf *cb_back;
312 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
314 sigjmp_buf **switch_buf = switch_buf_ptr;
317 *fork_buf_ptr = &initial_jmpbuf;
318 n = sigsetjmp(initial_jmpbuf, 1);
320 new_thread_proc((void *) stack, new_thread_handler);
325 siglongjmp(*cb_back, 1);
335 siglongjmp(**switch_buf, 1);
338 void remove_sigstack(void)
340 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
344 if(sigaltstack(&stack, NULL) != 0)
345 panic("disabling signal stack failed, errno = %d\n", errno);
348 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
357 if(sigsetjmp(here, 1) == 0)
358 siglongjmp(initial_jmpbuf, 2);
369 siglongjmp(initial_jmpbuf, 3);
372 void reboot_skas(void)
375 siglongjmp(initial_jmpbuf, 4);
378 void switch_mm_skas(int mm_fd)
382 #warning need cpu pid in switch_mm_skas
383 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
385 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
389 void kill_off_processes_skas(void)
391 #warning need to loop over userspace_pids in kill_off_processes_skas
392 os_kill_process(userspace_pid[0], 1);
395 void init_registers(int pid)
399 if(ptrace(PTRACE_GETREGS, pid, 0, exec_regs) < 0)
400 panic("check_ptrace : PTRACE_GETREGS failed, errno = %d",
403 err = ptrace(PTRACE_GETFPXREGS, pid, 0, exec_fpx_regs);
409 panic("check_ptrace : PTRACE_GETFPXREGS failed, errno = %d",
412 err = ptrace(PTRACE_GETFPREGS, pid, 0, exec_fp_regs);
414 panic("check_ptrace : PTRACE_GETFPREGS failed, errno = %d",
419 * Overrides for Emacs so that we follow Linus's tabbing style.
420 * Emacs will notice this stuff at the end of the file and automatically
421 * adjust the settings for this buffer only. This must remain at the end
423 * ---------------------------------------------------------------------------
425 * c-file-style: "linux"