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 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 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_SYSCALL failed, errno = %d\n",
157 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_SYSCALL failed, "
203 "errno = %d\n", errno);
207 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
208 void (*handler)(int))
211 jmp_buf switch_buf, fork_buf;
213 *switch_buf_ptr = &switch_buf;
214 *fork_buf_ptr = &fork_buf;
216 /* Somewhat subtle - siglongjmp restores the signal mask before doing
217 * the longjmp. This means that when jumping from one stack to another
218 * when the target stack has interrupts enabled, an interrupt may occur
219 * on the source stack. This is bad when starting up a process because
220 * it's not supposed to get timer ticks until it has been scheduled.
221 * So, we disable interrupts around the sigsetjmp to ensure that
222 * they can't happen until we get back here where they are safe.
224 flags = get_signals();
226 if(sigsetjmp(fork_buf, 1) == 0)
227 new_thread_proc(stack, handler);
233 void thread_wait(void *sw, void *fb)
235 jmp_buf buf, **switch_buf = sw, *fork_buf;
239 if(sigsetjmp(buf, 1) == 0)
240 siglongjmp(*fork_buf, 1);
243 static int move_registers(int pid, int int_op, int fp_op,
244 union uml_pt_regs *regs, unsigned long *fp_regs)
246 if(ptrace(int_op, pid, 0, regs->skas.regs) < 0)
248 if(ptrace(fp_op, pid, 0, fp_regs) < 0)
253 void save_registers(union uml_pt_regs *regs)
255 unsigned long *fp_regs;
259 fp_op = PTRACE_GETFPXREGS;
260 fp_regs = regs->skas.xfp;
263 fp_op = PTRACE_GETFPREGS;
264 fp_regs = regs->skas.fp;
267 err = move_registers(userspace_pid[0], PTRACE_GETREGS, fp_op, regs,
270 panic("save_registers - saving registers failed, errno = %d\n",
274 void restore_registers(union uml_pt_regs *regs)
276 unsigned long *fp_regs;
280 fp_op = PTRACE_SETFPXREGS;
281 fp_regs = regs->skas.xfp;
284 fp_op = PTRACE_SETFPREGS;
285 fp_regs = regs->skas.fp;
288 err = move_registers(userspace_pid[0], PTRACE_SETREGS, fp_op, regs,
291 panic("restore_registers - saving registers failed, "
292 "errno = %d\n", -err);
295 void switch_threads(void *me, void *next)
297 jmp_buf my_buf, **me_ptr = me, *next_buf = next;
300 if(sigsetjmp(my_buf, 1) == 0)
301 siglongjmp(*next_buf, 1);
304 static jmp_buf initial_jmpbuf;
306 /* XXX Make these percpu */
307 static void (*cb_proc)(void *arg);
309 static jmp_buf *cb_back;
311 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
313 jmp_buf **switch_buf = switch_buf_ptr;
316 *fork_buf_ptr = &initial_jmpbuf;
317 n = sigsetjmp(initial_jmpbuf, 1);
319 new_thread_proc((void *) stack, new_thread_handler);
324 siglongjmp(*cb_back, 1);
334 siglongjmp(**switch_buf, 1);
337 void remove_sigstack(void)
339 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
343 if(sigaltstack(&stack, NULL) != 0)
344 panic("disabling signal stack failed, errno = %d\n", errno);
347 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
356 if(sigsetjmp(here, 1) == 0)
357 siglongjmp(initial_jmpbuf, 2);
368 siglongjmp(initial_jmpbuf, 3);
371 void reboot_skas(void)
374 siglongjmp(initial_jmpbuf, 4);
379 struct proc_mm_op copy;
380 int n, fd = os_open_file("/proc/mm",
381 of_cloexec(of_write(OPENFLAGS())), 0);
387 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
389 { .copy_segments = from } } );
390 n = os_write_file(fd, ©, sizeof(copy));
391 if(n != sizeof(copy))
392 printk("new_mm : /proc/mm copy_segments failed, "
399 void switch_mm_skas(int mm_fd)
403 #warning need cpu pid in switch_mm_skas
404 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
406 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
410 void kill_off_processes_skas(void)
412 #warning need to loop over userspace_pids in kill_off_processes_skas
413 os_kill_process(userspace_pid[0], 1);
416 void init_registers(int pid)
420 if(ptrace(PTRACE_GETREGS, pid, 0, exec_regs) < 0)
421 panic("check_ptrace : PTRACE_GETREGS failed, errno = %d",
424 err = ptrace(PTRACE_GETFPXREGS, pid, 0, exec_fpx_regs);
430 panic("check_ptrace : PTRACE_GETFPXREGS failed, errno = %d",
433 err = ptrace(PTRACE_GETFPREGS, pid, 0, exec_fp_regs);
435 panic("check_ptrace : PTRACE_GETFPREGS failed, errno = %d",
440 * Overrides for Emacs so that we follow Linus's tabbing style.
441 * Emacs will notice this stuff at the end of the file and automatically
442 * adjust the settings for this buffer only. This must remain at the end
444 * ---------------------------------------------------------------------------
446 * c-file-style: "linux"