2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
13 #include <sys/ptrace.h>
15 #include <asm/unistd.h>
20 #define ARBITRARY_ADDR -1
21 #define FAILURE_PID -1
23 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
24 #define COMM_SCANF "%*[^)])"
26 unsigned long os_process_pc(int pid
)
28 char proc_stat
[STAT_PATH_LEN
], buf
[256];
29 unsigned long pc
= ARBITRARY_ADDR
;
32 sprintf(proc_stat
, "/proc/%d/stat", pid
);
33 fd
= open(proc_stat
, O_RDONLY
, 0);
35 printk(UM_KERN_ERR
"os_process_pc - couldn't open '%s', "
36 "errno = %d\n", proc_stat
, errno
);
39 CATCH_EINTR(err
= read(fd
, buf
, sizeof(buf
)));
41 printk(UM_KERN_ERR
"os_process_pc - couldn't read '%s', "
42 "err = %d\n", proc_stat
, errno
);
47 if (sscanf(buf
, "%*d " COMM_SCANF
" %*c %*d %*d %*d %*d %*d %*d %*d "
48 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
49 "%*d %*d %*d %*d %*d %lu", &pc
) != 1)
50 printk(UM_KERN_ERR
"os_process_pc - couldn't find pc in '%s'\n",
58 int os_process_parent(int pid
)
60 char stat
[STAT_PATH_LEN
];
62 int parent
= FAILURE_PID
, n
, fd
;
67 snprintf(stat
, sizeof(stat
), "/proc/%d/stat", pid
);
68 fd
= open(stat
, O_RDONLY
, 0);
70 printk(UM_KERN_ERR
"Couldn't open '%s', errno = %d\n", stat
,
75 CATCH_EINTR(n
= read(fd
, data
, sizeof(data
)));
79 printk(UM_KERN_ERR
"Couldn't read '%s', errno = %d\n", stat
,
85 n
= sscanf(data
, "%*d " COMM_SCANF
" %*c %d", &parent
);
87 printk(UM_KERN_ERR
"Failed to scan '%s'\n", data
);
92 void os_stop_process(int pid
)
97 void os_kill_process(int pid
, int reap_child
)
101 CATCH_EINTR(waitpid(pid
, NULL
, __WALL
));
104 /* Kill off a ptraced child by all means available. kill it normally first,
105 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
106 * which it can't exit directly.
109 void os_kill_ptraced_process(int pid
, int reap_child
)
112 ptrace(PTRACE_KILL
, pid
);
113 ptrace(PTRACE_CONT
, pid
);
115 CATCH_EINTR(waitpid(pid
, NULL
, __WALL
));
118 /* Don't use the glibc version, which caches the result in TLS. It misses some
119 * syscalls, and also breaks with clone(), which does not unshare the TLS.
124 return syscall(__NR_getpid
);
132 int os_map_memory(void *virt
, int fd
, unsigned long long off
, unsigned long len
,
138 prot
= (r
? PROT_READ
: 0) | (w
? PROT_WRITE
: 0) |
141 loc
= mmap64((void *) virt
, len
, prot
, MAP_SHARED
| MAP_FIXED
,
143 if (loc
== MAP_FAILED
)
148 int os_protect_memory(void *addr
, unsigned long len
, int r
, int w
, int x
)
150 int prot
= ((r
? PROT_READ
: 0) | (w
? PROT_WRITE
: 0) |
151 (x
? PROT_EXEC
: 0));
153 if (mprotect(addr
, len
, prot
) < 0)
159 int os_unmap_memory(void *addr
, int len
)
163 err
= munmap(addr
, len
);
170 #define MADV_REMOVE KERNEL_MADV_REMOVE
173 int os_drop_memory(void *addr
, int length
)
177 err
= madvise(addr
, length
, MADV_REMOVE
);
183 int __init
can_drop_memory(void)
188 printk(UM_KERN_INFO
"Checking host MADV_REMOVE support...");
189 fd
= create_mem_file(UM_KERN_PAGE_SIZE
);
191 printk(UM_KERN_ERR
"Creating test memory file failed, "
196 addr
= mmap64(NULL
, UM_KERN_PAGE_SIZE
, PROT_READ
| PROT_WRITE
,
198 if (addr
== MAP_FAILED
) {
199 printk(UM_KERN_ERR
"Mapping test memory file failed, "
200 "err = %d\n", -errno
);
204 if (madvise(addr
, UM_KERN_PAGE_SIZE
, MADV_REMOVE
) != 0) {
205 printk(UM_KERN_ERR
"MADV_REMOVE failed, err = %d\n", -errno
);
209 printk(UM_KERN_CONT
"OK\n");
213 munmap(addr
, UM_KERN_PAGE_SIZE
);
220 static int os_page_mincore(void *addr
)
225 ret
= mincore(addr
, UM_KERN_PAGE_SIZE
, vec
);
227 if (errno
== ENOMEM
|| errno
== EINVAL
)
236 int os_mincore(void *addr
, unsigned long len
)
241 if (len
<= UM_KERN_PAGE_SIZE
)
242 return os_page_mincore(addr
);
244 vec
= calloc(1, (len
+ UM_KERN_PAGE_SIZE
- 1) / UM_KERN_PAGE_SIZE
);
248 ret
= mincore(addr
, UM_KERN_PAGE_SIZE
, vec
);
250 if (errno
== ENOMEM
|| errno
== EINVAL
)
258 for (i
= 0; i
< ((len
+ UM_KERN_PAGE_SIZE
- 1) / UM_KERN_PAGE_SIZE
); i
++) {
271 void init_new_thread_signals(void)
273 set_handler(SIGSEGV
);
274 set_handler(SIGTRAP
);
278 signal(SIGHUP
, SIG_IGN
);
280 signal(SIGWINCH
, SIG_IGN
);