blob: 6c5b17ed59e1170baca1f67854211d2c127e869a [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
6#include <stdio.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -07007#include <stddef.h>
8#include <stdarg.h>
9#include <stdlib.h>
10#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <unistd.h>
12#include <signal.h>
13#include <sched.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070014#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <setjmp.h>
17#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
52 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
53 perror("ptrace");
54 os_kill_process(pid, 0);
55 }
56 os_stop_process(pid);
57
58 /*This syscall will be intercepted by the parent. Don't call more than
59 * once, please.*/
60 sc_result = os_getpid();
61
62 if (sc_result == pid)
63 ret = 1; /*Nothing modified by the parent, we are running
64 normally.*/
65 else if (sc_result == ppid)
66 ret = 0; /*Expected in check_ptrace and check_sysemu when they
67 succeed in modifying the stack frame*/
68 else
69 ret = 2; /*Serious trouble! This could be caused by a bug in
70 host 2.6 SKAS3/2.6 patch before release -V6, together
71 with a bug in the UML code itself.*/
72 _exit(ret);
73}
74
Jeff Dikeb85e9682005-07-28 21:16:01 -070075static int start_ptraced_child(void **stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Jeff Dikeb85e9682005-07-28 21:16:01 -070077 void *stack;
78 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int pid, n, status;
Gennady Sharapov60d339f62005-09-03 15:57:47 -070080
Jeff Dikeb85e9682005-07-28 21:16:01 -070081 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
82 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
83 if(stack == MAP_FAILED)
84 panic("check_ptrace : mmap failed, errno = %d", errno);
85 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
86 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if(pid < 0)
Gennady Sharapov60d339f62005-09-03 15:57:47 -070088 panic("start_ptraced_child : clone failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
90 if(n < 0)
Gennady Sharapov60d339f62005-09-03 15:57:47 -070091 panic("check_ptrace : clone failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
93 panic("check_ptrace : expected SIGSTOP, got status = %d",
94 status);
95
Jeff Dikeb85e9682005-07-28 21:16:01 -070096 *stack_out = stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return(pid);
98}
99
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700100/* When testing for SYSEMU support, if it is one of the broken versions, we
101 * must just avoid using sysemu, not panic, but only if SYSEMU features are
102 * broken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700104 * must work anyway!
105 */
106static int stop_ptraced_child(int pid, void *stack, int exitcode,
107 int mustpanic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int status, n, ret = 0;
110
111 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
Jeff Dikeb85e9682005-07-28 21:16:01 -0700112 panic("check_ptrace : ptrace failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 CATCH_EINTR(n = waitpid(pid, &status, 0));
114 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
115 int exit_with = WEXITSTATUS(status);
116 if (exit_with == 2)
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100117 printf("check_ptrace : child exited with status 2. "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 "Serious trouble happening! Try updating your "
119 "host skas patch!\nDisabling SYSEMU support.");
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100120 printf("check_ptrace : child exited with exitcode %d, while "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 "expecting %d; status 0x%x", exit_with,
122 exitcode, status);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700123 if (mustpanic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 panic("\n");
125 else
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100126 printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 ret = -1;
128 }
129
Jeff Dikeb85e9682005-07-28 21:16:01 -0700130 if(munmap(stack, PAGE_SIZE) < 0)
131 panic("check_ptrace : munmap failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return ret;
133}
134
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700135int ptrace_faultinfo = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800136int ptrace_ldt = 1;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700137int proc_mm = 1;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800138int skas_needs_stub = 0;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700139
140static int __init skas0_cmd_param(char *str, int* add)
141{
142 ptrace_faultinfo = proc_mm = 0;
143 return 0;
144}
145
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200146/* The two __uml_setup would conflict, without this stupid alias. */
147
148static int __init mode_skas0_cmd_param(char *str, int* add)
149 __attribute__((alias("skas0_cmd_param")));
150
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700151__uml_setup("skas0", skas0_cmd_param,
152 "skas0\n"
153 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
154 " you specify mode=tt.\n\n");
155
Paolo 'Blaisorblade' Giarrusso9e3d8622005-10-09 21:37:18 +0200156__uml_setup("mode=skas0", mode_skas0_cmd_param,
157 "mode=skas0\n"
158 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
159 " specify mode=tt. Note that this was recently added - on \n"
160 " older kernels you must use simply \"skas0\".\n\n");
161
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700162static int force_sysemu_disabled = 0;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static int __init nosysemu_cmd_param(char *str, int* add)
165{
166 force_sysemu_disabled = 1;
167 return 0;
168}
169
170__uml_setup("nosysemu", nosysemu_cmd_param,
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700171"nosysemu\n"
172" Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
173" SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
174" behaviour of ptrace() and helps reducing host context switch rate.\n"
175" To make it working, you need a kernel patch for your host, too.\n"
176" See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
177" information.\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179static void __init check_sysemu(void)
180{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700181 void *stack;
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700182 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100184 printf("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 sysemu_supported = 0;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700186 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
189 goto fail;
190
191 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
192 if (n < 0)
193 panic("check_sysemu : wait failed, errno = %d", errno);
194 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
195 panic("check_sysemu : expected SIGTRAP, "
196 "got status = %d", status);
197
198 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
199 os_getpid());
200 if(n < 0)
201 panic("check_sysemu : failed to modify system "
202 "call return, errno = %d", errno);
203
Jeff Dikeb85e9682005-07-28 21:16:01 -0700204 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto fail_stopped;
206
207 sysemu_supported = 1;
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100208 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 set_using_sysemu(!force_sysemu_disabled);
210
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100211 printf("Checking advanced syscall emulation patch for ptrace...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700212 pid = start_ptraced_child(&stack);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700213
214 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
215 (void *) PTRACE_O_TRACESYSGOOD) < 0)
216 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
217 errno);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 while(1){
220 count++;
221 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
222 goto fail;
223 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
224 if(n < 0)
225 panic("check_ptrace : wait failed, errno = %d", errno);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700226 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!count)
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700228 panic("check_ptrace : SYSEMU_SINGLESTEP "
229 "doesn't singlestep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
231 os_getpid());
232 if(n < 0)
233 panic("check_sysemu : failed to modify system "
234 "call return, errno = %d", errno);
235 break;
236 }
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700237 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
238 count++;
239 else
240 panic("check_ptrace : expected SIGTRAP or "
241 "(SIGTRAP|0x80), got status = %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700243 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto fail_stopped;
245
246 sysemu_supported = 2;
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100247 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if ( !force_sysemu_disabled )
250 set_using_sysemu(sysemu_supported);
251 return;
252
253fail:
Jeff Dikeb85e9682005-07-28 21:16:01 -0700254 stop_ptraced_child(pid, stack, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255fail_stopped:
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100256 printf("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700259static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jeff Dikeb85e9682005-07-28 21:16:01 -0700261 void *stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int pid, syscall, n, status;
263
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100264 printf("Checking that ptrace can change system call numbers...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700265 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700267 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
268 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 while(1){
271 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700272 panic("check_ptrace : ptrace failed, errno = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 errno);
274 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
275 if(n < 0)
276 panic("check_ptrace : wait failed, errno = %d", errno);
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700277 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
278 panic("check_ptrace : expected (SIGTRAP|0x80), "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 "got status = %d", status);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
282 0);
283 if(syscall == __NR_getpid){
284 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
285 __NR_getppid);
286 if(n < 0)
287 panic("check_ptrace : failed to modify system "
288 "call, errno = %d", errno);
289 break;
290 }
291 }
Jeff Dikeb85e9682005-07-28 21:16:01 -0700292 stop_ptraced_child(pid, stack, 0, 1);
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100293 printf("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 check_sysemu();
295}
296
Jeff Dikeae173812005-11-07 00:58:57 -0800297extern int create_tmp_file(unsigned long long len);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700298
299static void check_tmpexec(void)
300{
301 void *addr;
302 int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
303
304 addr = mmap(NULL, UM_KERN_PAGE_SIZE,
305 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
306 printf("Checking PROT_EXEC mmap in /tmp...");
307 fflush(stdout);
308 if(addr == MAP_FAILED){
309 err = errno;
310 perror("failed");
311 if(err == EPERM)
312 printf("/tmp must be not mounted noexec\n");
313 exit(1);
314 }
315 printf("OK\n");
316 munmap(addr, UM_KERN_PAGE_SIZE);
317
318 close(fd);
319}
320
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700321void os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700323 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700324
325 /* Need to check this early because mmapping happens before the
326 * kernel is running.
327 */
328 check_tmpexec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Bodo Stroesserd9838d82005-09-03 15:57:51 -0700331static int __init noprocmm_cmd_param(char *str, int* add)
332{
333 proc_mm = 0;
334 return 0;
335}
336
337__uml_setup("noprocmm", noprocmm_cmd_param,
338"noprocmm\n"
339" Turns off usage of /proc/mm, even if host supports it.\n"
340" To support /proc/mm, the host needs to be patched using\n"
341" the current skas3 patch.\n\n");
342
343static int __init noptracefaultinfo_cmd_param(char *str, int* add)
344{
345 ptrace_faultinfo = 0;
346 return 0;
347}
348
349__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
350"noptracefaultinfo\n"
351" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
352" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
353" using the current skas3 patch.\n\n");
354
Bodo Stroesser858259c2005-11-07 00:58:55 -0800355static int __init noptraceldt_cmd_param(char *str, int* add)
356{
357 ptrace_ldt = 0;
358 return 0;
359}
360
361__uml_setup("noptraceldt", noptraceldt_cmd_param,
362"noptraceldt\n"
363" Turns off usage of PTRACE_LDT, even if host supports it.\n"
364" To support PTRACE_LDT, the host needs to be patched using\n"
365" the current skas3 patch.\n\n");
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#ifdef UML_CONFIG_MODE_SKAS
Bodo Stroesser858259c2005-11-07 00:58:55 -0800368static inline void check_skas3_ptrace_faultinfo(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct ptrace_faultinfo fi;
Jeff Dikeb85e9682005-07-28 21:16:01 -0700371 void *stack;
Jeff Diked67b5692005-07-07 17:56:49 -0700372 int pid, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Bodo Stroesser858259c2005-11-07 00:58:55 -0800374 printf(" - PTRACE_FAULTINFO...");
Jeff Dikeb85e9682005-07-28 21:16:01 -0700375 pid = start_ptraced_child(&stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
378 if (n < 0) {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700379 ptrace_faultinfo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if(errno == EIO)
381 printf("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700382 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 perror("not found");
Jeff Diked67b5692005-07-07 17:56:49 -0700384 }
385 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700386 if (!ptrace_faultinfo)
387 printf("found but disabled on command line\n");
388 else
389 printf("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 init_registers(pid);
Jeff Dikeb85e9682005-07-28 21:16:01 -0700393 stop_ptraced_child(pid, stack, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Bodo Stroesser858259c2005-11-07 00:58:55 -0800396static inline void check_skas3_ptrace_ldt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Bodo Stroesser858259c2005-11-07 00:58:55 -0800398#ifdef PTRACE_LDT
399 void *stack;
400 int pid, n;
401 unsigned char ldtbuf[40];
402 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
403 .func = 2, /* read default ldt */
404 .ptr = ldtbuf,
405 .bytecount = sizeof(ldtbuf)};
406
407 printf(" - PTRACE_LDT...");
408 pid = start_ptraced_child(&stack);
409
410 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
411 if (n < 0) {
412 if(errno == EIO)
413 printf("not found\n");
414 else {
415 perror("not found");
416 }
417 ptrace_ldt = 0;
418 }
419 else {
420 if(ptrace_ldt)
421 printf("found\n");
422 else
423 printf("found, but use is disabled\n");
424 }
425
426 stop_ptraced_child(pid, stack, 1, 1);
427#else
428 /* PTRACE_LDT might be disabled via cmdline option.
429 * We want to override this, else we might use the stub
430 * without real need
431 */
432 ptrace_ldt = 1;
433#endif
434}
435
436static inline void check_skas3_proc_mm(void)
437{
438 printf(" - /proc/mm...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700440 proc_mm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 printf("not found\n");
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700442 }
443 else {
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700444 if (!proc_mm)
445 printf("found but disabled on command line\n");
446 else
447 printf("found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800449}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Bodo Stroesser858259c2005-11-07 00:58:55 -0800451int can_do_skas(void)
452{
453 printf("Checking for the skas3 patch in the host:\n");
454
455 check_skas3_proc_mm();
456 check_skas3_ptrace_faultinfo();
457 check_skas3_ptrace_ldt();
458
459 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
460 skas_needs_stub = 1;
461
Jeff Diked67b5692005-07-07 17:56:49 -0700462 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464#else
465int can_do_skas(void)
466{
467 return(0);
468}
469#endif
Jeff Dike0f80bc82005-09-16 19:27:50 -0700470
471int have_devanon = 0;
472
Paolo 'Blaisorblade' Giarrusso51694942005-12-29 17:39:51 +0100473/* Runs on boot kernel stack - already safe to use printk. */
474
Jeff Dike0f80bc82005-09-16 19:27:50 -0700475void check_devanon(void)
476{
477 int fd;
478
479 printk("Checking for /dev/anon on the host...");
480 fd = open("/dev/anon", O_RDWR);
481 if(fd < 0){
482 printk("Not available (open failed with errno %d)\n", errno);
483 return;
484 }
485
486 printk("OK\n");
487 have_devanon = 1;
488}
489
490int __init parse_iomem(char *str, int *add)
491{
492 struct iomem_region *new;
493 struct uml_stat buf;
494 char *file, *driver;
495 int fd, err, size;
496
497 driver = str;
498 file = strchr(str,',');
499 if(file == NULL){
500 printf("parse_iomem : failed to parse iomem\n");
501 goto out;
502 }
503 *file = '\0';
504 file++;
505 fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
506 if(fd < 0){
507 os_print_error(fd, "parse_iomem - Couldn't open io file");
508 goto out;
509 }
510
511 err = os_stat_fd(fd, &buf);
512 if(err < 0){
513 os_print_error(err, "parse_iomem - cannot stat_fd file");
514 goto out_close;
515 }
516
517 new = malloc(sizeof(*new));
518 if(new == NULL){
519 perror("Couldn't allocate iomem_region struct");
520 goto out_close;
521 }
522
523 size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
524
525 *new = ((struct iomem_region) { .next = iomem_regions,
526 .driver = driver,
527 .fd = fd,
528 .size = size,
529 .phys = 0,
530 .virt = 0 });
531 iomem_regions = new;
532 iomem_size += new->size + UM_KERN_PAGE_SIZE;
533
534 return(0);
535 out_close:
536 os_close_file(fd);
537 out:
538 return(1);
539}
540