blob: 7fe92680c7dd8de39fef42309818df27546ff17a [file] [log] [blame]
Gennady Sharapov60d339f2005-09-03 15:57:47 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
Jeff Dike8e367062006-03-27 01:14:32 -08006#include <pty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <stdio.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -07008#include <stddef.h>
9#include <stdarg.h>
10#include <stdlib.h>
11#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <unistd.h>
13#include <signal.h>
14#include <sched.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070015#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <sys/time.h>
18#include <sys/wait.h>
19#include <sys/mman.h>
20#include <asm/unistd.h>
21#include <asm/page.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070022#include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "user_util.h"
24#include "kern_util.h"
25#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "signal_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "sysdep/ptrace.h"
28#include "sysdep/sigcontext.h"
29#include "irq_user.h"
30#include "ptrace_user.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070031#include "mem_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "init.h"
33#include "os.h"
34#include "uml-config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "choose-mode.h"
36#include "mode.h"
Jeff Diked67b5692005-07-07 17:56:49 -070037#include "tempfile.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070038#include "kern_constants.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#ifdef UML_CONFIG_MODE_SKAS
41#include "skas.h"
42#include "skas_ptrace.h"
43#include "registers.h"
44#endif
45
Jeff Dikeb85e9682005-07-28 21:16:01 -070046static int ptrace_child(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 int ret;
49 int pid = os_getpid(), ppid = getppid();
50 int sc_result;
51
Jeff Dike43b00fd2006-02-07 12:58:42 -080052 change_sig(SIGWINCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
54 perror("ptrace");
55 os_kill_process(pid, 0);
56 }
57 os_stop_process(pid);
58
59 /*This syscall will be intercepted by the parent. Don't call more than
60 * once, please.*/
61 sc_result = os_getpid();
62
63 if (sc_result == pid)
64 ret = 1; /*Nothing modified by the parent, we are running
65 normally.*/
66 else if (sc_result == ppid)
67 ret = 0; /*Expected in check_ptrace and check_sysemu when they
68 succeed in modifying the stack frame*/
69 else
70 ret = 2; /*Serious trouble! This could be caused by a bug in
71 host 2.6 SKAS3/2.6 patch before release -V6, together
72 with a bug in the UML code itself.*/
73 _exit(ret);
74}
75
Jeff Dikeb85e9682005-07-28 21:16:01 -070076static int start_ptraced_child(void **stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Jeff Dikeb85e9682005-07-28 21:16:01 -070078 void *stack;
79 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int pid, n, status;
Gennady Sharapov60d339f2005-09-03 15:57:47 -070081
Jeff Dikeb85e9682005-07-28 21:16:01 -070082 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
83 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
84 if(stack == MAP_FAILED)
85 panic("check_ptrace : mmap failed, errno = %d", errno);
86 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
87 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if(pid < 0)
Gennady Sharapov60d339f2005-09-03 15:57:47 -070089 panic("start_ptraced_child : clone failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
91 if(n < 0)
Gennady Sharapov60d339f2005-09-03 15:57:47 -070092 panic("check_ptrace : clone failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
94 panic("check_ptrace : expected SIGSTOP, got status = %d",
95 status);
96
Jeff Dikeb85e9682005-07-28 21:16:01 -070097 *stack_out = stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return(pid);
99}
100
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700101/* When testing for SYSEMU support, if it is one of the broken versions, we
102 * must just avoid using sysemu, not panic, but only if SYSEMU features are
103 * broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700105 * must work anyway!
106 */
107static int stop_ptraced_child(int pid, void *stack, int exitcode,
108 int mustpanic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 int status, n, ret = 0;
111
112 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dikeb85e9682005-07-28 21:16:01 -0700113 panic("check_ptrace : ptrace failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 CATCH_EINTR(n = waitpid(pid, &status, 0));
115 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
116 int exit_with = WEXITSTATUS(status);
117 if (exit_with == 2)
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100118 printf("check_ptrace : child exited with status 2. "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 "Serious trouble happening! Try updating your "
120 "host skas patch!\nDisabling SYSEMU support.");
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100121 printf("check_ptrace : child exited with exitcode %d, while "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 "expecting %d; status 0x%x", exit_with,
123 exitcode, status);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700124 if (mustpanic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 panic("\n");
126 else
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100127 printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ret = -1;
129 }
130
Jeff Dikeb85e9682005-07-28 21:16:01 -0700131 if(munmap(stack, PAGE_SIZE) < 0)
132 panic("check_ptrace : munmap failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return ret;
134}
135
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700136int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800137int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700138int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800139int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700140
141static int __init skas0_cmd_param(char *str, int* add)
142{
143 ptrace_faultinfo = proc_mm = 0;
144 return 0;
145}
146
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200147/* The two __uml_setup would conflict, without this stupid alias. */
148
149static int __init mode_skas0_cmd_param(char *str, int* add)
150 __attribute__((alias("skas0_cmd_param")));
151
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700152__uml_setup("skas0", skas0_cmd_param,
153 "skas0\n"
154 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
155 " you specify mode=tt.\n\n");
156
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200157__uml_setup("mode=skas0", mode_skas0_cmd_param,
158 "mode=skas0\n"
159 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
160 " specify mode=tt. Note that this was recently added - on \n"
161 " older kernels you must use simply \"skas0\".\n\n");
162
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700163static int force_sysemu_disabled = 0;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static int __init nosysemu_cmd_param(char *str, int* add)
166{
167 force_sysemu_disabled = 1;
168 return 0;
169}
170
171__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700172"nosysemu\n"
173" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
174" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
175" behaviour of ptrace() and helps reducing host context switch rate.\n"
176" To make it working, you need a kernel patch for your host, too.\n"
177" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
178" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180static void __init check_sysemu(void)
181{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700182 void *stack;
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700183 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100185 printf("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 sysemu_supported = 0;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700187 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
190 goto fail;
191
192 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
193 if (n < 0)
194 panic("check_sysemu : wait failed, errno = %d", errno);
195 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
196 panic("check_sysemu : expected SIGTRAP, "
197 "got status = %d", status);
198
199 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
200 os_getpid());
201 if(n < 0)
202 panic("check_sysemu : failed to modify system "
203 "call return, errno = %d", errno);
204
Jeff Dikeb85e9682005-07-28 21:16:01 -0700205 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto fail_stopped;
207
208 sysemu_supported = 1;
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100209 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 set_using_sysemu(!force_sysemu_disabled);
211
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100212 printf("Checking advanced syscall emulation patch for ptrace...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700213 pid = start_ptraced_child(&stack);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700214
215 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
216 (void *) PTRACE_O_TRACESYSGOOD) < 0)
217 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
218 errno);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 while(1){
221 count++;
222 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
223 goto fail;
224 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
225 if(n < 0)
226 panic("check_ptrace : wait failed, errno = %d", errno);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700227 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!count)
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700229 panic("check_ptrace : SYSEMU_SINGLESTEP "
230 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
232 os_getpid());
233 if(n < 0)
234 panic("check_sysemu : failed to modify system "
235 "call return, errno = %d", errno);
236 break;
237 }
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700238 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
239 count++;
240 else
241 panic("check_ptrace : expected SIGTRAP or "
242 "(SIGTRAP|0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700244 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto fail_stopped;
246
247 sysemu_supported = 2;
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100248 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if ( !force_sysemu_disabled )
251 set_using_sysemu(sysemu_supported);
252 return;
253
254fail:
Jeff Dikeb85e9682005-07-28 21:16:01 -0700255 stop_ptraced_child(pid, stack, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256fail_stopped:
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100257 printf("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700260static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700262 void *stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int pid, syscall, n, status;
264
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100265 printf("Checking that ptrace can change system call numbers...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700266 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700268 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
269 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 while(1){
272 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700273 panic("check_ptrace : ptrace failed, errno = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 errno);
275 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
276 if(n < 0)
277 panic("check_ptrace : wait failed, errno = %d", errno);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700278 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
279 panic("check_ptrace : expected (SIGTRAP|0x80), "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 "got status = %d", status);
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
283 0);
284 if(syscall == __NR_getpid){
285 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
286 __NR_getppid);
287 if(n < 0)
288 panic("check_ptrace : failed to modify system "
289 "call, errno = %d", errno);
290 break;
291 }
292 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700293 stop_ptraced_child(pid, stack, 0, 1);
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100294 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 check_sysemu();
296}
297
Rob Landley966a0822006-04-18 22:21:43 -0700298extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700299
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700300void os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700302 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700303
304 /* Need to check this early because mmapping happens before the
305 * kernel is running.
306 */
307 check_tmpexec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700310static int __init noprocmm_cmd_param(char *str, int* add)
311{
312 proc_mm = 0;
313 return 0;
314}
315
316__uml_setup("noprocmm", noprocmm_cmd_param,
317"noprocmm\n"
318" Turns off usage of /proc/mm, even if host supports it.\n"
319" To support /proc/mm, the host needs to be patched using\n"
320" the current skas3 patch.\n\n");
321
322static int __init noptracefaultinfo_cmd_param(char *str, int* add)
323{
324 ptrace_faultinfo = 0;
325 return 0;
326}
327
328__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
329"noptracefaultinfo\n"
330" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
331" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
332" using the current skas3 patch.\n\n");
333
Bodo Stroesser858259c2005-11-07 00:58:55 -0800334static int __init noptraceldt_cmd_param(char *str, int* add)
335{
336 ptrace_ldt = 0;
337 return 0;
338}
339
340__uml_setup("noptraceldt", noptraceldt_cmd_param,
341"noptraceldt\n"
342" Turns off usage of PTRACE_LDT, even if host supports it.\n"
343" To support PTRACE_LDT, the host needs to be patched using\n"
344" the current skas3 patch.\n\n");
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#ifdef UML_CONFIG_MODE_SKAS
Bodo Stroesser858259c2005-11-07 00:58:55 -0800347static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct ptrace_faultinfo fi;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700350 void *stack;
Jeff Diked67b5692005-07-07 17:56:49 -0700351 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Bodo Stroesser858259c2005-11-07 00:58:55 -0800353 printf(" - PTRACE_FAULTINFO...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700354 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
357 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700358 ptrace_faultinfo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if(errno == EIO)
360 printf("not found\n");
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700361 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700363 }
364 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700365 if (!ptrace_faultinfo)
366 printf("found but disabled on command line\n");
367 else
368 printf("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 init_registers(pid);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700372 stop_ptraced_child(pid, stack, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Bodo Stroesser858259c2005-11-07 00:58:55 -0800375static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800377#ifdef PTRACE_LDT
378 void *stack;
379 int pid, n;
380 unsigned char ldtbuf[40];
381 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
382 .func = 2, /* read default ldt */
383 .ptr = ldtbuf,
384 .bytecount = sizeof(ldtbuf)};
385
386 printf(" - PTRACE_LDT...");
387 pid = start_ptraced_child(&stack);
388
389 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
390 if (n < 0) {
391 if(errno == EIO)
392 printf("not found\n");
393 else {
394 perror("not found");
395 }
396 ptrace_ldt = 0;
397 }
398 else {
399 if(ptrace_ldt)
400 printf("found\n");
401 else
402 printf("found, but use is disabled\n");
403 }
404
405 stop_ptraced_child(pid, stack, 1, 1);
406#else
407 /* PTRACE_LDT might be disabled via cmdline option.
408 * We want to override this, else we might use the stub
409 * without real need
410 */
411 ptrace_ldt = 1;
412#endif
413}
414
415static inline void check_skas3_proc_mm(void)
416{
417 printf(" - /proc/mm...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700419 proc_mm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 printf("not found\n");
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700421 }
422 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700423 if (!proc_mm)
424 printf("found but disabled on command line\n");
425 else
426 printf("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800428}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Bodo Stroesser858259c2005-11-07 00:58:55 -0800430int can_do_skas(void)
431{
432 printf("Checking for the skas3 patch in the host:\n");
433
434 check_skas3_proc_mm();
435 check_skas3_ptrace_faultinfo();
436 check_skas3_ptrace_ldt();
437
438 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
439 skas_needs_stub = 1;
440
Jeff Diked67b5692005-07-07 17:56:49 -0700441 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443#else
444int can_do_skas(void)
445{
446 return(0);
447}
448#endif
Jeff Dike0f80bc82005-09-16 19:27:50 -0700449
Jeff Dike0f80bc82005-09-16 19:27:50 -0700450int __init parse_iomem(char *str, int *add)
451{
452 struct iomem_region *new;
453 struct uml_stat buf;
454 char *file, *driver;
455 int fd, err, size;
456
457 driver = str;
458 file = strchr(str,',');
459 if(file == NULL){
460 printf("parse_iomem : failed to parse iomem\n");
461 goto out;
462 }
463 *file = '\0';
464 file++;
465 fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
466 if(fd < 0){
467 os_print_error(fd, "parse_iomem - Couldn't open io file");
468 goto out;
469 }
470
471 err = os_stat_fd(fd, &buf);
472 if(err < 0){
473 os_print_error(err, "parse_iomem - cannot stat_fd file");
474 goto out_close;
475 }
476
477 new = malloc(sizeof(*new));
478 if(new == NULL){
479 perror("Couldn't allocate iomem_region struct");
480 goto out_close;
481 }
482
483 size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
484
485 *new = ((struct iomem_region) { .next = iomem_regions,
486 .driver = driver,
487 .fd = fd,
488 .size = size,
489 .phys = 0,
490 .virt = 0 });
491 iomem_regions = new;
492 iomem_size += new->size + UM_KERN_PAGE_SIZE;
493
494 return(0);
495 out_close:
496 os_close_file(fd);
497 out:
498 return(1);
499}
500
Jeff Dike8e367062006-03-27 01:14:32 -0800501
502/* Changed during early boot */
503int pty_output_sigio = 0;
504int pty_close_sigio = 0;
505
506/* Used as a flag during SIGIO testing early in boot */
507static volatile int got_sigio = 0;
508
509static void __init handler(int sig)
510{
511 got_sigio = 1;
512}
513
514struct openpty_arg {
515 int master;
516 int slave;
517 int err;
518};
519
520static void openpty_cb(void *arg)
521{
522 struct openpty_arg *info = arg;
523
524 info->err = 0;
525 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
526 info->err = -errno;
527}
528
529static void __init check_one_sigio(void (*proc)(int, int))
530{
531 struct sigaction old, new;
532 struct openpty_arg pty = { .master = -1, .slave = -1 };
533 int master, slave, err;
534
535 initial_thread_cb(openpty_cb, &pty);
536 if(pty.err){
537 printk("openpty failed, errno = %d\n", -pty.err);
538 return;
539 }
540
541 master = pty.master;
542 slave = pty.slave;
543
544 if((master == -1) || (slave == -1)){
545 printk("openpty failed to allocate a pty\n");
546 return;
547 }
548
549 /* Not now, but complain so we now where we failed. */
550 err = raw(master);
551 if (err < 0)
552 panic("check_sigio : __raw failed, errno = %d\n", -err);
553
554 err = os_sigio_async(master, slave);
555 if(err < 0)
556 panic("tty_fds : sigio_async failed, err = %d\n", -err);
557
558 if(sigaction(SIGIO, NULL, &old) < 0)
559 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
560 new = old;
561 new.sa_handler = handler;
562 if(sigaction(SIGIO, &new, NULL) < 0)
563 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
564
565 got_sigio = 0;
566 (*proc)(master, slave);
567
568 close(master);
569 close(slave);
570
571 if(sigaction(SIGIO, &old, NULL) < 0)
572 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
573}
574
575static void tty_output(int master, int slave)
576{
577 int n;
578 char buf[512];
579
580 printk("Checking that host ptys support output SIGIO...");
581
582 memset(buf, 0, sizeof(buf));
583
584 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
585 if(errno != EAGAIN)
586 panic("check_sigio : write failed, errno = %d\n", errno);
587 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
588
589 if(got_sigio){
590 printk("Yes\n");
591 pty_output_sigio = 1;
592 }
593 else if(n == -EAGAIN) printk("No, enabling workaround\n");
594 else panic("check_sigio : read failed, err = %d\n", n);
595}
596
597static void tty_close(int master, int slave)
598{
599 printk("Checking that host ptys support SIGIO on close...");
600
601 close(slave);
602 if(got_sigio){
603 printk("Yes\n");
604 pty_close_sigio = 1;
605 }
606 else printk("No, enabling workaround\n");
607}
608
609void __init check_sigio(void)
610{
611 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
612 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
613 printk("No pseudo-terminals available - skipping pty SIGIO "
614 "check\n");
615 return;
616 }
617 check_one_sigio(tty_output);
618 check_one_sigio(tty_close);
619}
620
621void os_check_bugs(void)
622{
623 check_ptrace();
624 check_sigio();
Jeff Dike8e367062006-03-27 01:14:32 -0800625}
626