1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
19 #include <sys/resource.h>
20 #include <asm/unistd.h>
24 #include <ptrace_user.h>
25 #include <registers.h>
28 static void ptrace_child(void)
31 /* Calling os_getpid because some libcs cached getpid incorrectly */
32 int pid = os_getpid(), ppid = getppid();
35 if (change_sig(SIGWINCH, 0) < 0 ||
36 ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
43 * This syscall will be intercepted by the parent. Don't call more than
46 sc_result = os_getpid();
49 /* Nothing modified by the parent, we are running normally. */
51 else if (sc_result == ppid)
53 * Expected in check_ptrace and check_sysemu when they succeed
54 * in modifying the stack frame
58 /* Serious trouble! This could be caused by a bug in host 2.6
59 * SKAS3/2.6 patch before release -V6, together with a bug in
60 * the UML code itself.
67 static void fatal_perror(const char *str)
73 static void fatal(char *fmt, ...)
78 vfprintf(stderr, fmt, list);
84 static void non_fatal(char *fmt, ...)
89 vfprintf(stderr, fmt, list);
93 static int start_ptraced_child(void)
103 fatal_perror("start_ptraced_child : fork failed");
105 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
107 fatal_perror("check_ptrace : waitpid failed");
108 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
109 fatal("check_ptrace : expected SIGSTOP, got status = %d",
115 static void stop_ptraced_child(int pid, int exitcode)
119 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0)
120 fatal_perror("stop_ptraced_child : ptrace failed");
122 CATCH_EINTR(n = waitpid(pid, &status, 0));
123 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
124 int exit_with = WEXITSTATUS(status);
125 fatal("stop_ptraced_child : child exited with exitcode %d, "
126 "while expecting %d; status 0x%x\n", exit_with,
131 static void __init check_sysemu(void)
133 int pid, n, status, count=0;
135 os_info("Checking syscall emulation for ptrace...");
136 pid = start_ptraced_child();
138 if ((ptrace(PTRACE_SETOPTIONS, pid, 0,
139 (void *) PTRACE_O_TRACESYSGOOD) < 0))
140 fatal_perror("check_sysemu: PTRACE_SETOPTIONS failed");
144 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
146 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
148 fatal_perror("check_sysemu: wait failed");
150 if (WIFSTOPPED(status) &&
151 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
153 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
154 "doesn't singlestep");
157 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
160 fatal_perror("check_sysemu : failed to modify "
161 "system call return");
164 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
167 non_fatal("check_sysemu: expected SIGTRAP or "
168 "(SIGTRAP | 0x80), got status = %d\n",
173 stop_ptraced_child(pid, 0);
180 stop_ptraced_child(pid, 1);
184 static void __init check_ptrace(void)
186 int pid, syscall, n, status;
188 os_info("Checking that ptrace can change system call numbers...");
189 pid = start_ptraced_child();
191 if ((ptrace(PTRACE_SETOPTIONS, pid, 0,
192 (void *) PTRACE_O_TRACESYSGOOD) < 0))
193 fatal_perror("check_ptrace: PTRACE_SETOPTIONS failed");
196 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
197 fatal_perror("check_ptrace : ptrace failed");
199 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
201 fatal_perror("check_ptrace : wait failed");
203 if (!WIFSTOPPED(status) ||
204 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
205 fatal("check_ptrace : expected (SIGTRAP|0x80), "
206 "got status = %d", status);
208 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
210 if (syscall == __NR_getpid) {
211 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
214 fatal_perror("check_ptrace : failed to modify "
219 stop_ptraced_child(pid, 0);
224 extern void check_tmpexec(void);
226 static void __init check_coredump_limit(void)
229 int err = getrlimit(RLIMIT_CORE, &lim);
232 perror("Getting core dump limit");
236 os_info("Core dump limits :\n\tsoft - ");
237 if (lim.rlim_cur == RLIM_INFINITY)
240 os_info("%llu\n", (unsigned long long)lim.rlim_cur);
242 os_info("\thard - ");
243 if (lim.rlim_max == RLIM_INFINITY)
246 os_info("%llu\n", (unsigned long long)lim.rlim_max);
249 void __init get_host_cpu_features(
250 void (*flags_helper_func)(char *line),
251 void (*cache_helper_func)(char *line))
256 int done_parsing = 0;
258 cpuinfo = fopen("/proc/cpuinfo", "r");
259 if (cpuinfo == NULL) {
260 os_info("Failed to get host CPU features\n");
262 while ((getline(&line, &len, cpuinfo)) != -1) {
263 if (strstr(line, "flags")) {
264 flags_helper_func(line);
267 if (strstr(line, "cache_alignment")) {
268 cache_helper_func(line);
273 if (done_parsing > 1)
281 void __init os_early_checks(void)
285 /* Print out the core dump limits early */
286 check_coredump_limit();
290 /* Need to check this early because mmapping happens before the
295 pid = start_ptraced_child();
296 if (init_pid_registers(pid))
297 fatal("Failed to initialize default registers");
298 stop_ptraced_child(pid, 1);
301 int __init parse_iomem(char *str, int *add)
303 struct iomem_region *new;
309 file = strchr(str,',');
311 os_warn("parse_iomem : failed to parse iomem\n");
316 fd = open(file, O_RDWR, 0);
318 perror("parse_iomem - Couldn't open io file");
322 if (fstat64(fd, &buf) < 0) {
323 perror("parse_iomem - cannot stat_fd file");
327 new = malloc(sizeof(*new));
329 perror("Couldn't allocate iomem_region struct");
333 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
335 *new = ((struct iomem_region) { .next = iomem_regions,
342 iomem_size += new->size + UM_KERN_PAGE_SIZE;