diff options
author | Gennady Sharapov <Gennady.V.Sharapov@intel.com> | 2006-01-18 20:42:46 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-01-18 22:20:19 -0500 |
commit | abaf69773d8dda98b917d94c07757f6520da7bec (patch) | |
tree | 1000669c13511044d1f545cff6af26c1b1d946fb | |
parent | f45d9fc9d80678c2ee22c578e503055207c46fd0 (diff) |
[PATCH] uml: move libc-dependent skas process handling
The serial UML OS-abstraction layer patch (um/kernel/skas dir).
This moves all systemcalls from skas/process.c file under os-Linux dir and
join skas/process.c and skas/process_kern.c files.
Signed-off-by: Gennady Sharapov <gennady.v.sharapov@intel.com>
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r-- | arch/um/include/os.h | 19 | ||||
-rw-r--r-- | arch/um/include/skas/mode-skas.h | 3 | ||||
-rw-r--r-- | arch/um/include/skas/mode_kern_skas.h | 1 | ||||
-rw-r--r-- | arch/um/include/skas/skas.h | 9 | ||||
-rw-r--r-- | arch/um/kernel/skas/Makefile | 4 | ||||
-rw-r--r-- | arch/um/kernel/skas/process_kern.c | 18 | ||||
-rw-r--r-- | arch/um/os-Linux/signal.c | 5 | ||||
-rw-r--r-- | arch/um/os-Linux/skas/Makefile | 4 | ||||
-rw-r--r-- | arch/um/os-Linux/skas/process.c | 565 |
9 files changed, 601 insertions, 27 deletions
diff --git a/arch/um/include/os.h b/arch/um/include/os.h index 8006e085b675..eb1710b81255 100644 --- a/arch/um/include/os.h +++ b/arch/um/include/os.h | |||
@@ -232,6 +232,7 @@ extern void block_signals(void); | |||
232 | extern void unblock_signals(void); | 232 | extern void unblock_signals(void); |
233 | extern int get_signals(void); | 233 | extern int get_signals(void); |
234 | extern int set_signals(int enable); | 234 | extern int set_signals(int enable); |
235 | extern void os_usr1_signal(int on); | ||
235 | 236 | ||
236 | /* trap.c */ | 237 | /* trap.c */ |
237 | extern void os_fill_handlinfo(struct kern_handlers h); | 238 | extern void os_fill_handlinfo(struct kern_handlers h); |
@@ -272,4 +273,22 @@ extern int protect(struct mm_id * mm_idp, unsigned long addr, | |||
272 | unsigned long len, int r, int w, int x, int done, | 273 | unsigned long len, int r, int w, int x, int done, |
273 | void **data); | 274 | void **data); |
274 | 275 | ||
276 | /* skas/process.c */ | ||
277 | extern int is_skas_winch(int pid, int fd, void *data); | ||
278 | extern int start_userspace(unsigned long stub_stack); | ||
279 | extern int copy_context_skas0(unsigned long stack, int pid); | ||
280 | extern void userspace(union uml_pt_regs *regs); | ||
281 | extern void map_stub_pages(int fd, unsigned long code, | ||
282 | unsigned long data, unsigned long stack); | ||
283 | extern void new_thread(void *stack, void **switch_buf_ptr, | ||
284 | void **fork_buf_ptr, void (*handler)(int)); | ||
285 | extern void thread_wait(void *sw, void *fb); | ||
286 | extern void switch_threads(void *me, void *next); | ||
287 | extern int start_idle_thread(void *stack, void *switch_buf_ptr, | ||
288 | void **fork_buf_ptr); | ||
289 | extern void initial_thread_cb_skas(void (*proc)(void *), | ||
290 | void *arg); | ||
291 | extern void halt_skas(void); | ||
292 | extern void reboot_skas(void); | ||
293 | |||
275 | #endif | 294 | #endif |
diff --git a/arch/um/include/skas/mode-skas.h b/arch/um/include/skas/mode-skas.h index 64b960006f1e..260065cfeef1 100644 --- a/arch/um/include/skas/mode-skas.h +++ b/arch/um/include/skas/mode-skas.h | |||
@@ -14,9 +14,6 @@ extern unsigned long exec_fpx_regs[]; | |||
14 | extern int have_fpx_regs; | 14 | extern int have_fpx_regs; |
15 | 15 | ||
16 | extern void sig_handler_common_skas(int sig, void *sc_ptr); | 16 | extern void sig_handler_common_skas(int sig, void *sc_ptr); |
17 | extern void halt_skas(void); | ||
18 | extern void reboot_skas(void); | ||
19 | extern void kill_off_processes_skas(void); | 17 | extern void kill_off_processes_skas(void); |
20 | extern int is_skas_winch(int pid, int fd, void *data); | ||
21 | 18 | ||
22 | #endif | 19 | #endif |
diff --git a/arch/um/include/skas/mode_kern_skas.h b/arch/um/include/skas/mode_kern_skas.h index dd9f2d722fb9..63c58739bde0 100644 --- a/arch/um/include/skas/mode_kern_skas.h +++ b/arch/um/include/skas/mode_kern_skas.h | |||
@@ -18,7 +18,6 @@ extern int copy_thread_skas(int nr, unsigned long clone_flags, | |||
18 | unsigned long sp, unsigned long stack_top, | 18 | unsigned long sp, unsigned long stack_top, |
19 | struct task_struct *p, struct pt_regs *regs); | 19 | struct task_struct *p, struct pt_regs *regs); |
20 | extern void release_thread_skas(struct task_struct *task); | 20 | extern void release_thread_skas(struct task_struct *task); |
21 | extern void initial_thread_cb_skas(void (*proc)(void *), void *arg); | ||
22 | extern void init_idle_skas(void); | 21 | extern void init_idle_skas(void); |
23 | extern void flush_tlb_kernel_range_skas(unsigned long start, | 22 | extern void flush_tlb_kernel_range_skas(unsigned long start, |
24 | unsigned long end); | 23 | unsigned long end); |
diff --git a/arch/um/include/skas/skas.h b/arch/um/include/skas/skas.h index 7418f2addda2..86357282d681 100644 --- a/arch/um/include/skas/skas.h +++ b/arch/um/include/skas/skas.h | |||
@@ -13,21 +13,12 @@ extern int userspace_pid[]; | |||
13 | extern int proc_mm, ptrace_faultinfo, ptrace_ldt; | 13 | extern int proc_mm, ptrace_faultinfo, ptrace_ldt; |
14 | extern int skas_needs_stub; | 14 | extern int skas_needs_stub; |
15 | 15 | ||
16 | extern void switch_threads(void *me, void *next); | ||
17 | extern void thread_wait(void *sw, void *fb); | ||
18 | extern void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr, | ||
19 | void (*handler)(int)); | ||
20 | extern int start_idle_thread(void *stack, void *switch_buf_ptr, | ||
21 | void **fork_buf_ptr); | ||
22 | extern int user_thread(unsigned long stack, int flags); | 16 | extern int user_thread(unsigned long stack, int flags); |
23 | extern void userspace(union uml_pt_regs *regs); | ||
24 | extern void new_thread_proc(void *stack, void (*handler)(int sig)); | 17 | extern void new_thread_proc(void *stack, void (*handler)(int sig)); |
25 | extern void new_thread_handler(int sig); | 18 | extern void new_thread_handler(int sig); |
26 | extern void handle_syscall(union uml_pt_regs *regs); | 19 | extern void handle_syscall(union uml_pt_regs *regs); |
27 | extern void user_signal(int sig, union uml_pt_regs *regs, int pid); | 20 | extern void user_signal(int sig, union uml_pt_regs *regs, int pid); |
28 | extern int new_mm(unsigned long stack); | 21 | extern int new_mm(unsigned long stack); |
29 | extern int start_userspace(unsigned long stub_stack); | ||
30 | extern int copy_context_skas0(unsigned long stack, int pid); | ||
31 | extern void get_skas_faultinfo(int pid, struct faultinfo * fi); | 22 | extern void get_skas_faultinfo(int pid, struct faultinfo * fi); |
32 | extern long execute_syscall_skas(void *r); | 23 | extern long execute_syscall_skas(void *r); |
33 | extern unsigned long current_stub_stack(void); | 24 | extern unsigned long current_stub_stack(void); |
diff --git a/arch/um/kernel/skas/Makefile b/arch/um/kernel/skas/Makefile index eb664c67c3f4..57181a920d48 100644 --- a/arch/um/kernel/skas/Makefile +++ b/arch/um/kernel/skas/Makefile | |||
@@ -3,10 +3,10 @@ | |||
3 | # Licensed under the GPL | 3 | # Licensed under the GPL |
4 | # | 4 | # |
5 | 5 | ||
6 | obj-y := clone.o exec_kern.o mem.o mmu.o process.o process_kern.o \ | 6 | obj-y := clone.o exec_kern.o mem.o mmu.o process_kern.o \ |
7 | syscall.o tlb.o uaccess.o | 7 | syscall.o tlb.o uaccess.o |
8 | 8 | ||
9 | USER_OBJS := process.o clone.o | 9 | USER_OBJS := clone.o |
10 | 10 | ||
11 | include arch/um/scripts/Makefile.rules | 11 | include arch/um/scripts/Makefile.rules |
12 | 12 | ||
diff --git a/arch/um/kernel/skas/process_kern.c b/arch/um/kernel/skas/process_kern.c index a340006dec47..3f70a2e12f06 100644 --- a/arch/um/kernel/skas/process_kern.c +++ b/arch/um/kernel/skas/process_kern.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) | 2 | * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) |
3 | * Licensed under the GPL | 3 | * Licensed under the GPL |
4 | */ | 4 | */ |
@@ -32,7 +32,7 @@ void switch_to_skas(void *prev, void *next) | |||
32 | if(current->pid == 0) | 32 | if(current->pid == 0) |
33 | switch_timers(0); | 33 | switch_timers(0); |
34 | 34 | ||
35 | switch_threads(&from->thread.mode.skas.switch_buf, | 35 | switch_threads(&from->thread.mode.skas.switch_buf, |
36 | to->thread.mode.skas.switch_buf); | 36 | to->thread.mode.skas.switch_buf); |
37 | 37 | ||
38 | if(current->pid == 0) | 38 | if(current->pid == 0) |
@@ -48,8 +48,8 @@ void new_thread_handler(int sig) | |||
48 | 48 | ||
49 | fn = current->thread.request.u.thread.proc; | 49 | fn = current->thread.request.u.thread.proc; |
50 | arg = current->thread.request.u.thread.arg; | 50 | arg = current->thread.request.u.thread.arg; |
51 | change_sig(SIGUSR1, 1); | 51 | os_usr1_signal(1); |
52 | thread_wait(¤t->thread.mode.skas.switch_buf, | 52 | thread_wait(¤t->thread.mode.skas.switch_buf, |
53 | current->thread.mode.skas.fork_buf); | 53 | current->thread.mode.skas.fork_buf); |
54 | 54 | ||
55 | if(current->thread.prev_sched != NULL) | 55 | if(current->thread.prev_sched != NULL) |
@@ -80,8 +80,8 @@ void release_thread_skas(struct task_struct *task) | |||
80 | 80 | ||
81 | void fork_handler(int sig) | 81 | void fork_handler(int sig) |
82 | { | 82 | { |
83 | change_sig(SIGUSR1, 1); | 83 | os_usr1_signal(1); |
84 | thread_wait(¤t->thread.mode.skas.switch_buf, | 84 | thread_wait(¤t->thread.mode.skas.switch_buf, |
85 | current->thread.mode.skas.fork_buf); | 85 | current->thread.mode.skas.fork_buf); |
86 | 86 | ||
87 | force_flush_all(); | 87 | force_flush_all(); |
@@ -91,13 +91,13 @@ void fork_handler(int sig) | |||
91 | schedule_tail(current->thread.prev_sched); | 91 | schedule_tail(current->thread.prev_sched); |
92 | current->thread.prev_sched = NULL; | 92 | current->thread.prev_sched = NULL; |
93 | 93 | ||
94 | /* Handle any immediate reschedules or signals */ | 94 | /* Handle any immediate reschedules or signals */ |
95 | interrupt_end(); | 95 | interrupt_end(); |
96 | userspace(¤t->thread.regs.regs); | 96 | userspace(¤t->thread.regs.regs); |
97 | } | 97 | } |
98 | 98 | ||
99 | int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp, | 99 | int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp, |
100 | unsigned long stack_top, struct task_struct * p, | 100 | unsigned long stack_top, struct task_struct * p, |
101 | struct pt_regs *regs) | 101 | struct pt_regs *regs) |
102 | { | 102 | { |
103 | void (*handler)(int); | 103 | void (*handler)(int); |
@@ -121,8 +121,6 @@ int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp, | |||
121 | return(0); | 121 | return(0); |
122 | } | 122 | } |
123 | 123 | ||
124 | extern void map_stub_pages(int fd, unsigned long code, | ||
125 | unsigned long data, unsigned long stack); | ||
126 | int new_mm(unsigned long stack) | 124 | int new_mm(unsigned long stack) |
127 | { | 125 | { |
128 | int fd; | 126 | int fd; |
diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c index 884e45751944..56ca95931b41 100644 --- a/arch/um/os-Linux/signal.c +++ b/arch/um/os-Linux/signal.c | |||
@@ -194,3 +194,8 @@ int set_signals(int enable) | |||
194 | 194 | ||
195 | return(ret); | 195 | return(ret); |
196 | } | 196 | } |
197 | |||
198 | void os_usr1_signal(int on) | ||
199 | { | ||
200 | change_sig(SIGUSR1, on); | ||
201 | } | ||
diff --git a/arch/um/os-Linux/skas/Makefile b/arch/um/os-Linux/skas/Makefile index 79cc6c72aa92..5fd8d4dad66a 100644 --- a/arch/um/os-Linux/skas/Makefile +++ b/arch/um/os-Linux/skas/Makefile | |||
@@ -3,8 +3,8 @@ | |||
3 | # Licensed under the GPL | 3 | # Licensed under the GPL |
4 | # | 4 | # |
5 | 5 | ||
6 | obj-y := mem.o trap.o | 6 | obj-y := mem.o process.o trap.o |
7 | 7 | ||
8 | USER_OBJS := mem.o trap.o | 8 | USER_OBJS := mem.o process.o trap.o |
9 | 9 | ||
10 | include arch/um/scripts/Makefile.rules | 10 | include arch/um/scripts/Makefile.rules |
diff --git a/arch/um/os-Linux/skas/process.c b/arch/um/os-Linux/skas/process.c new file mode 100644 index 000000000000..f787854322c4 --- /dev/null +++ b/arch/um/os-Linux/skas/process.c | |||
@@ -0,0 +1,565 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com) | ||
3 | * Licensed under the GPL | ||
4 | */ | ||
5 | |||
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" | ||
37 | |||
38 | int is_skas_winch(int pid, int fd, void *data) | ||
39 | { | ||
40 | if(pid != os_getpgrp()) | ||
41 | return(0); | ||
42 | |||
43 | register_winch_irq(-1, fd, -1, data); | ||
44 | return(1); | ||
45 | } | ||
46 | |||
47 | void wait_stub_done(int pid, int sig, char * fname) | ||
48 | { | ||
49 | int n, status, err; | ||
50 | |||
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); | ||
57 | } | ||
58 | sig = 0; | ||
59 | |||
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 | ||
65 | */ | ||
66 | (WSTOPSIG(status) == SIGWINCH))); | ||
67 | |||
68 | if((n < 0) || !WIFSTOPPED(status) || | ||
69 | (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGTRAP)){ | ||
70 | unsigned long regs[HOST_FRAME_SIZE]; | ||
71 | |||
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; | ||
77 | |||
78 | printk("Stub registers -\n"); | ||
79 | for(i = 0; i < HOST_FRAME_SIZE; i++) | ||
80 | printk("\t%d - %lx\n", i, regs[i]); | ||
81 | } | ||
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); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | extern unsigned long current_stub_stack(void); | ||
89 | |||
90 | void get_skas_faultinfo(int pid, struct faultinfo * fi) | ||
91 | { | ||
92 | int err; | ||
93 | |||
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); | ||
99 | |||
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)); | ||
105 | } | ||
106 | else { | ||
107 | wait_stub_done(pid, SIGSEGV, "get_skas_faultinfo"); | ||
108 | |||
109 | /* faultinfo is prepared by the stub-segv-handler at start of | ||
110 | * the stub stack page. We just have to copy it. | ||
111 | */ | ||
112 | memcpy(fi, (void *)current_stub_stack(), sizeof(*fi)); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | static void handle_segv(int pid, union uml_pt_regs * regs) | ||
117 | { | ||
118 | get_skas_faultinfo(pid, ®s->skas.faultinfo); | ||
119 | segv(regs->skas.faultinfo, 0, 1, NULL); | ||
120 | } | ||
121 | |||
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) | ||
124 | { | ||
125 | int err, status; | ||
126 | |||
127 | /* Mark this as a syscall */ | ||
128 | UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs); | ||
129 | |||
130 | if (!local_using_sysemu) | ||
131 | { | ||
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); | ||
137 | |||
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); | ||
142 | |||
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); | ||
148 | } | ||
149 | |||
150 | handle_syscall(regs); | ||
151 | } | ||
152 | |||
153 | extern int __syscall_stub_start; | ||
154 | int stub_code_fd = -1; | ||
155 | __u64 stub_code_offset; | ||
156 | |||
157 | static int userspace_tramp(void *stack) | ||
158 | { | ||
159 | void *addr; | ||
160 | |||
161 | ptrace(PTRACE_TRACEME, 0, 0, 0); | ||
162 | |||
163 | init_new_thread_signals(1); | ||
164 | enable_timer(); | ||
165 | |||
166 | if(!proc_mm){ | ||
167 | /* This has a pte, but it can't be mapped in with the usual | ||
168 | * tlb_flush mechanism because this is part of that mechanism | ||
169 | */ | ||
170 | addr = mmap64((void *) UML_CONFIG_STUB_CODE, page_size(), | ||
171 | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, | ||
172 | stub_code_fd, stub_code_offset); | ||
173 | if(addr == MAP_FAILED){ | ||
174 | printk("mapping stub code failed, errno = %d\n", | ||
175 | errno); | ||
176 | exit(1); | ||
177 | } | ||
178 | |||
179 | if(stack != NULL){ | ||
180 | int fd; | ||
181 | __u64 offset; | ||
182 | fd = phys_mapping(to_phys(stack), &offset); | ||
183 | addr = mmap((void *) UML_CONFIG_STUB_DATA, page_size(), | ||
184 | PROT_READ | PROT_WRITE, | ||
185 | MAP_FIXED | MAP_SHARED, fd, offset); | ||
186 | if(addr == MAP_FAILED){ | ||
187 | printk("mapping stub stack failed, " | ||
188 | "errno = %d\n", errno); | ||
189 | exit(1); | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | if(!ptrace_faultinfo){ | ||
194 | unsigned long v = UML_CONFIG_STUB_CODE + | ||
195 | (unsigned long) stub_segv_handler - | ||
196 | (unsigned long) &__syscall_stub_start; | ||
197 | |||
198 | set_sigstack((void *) UML_CONFIG_STUB_DATA, page_size()); | ||
199 | set_handler(SIGSEGV, (void *) v, SA_ONSTACK, | ||
200 | SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, | ||
201 | SIGUSR1, -1); | ||
202 | } | ||
203 | |||
204 | os_stop_process(os_getpid()); | ||
205 | return(0); | ||
206 | } | ||
207 | |||
208 | /* Each element set once, and only accessed by a single processor anyway */ | ||
209 | #undef NR_CPUS | ||
210 | #define NR_CPUS 1 | ||
211 | int userspace_pid[NR_CPUS]; | ||
212 | |||
213 | int start_userspace(unsigned long stub_stack) | ||
214 | { | ||
215 | void *stack; | ||
216 | unsigned long sp; | ||
217 | int pid, status, n, flags; | ||
218 | |||
219 | if ( stub_code_fd == -1 ) | ||
220 | stub_code_fd = phys_mapping(to_phys(&__syscall_stub_start), | ||
221 | &stub_code_offset); | ||
222 | |||
223 | stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, | ||
224 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
225 | if(stack == MAP_FAILED) | ||
226 | panic("start_userspace : mmap failed, errno = %d", errno); | ||
227 | sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *); | ||
228 | |||
229 | flags = CLONE_FILES | SIGCHLD; | ||
230 | if(proc_mm) flags |= CLONE_VM; | ||
231 | pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack); | ||
232 | if(pid < 0) | ||
233 | panic("start_userspace : clone failed, errno = %d", errno); | ||
234 | |||
235 | do { | ||
236 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); | ||
237 | if(n < 0) | ||
238 | panic("start_userspace : wait failed, errno = %d", | ||
239 | errno); | ||
240 | } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM)); | ||
241 | |||
242 | if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) | ||
243 | panic("start_userspace : expected SIGSTOP, got status = %d", | ||
244 | status); | ||
245 | |||
246 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0) | ||
247 | panic("start_userspace : PTRACE_OLDSETOPTIONS failed, errno=%d\n", | ||
248 | errno); | ||
249 | |||
250 | if(munmap(stack, PAGE_SIZE) < 0) | ||
251 | panic("start_userspace : munmap failed, errno = %d\n", errno); | ||
252 | |||
253 | return(pid); | ||
254 | } | ||
255 | |||
256 | void userspace(union uml_pt_regs *regs) | ||
257 | { | ||
258 | int err, status, op, pid = userspace_pid[0]; | ||
259 | int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/ | ||
260 | |||
261 | while(1){ | ||
262 | restore_registers(pid, regs); | ||
263 | |||
264 | /* Now we set local_using_sysemu to be used for one loop */ | ||
265 | local_using_sysemu = get_using_sysemu(); | ||
266 | |||
267 | op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL)); | ||
268 | |||
269 | err = ptrace(op, pid, 0, 0); | ||
270 | if(err) | ||
271 | panic("userspace - could not resume userspace process, " | ||
272 | "pid=%d, ptrace operation = %d, errno = %d\n", | ||
273 | op, errno); | ||
274 | |||
275 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); | ||
276 | if(err < 0) | ||
277 | panic("userspace - waitpid failed, errno = %d\n", | ||
278 | errno); | ||
279 | |||
280 | regs->skas.is_user = 1; | ||
281 | save_registers(pid, regs); | ||
282 | UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */ | ||
283 | |||
284 | if(WIFSTOPPED(status)){ | ||
285 | switch(WSTOPSIG(status)){ | ||
286 | case SIGSEGV: | ||
287 | if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo) | ||
288 | user_signal(SIGSEGV, regs, pid); | ||
289 | else handle_segv(pid, regs); | ||
290 | break; | ||
291 | case SIGTRAP + 0x80: | ||
292 | handle_trap(pid, regs, local_using_sysemu); | ||
293 | break; | ||
294 | case SIGTRAP: | ||
295 | relay_signal(SIGTRAP, regs); | ||
296 | break; | ||
297 | case SIGIO: | ||
298 | case SIGVTALRM: | ||
299 | case SIGILL: | ||
300 | case SIGBUS: | ||
301 | case SIGFPE: | ||
302 | case SIGWINCH: | ||
303 | user_signal(WSTOPSIG(status), regs, pid); | ||
304 | break; | ||
305 | default: | ||
306 | printk("userspace - child stopped with signal " | ||
307 | "%d\n", WSTOPSIG(status)); | ||
308 | } | ||
309 | again: | ||
310 | pid = userspace_pid[0]; | ||
311 | interrupt_end(); | ||
312 | |||
313 | /* Avoid -ERESTARTSYS handling in host */ | ||
314 | if(PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET) | ||
315 | PT_SYSCALL_NR(regs->skas.regs) = -1; | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | #define INIT_JMP_NEW_THREAD 0 | ||
320 | #define INIT_JMP_REMOVE_SIGSTACK 1 | ||
321 | #define INIT_JMP_CALLBACK 2 | ||
322 | #define INIT_JMP_HALT 3 | ||
323 | #define INIT_JMP_REBOOT 4 | ||
324 | |||
325 | int copy_context_skas0(unsigned long new_stack, int pid) | ||
326 | { | ||
327 | int err; | ||
328 | unsigned long regs[MAX_REG_NR]; | ||
329 | unsigned long current_stack = current_stub_stack(); | ||
330 | struct stub_data *data = (struct stub_data *) current_stack; | ||
331 | struct stub_data *child_data = (struct stub_data *) new_stack; | ||
332 | __u64 new_offset; | ||
333 | int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset); | ||
334 | |||
335 | /* prepare offset and fd of child's stack as argument for parent's | ||
336 | * and child's mmap2 calls | ||
337 | */ | ||
338 | *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset), | ||
339 | .fd = new_fd, | ||
340 | .timer = ((struct itimerval) | ||
341 | { { 0, 1000000 / hz() }, | ||
342 | { 0, 1000000 / hz() }})}); | ||
343 | get_safe_registers(regs); | ||
344 | |||
345 | /* Set parent's instruction pointer to start of clone-stub */ | ||
346 | regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE + | ||
347 | (unsigned long) stub_clone_handler - | ||
348 | (unsigned long) &__syscall_stub_start; | ||
349 | regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + PAGE_SIZE - | ||
350 | sizeof(void *); | ||
351 | #ifdef __SIGNAL_FRAMESIZE | ||
352 | regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE; | ||
353 | #endif | ||
354 | err = ptrace_setregs(pid, regs); | ||
355 | if(err < 0) | ||
356 | panic("copy_context_skas0 : PTRACE_SETREGS failed, " | ||
357 | "pid = %d, errno = %d\n", pid, errno); | ||
358 | |||
359 | /* set a well known return code for detection of child write failure */ | ||
360 | child_data->err = 12345678; | ||
361 | |||
362 | /* Wait, until parent has finished its work: read child's pid from | ||
363 | * parent's stack, and check, if bad result. | ||
364 | */ | ||
365 | wait_stub_done(pid, 0, "copy_context_skas0"); | ||
366 | |||
367 | pid = data->err; | ||
368 | if(pid < 0) | ||
369 | panic("copy_context_skas0 - stub-parent reports error %d\n", | ||
370 | pid); | ||
371 | |||
372 | /* Wait, until child has finished too: read child's result from | ||
373 | * child's stack and check it. | ||
374 | */ | ||
375 | wait_stub_done(pid, -1, "copy_context_skas0"); | ||
376 | if (child_data->err != UML_CONFIG_STUB_DATA) | ||
377 | panic("copy_context_skas0 - stub-child reports error %d\n", | ||
378 | child_data->err); | ||
379 | |||
380 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, | ||
381 | (void *)PTRACE_O_TRACESYSGOOD) < 0) | ||
382 | panic("copy_context_skas0 : PTRACE_OLDSETOPTIONS failed, " | ||
383 | "errno = %d\n", errno); | ||
384 | |||
385 | return pid; | ||
386 | } | ||
387 | |||
388 | /* | ||
389 | * This is used only, if stub pages are needed, while proc_mm is | ||
390 | * availabl. Opening /proc/mm creates a new mm_context, which lacks | ||
391 | * the stub-pages. Thus, we map them using /proc/mm-fd | ||
392 | */ | ||
393 | void map_stub_pages(int fd, unsigned long code, | ||
394 | unsigned long data, unsigned long stack) | ||
395 | { | ||
396 | struct proc_mm_op mmop; | ||
397 | int n; | ||
398 | |||
399 | mmop = ((struct proc_mm_op) { .op = MM_MMAP, | ||
400 | .u = | ||
401 | { .mmap = | ||
402 | { .addr = code, | ||
403 | .len = PAGE_SIZE, | ||
404 | .prot = PROT_EXEC, | ||
405 | .flags = MAP_FIXED | MAP_PRIVATE, | ||
406 | .fd = stub_code_fd, | ||
407 | .offset = stub_code_offset | ||
408 | } } }); | ||
409 | n = os_write_file(fd, &mmop, sizeof(mmop)); | ||
410 | if(n != sizeof(mmop)) | ||
411 | panic("map_stub_pages : /proc/mm map for code failed, " | ||
412 | "err = %d\n", -n); | ||
413 | |||
414 | if ( stack ) { | ||
415 | __u64 map_offset; | ||
416 | int map_fd = phys_mapping(to_phys((void *)stack), &map_offset); | ||
417 | mmop = ((struct proc_mm_op) | ||
418 | { .op = MM_MMAP, | ||
419 | .u = | ||
420 | { .mmap = | ||
421 | { .addr = data, | ||
422 | .len = PAGE_SIZE, | ||
423 | .prot = PROT_READ | PROT_WRITE, | ||
424 | .flags = MAP_FIXED | MAP_SHARED, | ||
425 | .fd = map_fd, | ||
426 | .offset = map_offset | ||
427 | } } }); | ||
428 | n = os_write_file(fd, &mmop, sizeof(mmop)); | ||
429 | if(n != sizeof(mmop)) | ||
430 | panic("map_stub_pages : /proc/mm map for data failed, " | ||
431 | "err = %d\n", -n); | ||
432 | } | ||
433 | } | ||
434 | |||
435 | void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr, | ||
436 | void (*handler)(int)) | ||
437 | { | ||
438 | unsigned long flags; | ||
439 | sigjmp_buf switch_buf, fork_buf; | ||
440 | |||
441 | *switch_buf_ptr = &switch_buf; | ||
442 | *fork_buf_ptr = &fork_buf; | ||
443 | |||
444 | /* Somewhat subtle - siglongjmp restores the signal mask before doing | ||
445 | * the longjmp. This means that when jumping from one stack to another | ||
446 | * when the target stack has interrupts enabled, an interrupt may occur | ||
447 | * on the source stack. This is bad when starting up a process because | ||
448 | * it's not supposed to get timer ticks until it has been scheduled. | ||
449 | * So, we disable interrupts around the sigsetjmp to ensure that | ||
450 | * they can't happen until we get back here where they are safe. | ||
451 | */ | ||
452 | flags = get_signals(); | ||
453 | block_signals(); | ||
454 | if(sigsetjmp(fork_buf, 1) == 0) | ||
455 | new_thread_proc(stack, handler); | ||
456 | |||
457 | remove_sigstack(); | ||
458 | |||
459 | set_signals(flags); | ||
460 | } | ||
461 | |||
462 | void thread_wait(void *sw, void *fb) | ||
463 | { | ||
464 | sigjmp_buf buf, **switch_buf = sw, *fork_buf; | ||
465 | |||
466 | *switch_buf = &buf; | ||
467 | fork_buf = fb; | ||
468 | if(sigsetjmp(buf, 1) == 0) | ||
469 | siglongjmp(*fork_buf, INIT_JMP_REMOVE_SIGSTACK); | ||
470 | } | ||
471 | |||
472 | void switch_threads(void *me, void *next) | ||
473 | { | ||
474 | sigjmp_buf my_buf, **me_ptr = me, *next_buf = next; | ||
475 | |||
476 | *me_ptr = &my_buf; | ||
477 | if(sigsetjmp(my_buf, 1) == 0) | ||
478 | siglongjmp(*next_buf, 1); | ||
479 | } | ||
480 | |||
481 | static sigjmp_buf initial_jmpbuf; | ||
482 | |||
483 | /* XXX Make these percpu */ | ||
484 | static void (*cb_proc)(void *arg); | ||
485 | static void *cb_arg; | ||
486 | static sigjmp_buf *cb_back; | ||
487 | |||
488 | int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr) | ||
489 | { | ||
490 | sigjmp_buf **switch_buf = switch_buf_ptr; | ||
491 | int n; | ||
492 | |||
493 | set_handler(SIGWINCH, (__sighandler_t) sig_handler, | ||
494 | SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM, | ||
495 | SIGVTALRM, -1); | ||
496 | |||
497 | *fork_buf_ptr = &initial_jmpbuf; | ||
498 | n = sigsetjmp(initial_jmpbuf, 1); | ||
499 | switch(n){ | ||
500 | case INIT_JMP_NEW_THREAD: | ||
501 | new_thread_proc((void *) stack, new_thread_handler); | ||
502 | break; | ||
503 | case INIT_JMP_REMOVE_SIGSTACK: | ||
504 | remove_sigstack(); | ||
505 | break; | ||
506 | case INIT_JMP_CALLBACK: | ||
507 | (*cb_proc)(cb_arg); | ||
508 | siglongjmp(*cb_back, 1); | ||
509 | break; | ||
510 | case INIT_JMP_HALT: | ||
511 | kmalloc_ok = 0; | ||
512 | return(0); | ||
513 | case INIT_JMP_REBOOT: | ||
514 | kmalloc_ok = 0; | ||
515 | return(1); | ||
516 | default: | ||
517 | panic("Bad sigsetjmp return in start_idle_thread - %d\n", n); | ||
518 | } | ||
519 | siglongjmp(**switch_buf, 1); | ||
520 | } | ||
521 | |||
522 | void initial_thread_cb_skas(void (*proc)(void *), void *arg) | ||
523 | { | ||
524 | sigjmp_buf here; | ||
525 | |||
526 | cb_proc = proc; | ||
527 | cb_arg = arg; | ||
528 | cb_back = &here; | ||
529 | |||
530 | block_signals(); | ||
531 | if(sigsetjmp(here, 1) == 0) | ||
532 | siglongjmp(initial_jmpbuf, INIT_JMP_CALLBACK); | ||
533 | unblock_signals(); | ||
534 | |||
535 | cb_proc = NULL; | ||
536 | cb_arg = NULL; | ||
537 | cb_back = NULL; | ||
538 | } | ||
539 | |||
540 | void halt_skas(void) | ||
541 | { | ||
542 | block_signals(); | ||
543 | siglongjmp(initial_jmpbuf, INIT_JMP_HALT); | ||
544 | } | ||
545 | |||
546 | void reboot_skas(void) | ||
547 | { | ||
548 | block_signals(); | ||
549 | siglongjmp(initial_jmpbuf, INIT_JMP_REBOOT); | ||
550 | } | ||
551 | |||
552 | void switch_mm_skas(struct mm_id *mm_idp) | ||
553 | { | ||
554 | int err; | ||
555 | |||
556 | #warning need cpu pid in switch_mm_skas | ||
557 | if(proc_mm){ | ||
558 | err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, | ||
559 | mm_idp->u.mm_fd); | ||
560 | if(err) | ||
561 | panic("switch_mm_skas - PTRACE_SWITCH_MM failed, " | ||
562 | "errno = %d\n", errno); | ||
563 | } | ||
564 | else userspace_pid[0] = mm_idp->u.pid; | ||
565 | } | ||