blob: 4dc13bc8cfd8faf87d499f2a9dfca050327905c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdlib.h>
Bodo Stroesserc5784552005-05-05 16:15:31 -07007#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <unistd.h>
9#include <errno.h>
10#include <signal.h>
11#include <setjmp.h>
12#include <sched.h>
13#include <sys/wait.h>
14#include <sys/mman.h>
15#include <sys/user.h>
16#include <asm/unistd.h>
17#include "user.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"
23#include "skas.h"
24#include "sysdep/sigcontext.h"
25#include "os.h"
26#include "proc_mm.h"
27#include "skas_ptrace.h"
28#include "chan_user.h"
29#include "signal_user.h"
30#include "registers.h"
Jeff Dikecd2ee4a2005-05-05 16:15:32 -070031#include "process.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33int is_skas_winch(int pid, int fd, void *data)
34{
Jeff Dikecd2ee4a2005-05-05 16:15:32 -070035 if(pid != os_getpgrp())
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return(0);
37
38 register_winch_irq(-1, fd, -1, data);
39 return(1);
40}
41
Bodo Stroesserc5784552005-05-05 16:15:31 -070042void get_skas_faultinfo(int pid, struct faultinfo * fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int err;
45
Bodo Stroesserc5784552005-05-05 16:15:31 -070046 err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if(err)
Bodo Stroesserc5784552005-05-05 16:15:31 -070048 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
49 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Bodo Stroesserc5784552005-05-05 16:15:31 -070051 /* 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));
56}
57
58static void handle_segv(int pid, union uml_pt_regs * regs)
59{
60 get_skas_faultinfo(pid, &regs->skas.faultinfo);
61 segv(regs->skas.faultinfo, 0, 1, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64/*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
65static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
66{
67 int err, status;
68
69 /* Mark this as a syscall */
70 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
71
72 if (!local_using_sysemu)
73 {
74 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
75 if(err < 0)
76 panic("handle_trap - nullifying syscall failed errno = %d\n",
77 errno);
78
79 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
80 if(err < 0)
81 panic("handle_trap - continuing to end of syscall failed, "
82 "errno = %d\n", errno);
83
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);
89 }
90
91 handle_syscall(regs);
92}
93
94static int userspace_tramp(void *arg)
95{
96 init_new_thread_signals(0);
97 enable_timer();
98 ptrace(PTRACE_TRACEME, 0, 0, 0);
99 os_stop_process(os_getpid());
100 return(0);
101}
102
103/* Each element set once, and only accessed by a single processor anyway */
104#undef NR_CPUS
105#define NR_CPUS 1
106int userspace_pid[NR_CPUS];
107
108void start_userspace(int cpu)
109{
110 void *stack;
111 unsigned long sp;
112 int pid, status, n;
113
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 *);
119
120 pid = clone(userspace_tramp, (void *) sp,
121 CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
122 if(pid < 0)
123 panic("start_userspace : clone failed, errno = %d", errno);
124
125 do {
126 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
127 if(n < 0)
128 panic("start_userspace : wait failed, errno = %d",
129 errno);
130 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
131
132 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
133 panic("start_userspace : expected SIGSTOP, got status = %d",
134 status);
135
136 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
137 panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
138 errno);
139
140 if(munmap(stack, PAGE_SIZE) < 0)
141 panic("start_userspace : munmap failed, errno = %d\n", errno);
142
143 userspace_pid[cpu] = pid;
144}
145
146void userspace(union uml_pt_regs *regs)
147{
148 int err, status, op, pid = userspace_pid[0];
149 int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
150
151 while(1){
152 restore_registers(pid, regs);
153
154 /* Now we set local_using_sysemu to be used for one loop */
155 local_using_sysemu = get_using_sysemu();
156
157 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
158
159 err = ptrace(op, pid, 0, 0);
160 if(err)
161 panic("userspace - could not resume userspace process, "
162 "pid=%d, ptrace operation = %d, errno = %d\n",
163 op, errno);
164
165 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
166 if(err < 0)
167 panic("userspace - waitpid failed, errno = %d\n",
168 errno);
169
170 regs->skas.is_user = 1;
171 save_registers(pid, regs);
172 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
173
174 if(WIFSTOPPED(status)){
175 switch(WSTOPSIG(status)){
176 case SIGSEGV:
Bodo Stroesserc5784552005-05-05 16:15:31 -0700177 handle_segv(pid, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179 case SIGTRAP + 0x80:
180 handle_trap(pid, regs, local_using_sysemu);
181 break;
182 case SIGTRAP:
183 relay_signal(SIGTRAP, regs);
184 break;
185 case SIGIO:
186 case SIGVTALRM:
187 case SIGILL:
188 case SIGBUS:
189 case SIGFPE:
190 case SIGWINCH:
Bodo Stroesserc5784552005-05-05 16:15:31 -0700191 user_signal(WSTOPSIG(status), regs, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193 default:
194 printk("userspace - child stopped with signal "
195 "%d\n", WSTOPSIG(status));
196 }
197 interrupt_end();
198
199 /* Avoid -ERESTARTSYS handling in host */
200 PT_SYSCALL_NR(regs->skas.regs) = -1;
201 }
202 }
203}
204
205void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
206 void (*handler)(int))
207{
208 unsigned long flags;
209 sigjmp_buf switch_buf, fork_buf;
210
211 *switch_buf_ptr = &switch_buf;
212 *fork_buf_ptr = &fork_buf;
213
214 /* Somewhat subtle - siglongjmp restores the signal mask before doing
215 * the longjmp. This means that when jumping from one stack to another
216 * when the target stack has interrupts enabled, an interrupt may occur
217 * on the source stack. This is bad when starting up a process because
218 * it's not supposed to get timer ticks until it has been scheduled.
219 * So, we disable interrupts around the sigsetjmp to ensure that
220 * they can't happen until we get back here where they are safe.
221 */
222 flags = get_signals();
223 block_signals();
224 if(sigsetjmp(fork_buf, 1) == 0)
225 new_thread_proc(stack, handler);
226
227 remove_sigstack();
228
229 set_signals(flags);
230}
231
232void thread_wait(void *sw, void *fb)
233{
234 sigjmp_buf buf, **switch_buf = sw, *fork_buf;
235
236 *switch_buf = &buf;
237 fork_buf = fb;
238 if(sigsetjmp(buf, 1) == 0)
239 siglongjmp(*fork_buf, 1);
240}
241
242void switch_threads(void *me, void *next)
243{
244 sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
245
246 *me_ptr = &my_buf;
247 if(sigsetjmp(my_buf, 1) == 0)
248 siglongjmp(*next_buf, 1);
249}
250
251static sigjmp_buf initial_jmpbuf;
252
253/* XXX Make these percpu */
254static void (*cb_proc)(void *arg);
255static void *cb_arg;
256static sigjmp_buf *cb_back;
257
258int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
259{
260 sigjmp_buf **switch_buf = switch_buf_ptr;
261 int n;
262
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700263 set_handler(SIGWINCH, (__sighandler_t) sig_handler,
264 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
265 SIGVTALRM, -1);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *fork_buf_ptr = &initial_jmpbuf;
268 n = sigsetjmp(initial_jmpbuf, 1);
269 if(n == 0)
270 new_thread_proc((void *) stack, new_thread_handler);
271 else if(n == 1)
272 remove_sigstack();
273 else if(n == 2){
274 (*cb_proc)(cb_arg);
275 siglongjmp(*cb_back, 1);
276 }
277 else if(n == 3){
278 kmalloc_ok = 0;
279 return(0);
280 }
281 else if(n == 4){
282 kmalloc_ok = 0;
283 return(1);
284 }
285 siglongjmp(**switch_buf, 1);
286}
287
288void remove_sigstack(void)
289{
290 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
291 .ss_sp = NULL,
292 .ss_size = 0 });
293
294 if(sigaltstack(&stack, NULL) != 0)
295 panic("disabling signal stack failed, errno = %d\n", errno);
296}
297
298void initial_thread_cb_skas(void (*proc)(void *), void *arg)
299{
300 sigjmp_buf here;
301
302 cb_proc = proc;
303 cb_arg = arg;
304 cb_back = &here;
305
306 block_signals();
307 if(sigsetjmp(here, 1) == 0)
308 siglongjmp(initial_jmpbuf, 2);
309 unblock_signals();
310
311 cb_proc = NULL;
312 cb_arg = NULL;
313 cb_back = NULL;
314}
315
316void halt_skas(void)
317{
318 block_signals();
319 siglongjmp(initial_jmpbuf, 3);
320}
321
322void reboot_skas(void)
323{
324 block_signals();
325 siglongjmp(initial_jmpbuf, 4);
326}
327
328void switch_mm_skas(int mm_fd)
329{
330 int err;
331
332#warning need cpu pid in switch_mm_skas
333 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
334 if(err)
335 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
336 errno);
337}
338
339void kill_off_processes_skas(void)
340{
341#warning need to loop over userspace_pids in kill_off_processes_skas
342 os_kill_ptraced_process(userspace_pid[0], 1);
343}
344
345/*
346 * Overrides for Emacs so that we follow Linus's tabbing style.
347 * Emacs will notice this stuff at the end of the file and automatically
348 * adjust the settings for this buffer only. This must remain at the end
349 * of the file.
350 * ---------------------------------------------------------------------------
351 * Local variables:
352 * c-file-style: "linux"
353 * End:
354 */