vserver 1.9.5.x5
[linux-2.6.git] / arch / um / kernel / skas / process.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <setjmp.h>
11 #include <sched.h>
12 #include <sys/wait.h>
13 #include <sys/ptrace.h>
14 #include <linux/ptrace.h>
15 #include <sys/mman.h>
16 #include <sys/user.h>
17 #include <asm/unistd.h>
18 #include "user.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"
24 #include "skas.h"
25 #include "sysdep/sigcontext.h"
26 #include "os.h"
27 #include "proc_mm.h"
28 #include "skas_ptrace.h"
29 #include "chan_user.h"
30 #include "signal_user.h"
31 #include "registers.h"
32
33 int is_skas_winch(int pid, int fd, void *data)
34 {
35         if(pid != os_getpid())
36                 return(0);
37
38         register_winch_irq(-1, fd, -1, data);
39         return(1);
40 }
41
42 static void handle_segv(int pid)
43 {
44         struct ptrace_faultinfo fault;
45         int err;
46
47         err = ptrace(PTRACE_FAULTINFO, pid, 0, &fault);
48         if(err)
49                 panic("handle_segv - PTRACE_FAULTINFO failed, errno = %d\n",
50                       errno);
51
52         segv(fault.addr, 0, FAULT_WRITE(fault.is_write), 1, NULL);
53 }
54
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)
57 {
58         int err, status;
59
60         /* Mark this as a syscall */
61         UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
62
63         if (!local_using_sysemu)
64         {
65                 err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
66                 if(err < 0)
67                         panic("handle_trap - nullifying syscall failed errno = %d\n",
68                               errno);
69
70                 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
71                 if(err < 0)
72                         panic("handle_trap - continuing to end of syscall failed, "
73                               "errno = %d\n", errno);
74
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);
80         }
81
82         handle_syscall(regs);
83 }
84
85 static int userspace_tramp(void *arg)
86 {
87         init_new_thread_signals(0);
88         enable_timer();
89         ptrace(PTRACE_TRACEME, 0, 0, 0);
90         os_stop_process(os_getpid());
91         return(0);
92 }
93
94 /* Each element set once, and only accessed by a single processor anyway */
95 #undef NR_CPUS
96 #define NR_CPUS 1
97 int userspace_pid[NR_CPUS];
98
99 void start_userspace(int cpu)
100 {
101         void *stack;
102         unsigned long sp;
103         int pid, status, n;
104
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 *);
110
111         pid = clone(userspace_tramp, (void *) sp, 
112                     CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
113         if(pid < 0)
114                 panic("start_userspace : clone failed, errno = %d", errno);
115
116         do {
117                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
118                 if(n < 0)
119                         panic("start_userspace : wait failed, errno = %d", 
120                               errno);
121         } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
122
123         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
124                 panic("start_userspace : expected SIGSTOP, got status = %d",
125                       status);
126
127         if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
128                 panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
129                       errno);
130
131         if(munmap(stack, PAGE_SIZE) < 0)
132                 panic("start_userspace : munmap failed, errno = %d\n", errno);
133
134         userspace_pid[cpu] = pid;
135 }
136
137 void userspace(union uml_pt_regs *regs)
138 {
139         int err, status, op, pid = userspace_pid[0];
140         int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
141
142         restore_registers(pid, regs);
143                 
144         local_using_sysemu = get_using_sysemu();
145
146         op = local_using_sysemu ? PTRACE_SYSEMU : PTRACE_SYSCALL;
147         err = ptrace(op, pid, 0, 0);
148
149         if(err)
150                 panic("userspace - PTRACE_%s failed, errno = %d\n",
151                        local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
152         while(1){
153                 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
154                 if(err < 0)
155                         panic("userspace - waitpid failed, errno = %d\n", 
156                               errno);
157
158                 regs->skas.is_user = 1;
159                 save_registers(pid, regs);
160                 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
161
162                 if(WIFSTOPPED(status)){
163                         switch(WSTOPSIG(status)){
164                         case SIGSEGV:
165                                 handle_segv(pid);
166                                 break;
167                         case SIGTRAP + 0x80:
168                                 handle_trap(pid, regs, local_using_sysemu);
169                                 break;
170                         case SIGTRAP:
171                                 relay_signal(SIGTRAP, regs);
172                                 break;
173                         case SIGIO:
174                         case SIGVTALRM:
175                         case SIGILL:
176                         case SIGBUS:
177                         case SIGFPE:
178                         case SIGWINCH:
179                                 user_signal(WSTOPSIG(status), regs);
180                                 break;
181                         default:
182                                 printk("userspace - child stopped with signal "
183                                        "%d\n", WSTOPSIG(status));
184                         }
185                         interrupt_end();
186
187                         /* Avoid -ERESTARTSYS handling in host */
188                         PT_SYSCALL_NR(regs->skas.regs) = -1;
189                 }
190
191                 restore_registers(pid, regs);
192
193                 /*Now we ended the syscall, so re-read local_using_sysemu.*/
194                 local_using_sysemu = get_using_sysemu();
195
196                 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
197
198                 err = ptrace(op, pid, 0, 0);
199                 if(err)
200                         panic("userspace - PTRACE_%s failed, "
201                               "errno = %d\n",
202                               local_using_sysemu ? "SYSEMU" : "SYSCALL", errno);
203         }
204 }
205
206 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
207                 void (*handler)(int))
208 {
209         unsigned long flags;
210         sigjmp_buf switch_buf, fork_buf;
211
212         *switch_buf_ptr = &switch_buf;
213         *fork_buf_ptr = &fork_buf;
214
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.
222          */
223         flags = get_signals();
224         block_signals();
225         if(sigsetjmp(fork_buf, 1) == 0)
226                 new_thread_proc(stack, handler);
227
228         remove_sigstack();
229
230         set_signals(flags);
231 }
232
233 void thread_wait(void *sw, void *fb)
234 {
235         sigjmp_buf buf, **switch_buf = sw, *fork_buf;
236
237         *switch_buf = &buf;
238         fork_buf = fb;
239         if(sigsetjmp(buf, 1) == 0)
240                 siglongjmp(*fork_buf, 1);
241 }
242
243 void switch_threads(void *me, void *next)
244 {
245         sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
246         
247         *me_ptr = &my_buf;
248         if(sigsetjmp(my_buf, 1) == 0)
249                 siglongjmp(*next_buf, 1);
250 }
251
252 static sigjmp_buf initial_jmpbuf;
253
254 /* XXX Make these percpu */
255 static void (*cb_proc)(void *arg);
256 static void *cb_arg;
257 static sigjmp_buf *cb_back;
258
259 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
260 {
261         sigjmp_buf **switch_buf = switch_buf_ptr;
262         int n;
263
264         *fork_buf_ptr = &initial_jmpbuf;
265         n = sigsetjmp(initial_jmpbuf, 1);
266         if(n == 0)
267                 new_thread_proc((void *) stack, new_thread_handler);
268         else if(n == 1)
269                 remove_sigstack();
270         else if(n == 2){
271                 (*cb_proc)(cb_arg);
272                 siglongjmp(*cb_back, 1);
273         }
274         else if(n == 3){
275                 kmalloc_ok = 0;
276                 return(0);
277         }
278         else if(n == 4){
279                 kmalloc_ok = 0;
280                 return(1);
281         }
282         siglongjmp(**switch_buf, 1);
283 }
284
285 void remove_sigstack(void)
286 {
287         stack_t stack = ((stack_t) { .ss_flags  = SS_DISABLE,
288                                      .ss_sp     = NULL,
289                                      .ss_size   = 0 });
290
291         if(sigaltstack(&stack, NULL) != 0)
292                 panic("disabling signal stack failed, errno = %d\n", errno);
293 }
294
295 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
296 {
297         sigjmp_buf here;
298
299         cb_proc = proc;
300         cb_arg = arg;
301         cb_back = &here;
302
303         block_signals();
304         if(sigsetjmp(here, 1) == 0)
305                 siglongjmp(initial_jmpbuf, 2);
306         unblock_signals();
307
308         cb_proc = NULL;
309         cb_arg = NULL;
310         cb_back = NULL;
311 }
312
313 void halt_skas(void)
314 {
315         block_signals();
316         siglongjmp(initial_jmpbuf, 3);
317 }
318
319 void reboot_skas(void)
320 {
321         block_signals();
322         siglongjmp(initial_jmpbuf, 4);
323 }
324
325 void switch_mm_skas(int mm_fd)
326 {
327         int err;
328
329 #warning need cpu pid in switch_mm_skas
330         err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
331         if(err)
332                 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
333                       errno);
334 }
335
336 void kill_off_processes_skas(void)
337 {
338 #warning need to loop over userspace_pids in kill_off_processes_skas
339         os_kill_ptraced_process(userspace_pid[0], 1);
340 }
341
342 /*
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
346  * of the file.
347  * ---------------------------------------------------------------------------
348  * Local variables:
349  * c-file-style: "linux"
350  * End:
351  */