]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/um/os-Linux/start_up.c
[PATCH] uml: allow host capability usage to be disabled
[mirror_ubuntu-bionic-kernel.git] / arch / um / os-Linux / start_up.c
CommitLineData
60d339f6 1/*
1da177e4
LT
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"
1da177e4
LT
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"
1da177e4
LT
32#include "choose-mode.h"
33#include "mode.h"
d67b569f 34#include "tempfile.h"
1da177e4
LT
35#ifdef UML_CONFIG_MODE_SKAS
36#include "skas.h"
37#include "skas_ptrace.h"
38#include "registers.h"
39#endif
40
b85e9680 41static int ptrace_child(void *arg)
1da177e4
LT
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
b85e9680 70static int start_ptraced_child(void **stack_out)
1da177e4 71{
b85e9680
JD
72 void *stack;
73 unsigned long sp;
1da177e4 74 int pid, n, status;
60d339f6 75
b85e9680
JD
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);
1da177e4 82 if(pid < 0)
60d339f6 83 panic("start_ptraced_child : clone failed, errno = %d", errno);
1da177e4
LT
84 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
85 if(n < 0)
60d339f6 86 panic("check_ptrace : clone failed, errno = %d", errno);
1da177e4
LT
87 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
88 panic("check_ptrace : expected SIGSTOP, got status = %d",
89 status);
90
b85e9680 91 *stack_out = stack;
1da177e4
LT
92 return(pid);
93}
94
60d339f6
GS
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.
1da177e4 98 * So only for SYSEMU features we test mustpanic, while normal host features
60d339f6
GS
99 * must work anyway!
100 */
101static int stop_ptraced_child(int pid, void *stack, int exitcode,
102 int mustpanic)
1da177e4
LT
103{
104 int status, n, ret = 0;
105
106 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
b85e9680 107 panic("check_ptrace : ptrace failed, errno = %d", errno);
1da177e4
LT
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);
b85e9680 118 if (mustpanic)
1da177e4
LT
119 panic("\n");
120 else
121 printk("\n");
122 ret = -1;
123 }
124
b85e9680
JD
125 if(munmap(stack, PAGE_SIZE) < 0)
126 panic("check_ptrace : munmap failed, errno = %d", errno);
1da177e4
LT
127 return ret;
128}
129
cb66504d
PBG
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
60d339f6
GS
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
1da177e4
LT
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,
60d339f6
GS
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");
1da177e4
LT
160
161static void __init check_sysemu(void)
162{
b85e9680 163 void *stack;
1da177e4
LT
164 int pid, syscall, n, status, count=0;
165
166 printk("Checking syscall emulation patch for ptrace...");
167 sysemu_supported = 0;
b85e9680 168 pid = start_ptraced_child(&stack);
1da177e4
LT
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
b85e9680 186 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
1da177e4
LT
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...");
b85e9680 194 pid = start_ptraced_child(&stack);
1da177e4
LT
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 }
b85e9680 219 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
1da177e4
LT
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:
b85e9680 230 stop_ptraced_child(pid, stack, 1, 0);
1da177e4
LT
231fail_stopped:
232 printk("missing\n");
233}
234
60d339f6 235static void __init check_ptrace(void)
1da177e4 236{
b85e9680 237 void *stack;
1da177e4
LT
238 int pid, syscall, n, status;
239
240 printk("Checking that ptrace can change system call numbers...");
b85e9680 241 pid = start_ptraced_child(&stack);
1da177e4 242
60d339f6
GS
243 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
244 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
1da177e4
LT
245
246 while(1){
247 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
60d339f6 248 panic("check_ptrace : ptrace failed, errno = %d",
1da177e4
LT
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);
60d339f6 256
1da177e4
LT
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 }
b85e9680 268 stop_ptraced_child(pid, stack, 0, 1);
1da177e4
LT
269 printk("OK\n");
270 check_sysemu();
271}
272
60d339f6 273void os_early_checks(void)
1da177e4 274{
60d339f6 275 check_ptrace();
1da177e4
LT
276}
277
d9838d86
BS
278static int __init noprocmm_cmd_param(char *str, int* add)
279{
280 proc_mm = 0;
281 return 0;
282}
283
284__uml_setup("noprocmm", noprocmm_cmd_param,
285"noprocmm\n"
286" Turns off usage of /proc/mm, even if host supports it.\n"
287" To support /proc/mm, the host needs to be patched using\n"
288" the current skas3 patch.\n\n");
289
290static int __init noptracefaultinfo_cmd_param(char *str, int* add)
291{
292 ptrace_faultinfo = 0;
293 return 0;
294}
295
296__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
297"noptracefaultinfo\n"
298" Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
299" it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
300" using the current skas3 patch.\n\n");
301
1da177e4 302#ifdef UML_CONFIG_MODE_SKAS
d67b569f 303static inline void check_skas3_ptrace_support(void)
1da177e4
LT
304{
305 struct ptrace_faultinfo fi;
b85e9680 306 void *stack;
d67b569f 307 int pid, n;
1da177e4
LT
308
309 printf("Checking for the skas3 patch in the host...");
b85e9680 310 pid = start_ptraced_child(&stack);
1da177e4
LT
311
312 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
313 if (n < 0) {
cb66504d 314 ptrace_faultinfo = 0;
1da177e4
LT
315 if(errno == EIO)
316 printf("not found\n");
60d339f6 317 else
1da177e4 318 perror("not found");
d67b569f
JD
319 }
320 else {
cb66504d
PBG
321 if (!ptrace_faultinfo)
322 printf("found but disabled on command line\n");
323 else
324 printf("found\n");
1da177e4
LT
325 }
326
327 init_registers(pid);
b85e9680 328 stop_ptraced_child(pid, stack, 1, 1);
1da177e4
LT
329}
330
331int can_do_skas(void)
332{
1da177e4
LT
333 printf("Checking for /proc/mm...");
334 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
60d339f6 335 proc_mm = 0;
1da177e4 336 printf("not found\n");
60d339f6
GS
337 }
338 else {
cb66504d
PBG
339 if (!proc_mm)
340 printf("found but disabled on command line\n");
341 else
342 printf("found\n");
1da177e4
LT
343 }
344
d67b569f
JD
345 check_skas3_ptrace_support();
346 return 1;
1da177e4
LT
347}
348#else
349int can_do_skas(void)
350{
351 return(0);
352}
353#endif