blob: 735d035a7f33f06408c2462a01cc572ab3511608 [file] [log] [blame]
Gennady Sharapov60d339f62005-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 }
Jeff Dike73c8f4442007-02-10 01:44:20 -080057 kill(pid, SIGSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
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 Sharapov60d339f62005-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 Sharapov60d339f62005-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 Sharapov60d339f62005-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;
Jeff Dike9eae9b12007-02-10 01:44:20 -080098 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Gennady Sharapov60d339f62005-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 Sharapov60d339f62005-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
Jeff Dike7242a402007-02-10 01:44:19 -0800136/* Changed only during early boot */
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700137int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800138int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700139int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800140int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700141
142static int __init skas0_cmd_param(char *str, int* add)
143{
144 ptrace_faultinfo = proc_mm = 0;
145 return 0;
146}
147
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200148/* The two __uml_setup would conflict, without this stupid alias. */
149
150static int __init mode_skas0_cmd_param(char *str, int* add)
151 __attribute__((alias("skas0_cmd_param")));
152
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700153__uml_setup("skas0", skas0_cmd_param,
154 "skas0\n"
155 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
156 " you specify mode=tt.\n\n");
157
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200158__uml_setup("mode=skas0", mode_skas0_cmd_param,
159 "mode=skas0\n"
160 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
161 " specify mode=tt. Note that this was recently added - on \n"
162 " older kernels you must use simply \"skas0\".\n\n");
163
Jeff Dike7242a402007-02-10 01:44:19 -0800164/* Changed only during early boot */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700165static int force_sysemu_disabled = 0;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static int __init nosysemu_cmd_param(char *str, int* add)
168{
169 force_sysemu_disabled = 1;
170 return 0;
171}
172
173__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700174"nosysemu\n"
175" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
176" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
177" behaviour of ptrace() and helps reducing host context switch rate.\n"
178" To make it working, you need a kernel patch for your host, too.\n"
179" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
180" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182static void __init check_sysemu(void)
183{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700184 void *stack;
Jeff Dike9eae9b12007-02-10 01:44:20 -0800185 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100187 printf("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 sysemu_supported = 0;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700189 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
192 goto fail;
193
194 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
195 if (n < 0)
196 panic("check_sysemu : wait failed, errno = %d", errno);
197 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
198 panic("check_sysemu : expected SIGTRAP, "
199 "got status = %d", status);
200
201 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
202 os_getpid());
203 if(n < 0)
204 panic("check_sysemu : failed to modify system "
205 "call return, errno = %d", errno);
206
Jeff Dikeb85e9682005-07-28 21:16:01 -0700207 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 goto fail_stopped;
209
210 sysemu_supported = 1;
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100211 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 set_using_sysemu(!force_sysemu_disabled);
213
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100214 printf("Checking advanced syscall emulation patch for ptrace...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700215 pid = start_ptraced_child(&stack);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700216
217 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
218 (void *) PTRACE_O_TRACESYSGOOD) < 0)
219 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
220 errno);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 while(1){
223 count++;
224 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
225 goto fail;
226 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
227 if(n < 0)
228 panic("check_ptrace : wait failed, errno = %d", errno);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700229 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (!count)
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700231 panic("check_ptrace : SYSEMU_SINGLESTEP "
232 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
234 os_getpid());
235 if(n < 0)
236 panic("check_sysemu : failed to modify system "
237 "call return, errno = %d", errno);
238 break;
239 }
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700240 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
241 count++;
242 else
243 panic("check_ptrace : expected SIGTRAP or "
244 "(SIGTRAP|0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700246 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto fail_stopped;
248
249 sysemu_supported = 2;
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100250 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if ( !force_sysemu_disabled )
253 set_using_sysemu(sysemu_supported);
254 return;
255
256fail:
Jeff Dikeb85e9682005-07-28 21:16:01 -0700257 stop_ptraced_child(pid, stack, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258fail_stopped:
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100259 printf("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700262static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700264 void *stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int pid, syscall, n, status;
266
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100267 printf("Checking that ptrace can change system call numbers...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700268 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700270 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
271 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 while(1){
274 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700275 panic("check_ptrace : ptrace failed, errno = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 errno);
277 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
278 if(n < 0)
279 panic("check_ptrace : wait failed, errno = %d", errno);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700280 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
281 panic("check_ptrace : expected (SIGTRAP|0x80), "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 "got status = %d", status);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
285 0);
286 if(syscall == __NR_getpid){
287 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
288 __NR_getppid);
289 if(n < 0)
290 panic("check_ptrace : failed to modify system "
291 "call, errno = %d", errno);
292 break;
293 }
294 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700295 stop_ptraced_child(pid, stack, 0, 1);
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100296 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 check_sysemu();
298}
299
Rob Landley966a082f2006-04-18 22:21:43 -0700300extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700301
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700302void os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700304 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700305
306 /* Need to check this early because mmapping happens before the
307 * kernel is running.
308 */
309 check_tmpexec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700312static int __init noprocmm_cmd_param(char *str, int* add)
313{
314 proc_mm = 0;
315 return 0;
316}
317
318__uml_setup("noprocmm", noprocmm_cmd_param,
319"noprocmm\n"
320" Turns off usage of /proc/mm, even if host supports it.\n"
321" To support /proc/mm, the host needs to be patched using\n"
322" the current skas3 patch.\n\n");
323
324static int __init noptracefaultinfo_cmd_param(char *str, int* add)
325{
326 ptrace_faultinfo = 0;
327 return 0;
328}
329
330__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
331"noptracefaultinfo\n"
332" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
333" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
334" using the current skas3 patch.\n\n");
335
Bodo Stroesser858259c2005-11-07 00:58:55 -0800336static int __init noptraceldt_cmd_param(char *str, int* add)
337{
338 ptrace_ldt = 0;
339 return 0;
340}
341
342__uml_setup("noptraceldt", noptraceldt_cmd_param,
343"noptraceldt\n"
344" Turns off usage of PTRACE_LDT, even if host supports it.\n"
345" To support PTRACE_LDT, the host needs to be patched using\n"
346" the current skas3 patch.\n\n");
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#ifdef UML_CONFIG_MODE_SKAS
Bodo Stroesser858259c2005-11-07 00:58:55 -0800349static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct ptrace_faultinfo fi;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700352 void *stack;
Jeff Diked67b5692005-07-07 17:56:49 -0700353 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Bodo Stroesser858259c2005-11-07 00:58:55 -0800355 printf(" - PTRACE_FAULTINFO...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700356 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
359 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700360 ptrace_faultinfo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if(errno == EIO)
362 printf("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700363 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700365 }
366 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700367 if (!ptrace_faultinfo)
368 printf("found but disabled on command line\n");
369 else
370 printf("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 init_registers(pid);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700374 stop_ptraced_child(pid, stack, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Bodo Stroesser858259c2005-11-07 00:58:55 -0800377static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800379#ifdef PTRACE_LDT
380 void *stack;
381 int pid, n;
382 unsigned char ldtbuf[40];
383 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
384 .func = 2, /* read default ldt */
385 .ptr = ldtbuf,
386 .bytecount = sizeof(ldtbuf)};
387
388 printf(" - PTRACE_LDT...");
389 pid = start_ptraced_child(&stack);
390
391 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
392 if (n < 0) {
393 if(errno == EIO)
394 printf("not found\n");
395 else {
396 perror("not found");
397 }
398 ptrace_ldt = 0;
399 }
400 else {
401 if(ptrace_ldt)
402 printf("found\n");
403 else
404 printf("found, but use is disabled\n");
405 }
406
407 stop_ptraced_child(pid, stack, 1, 1);
408#else
409 /* PTRACE_LDT might be disabled via cmdline option.
410 * We want to override this, else we might use the stub
411 * without real need
412 */
413 ptrace_ldt = 1;
414#endif
415}
416
417static inline void check_skas3_proc_mm(void)
418{
419 printf(" - /proc/mm...");
Jeff Dike73c8f4442007-02-10 01:44:20 -0800420 if (access("/proc/mm", W_OK) < 0) {
Jeff Dike9eae9b12007-02-10 01:44:20 -0800421 proc_mm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 printf("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700423 }
424 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700425 if (!proc_mm)
426 printf("found but disabled on command line\n");
427 else
428 printf("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800430}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Bodo Stroesser858259c2005-11-07 00:58:55 -0800432int can_do_skas(void)
433{
434 printf("Checking for the skas3 patch in the host:\n");
435
436 check_skas3_proc_mm();
437 check_skas3_ptrace_faultinfo();
438 check_skas3_ptrace_ldt();
439
440 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
441 skas_needs_stub = 1;
442
Jeff Diked67b5692005-07-07 17:56:49 -0700443 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445#else
446int can_do_skas(void)
447{
Jeff Dike9eae9b12007-02-10 01:44:20 -0800448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450#endif
Jeff Dike0f80bc82005-09-16 19:27:50 -0700451
Jeff Dike0f80bc82005-09-16 19:27:50 -0700452int __init parse_iomem(char *str, int *add)
453{
454 struct iomem_region *new;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800455 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700456 char *file, *driver;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800457 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700458
459 driver = str;
460 file = strchr(str,',');
461 if(file == NULL){
462 printf("parse_iomem : failed to parse iomem\n");
463 goto out;
464 }
465 *file = '\0';
466 file++;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800467 fd = open(file, O_RDWR, 0);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700468 if(fd < 0){
469 os_print_error(fd, "parse_iomem - Couldn't open io file");
470 goto out;
471 }
472
Jeff Dike73c8f4442007-02-10 01:44:20 -0800473 if(fstat64(fd, &buf) < 0){
474 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700475 goto out_close;
476 }
477
478 new = malloc(sizeof(*new));
479 if(new == NULL){
480 perror("Couldn't allocate iomem_region struct");
481 goto out_close;
482 }
483
Jeff Dike73c8f4442007-02-10 01:44:20 -0800484 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700485
486 *new = ((struct iomem_region) { .next = iomem_regions,
487 .driver = driver,
488 .fd = fd,
489 .size = size,
490 .phys = 0,
491 .virt = 0 });
492 iomem_regions = new;
493 iomem_size += new->size + UM_KERN_PAGE_SIZE;
494
Jeff Dike9eae9b12007-02-10 01:44:20 -0800495 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700496 out_close:
Jeff Dike73c8f4442007-02-10 01:44:20 -0800497 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700498 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800499 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700500}
501
Jeff Dike8e367062006-03-27 01:14:32 -0800502
503/* Changed during early boot */
504int pty_output_sigio = 0;
505int pty_close_sigio = 0;
506
507/* Used as a flag during SIGIO testing early in boot */
508static volatile int got_sigio = 0;
509
510static void __init handler(int sig)
511{
512 got_sigio = 1;
513}
514
515struct openpty_arg {
516 int master;
517 int slave;
518 int err;
519};
520
521static void openpty_cb(void *arg)
522{
523 struct openpty_arg *info = arg;
524
525 info->err = 0;
526 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
527 info->err = -errno;
528}
529
Jeff Dike73c8f4442007-02-10 01:44:20 -0800530static int async_pty(int master, int slave)
531{
532 int flags;
533
534 flags = fcntl(master, F_GETFL);
535 if(flags < 0)
536 return -errno;
537
538 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
539 (fcntl(master, F_SETOWN, os_getpid()) < 0))
540 return -errno;
541
542 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
543 return -errno;
544
545 return(0);
546}
547
Jeff Dike8e367062006-03-27 01:14:32 -0800548static void __init check_one_sigio(void (*proc)(int, int))
549{
550 struct sigaction old, new;
551 struct openpty_arg pty = { .master = -1, .slave = -1 };
552 int master, slave, err;
553
554 initial_thread_cb(openpty_cb, &pty);
555 if(pty.err){
556 printk("openpty failed, errno = %d\n", -pty.err);
557 return;
558 }
559
560 master = pty.master;
561 slave = pty.slave;
562
563 if((master == -1) || (slave == -1)){
564 printk("openpty failed to allocate a pty\n");
565 return;
566 }
567
568 /* Not now, but complain so we now where we failed. */
569 err = raw(master);
570 if (err < 0)
571 panic("check_sigio : __raw failed, errno = %d\n", -err);
572
Jeff Dike73c8f4442007-02-10 01:44:20 -0800573 err = async_pty(master, slave);
Jeff Dike8e367062006-03-27 01:14:32 -0800574 if(err < 0)
575 panic("tty_fds : sigio_async failed, err = %d\n", -err);
576
577 if(sigaction(SIGIO, NULL, &old) < 0)
578 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
579 new = old;
580 new.sa_handler = handler;
581 if(sigaction(SIGIO, &new, NULL) < 0)
582 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
583
584 got_sigio = 0;
585 (*proc)(master, slave);
586
587 close(master);
588 close(slave);
589
590 if(sigaction(SIGIO, &old, NULL) < 0)
591 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
592}
593
594static void tty_output(int master, int slave)
595{
596 int n;
597 char buf[512];
598
599 printk("Checking that host ptys support output SIGIO...");
600
601 memset(buf, 0, sizeof(buf));
602
603 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
604 if(errno != EAGAIN)
605 panic("check_sigio : write failed, errno = %d\n", errno);
606 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
607
608 if(got_sigio){
609 printk("Yes\n");
610 pty_output_sigio = 1;
611 }
612 else if(n == -EAGAIN) printk("No, enabling workaround\n");
613 else panic("check_sigio : read failed, err = %d\n", n);
614}
615
616static void tty_close(int master, int slave)
617{
618 printk("Checking that host ptys support SIGIO on close...");
619
620 close(slave);
621 if(got_sigio){
622 printk("Yes\n");
623 pty_close_sigio = 1;
624 }
625 else printk("No, enabling workaround\n");
626}
627
628void __init check_sigio(void)
629{
630 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
631 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
632 printk("No pseudo-terminals available - skipping pty SIGIO "
633 "check\n");
634 return;
635 }
636 check_one_sigio(tty_output);
637 check_one_sigio(tty_close);
638}
639
640void os_check_bugs(void)
641{
642 check_ptrace();
643 check_sigio();
Jeff Dike8e367062006-03-27 01:14:32 -0800644}
645