2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <sys/ptrace.h>
14 #include <linux/ptrace.h>
17 #include <asm/unistd.h>
19 #include "ptrace_user.h"
20 #include "time_user.h"
21 #include "sysdep/ptrace.h"
22 #include "user_util.h"
23 #include "kern_util.h"
25 #include "sysdep/sigcontext.h"
28 #include "skas_ptrace.h"
29 #include "chan_user.h"
30 #include "signal_user.h"
31 #include "registers.h"
33 int is_skas_winch(int pid, int fd, void *data)
35 if(pid != os_getpid())
38 register_winch_irq(-1, fd, -1, data);
42 static void handle_segv(int pid)
44 struct ptrace_faultinfo fault;
47 err = ptrace(PTRACE_FAULTINFO, pid, 0, &fault);
49 panic("handle_segv - PTRACE_FAULTINFO failed, errno = %d\n",
52 segv(fault.addr, 0, FAULT_WRITE(fault.is_write), 1, NULL);
55 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
56 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
60 /* Mark this as a syscall */
61 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
63 if (!local_using_sysemu)
65 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
67 panic("handle_trap - nullifying syscall failed errno = %d\n",
70 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
72 panic("handle_trap - continuing to end of syscall failed, "
73 "errno = %d\n", errno);
75 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
76 if((err < 0) || !WIFSTOPPED(status) ||
77 (WSTOPSIG(status) != SIGTRAP + 0x80))
78 panic("handle_trap - failed to wait at end of syscall, "
79 "errno = %d, status = %d\n", errno, status);
85 static int userspace_tramp(void *arg)
87 init_new_thread_signals(0);
89 ptrace(PTRACE_TRACEME, 0, 0, 0);
90 os_stop_process(os_getpid());
94 /* Each element set once, and only accessed by a single processor anyway */
97 int userspace_pid[NR_CPUS];
99 void start_userspace(int cpu)
105 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
106 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
107 if(stack == MAP_FAILED)
108 panic("start_userspace : mmap failed, errno = %d", errno);
109 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
111 pid = clone(userspace_tramp, (void *) sp,
112 CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
114 panic("start_userspace : clone failed, errno = %d", errno);
117 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
119 panic("start_userspace : wait failed, errno = %d",
121 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
123 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
124 panic("start_userspace : expected SIGSTOP, got status = %d",
127 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
128 panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
131 if(munmap(stack, PAGE_SIZE) < 0)
132 panic("start_userspace : munmap failed, errno = %d\n", errno);
134 userspace_pid[cpu] = pid;
137 void userspace(union uml_pt_regs *regs)
139 int err, status, op, pid = userspace_pid[0];
140 int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
142 restore_registers(pid, regs);
144 local_using_sysemu = get_using_sysemu();
146 op = local_using_sysemu ? PTRACE_SYSEMU : PTRACE_SYSCALL;
147 err = ptrace(op, pid, 0, 0);
150 panic("userspace - PTRACE_%s failed, errno = %d\n",
151 local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
153 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
155 panic("userspace - waitpid failed, errno = %d\n",
158 regs->skas.is_user = 1;
159 save_registers(pid, regs);
160 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
162 if(WIFSTOPPED(status)){
163 switch(WSTOPSIG(status)){
168 handle_trap(pid, regs, local_using_sysemu);
171 relay_signal(SIGTRAP, regs);
179 user_signal(WSTOPSIG(status), regs);
182 printk("userspace - child stopped with signal "
183 "%d\n", WSTOPSIG(status));
187 /* Avoid -ERESTARTSYS handling in host */
188 PT_SYSCALL_NR(regs->skas.regs) = -1;
191 restore_registers(pid, regs);
193 /*Now we ended the syscall, so re-read local_using_sysemu.*/
194 local_using_sysemu = get_using_sysemu();
196 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
198 err = ptrace(op, pid, 0, 0);
200 panic("userspace - PTRACE_%s failed, "
202 local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
206 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
207 void (*handler)(int))
210 sigjmp_buf switch_buf, fork_buf;
212 *switch_buf_ptr = &switch_buf;
213 *fork_buf_ptr = &fork_buf;
215 /* Somewhat subtle - siglongjmp restores the signal mask before doing
216 * the longjmp. This means that when jumping from one stack to another
217 * when the target stack has interrupts enabled, an interrupt may occur
218 * on the source stack. This is bad when starting up a process because
219 * it's not supposed to get timer ticks until it has been scheduled.
220 * So, we disable interrupts around the sigsetjmp to ensure that
221 * they can't happen until we get back here where they are safe.
223 flags = get_signals();
225 if(sigsetjmp(fork_buf, 1) == 0)
226 new_thread_proc(stack, handler);
233 void thread_wait(void *sw, void *fb)
235 sigjmp_buf buf, **switch_buf = sw, *fork_buf;
239 if(sigsetjmp(buf, 1) == 0)
240 siglongjmp(*fork_buf, 1);
243 void switch_threads(void *me, void *next)
245 sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
248 if(sigsetjmp(my_buf, 1) == 0)
249 siglongjmp(*next_buf, 1);
252 static sigjmp_buf initial_jmpbuf;
254 /* XXX Make these percpu */
255 static void (*cb_proc)(void *arg);
257 static sigjmp_buf *cb_back;
259 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
261 sigjmp_buf **switch_buf = switch_buf_ptr;
264 *fork_buf_ptr = &initial_jmpbuf;
265 n = sigsetjmp(initial_jmpbuf, 1);
267 new_thread_proc((void *) stack, new_thread_handler);
272 siglongjmp(*cb_back, 1);
282 siglongjmp(**switch_buf, 1);
285 void remove_sigstack(void)
287 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
291 if(sigaltstack(&stack, NULL) != 0)
292 panic("disabling signal stack failed, errno = %d\n", errno);
295 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
304 if(sigsetjmp(here, 1) == 0)
305 siglongjmp(initial_jmpbuf, 2);
316 siglongjmp(initial_jmpbuf, 3);
319 void reboot_skas(void)
322 siglongjmp(initial_jmpbuf, 4);
325 void switch_mm_skas(int mm_fd)
329 #warning need cpu pid in switch_mm_skas
330 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
332 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
336 void kill_off_processes_skas(void)
338 #warning need to loop over userspace_pids in kill_off_processes_skas
339 os_kill_ptraced_process(userspace_pid[0], 1);
343 * Overrides for Emacs so that we follow Linus's tabbing style.
344 * Emacs will notice this stuff at the end of the file and automatically
345 * adjust the settings for this buffer only. This must remain at the end
347 * ---------------------------------------------------------------------------
349 * c-file-style: "linux"