2 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
14 #include <sys/ptrace.h>
16 #include <asm/unistd.h>
21 #define ARBITRARY_ADDR -1
22 #define FAILURE_PID -1
24 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
25 #define COMM_SCANF "%*[^)])"
27 unsigned long os_process_pc(int pid
)
29 char proc_stat
[STAT_PATH_LEN
], buf
[256];
30 unsigned long pc
= ARBITRARY_ADDR
;
33 sprintf(proc_stat
, "/proc/%d/stat", pid
);
34 fd
= open(proc_stat
, O_RDONLY
, 0);
36 printk(UM_KERN_ERR
"os_process_pc - couldn't open '%s', "
37 "errno = %d\n", proc_stat
, errno
);
40 CATCH_EINTR(err
= read(fd
, buf
, sizeof(buf
)));
42 printk(UM_KERN_ERR
"os_process_pc - couldn't read '%s', "
43 "err = %d\n", proc_stat
, errno
);
48 if (sscanf(buf
, "%*d " COMM_SCANF
" %*c %*d %*d %*d %*d %*d %*d %*d "
49 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
50 "%*d %*d %*d %*d %*d %lu", &pc
) != 1)
51 printk(UM_KERN_ERR
"os_process_pc - couldn't find pc in '%s'\n",
59 int os_process_parent(int pid
)
61 char stat
[STAT_PATH_LEN
];
63 int parent
= FAILURE_PID
, n
, fd
;
68 snprintf(stat
, sizeof(stat
), "/proc/%d/stat", pid
);
69 fd
= open(stat
, O_RDONLY
, 0);
71 printk(UM_KERN_ERR
"Couldn't open '%s', errno = %d\n", stat
,
76 CATCH_EINTR(n
= read(fd
, data
, sizeof(data
)));
80 printk(UM_KERN_ERR
"Couldn't read '%s', errno = %d\n", stat
,
86 n
= sscanf(data
, "%*d " COMM_SCANF
" %*c %d", &parent
);
88 printk(UM_KERN_ERR
"Failed to scan '%s'\n", data
);
93 void os_alarm_process(int pid
)
98 void os_stop_process(int pid
)
103 void os_kill_process(int pid
, int reap_child
)
107 CATCH_EINTR(waitpid(pid
, NULL
, __WALL
));
110 /* Kill off a ptraced child by all means available. kill it normally first,
111 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
112 * which it can't exit directly.
115 void os_kill_ptraced_process(int pid
, int reap_child
)
118 ptrace(PTRACE_KILL
, pid
);
119 ptrace(PTRACE_CONT
, pid
);
121 CATCH_EINTR(waitpid(pid
, NULL
, __WALL
));
124 /* Don't use the glibc version, which caches the result in TLS. It misses some
125 * syscalls, and also breaks with clone(), which does not unshare the TLS.
130 return syscall(__NR_getpid
);
138 int os_map_memory(void *virt
, int fd
, unsigned long long off
, unsigned long len
,
144 prot
= (r
? PROT_READ
: 0) | (w
? PROT_WRITE
: 0) |
147 loc
= mmap64((void *) virt
, len
, prot
, MAP_SHARED
| MAP_FIXED
,
149 if (loc
== MAP_FAILED
)
154 int os_protect_memory(void *addr
, unsigned long len
, int r
, int w
, int x
)
156 int prot
= ((r
? PROT_READ
: 0) | (w
? PROT_WRITE
: 0) |
157 (x
? PROT_EXEC
: 0));
159 if (mprotect(addr
, len
, prot
) < 0)
165 int os_unmap_memory(void *addr
, int len
)
169 err
= munmap(addr
, len
);
176 #define MADV_REMOVE KERNEL_MADV_REMOVE
179 int os_drop_memory(void *addr
, int length
)
183 err
= madvise(addr
, length
, MADV_REMOVE
);
189 int __init
can_drop_memory(void)
194 printk(UM_KERN_INFO
"Checking host MADV_REMOVE support...");
195 fd
= create_mem_file(UM_KERN_PAGE_SIZE
);
197 printk(UM_KERN_ERR
"Creating test memory file failed, "
202 addr
= mmap64(NULL
, UM_KERN_PAGE_SIZE
, PROT_READ
| PROT_WRITE
,
204 if (addr
== MAP_FAILED
) {
205 printk(UM_KERN_ERR
"Mapping test memory file failed, "
206 "err = %d\n", -errno
);
210 if (madvise(addr
, UM_KERN_PAGE_SIZE
, MADV_REMOVE
) != 0) {
211 printk(UM_KERN_ERR
"MADV_REMOVE failed, err = %d\n", -errno
);
215 printk(UM_KERN_CONT
"OK\n");
219 munmap(addr
, UM_KERN_PAGE_SIZE
);
226 static int os_page_mincore(void *addr
)
231 ret
= mincore(addr
, UM_KERN_PAGE_SIZE
, vec
);
233 if (errno
== ENOMEM
|| errno
== EINVAL
)
242 int os_mincore(void *addr
, unsigned long len
)
247 if (len
<= UM_KERN_PAGE_SIZE
)
248 return os_page_mincore(addr
);
250 vec
= calloc(1, (len
+ UM_KERN_PAGE_SIZE
- 1) / UM_KERN_PAGE_SIZE
);
254 ret
= mincore(addr
, UM_KERN_PAGE_SIZE
, vec
);
256 if (errno
== ENOMEM
|| errno
== EINVAL
)
264 for (i
= 0; i
< ((len
+ UM_KERN_PAGE_SIZE
- 1) / UM_KERN_PAGE_SIZE
); i
++) {
277 void init_new_thread_signals(void)
279 set_handler(SIGSEGV
);
280 set_handler(SIGTRAP
);
284 signal(SIGHUP
, SIG_IGN
);
286 signal(SIGWINCH
, SIG_IGN
);