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 static void handle_trap(int pid, union uml_pt_regs *regs)
62 int err, syscall_nr, status;
64 syscall_nr = PT_SYSCALL_NR(regs->skas.regs);
65 UPT_SYSCALL_NR(regs) = syscall_nr;
67 relay_signal(SIGTRAP, regs);
71 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
73 panic("handle_trap - nullifying syscall failed errno = %d\n",
76 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
78 panic("handle_trap - continuing to end of syscall failed, "
79 "errno = %d\n", errno);
81 err = waitpid(pid, &status, WUNTRACED);
82 if((err < 0) || !WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
83 panic("handle_trap - failed to wait at end of syscall, "
84 "errno = %d, status = %d\n", errno, status);
89 static int userspace_tramp(void *arg)
91 init_new_thread_signals(0);
93 ptrace(PTRACE_TRACEME, 0, 0, 0);
94 os_stop_process(os_getpid());
98 /* Each element set once, and only accessed by a single processor anyway */
100 int userspace_pid[NR_CPUS];
102 void start_userspace(int cpu)
108 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
109 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
110 if(stack == MAP_FAILED)
111 panic("start_userspace : mmap failed, errno = %d", errno);
112 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
114 pid = clone(userspace_tramp, (void *) sp,
115 CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
117 panic("start_userspace : clone failed, errno = %d", errno);
120 n = waitpid(pid, &status, WUNTRACED);
122 panic("start_userspace : wait failed, errno = %d",
124 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
126 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
127 panic("start_userspace : expected SIGSTOP, got status = %d",
130 if(munmap(stack, PAGE_SIZE) < 0)
131 panic("start_userspace : munmap failed, errno = %d\n", errno);
133 userspace_pid[cpu] = pid;
136 void userspace(union uml_pt_regs *regs)
138 int err, status, op, pid = userspace_pid[0];
140 restore_registers(regs);
142 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
144 panic("userspace - PTRACE_SYSCALL failed, errno = %d\n",
147 err = waitpid(pid, &status, WUNTRACED);
149 panic("userspace - waitpid failed, errno = %d\n",
152 regs->skas.is_user = 1;
153 save_registers(regs);
155 if(WIFSTOPPED(status)){
156 switch(WSTOPSIG(status)){
161 handle_trap(pid, regs);
169 user_signal(WSTOPSIG(status), regs);
172 printk("userspace - child stopped with signal "
173 "%d\n", WSTOPSIG(status));
178 restore_registers(regs);
180 op = singlestepping_skas() ? PTRACE_SINGLESTEP :
182 err = ptrace(op, pid, 0, 0);
184 panic("userspace - PTRACE_SYSCALL failed, "
185 "errno = %d\n", errno);
189 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
190 void (*handler)(int))
193 jmp_buf switch_buf, fork_buf;
195 *switch_buf_ptr = &switch_buf;
196 *fork_buf_ptr = &fork_buf;
198 /* Somewhat subtle - siglongjmp restores the signal mask before doing
199 * the longjmp. This means that when jumping from one stack to another
200 * when the target stack has interrupts enabled, an interrupt may occur
201 * on the source stack. This is bad when starting up a process because
202 * it's not supposed to get timer ticks until it has been scheduled.
203 * So, we disable interrupts around the sigsetjmp to ensure that
204 * they can't happen until we get back here where they are safe.
206 flags = get_signals();
208 if(sigsetjmp(fork_buf, 1) == 0)
209 new_thread_proc(stack, handler);
215 void thread_wait(void *sw, void *fb)
217 jmp_buf buf, **switch_buf = sw, *fork_buf;
221 if(sigsetjmp(buf, 1) == 0)
222 siglongjmp(*fork_buf, 1);
225 static int move_registers(int pid, int int_op, int fp_op,
226 union uml_pt_regs *regs, unsigned long *fp_regs)
228 if(ptrace(int_op, pid, 0, regs->skas.regs) < 0)
230 if(ptrace(fp_op, pid, 0, fp_regs) < 0)
235 void save_registers(union uml_pt_regs *regs)
237 unsigned long *fp_regs;
241 fp_op = PTRACE_GETFPXREGS;
242 fp_regs = regs->skas.xfp;
245 fp_op = PTRACE_GETFPREGS;
246 fp_regs = regs->skas.fp;
249 err = move_registers(userspace_pid[0], PTRACE_GETREGS, fp_op, regs,
252 panic("save_registers - saving registers failed, errno = %d\n",
256 void restore_registers(union uml_pt_regs *regs)
258 unsigned long *fp_regs;
262 fp_op = PTRACE_SETFPXREGS;
263 fp_regs = regs->skas.xfp;
266 fp_op = PTRACE_SETFPREGS;
267 fp_regs = regs->skas.fp;
270 err = move_registers(userspace_pid[0], PTRACE_SETREGS, fp_op, regs,
273 panic("restore_registers - saving registers failed, "
274 "errno = %d\n", -err);
277 void switch_threads(void *me, void *next)
279 jmp_buf my_buf, **me_ptr = me, *next_buf = next;
282 if(sigsetjmp(my_buf, 1) == 0)
283 siglongjmp(*next_buf, 1);
286 static jmp_buf initial_jmpbuf;
288 /* XXX Make these percpu */
289 static void (*cb_proc)(void *arg);
291 static jmp_buf *cb_back;
293 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
295 jmp_buf **switch_buf = switch_buf_ptr;
298 *fork_buf_ptr = &initial_jmpbuf;
299 n = sigsetjmp(initial_jmpbuf, 1);
301 new_thread_proc((void *) stack, new_thread_handler);
306 siglongjmp(*cb_back, 1);
316 siglongjmp(**switch_buf, 1);
319 void remove_sigstack(void)
321 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
325 if(sigaltstack(&stack, NULL) != 0)
326 panic("disabling signal stack failed, errno = %d\n", errno);
329 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
338 if(sigsetjmp(here, 1) == 0)
339 siglongjmp(initial_jmpbuf, 2);
350 siglongjmp(initial_jmpbuf, 3);
353 void reboot_skas(void)
356 siglongjmp(initial_jmpbuf, 4);
361 struct proc_mm_op copy;
362 int n, fd = os_open_file("/proc/mm",
363 of_cloexec(of_write(OPENFLAGS())), 0);
369 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
371 { .copy_segments = from } } );
372 n = os_write_file(fd, ©, sizeof(copy));
373 if(n != sizeof(copy))
374 printk("new_mm : /proc/mm copy_segments failed, "
381 void switch_mm_skas(int mm_fd)
385 #warning need cpu pid in switch_mm_skas
386 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
388 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
392 void kill_off_processes_skas(void)
394 #warning need to loop over userspace_pids in kill_off_processes_skas
395 os_kill_process(userspace_pid[0], 1);
398 void init_registers(int pid)
402 if(ptrace(PTRACE_GETREGS, pid, 0, exec_regs) < 0)
403 panic("check_ptrace : PTRACE_GETREGS failed, errno = %d",
406 err = ptrace(PTRACE_GETFPXREGS, pid, 0, exec_fpx_regs);
412 panic("check_ptrace : PTRACE_GETFPXREGS failed, errno = %d",
415 err = ptrace(PTRACE_GETFPREGS, pid, 0, exec_fp_regs);
417 panic("check_ptrace : PTRACE_GETFPREGS failed, errno = %d",
422 * Overrides for Emacs so that we follow Linus's tabbing style.
423 * Emacs will notice this stuff at the end of the file and automatically
424 * adjust the settings for this buffer only. This must remain at the end
426 * ---------------------------------------------------------------------------
428 * c-file-style: "linux"