aboutsummaryrefslogtreecommitdiffstats
path: root/arch/um/os-Linux
diff options
context:
space:
mode:
authorGennady Sharapov <gennady.v.sharapov@intel.com>2005-09-03 18:57:47 -0400
committerLinus Torvalds <torvalds@evo.osdl.org>2005-09-05 03:06:24 -0400
commit60d339f6fe0831060600c62418b71a62ad26c281 (patch)
treea2f9527bbcfe85b3eb7c063b8af7d2f499ba4cbb /arch/um/os-Linux
parent09ace81c1d737bcbb2423db235ac980cac4d5de9 (diff)
[PATCH] uml: move libc-dependent startup and signal code
The serial UML OS-abstraction layer patch (um/kernel dir). This moves all systemcalls from process.c file under os-Linux dir and join process.c and process_kern.c files. Signed-off-by: Gennady Sharapov <gennady.v.sharapov@intel.com> Signed-off-by: Jeff Dike <jdike@addtoit.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'arch/um/os-Linux')
-rw-r--r--arch/um/os-Linux/Makefile7
-rw-r--r--arch/um/os-Linux/process.c58
-rw-r--r--arch/um/os-Linux/start_up.c329
-rw-r--r--arch/um/os-Linux/tt.c113
4 files changed, 501 insertions, 6 deletions
diff --git a/arch/um/os-Linux/Makefile b/arch/um/os-Linux/Makefile
index 351d96934679..d3c1560e3ed8 100644
--- a/arch/um/os-Linux/Makefile
+++ b/arch/um/os-Linux/Makefile
@@ -3,10 +3,11 @@
3# Licensed under the GPL 3# Licensed under the GPL
4# 4#
5 5
6obj-y = aio.o elf_aux.o file.o process.o signal.o time.o tty.o user_syms.o \ 6obj-y = aio.o elf_aux.o file.o process.o signal.o start_up.o time.o tt.o \
7 drivers/ sys-$(SUBARCH)/ 7 tty.o user_syms.o drivers/ sys-$(SUBARCH)/
8 8
9USER_OBJS := aio.o elf_aux.o file.o process.o signal.o time.o tty.o 9USER_OBJS := aio.o elf_aux.o file.o process.o signal.o start_up.o time.o tt.o \
10 tty.o
10 11
11CFLAGS_user_syms.o += -DSUBARCH_$(SUBARCH) 12CFLAGS_user_syms.o += -DSUBARCH_$(SUBARCH)
12 13
diff --git a/arch/um/os-Linux/process.c b/arch/um/os-Linux/process.c
index 1e126bfd31a7..d32413e4b4ce 100644
--- a/arch/um/os-Linux/process.c
+++ b/arch/um/os-Linux/process.c
@@ -3,10 +3,10 @@
3 * Licensed under the GPL 3 * Licensed under the GPL
4 */ 4 */
5 5
6#include <unistd.h>
7#include <stdio.h> 6#include <stdio.h>
8#include <errno.h> 7#include <errno.h>
9#include <signal.h> 8#include <signal.h>
9#include <setjmp.h>
10#include <linux/unistd.h> 10#include <linux/unistd.h>
11#include <sys/mman.h> 11#include <sys/mman.h>
12#include <sys/wait.h> 12#include <sys/wait.h>
@@ -14,6 +14,10 @@
14#include "os.h" 14#include "os.h"
15#include "user.h" 15#include "user.h"
16#include "user_util.h" 16#include "user_util.h"
17#include "signal_user.h"
18#include "process.h"
19#include "irq_user.h"
20#include "kern_util.h"
17 21
18#define ARBITRARY_ADDR -1 22#define ARBITRARY_ADDR -1
19#define FAILURE_PID -1 23#define FAILURE_PID -1
@@ -114,8 +118,10 @@ void os_usr1_process(int pid)
114 kill(pid, SIGUSR1); 118 kill(pid, SIGUSR1);
115} 119}
116 120
117/*Don't use the glibc version, which caches the result in TLS. It misses some 121/* Don't use the glibc version, which caches the result in TLS. It misses some
118 * syscalls, and also breaks with clone(), which does not unshare the TLS.*/ 122 * syscalls, and also breaks with clone(), which does not unshare the TLS.
123 */
124
119inline _syscall0(pid_t, getpid) 125inline _syscall0(pid_t, getpid)
120 126
121int os_getpid(void) 127int os_getpid(void)
@@ -164,6 +170,52 @@ int os_unmap_memory(void *addr, int len)
164 return(0); 170 return(0);
165} 171}
166 172
173void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
174{
175 int flags = 0, pages;
176
177 if(sig_stack != NULL){
178 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
179 set_sigstack(sig_stack, pages * page_size());
180 flags = SA_ONSTACK;
181 }
182 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
183}
184
185void init_new_thread_signals(int altstack)
186{
187 int flags = altstack ? SA_ONSTACK : 0;
188
189 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
190 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
191 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
192 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
193 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
194 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
195 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
196 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
197 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
198 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
199 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
200 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
201 signal(SIGHUP, SIG_IGN);
202
203 init_irq_signals(altstack);
204}
205
206int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
207{
208 sigjmp_buf buf;
209 int n;
210
211 *jmp_ptr = &buf;
212 n = sigsetjmp(buf, 1);
213 if(n != 0)
214 return(n);
215 (*fn)(arg);
216 return(0);
217}
218
167/* 219/*
168 * Overrides for Emacs so that we follow Linus's tabbing style. 220 * Overrides for Emacs so that we follow Linus's tabbing style.
169 * Emacs will notice this stuff at the end of the file and automatically 221 * Emacs will notice this stuff at the end of the file and automatically
diff --git a/arch/um/os-Linux/start_up.c b/arch/um/os-Linux/start_up.c
new file mode 100644
index 000000000000..a3eab25f3791
--- /dev/null
+++ b/arch/um/os-Linux/start_up.c
@@ -0,0 +1,329 @@
1/*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <signal.h>
9#include <sched.h>
10#include <errno.h>
11#include <stdarg.h>
12#include <stdlib.h>
13#include <setjmp.h>
14#include <sys/time.h>
15#include <sys/wait.h>
16#include <sys/mman.h>
17#include <asm/unistd.h>
18#include <asm/page.h>
19#include "user_util.h"
20#include "kern_util.h"
21#include "user.h"
22#include "signal_kern.h"
23#include "signal_user.h"
24#include "sysdep/ptrace.h"
25#include "sysdep/sigcontext.h"
26#include "irq_user.h"
27#include "ptrace_user.h"
28#include "time_user.h"
29#include "init.h"
30#include "os.h"
31#include "uml-config.h"
32#include "choose-mode.h"
33#include "mode.h"
34#include "tempfile.h"
35#ifdef UML_CONFIG_MODE_SKAS
36#include "skas.h"
37#include "skas_ptrace.h"
38#include "registers.h"
39#endif
40
41static int ptrace_child(void *arg)
42{
43 int ret;
44 int pid = os_getpid(), ppid = getppid();
45 int sc_result;
46
47 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
48 perror("ptrace");
49 os_kill_process(pid, 0);
50 }
51 os_stop_process(pid);
52
53 /*This syscall will be intercepted by the parent. Don't call more than
54 * once, please.*/
55 sc_result = os_getpid();
56
57 if (sc_result == pid)
58 ret = 1; /*Nothing modified by the parent, we are running
59 normally.*/
60 else if (sc_result == ppid)
61 ret = 0; /*Expected in check_ptrace and check_sysemu when they
62 succeed in modifying the stack frame*/
63 else
64 ret = 2; /*Serious trouble! This could be caused by a bug in
65 host 2.6 SKAS3/2.6 patch before release -V6, together
66 with a bug in the UML code itself.*/
67 _exit(ret);
68}
69
70static int start_ptraced_child(void **stack_out)
71{
72 void *stack;
73 unsigned long sp;
74 int pid, n, status;
75
76 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
77 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
78 if(stack == MAP_FAILED)
79 panic("check_ptrace : mmap failed, errno = %d", errno);
80 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
81 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
82 if(pid < 0)
83 panic("start_ptraced_child : clone failed, errno = %d", errno);
84 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
85 if(n < 0)
86 panic("check_ptrace : clone failed, errno = %d", errno);
87 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
88 panic("check_ptrace : expected SIGSTOP, got status = %d",
89 status);
90
91 *stack_out = stack;
92 return(pid);
93}
94
95/* When testing for SYSEMU support, if it is one of the broken versions, we
96 * must just avoid using sysemu, not panic, but only if SYSEMU features are
97 * broken.
98 * So only for SYSEMU features we test mustpanic, while normal host features
99 * must work anyway!
100 */
101static int stop_ptraced_child(int pid, void *stack, int exitcode,
102 int mustpanic)
103{
104 int status, n, ret = 0;
105
106 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
107 panic("check_ptrace : ptrace failed, errno = %d", errno);
108 CATCH_EINTR(n = waitpid(pid, &status, 0));
109 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
110 int exit_with = WEXITSTATUS(status);
111 if (exit_with == 2)
112 printk("check_ptrace : child exited with status 2. "
113 "Serious trouble happening! Try updating your "
114 "host skas patch!\nDisabling SYSEMU support.");
115 printk("check_ptrace : child exited with exitcode %d, while "
116 "expecting %d; status 0x%x", exit_with,
117 exitcode, status);
118 if (mustpanic)
119 panic("\n");
120 else
121 printk("\n");
122 ret = -1;
123 }
124
125 if(munmap(stack, PAGE_SIZE) < 0)
126 panic("check_ptrace : munmap failed, errno = %d", errno);
127 return ret;
128}
129
130int ptrace_faultinfo = 1;
131int proc_mm = 1;
132
133static int __init skas0_cmd_param(char *str, int* add)
134{
135 ptrace_faultinfo = proc_mm = 0;
136 return 0;
137}
138
139__uml_setup("skas0", skas0_cmd_param,
140 "skas0\n"
141 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
142 " you specify mode=tt.\n\n");
143
144static int force_sysemu_disabled = 0;
145
146static int __init nosysemu_cmd_param(char *str, int* add)
147{
148 force_sysemu_disabled = 1;
149 return 0;
150}
151
152__uml_setup("nosysemu", nosysemu_cmd_param,
153"nosysemu\n"
154" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
155" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
156" behaviour of ptrace() and helps reducing host context switch rate.\n"
157" To make it working, you need a kernel patch for your host, too.\n"
158" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
159" information.\n\n");
160
161static void __init check_sysemu(void)
162{
163 void *stack;
164 int pid, syscall, n, status, count=0;
165
166 printk("Checking syscall emulation patch for ptrace...");
167 sysemu_supported = 0;
168 pid = start_ptraced_child(&stack);
169
170 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
171 goto fail;
172
173 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
174 if (n < 0)
175 panic("check_sysemu : wait failed, errno = %d", errno);
176 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
177 panic("check_sysemu : expected SIGTRAP, "
178 "got status = %d", status);
179
180 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
181 os_getpid());
182 if(n < 0)
183 panic("check_sysemu : failed to modify system "
184 "call return, errno = %d", errno);
185
186 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
187 goto fail_stopped;
188
189 sysemu_supported = 1;
190 printk("OK\n");
191 set_using_sysemu(!force_sysemu_disabled);
192
193 printk("Checking advanced syscall emulation patch for ptrace...");
194 pid = start_ptraced_child(&stack);
195 while(1){
196 count++;
197 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
198 goto fail;
199 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
200 if(n < 0)
201 panic("check_ptrace : wait failed, errno = %d", errno);
202 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
203 panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
204 "got status = %d", status);
205
206 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
207 0);
208 if(syscall == __NR_getpid){
209 if (!count)
210 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
211 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
212 os_getpid());
213 if(n < 0)
214 panic("check_sysemu : failed to modify system "
215 "call return, errno = %d", errno);
216 break;
217 }
218 }
219 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
220 goto fail_stopped;
221
222 sysemu_supported = 2;
223 printk("OK\n");
224
225 if ( !force_sysemu_disabled )
226 set_using_sysemu(sysemu_supported);
227 return;
228
229fail:
230 stop_ptraced_child(pid, stack, 1, 0);
231fail_stopped:
232 printk("missing\n");
233}
234
235static void __init check_ptrace(void)
236{
237 void *stack;
238 int pid, syscall, n, status;
239
240 printk("Checking that ptrace can change system call numbers...");
241 pid = start_ptraced_child(&stack);
242
243 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
244 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
245
246 while(1){
247 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
248 panic("check_ptrace : ptrace failed, errno = %d",
249 errno);
250 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
251 if(n < 0)
252 panic("check_ptrace : wait failed, errno = %d", errno);
253 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
254 panic("check_ptrace : expected SIGTRAP + 0x80, "
255 "got status = %d", status);
256
257 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
258 0);
259 if(syscall == __NR_getpid){
260 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
261 __NR_getppid);
262 if(n < 0)
263 panic("check_ptrace : failed to modify system "
264 "call, errno = %d", errno);
265 break;
266 }
267 }
268 stop_ptraced_child(pid, stack, 0, 1);
269 printk("OK\n");
270 check_sysemu();
271}
272
273void os_early_checks(void)
274{
275 check_ptrace();
276}
277
278#ifdef UML_CONFIG_MODE_SKAS
279static inline void check_skas3_ptrace_support(void)
280{
281 struct ptrace_faultinfo fi;
282 void *stack;
283 int pid, n;
284
285 printf("Checking for the skas3 patch in the host...");
286 pid = start_ptraced_child(&stack);
287
288 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
289 if (n < 0) {
290 ptrace_faultinfo = 0;
291 if(errno == EIO)
292 printf("not found\n");
293 else
294 perror("not found");
295 }
296 else {
297 if (!ptrace_faultinfo)
298 printf("found but disabled on command line\n");
299 else
300 printf("found\n");
301 }
302
303 init_registers(pid);
304 stop_ptraced_child(pid, stack, 1, 1);
305}
306
307int can_do_skas(void)
308{
309 printf("Checking for /proc/mm...");
310 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
311 proc_mm = 0;
312 printf("not found\n");
313 }
314 else {
315 if (!proc_mm)
316 printf("found but disabled on command line\n");
317 else
318 printf("found\n");
319 }
320
321 check_skas3_ptrace_support();
322 return 1;
323}
324#else
325int can_do_skas(void)
326{
327 return(0);
328}
329#endif
diff --git a/arch/um/os-Linux/tt.c b/arch/um/os-Linux/tt.c
new file mode 100644
index 000000000000..5b047ab8416a
--- /dev/null
+++ b/arch/um/os-Linux/tt.c
@@ -0,0 +1,113 @@
1/*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <signal.h>
9#include <sched.h>
10#include <errno.h>
11#include <stdarg.h>
12#include <stdlib.h>
13#include <setjmp.h>
14#include <sys/time.h>
15#include <sys/ptrace.h>
16#include <linux/ptrace.h>
17#include <sys/wait.h>
18#include <sys/mman.h>
19#include <asm/ptrace.h>
20#include <asm/unistd.h>
21#include <asm/page.h>
22#include "user_util.h"
23#include "kern_util.h"
24#include "user.h"
25#include "signal_kern.h"
26#include "signal_user.h"
27#include "sysdep/ptrace.h"
28#include "sysdep/sigcontext.h"
29#include "irq_user.h"
30#include "ptrace_user.h"
31#include "time_user.h"
32#include "init.h"
33#include "os.h"
34#include "uml-config.h"
35#include "choose-mode.h"
36#include "mode.h"
37#include "tempfile.h"
38
39/*
40 *-------------------------
41 * only for tt mode (will be deleted in future...)
42 *-------------------------
43 */
44
45struct tramp {
46 int (*tramp)(void *);
47 void *tramp_data;
48 unsigned long temp_stack;
49 int flags;
50 int pid;
51};
52
53/* See above for why sigkill is here */
54
55int sigkill = SIGKILL;
56
57int outer_tramp(void *arg)
58{
59 struct tramp *t;
60 int sig = sigkill;
61
62 t = arg;
63 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
64 t->flags, t->tramp_data);
65 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
66 kill(os_getpid(), sig);
67 _exit(0);
68}
69
70int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
71 int clone_flags, int (*tramp)(void *))
72{
73 struct tramp arg;
74 unsigned long sp;
75 int new_pid, status, err;
76
77 /* The trampoline will run on the temporary stack */
78 sp = stack_sp(temp_stack);
79
80 clone_flags |= CLONE_FILES | SIGCHLD;
81
82 arg.tramp = tramp;
83 arg.tramp_data = thread_arg;
84 arg.temp_stack = temp_stack;
85 arg.flags = clone_flags;
86
87 /* Start the process and wait for it to kill itself */
88 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
89 if(new_pid < 0)
90 return(new_pid);
91
92 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
93 if(err < 0)
94 panic("Waiting for outer trampoline failed - errno = %d",
95 errno);
96
97 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
98 panic("outer trampoline didn't exit with SIGKILL, "
99 "status = %d", status);
100
101 return(arg.pid);
102}
103
104void forward_pending_sigio(int target)
105{
106 sigset_t sigs;
107
108 if(sigpending(&sigs))
109 panic("forward_pending_sigio : sigpending failed");
110 if(sigismember(&sigs, SIGIO))
111 kill(target, SIGIO);
112}
113