[PATCH] uml: eliminate some globals
[linux/fpc-iii.git] / arch / um / os-Linux / skas / process.c
blobb959b2618b7f8e18b086f1370105815208a5066c
1 /*
2 * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <signal.h>
11 #include <setjmp.h>
12 #include <sched.h>
13 #include "ptrace_user.h"
14 #include <sys/wait.h>
15 #include <sys/mman.h>
16 #include <sys/user.h>
17 #include <sys/time.h>
18 #include <asm/unistd.h>
19 #include <asm/types.h>
20 #include "user.h"
21 #include "sysdep/ptrace.h"
22 #include "user_util.h"
23 #include "kern_util.h"
24 #include "skas.h"
25 #include "stub-data.h"
26 #include "mm_id.h"
27 #include "sysdep/sigcontext.h"
28 #include "sysdep/stub.h"
29 #include "os.h"
30 #include "proc_mm.h"
31 #include "skas_ptrace.h"
32 #include "chan_user.h"
33 #include "registers.h"
34 #include "mem.h"
35 #include "uml-config.h"
36 #include "process.h"
38 int is_skas_winch(int pid, int fd, void *data)
40 if(pid != os_getpgrp())
41 return(0);
43 register_winch_irq(-1, fd, -1, data);
44 return(1);
47 void wait_stub_done(int pid, int sig, char * fname)
49 int n, status, err;
51 do {
52 if ( sig != -1 ) {
53 err = ptrace(PTRACE_CONT, pid, 0, sig);
54 if(err)
55 panic("%s : continue failed, errno = %d\n",
56 fname, errno);
58 sig = 0;
60 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
61 } while((n >= 0) && WIFSTOPPED(status) &&
62 ((WSTOPSIG(status) == SIGVTALRM) ||
63 /* running UML inside a detached screen can cause
64 * SIGWINCHes
66 (WSTOPSIG(status) == SIGWINCH)));
68 if((n < 0) || !WIFSTOPPED(status) ||
69 (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGTRAP)){
70 unsigned long regs[HOST_FRAME_SIZE];
72 if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
73 printk("Failed to get registers from stub, "
74 "errno = %d\n", errno);
75 else {
76 int i;
78 printk("Stub registers -\n");
79 for(i = 0; i < HOST_FRAME_SIZE; i++)
80 printk("\t%d - %lx\n", i, regs[i]);
82 panic("%s : failed to wait for SIGUSR1/SIGTRAP, "
83 "pid = %d, n = %d, errno = %d, status = 0x%x\n",
84 fname, pid, n, errno, status);
88 extern unsigned long current_stub_stack(void);
90 void get_skas_faultinfo(int pid, struct faultinfo * fi)
92 int err;
94 if(ptrace_faultinfo){
95 err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
96 if(err)
97 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
98 "errno = %d\n", errno);
100 /* Special handling for i386, which has different structs */
101 if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo))
102 memset((char *)fi + sizeof(struct ptrace_faultinfo), 0,
103 sizeof(struct faultinfo) -
104 sizeof(struct ptrace_faultinfo));
106 else {
107 wait_stub_done(pid, SIGSEGV, "get_skas_faultinfo");
109 /* faultinfo is prepared by the stub-segv-handler at start of
110 * the stub stack page. We just have to copy it.
112 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
116 static void handle_segv(int pid, union uml_pt_regs * regs)
118 get_skas_faultinfo(pid, &regs->skas.faultinfo);
119 segv(regs->skas.faultinfo, 0, 1, NULL);
122 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
123 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
125 int err, status;
127 /* Mark this as a syscall */
128 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
130 if (!local_using_sysemu)
132 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
133 __NR_getpid);
134 if(err < 0)
135 panic("handle_trap - nullifying syscall failed errno = %d\n",
136 errno);
138 err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
139 if(err < 0)
140 panic("handle_trap - continuing to end of syscall failed, "
141 "errno = %d\n", errno);
143 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
144 if((err < 0) || !WIFSTOPPED(status) ||
145 (WSTOPSIG(status) != SIGTRAP + 0x80))
146 panic("handle_trap - failed to wait at end of syscall, "
147 "errno = %d, status = %d\n", errno, status);
150 handle_syscall(regs);
153 extern int __syscall_stub_start;
155 static int userspace_tramp(void *stack)
157 void *addr;
159 ptrace(PTRACE_TRACEME, 0, 0, 0);
161 init_new_thread_signals(1);
162 enable_timer();
164 if(!proc_mm){
165 /* This has a pte, but it can't be mapped in with the usual
166 * tlb_flush mechanism because this is part of that mechanism
168 int fd;
169 __u64 offset;
170 fd = phys_mapping(to_phys(&__syscall_stub_start), &offset);
171 addr = mmap64((void *) UML_CONFIG_STUB_CODE, page_size(),
172 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
173 if(addr == MAP_FAILED){
174 printk("mapping mmap stub failed, errno = %d\n",
175 errno);
176 exit(1);
179 if(stack != NULL){
180 fd = phys_mapping(to_phys(stack), &offset);
181 addr = mmap((void *) UML_CONFIG_STUB_DATA, page_size(),
182 PROT_READ | PROT_WRITE,
183 MAP_FIXED | MAP_SHARED, fd, offset);
184 if(addr == MAP_FAILED){
185 printk("mapping segfault stack failed, "
186 "errno = %d\n", errno);
187 exit(1);
191 if(!ptrace_faultinfo && (stack != NULL)){
192 unsigned long v = UML_CONFIG_STUB_CODE +
193 (unsigned long) stub_segv_handler -
194 (unsigned long) &__syscall_stub_start;
196 set_sigstack((void *) UML_CONFIG_STUB_DATA, page_size());
197 set_handler(SIGSEGV, (void *) v, SA_ONSTACK,
198 SIGIO, SIGWINCH, SIGALRM, SIGVTALRM,
199 SIGUSR1, -1);
202 os_stop_process(os_getpid());
203 return(0);
206 /* Each element set once, and only accessed by a single processor anyway */
207 #undef NR_CPUS
208 #define NR_CPUS 1
209 int userspace_pid[NR_CPUS];
211 int start_userspace(unsigned long stub_stack)
213 void *stack;
214 unsigned long sp;
215 int pid, status, n, flags;
217 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
218 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
219 if(stack == MAP_FAILED)
220 panic("start_userspace : mmap failed, errno = %d", errno);
221 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
223 flags = CLONE_FILES | SIGCHLD;
224 if(proc_mm) flags |= CLONE_VM;
225 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
226 if(pid < 0)
227 panic("start_userspace : clone failed, errno = %d", errno);
229 do {
230 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
231 if(n < 0)
232 panic("start_userspace : wait failed, errno = %d",
233 errno);
234 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
236 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
237 panic("start_userspace : expected SIGSTOP, got status = %d",
238 status);
240 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
241 panic("start_userspace : PTRACE_OLDSETOPTIONS failed, errno=%d\n",
242 errno);
244 if(munmap(stack, PAGE_SIZE) < 0)
245 panic("start_userspace : munmap failed, errno = %d\n", errno);
247 return(pid);
250 void userspace(union uml_pt_regs *regs)
252 int err, status, op, pid = userspace_pid[0];
253 int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
255 while(1){
256 restore_registers(pid, regs);
258 /* Now we set local_using_sysemu to be used for one loop */
259 local_using_sysemu = get_using_sysemu();
261 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
263 err = ptrace(op, pid, 0, 0);
264 if(err)
265 panic("userspace - could not resume userspace process, "
266 "pid=%d, ptrace operation = %d, errno = %d\n",
267 op, errno);
269 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
270 if(err < 0)
271 panic("userspace - waitpid failed, errno = %d\n",
272 errno);
274 regs->skas.is_user = 1;
275 save_registers(pid, regs);
276 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
278 if(WIFSTOPPED(status)){
279 switch(WSTOPSIG(status)){
280 case SIGSEGV:
281 if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo)
282 user_signal(SIGSEGV, regs, pid);
283 else handle_segv(pid, regs);
284 break;
285 case SIGTRAP + 0x80:
286 handle_trap(pid, regs, local_using_sysemu);
287 break;
288 case SIGTRAP:
289 relay_signal(SIGTRAP, regs);
290 break;
291 case SIGIO:
292 case SIGVTALRM:
293 case SIGILL:
294 case SIGBUS:
295 case SIGFPE:
296 case SIGWINCH:
297 user_signal(WSTOPSIG(status), regs, pid);
298 break;
299 default:
300 printk("userspace - child stopped with signal "
301 "%d\n", WSTOPSIG(status));
303 pid = userspace_pid[0];
304 interrupt_end();
306 /* Avoid -ERESTARTSYS handling in host */
307 if(PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
308 PT_SYSCALL_NR(regs->skas.regs) = -1;
312 #define INIT_JMP_NEW_THREAD 0
313 #define INIT_JMP_REMOVE_SIGSTACK 1
314 #define INIT_JMP_CALLBACK 2
315 #define INIT_JMP_HALT 3
316 #define INIT_JMP_REBOOT 4
318 int copy_context_skas0(unsigned long new_stack, int pid)
320 int err;
321 unsigned long regs[MAX_REG_NR];
322 unsigned long current_stack = current_stub_stack();
323 struct stub_data *data = (struct stub_data *) current_stack;
324 struct stub_data *child_data = (struct stub_data *) new_stack;
325 __u64 new_offset;
326 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
328 /* prepare offset and fd of child's stack as argument for parent's
329 * and child's mmap2 calls
331 *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset),
332 .fd = new_fd,
333 .timer = ((struct itimerval)
334 { { 0, 1000000 / hz() },
335 { 0, 1000000 / hz() }})});
336 get_safe_registers(regs);
338 /* Set parent's instruction pointer to start of clone-stub */
339 regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
340 (unsigned long) stub_clone_handler -
341 (unsigned long) &__syscall_stub_start;
342 regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + PAGE_SIZE -
343 sizeof(void *);
344 #ifdef __SIGNAL_FRAMESIZE
345 regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
346 #endif
347 err = ptrace_setregs(pid, regs);
348 if(err < 0)
349 panic("copy_context_skas0 : PTRACE_SETREGS failed, "
350 "pid = %d, errno = %d\n", pid, errno);
352 /* set a well known return code for detection of child write failure */
353 child_data->err = 12345678;
355 /* Wait, until parent has finished its work: read child's pid from
356 * parent's stack, and check, if bad result.
358 wait_stub_done(pid, 0, "copy_context_skas0");
360 pid = data->err;
361 if(pid < 0)
362 panic("copy_context_skas0 - stub-parent reports error %d\n",
363 pid);
365 /* Wait, until child has finished too: read child's result from
366 * child's stack and check it.
368 wait_stub_done(pid, -1, "copy_context_skas0");
369 if (child_data->err != UML_CONFIG_STUB_DATA)
370 panic("copy_context_skas0 - stub-child reports error %d\n",
371 child_data->err);
373 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
374 (void *)PTRACE_O_TRACESYSGOOD) < 0)
375 panic("copy_context_skas0 : PTRACE_OLDSETOPTIONS failed, "
376 "errno = %d\n", errno);
378 return pid;
382 * This is used only, if stub pages are needed, while proc_mm is
383 * availabl. Opening /proc/mm creates a new mm_context, which lacks
384 * the stub-pages. Thus, we map them using /proc/mm-fd
386 void map_stub_pages(int fd, unsigned long code,
387 unsigned long data, unsigned long stack)
389 struct proc_mm_op mmop;
390 int n;
391 __u64 code_offset;
392 int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start),
393 &code_offset);
395 mmop = ((struct proc_mm_op) { .op = MM_MMAP,
396 .u =
397 { .mmap =
398 { .addr = code,
399 .len = PAGE_SIZE,
400 .prot = PROT_EXEC,
401 .flags = MAP_FIXED | MAP_PRIVATE,
402 .fd = code_fd,
403 .offset = code_offset
404 } } });
405 n = os_write_file(fd, &mmop, sizeof(mmop));
406 if(n != sizeof(mmop))
407 panic("map_stub_pages : /proc/mm map for code failed, "
408 "err = %d\n", -n);
410 if ( stack ) {
411 __u64 map_offset;
412 int map_fd = phys_mapping(to_phys((void *)stack), &map_offset);
413 mmop = ((struct proc_mm_op)
414 { .op = MM_MMAP,
415 .u =
416 { .mmap =
417 { .addr = data,
418 .len = PAGE_SIZE,
419 .prot = PROT_READ | PROT_WRITE,
420 .flags = MAP_FIXED | MAP_SHARED,
421 .fd = map_fd,
422 .offset = map_offset
423 } } });
424 n = os_write_file(fd, &mmop, sizeof(mmop));
425 if(n != sizeof(mmop))
426 panic("map_stub_pages : /proc/mm map for data failed, "
427 "err = %d\n", -n);
431 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
432 void (*handler)(int))
434 unsigned long flags;
435 sigjmp_buf switch_buf, fork_buf;
437 *switch_buf_ptr = &switch_buf;
438 *fork_buf_ptr = &fork_buf;
440 /* Somewhat subtle - siglongjmp restores the signal mask before doing
441 * the longjmp. This means that when jumping from one stack to another
442 * when the target stack has interrupts enabled, an interrupt may occur
443 * on the source stack. This is bad when starting up a process because
444 * it's not supposed to get timer ticks until it has been scheduled.
445 * So, we disable interrupts around the sigsetjmp to ensure that
446 * they can't happen until we get back here where they are safe.
448 flags = get_signals();
449 block_signals();
450 if(sigsetjmp(fork_buf, 1) == 0)
451 new_thread_proc(stack, handler);
453 remove_sigstack();
455 set_signals(flags);
458 void thread_wait(void *sw, void *fb)
460 sigjmp_buf buf, **switch_buf = sw, *fork_buf;
462 *switch_buf = &buf;
463 fork_buf = fb;
464 if(sigsetjmp(buf, 1) == 0)
465 siglongjmp(*fork_buf, INIT_JMP_REMOVE_SIGSTACK);
468 void switch_threads(void *me, void *next)
470 sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
472 *me_ptr = &my_buf;
473 if(sigsetjmp(my_buf, 1) == 0)
474 siglongjmp(*next_buf, 1);
477 static sigjmp_buf initial_jmpbuf;
479 /* XXX Make these percpu */
480 static void (*cb_proc)(void *arg);
481 static void *cb_arg;
482 static sigjmp_buf *cb_back;
484 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
486 sigjmp_buf **switch_buf = switch_buf_ptr;
487 int n;
489 set_handler(SIGWINCH, (__sighandler_t) sig_handler,
490 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
491 SIGVTALRM, -1);
493 *fork_buf_ptr = &initial_jmpbuf;
494 n = sigsetjmp(initial_jmpbuf, 1);
495 switch(n){
496 case INIT_JMP_NEW_THREAD:
497 new_thread_proc((void *) stack, new_thread_handler);
498 break;
499 case INIT_JMP_REMOVE_SIGSTACK:
500 remove_sigstack();
501 break;
502 case INIT_JMP_CALLBACK:
503 (*cb_proc)(cb_arg);
504 siglongjmp(*cb_back, 1);
505 break;
506 case INIT_JMP_HALT:
507 kmalloc_ok = 0;
508 return(0);
509 case INIT_JMP_REBOOT:
510 kmalloc_ok = 0;
511 return(1);
512 default:
513 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n);
515 siglongjmp(**switch_buf, 1);
518 void initial_thread_cb_skas(void (*proc)(void *), void *arg)
520 sigjmp_buf here;
522 cb_proc = proc;
523 cb_arg = arg;
524 cb_back = &here;
526 block_signals();
527 if(sigsetjmp(here, 1) == 0)
528 siglongjmp(initial_jmpbuf, INIT_JMP_CALLBACK);
529 unblock_signals();
531 cb_proc = NULL;
532 cb_arg = NULL;
533 cb_back = NULL;
536 void halt_skas(void)
538 block_signals();
539 siglongjmp(initial_jmpbuf, INIT_JMP_HALT);
542 void reboot_skas(void)
544 block_signals();
545 siglongjmp(initial_jmpbuf, INIT_JMP_REBOOT);
548 void switch_mm_skas(struct mm_id *mm_idp)
550 int err;
552 #warning need cpu pid in switch_mm_skas
553 if(proc_mm){
554 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
555 mm_idp->u.mm_fd);
556 if(err)
557 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, "
558 "errno = %d\n", errno);
560 else userspace_pid[0] = mm_idp->u.pid;