2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
15 #include <asm/unistd.h>
17 #include "ptrace_user.h"
18 #include "time_user.h"
19 #include "sysdep/ptrace.h"
20 #include "user_util.h"
21 #include "kern_util.h"
23 #include "sysdep/sigcontext.h"
26 #include "skas_ptrace.h"
27 #include "chan_user.h"
28 #include "signal_user.h"
29 #include "registers.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 static void handle_segv(int pid)
42 struct ptrace_faultinfo fault;
45 err = ptrace(PTRACE_FAULTINFO, pid, 0, &fault);
47 panic("handle_segv - PTRACE_FAULTINFO failed, errno = %d\n",
50 segv(fault.addr, 0, FAULT_WRITE(fault.is_write), 1, NULL);
53 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
54 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
58 /* Mark this as a syscall */
59 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
61 if (!local_using_sysemu)
63 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
65 panic("handle_trap - nullifying syscall failed errno = %d\n",
68 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
70 panic("handle_trap - continuing to end of syscall failed, "
71 "errno = %d\n", errno);
73 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
74 if((err < 0) || !WIFSTOPPED(status) ||
75 (WSTOPSIG(status) != SIGTRAP + 0x80))
76 panic("handle_trap - failed to wait at end of syscall, "
77 "errno = %d, status = %d\n", errno, status);
83 static int userspace_tramp(void *arg)
85 init_new_thread_signals(0);
87 ptrace(PTRACE_TRACEME, 0, 0, 0);
88 os_stop_process(os_getpid());
92 /* Each element set once, and only accessed by a single processor anyway */
95 int userspace_pid[NR_CPUS];
97 void start_userspace(int cpu)
103 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
104 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
105 if(stack == MAP_FAILED)
106 panic("start_userspace : mmap failed, errno = %d", errno);
107 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
109 pid = clone(userspace_tramp, (void *) sp,
110 CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
112 panic("start_userspace : clone failed, errno = %d", errno);
115 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
117 panic("start_userspace : wait failed, errno = %d",
119 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
121 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
122 panic("start_userspace : expected SIGSTOP, got status = %d",
125 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
126 panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
129 if(munmap(stack, PAGE_SIZE) < 0)
130 panic("start_userspace : munmap failed, errno = %d\n", errno);
132 userspace_pid[cpu] = pid;
135 void userspace(union uml_pt_regs *regs)
137 int err, status, op, pid = userspace_pid[0];
138 int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
141 restore_registers(pid, regs);
143 /* Now we set local_using_sysemu to be used for one loop */
144 local_using_sysemu = get_using_sysemu();
146 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
148 err = ptrace(op, pid, 0, 0);
150 panic("userspace - could not resume userspace process, "
151 "pid=%d, ptrace operation = %d, errno = %d\n",
154 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
156 panic("userspace - waitpid failed, errno = %d\n",
159 regs->skas.is_user = 1;
160 save_registers(pid, regs);
161 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
163 if(WIFSTOPPED(status)){
164 switch(WSTOPSIG(status)){
169 handle_trap(pid, regs, local_using_sysemu);
172 relay_signal(SIGTRAP, regs);
180 user_signal(WSTOPSIG(status), regs);
183 printk("userspace - child stopped with signal "
184 "%d\n", WSTOPSIG(status));
188 /* Avoid -ERESTARTSYS handling in host */
189 PT_SYSCALL_NR(regs->skas.regs) = -1;
194 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
195 void (*handler)(int))
198 sigjmp_buf switch_buf, fork_buf;
200 *switch_buf_ptr = &switch_buf;
201 *fork_buf_ptr = &fork_buf;
203 /* Somewhat subtle - siglongjmp restores the signal mask before doing
204 * the longjmp. This means that when jumping from one stack to another
205 * when the target stack has interrupts enabled, an interrupt may occur
206 * on the source stack. This is bad when starting up a process because
207 * it's not supposed to get timer ticks until it has been scheduled.
208 * So, we disable interrupts around the sigsetjmp to ensure that
209 * they can't happen until we get back here where they are safe.
211 flags = get_signals();
213 if(sigsetjmp(fork_buf, 1) == 0)
214 new_thread_proc(stack, handler);
221 void thread_wait(void *sw, void *fb)
223 sigjmp_buf buf, **switch_buf = sw, *fork_buf;
227 if(sigsetjmp(buf, 1) == 0)
228 siglongjmp(*fork_buf, 1);
231 void switch_threads(void *me, void *next)
233 sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
236 if(sigsetjmp(my_buf, 1) == 0)
237 siglongjmp(*next_buf, 1);
240 static sigjmp_buf initial_jmpbuf;
242 /* XXX Make these percpu */
243 static void (*cb_proc)(void *arg);
245 static sigjmp_buf *cb_back;
247 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
249 sigjmp_buf **switch_buf = switch_buf_ptr;
252 *fork_buf_ptr = &initial_jmpbuf;
253 n = sigsetjmp(initial_jmpbuf, 1);
255 new_thread_proc((void *) stack, new_thread_handler);
260 siglongjmp(*cb_back, 1);
270 siglongjmp(**switch_buf, 1);
273 void remove_sigstack(void)
275 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
279 if(sigaltstack(&stack, NULL) != 0)
280 panic("disabling signal stack failed, errno = %d\n", errno);
283 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
292 if(sigsetjmp(here, 1) == 0)
293 siglongjmp(initial_jmpbuf, 2);
304 siglongjmp(initial_jmpbuf, 3);
307 void reboot_skas(void)
310 siglongjmp(initial_jmpbuf, 4);
313 void switch_mm_skas(int mm_fd)
317 #warning need cpu pid in switch_mm_skas
318 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
320 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
324 void kill_off_processes_skas(void)
326 #warning need to loop over userspace_pids in kill_off_processes_skas
327 os_kill_ptraced_process(userspace_pid[0], 1);
331 * Overrides for Emacs so that we follow Linus's tabbing style.
332 * Emacs will notice this stuff at the end of the file and automatically
333 * adjust the settings for this buffer only. This must remain at the end
335 * ---------------------------------------------------------------------------
337 * c-file-style: "linux"