1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
19 #include <sys/resource.h>
21 #include <asm/unistd.h>
24 #include <kern_util.h>
26 #include <ptrace_user.h>
27 #include <registers.h>
31 static void ptrace_child(void)
34 /* Calling os_getpid because some libcs cached getpid incorrectly */
35 int pid
= os_getpid(), ppid
= getppid();
38 if (change_sig(SIGWINCH
, 0) < 0 ||
39 ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0) {
46 * This syscall will be intercepted by the parent. Don't call more than
49 sc_result
= os_getpid();
52 /* Nothing modified by the parent, we are running normally. */
54 else if (sc_result
== ppid
)
56 * Expected in check_ptrace and check_sysemu when they succeed
57 * in modifying the stack frame
61 /* Serious trouble! This could be caused by a bug in host 2.6
62 * SKAS3/2.6 patch before release -V6, together with a bug in
63 * the UML code itself.
70 static void fatal_perror(const char *str
)
76 static void fatal(char *fmt
, ...)
81 vfprintf(stderr
, fmt
, list
);
87 static void non_fatal(char *fmt
, ...)
92 vfprintf(stderr
, fmt
, list
);
96 static int start_ptraced_child(void)
106 fatal_perror("start_ptraced_child : fork failed");
108 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
110 fatal_perror("check_ptrace : waitpid failed");
111 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
112 fatal("check_ptrace : expected SIGSTOP, got status = %d",
118 static void stop_ptraced_child(int pid
, int exitcode
)
122 if (ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
123 fatal_perror("stop_ptraced_child : ptrace failed");
125 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
126 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
127 int exit_with
= WEXITSTATUS(status
);
128 fatal("stop_ptraced_child : child exited with exitcode %d, "
129 "while expecting %d; status 0x%x\n", exit_with
,
134 static void __init
check_sysemu(void)
136 int pid
, n
, status
, count
=0;
138 os_info("Checking syscall emulation for ptrace...");
139 pid
= start_ptraced_child();
141 if ((ptrace(PTRACE_SETOPTIONS
, pid
, 0,
142 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
143 fatal_perror("check_sysemu: PTRACE_SETOPTIONS failed");
147 if (ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
149 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
151 fatal_perror("check_sysemu: wait failed");
153 if (WIFSTOPPED(status
) &&
154 (WSTOPSIG(status
) == (SIGTRAP
|0x80))) {
156 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
157 "doesn't singlestep");
160 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_RET_OFFSET
,
163 fatal_perror("check_sysemu : failed to modify "
164 "system call return");
167 else if (WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
170 non_fatal("check_sysemu: expected SIGTRAP or "
171 "(SIGTRAP | 0x80), got status = %d\n",
176 stop_ptraced_child(pid
, 0);
183 stop_ptraced_child(pid
, 1);
187 static void __init
check_ptrace(void)
189 int pid
, syscall
, n
, status
;
191 os_info("Checking that ptrace can change system call numbers...");
192 pid
= start_ptraced_child();
194 if ((ptrace(PTRACE_SETOPTIONS
, pid
, 0,
195 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
196 fatal_perror("check_ptrace: PTRACE_SETOPTIONS failed");
199 if (ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
200 fatal_perror("check_ptrace : ptrace failed");
202 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
204 fatal_perror("check_ptrace : wait failed");
206 if (!WIFSTOPPED(status
) ||
207 (WSTOPSIG(status
) != (SIGTRAP
| 0x80)))
208 fatal("check_ptrace : expected (SIGTRAP|0x80), "
209 "got status = %d", status
);
211 syscall
= ptrace(PTRACE_PEEKUSER
, pid
, PT_SYSCALL_NR_OFFSET
,
213 if (syscall
== __NR_getpid
) {
214 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_NR_OFFSET
,
217 fatal_perror("check_ptrace : failed to modify "
222 stop_ptraced_child(pid
, 0);
227 static void __init
check_coredump_limit(void)
230 int err
= getrlimit(RLIMIT_CORE
, &lim
);
233 perror("Getting core dump limit");
237 os_info("Core dump limits :\n\tsoft - ");
238 if (lim
.rlim_cur
== RLIM_INFINITY
)
241 os_info("%llu\n", (unsigned long long)lim
.rlim_cur
);
243 os_info("\thard - ");
244 if (lim
.rlim_max
== RLIM_INFINITY
)
247 os_info("%llu\n", (unsigned long long)lim
.rlim_max
);
250 void __init
get_host_cpu_features(
251 void (*flags_helper_func
)(char *line
),
252 void (*cache_helper_func
)(char *line
))
257 int done_parsing
= 0;
259 cpuinfo
= fopen("/proc/cpuinfo", "r");
260 if (cpuinfo
== NULL
) {
261 os_info("Failed to get host CPU features\n");
263 while ((getline(&line
, &len
, cpuinfo
)) != -1) {
264 if (strstr(line
, "flags")) {
265 flags_helper_func(line
);
268 if (strstr(line
, "cache_alignment")) {
269 cache_helper_func(line
);
274 if (done_parsing
> 1)
282 void __init
os_early_checks(void)
286 /* Print out the core dump limits early */
287 check_coredump_limit();
291 /* Need to check this early because mmapping happens before the
296 pid
= start_ptraced_child();
297 if (init_pid_registers(pid
))
298 fatal("Failed to initialize default registers");
299 stop_ptraced_child(pid
, 1);
302 int __init
parse_iomem(char *str
, int *add
)
304 struct iomem_region
*new;
310 file
= strchr(str
,',');
312 os_warn("parse_iomem : failed to parse iomem\n");
317 fd
= open(file
, O_RDWR
, 0);
319 perror("parse_iomem - Couldn't open io file");
323 if (fstat64(fd
, &buf
) < 0) {
324 perror("parse_iomem - cannot stat_fd file");
328 new = malloc(sizeof(*new));
330 perror("Couldn't allocate iomem_region struct");
334 size
= (buf
.st_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
336 *new = ((struct iomem_region
) { .next
= iomem_regions
,
343 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;