2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
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"
30 #include "registers.h"
33 int is_skas_winch(int pid
, int fd
, void *data
)
35 if(pid
!= os_getpgrp())
38 register_winch_irq(-1, fd
, -1, data
);
42 void get_skas_faultinfo(int pid
, struct faultinfo
* fi
)
46 err
= ptrace(PTRACE_FAULTINFO
, pid
, 0, fi
);
48 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
49 "errno = %d\n", errno
);
51 /* Special handling for i386, which has different structs */
52 if (sizeof(struct ptrace_faultinfo
) < sizeof(struct faultinfo
))
53 memset((char *)fi
+ sizeof(struct ptrace_faultinfo
), 0,
54 sizeof(struct faultinfo
) -
55 sizeof(struct ptrace_faultinfo
));
58 static void handle_segv(int pid
, union uml_pt_regs
* regs
)
60 get_skas_faultinfo(pid
, ®s
->skas
.faultinfo
);
61 segv(regs
->skas
.faultinfo
, 0, 1, NULL
);
64 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
65 static void handle_trap(int pid
, union uml_pt_regs
*regs
, int local_using_sysemu
)
69 /* Mark this as a syscall */
70 UPT_SYSCALL_NR(regs
) = PT_SYSCALL_NR(regs
->skas
.regs
);
72 if (!local_using_sysemu
)
74 err
= ptrace(PTRACE_POKEUSR
, 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
) ||
86 (WSTOPSIG(status
) != SIGTRAP
+ 0x80))
87 panic("handle_trap - failed to wait at end of syscall, "
88 "errno = %d, status = %d\n", errno
, status
);
94 static int userspace_tramp(void *arg
)
96 init_new_thread_signals(0);
98 ptrace(PTRACE_TRACEME
, 0, 0, 0);
99 os_stop_process(os_getpid());
103 /* Each element set once, and only accessed by a single processor anyway */
106 int userspace_pid
[NR_CPUS
];
108 void start_userspace(int cpu
)
114 stack
= mmap(NULL
, PAGE_SIZE
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
115 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
116 if(stack
== MAP_FAILED
)
117 panic("start_userspace : mmap failed, errno = %d", errno
);
118 sp
= (unsigned long) stack
+ PAGE_SIZE
- sizeof(void *);
120 pid
= clone(userspace_tramp
, (void *) sp
,
121 CLONE_FILES
| CLONE_VM
| SIGCHLD
, NULL
);
123 panic("start_userspace : clone failed, errno = %d", errno
);
126 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
128 panic("start_userspace : wait failed, errno = %d",
130 } while(WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGVTALRM
));
132 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
133 panic("start_userspace : expected SIGSTOP, got status = %d",
136 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, NULL
, (void *)PTRACE_O_TRACESYSGOOD
) < 0)
137 panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
140 if(munmap(stack
, PAGE_SIZE
) < 0)
141 panic("start_userspace : munmap failed, errno = %d\n", errno
);
143 userspace_pid
[cpu
] = pid
;
146 void userspace(union uml_pt_regs
*regs
)
148 int err
, status
, op
, pid
= userspace_pid
[0];
149 int local_using_sysemu
; /*To prevent races if using_sysemu changes under us.*/
152 restore_registers(pid
, regs
);
154 /* Now we set local_using_sysemu to be used for one loop */
155 local_using_sysemu
= get_using_sysemu();
157 op
= SELECT_PTRACE_OPERATION(local_using_sysemu
, singlestepping(NULL
));
159 err
= ptrace(op
, pid
, 0, 0);
161 panic("userspace - could not resume userspace process, "
162 "pid=%d, ptrace operation = %d, errno = %d\n",
165 CATCH_EINTR(err
= waitpid(pid
, &status
, WUNTRACED
));
167 panic("userspace - waitpid failed, errno = %d\n",
170 regs
->skas
.is_user
= 1;
171 save_registers(pid
, regs
);
172 UPT_SYSCALL_NR(regs
) = -1; /* Assume: It's not a syscall */
174 if(WIFSTOPPED(status
)){
175 switch(WSTOPSIG(status
)){
177 handle_segv(pid
, regs
);
180 handle_trap(pid
, regs
, local_using_sysemu
);
183 relay_signal(SIGTRAP
, regs
);
191 user_signal(WSTOPSIG(status
), regs
, pid
);
194 printk("userspace - child stopped with signal "
195 "%d\n", WSTOPSIG(status
));
199 /* Avoid -ERESTARTSYS handling in host */
200 PT_SYSCALL_NR(regs
->skas
.regs
) = -1;
204 #define INIT_JMP_NEW_THREAD 0
205 #define INIT_JMP_REMOVE_SIGSTACK 1
206 #define INIT_JMP_CALLBACK 2
207 #define INIT_JMP_HALT 3
208 #define INIT_JMP_REBOOT 4
210 void new_thread(void *stack
, void **switch_buf_ptr
, void **fork_buf_ptr
,
211 void (*handler
)(int))
214 sigjmp_buf switch_buf
, fork_buf
;
216 *switch_buf_ptr
= &switch_buf
;
217 *fork_buf_ptr
= &fork_buf
;
219 /* Somewhat subtle - siglongjmp restores the signal mask before doing
220 * the longjmp. This means that when jumping from one stack to another
221 * when the target stack has interrupts enabled, an interrupt may occur
222 * on the source stack. This is bad when starting up a process because
223 * it's not supposed to get timer ticks until it has been scheduled.
224 * So, we disable interrupts around the sigsetjmp to ensure that
225 * they can't happen until we get back here where they are safe.
227 flags
= get_signals();
229 if(sigsetjmp(fork_buf
, 1) == 0)
230 new_thread_proc(stack
, handler
);
237 void thread_wait(void *sw
, void *fb
)
239 sigjmp_buf buf
, **switch_buf
= sw
, *fork_buf
;
243 if(sigsetjmp(buf
, 1) == 0)
244 siglongjmp(*fork_buf
, INIT_JMP_REMOVE_SIGSTACK
);
247 void switch_threads(void *me
, void *next
)
249 sigjmp_buf my_buf
, **me_ptr
= me
, *next_buf
= next
;
252 if(sigsetjmp(my_buf
, 1) == 0)
253 siglongjmp(*next_buf
, 1);
256 static sigjmp_buf initial_jmpbuf
;
258 /* XXX Make these percpu */
259 static void (*cb_proc
)(void *arg
);
261 static sigjmp_buf
*cb_back
;
263 int start_idle_thread(void *stack
, void *switch_buf_ptr
, void **fork_buf_ptr
)
265 sigjmp_buf
**switch_buf
= switch_buf_ptr
;
268 set_handler(SIGWINCH
, (__sighandler_t
) sig_handler
,
269 SA_ONSTACK
| SA_RESTART
, SIGUSR1
, SIGIO
, SIGALRM
,
272 *fork_buf_ptr
= &initial_jmpbuf
;
273 n
= sigsetjmp(initial_jmpbuf
, 1);
275 case INIT_JMP_NEW_THREAD
:
276 new_thread_proc((void *) stack
, new_thread_handler
);
278 case INIT_JMP_REMOVE_SIGSTACK
:
281 case INIT_JMP_CALLBACK
:
283 siglongjmp(*cb_back
, 1);
288 case INIT_JMP_REBOOT
:
292 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n
);
294 siglongjmp(**switch_buf
, 1);
297 void remove_sigstack(void)
299 stack_t stack
= ((stack_t
) { .ss_flags
= SS_DISABLE
,
303 if(sigaltstack(&stack
, NULL
) != 0)
304 panic("disabling signal stack failed, errno = %d\n", errno
);
307 void initial_thread_cb_skas(void (*proc
)(void *), void *arg
)
316 if(sigsetjmp(here
, 1) == 0)
317 siglongjmp(initial_jmpbuf
, INIT_JMP_CALLBACK
);
328 siglongjmp(initial_jmpbuf
, INIT_JMP_HALT
);
331 void reboot_skas(void)
334 siglongjmp(initial_jmpbuf
, INIT_JMP_REBOOT
);
337 void switch_mm_skas(int mm_fd
)
341 #warning need cpu pid in switch_mm_skas
342 err
= ptrace(PTRACE_SWITCH_MM
, userspace_pid
[0], 0, mm_fd
);
344 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
348 void kill_off_processes_skas(void)
350 #warning need to loop over userspace_pids in kill_off_processes_skas
351 os_kill_ptraced_process(userspace_pid
[0], 1);
355 * Overrides for Emacs so that we follow Linus's tabbing style.
356 * Emacs will notice this stuff at the end of the file and automatically
357 * adjust the settings for this buffer only. This must remain at the end
359 * ---------------------------------------------------------------------------
361 * c-file-style: "linux"