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)
33 if(pid != os_getpid())
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, pt_syscall_parm, 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 pt_syscall_parm = local_using_sysemu ? PTRACE_SYSEMU : PTRACE_SYSCALL;
150 err = ptrace(pt_syscall_parm, pid, 0, 0);
153 panic("userspace - PTRACE_%s failed, errno = %d\n",
154 local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
156 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
158 panic("userspace - waitpid failed, errno = %d\n",
161 regs->skas.is_user = 1;
162 save_registers(regs);
164 if(WIFSTOPPED(status)){
165 switch(WSTOPSIG(status)){
170 handle_trap(pid, regs, local_using_sysemu);
178 user_signal(WSTOPSIG(status), regs);
181 printk("userspace - child stopped with signal "
182 "%d\n", WSTOPSIG(status));
187 restore_registers(regs);
189 /*Now we ended the syscall, so re-read local_using_sysemu.*/
190 local_using_sysemu = get_using_sysemu();
191 pt_syscall_parm = local_using_sysemu ? PTRACE_SYSEMU : PTRACE_SYSCALL;
193 op = singlestepping(NULL) ? PTRACE_SINGLESTEP :
196 err = ptrace(op, pid, 0, 0);
198 panic("userspace - PTRACE_%s failed, "
200 local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
204 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
205 void (*handler)(int))
208 sigjmp_buf switch_buf, fork_buf;
210 *switch_buf_ptr = &switch_buf;
211 *fork_buf_ptr = &fork_buf;
213 /* Somewhat subtle - siglongjmp restores the signal mask before doing
214 * the longjmp. This means that when jumping from one stack to another
215 * when the target stack has interrupts enabled, an interrupt may occur
216 * on the source stack. This is bad when starting up a process because
217 * it's not supposed to get timer ticks until it has been scheduled.
218 * So, we disable interrupts around the sigsetjmp to ensure that
219 * they can't happen until we get back here where they are safe.
221 flags = get_signals();
223 if(sigsetjmp(fork_buf, 1) == 0)
224 new_thread_proc(stack, handler);
230 void thread_wait(void *sw, void *fb)
232 sigjmp_buf buf, **switch_buf = sw, *fork_buf;
236 if(sigsetjmp(buf, 1) == 0)
237 siglongjmp(*fork_buf, 1);
240 static int move_registers(int pid, int int_op, int fp_op,
241 union uml_pt_regs *regs, unsigned long *fp_regs)
243 if(ptrace(int_op, pid, 0, regs->skas.regs) < 0)
245 if(ptrace(fp_op, pid, 0, fp_regs) < 0)
250 void save_registers(union uml_pt_regs *regs)
252 unsigned long *fp_regs;
256 fp_op = PTRACE_GETFPXREGS;
257 fp_regs = regs->skas.xfp;
260 fp_op = PTRACE_GETFPREGS;
261 fp_regs = regs->skas.fp;
264 err = move_registers(userspace_pid[0], PTRACE_GETREGS, fp_op, regs,
267 panic("save_registers - saving registers failed, errno = %d\n",
271 void restore_registers(union uml_pt_regs *regs)
273 unsigned long *fp_regs;
277 fp_op = PTRACE_SETFPXREGS;
278 fp_regs = regs->skas.xfp;
281 fp_op = PTRACE_SETFPREGS;
282 fp_regs = regs->skas.fp;
285 err = move_registers(userspace_pid[0], PTRACE_SETREGS, fp_op, regs,
288 panic("restore_registers - saving registers failed, "
289 "errno = %d\n", -err);
292 void switch_threads(void *me, void *next)
294 sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
297 if(sigsetjmp(my_buf, 1) == 0)
298 siglongjmp(*next_buf, 1);
301 static sigjmp_buf initial_jmpbuf;
303 /* XXX Make these percpu */
304 static void (*cb_proc)(void *arg);
306 static sigjmp_buf *cb_back;
308 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
310 sigjmp_buf **switch_buf = switch_buf_ptr;
313 *fork_buf_ptr = &initial_jmpbuf;
314 n = sigsetjmp(initial_jmpbuf, 1);
316 new_thread_proc((void *) stack, new_thread_handler);
321 siglongjmp(*cb_back, 1);
331 siglongjmp(**switch_buf, 1);
334 void remove_sigstack(void)
336 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
340 if(sigaltstack(&stack, NULL) != 0)
341 panic("disabling signal stack failed, errno = %d\n", errno);
344 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
353 if(sigsetjmp(here, 1) == 0)
354 siglongjmp(initial_jmpbuf, 2);
365 siglongjmp(initial_jmpbuf, 3);
368 void reboot_skas(void)
371 siglongjmp(initial_jmpbuf, 4);
374 void switch_mm_skas(int mm_fd)
378 #warning need cpu pid in switch_mm_skas
379 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
381 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
385 void kill_off_processes_skas(void)
387 #warning need to loop over userspace_pids in kill_off_processes_skas
388 os_kill_ptraced_process(userspace_pid[0], 1);
391 void init_registers(int pid)
395 if(ptrace(PTRACE_GETREGS, pid, 0, exec_regs) < 0)
396 panic("check_ptrace : PTRACE_GETREGS failed, errno = %d",
399 err = ptrace(PTRACE_GETFPXREGS, pid, 0, exec_fpx_regs);
405 panic("check_ptrace : PTRACE_GETFPXREGS failed, errno = %d",
408 err = ptrace(PTRACE_GETFPREGS, pid, 0, exec_fp_regs);
410 panic("check_ptrace : PTRACE_GETFPREGS failed, errno = %d",
415 * Overrides for Emacs so that we follow Linus's tabbing style.
416 * Emacs will notice this stuff at the end of the file and automatically
417 * adjust the settings for this buffer only. This must remain at the end
419 * ---------------------------------------------------------------------------
421 * c-file-style: "linux"