2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
12 #include <sys/ptrace.h>
14 #include <asm/unistd.h>
16 #include "kern_constants.h"
20 #include "skas_ptrace.h"
23 #define ARBITRARY_ADDR -1
24 #define FAILURE_PID -1
26 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
27 #define COMM_SCANF "%*[^)])"
29 unsigned long os_process_pc(int pid
)
31 char proc_stat
[STAT_PATH_LEN
], buf
[256];
32 unsigned long pc
= ARBITRARY_ADDR
;
35 sprintf(proc_stat
, "/proc/%d/stat", pid
);
36 fd
= open(proc_stat
, O_RDONLY
, 0);
38 printk(UM_KERN_ERR
"os_process_pc - couldn't open '%s', "
39 "errno = %d\n", proc_stat
, errno
);
42 CATCH_EINTR(err
= read(fd
, buf
, sizeof(buf
)));
44 printk(UM_KERN_ERR
"os_process_pc - couldn't read '%s', "
45 "err = %d\n", proc_stat
, errno
);
50 if (sscanf(buf
, "%*d " COMM_SCANF
" %*c %*d %*d %*d %*d %*d %*d %*d "
51 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
52 "%*d %*d %*d %*d %*d %lu", &pc
) != 1)
53 printk(UM_KERN_ERR
"os_process_pc - couldn't find pc in '%s'\n",
61 int os_process_parent(int pid
)
63 char stat
[STAT_PATH_LEN
];
65 int parent
= FAILURE_PID
, n
, fd
;
70 snprintf(stat
, sizeof(stat
), "/proc/%d/stat", pid
);
71 fd
= open(stat
, O_RDONLY
, 0);
73 printk(UM_KERN_ERR
"Couldn't open '%s', errno = %d\n", stat
,
78 CATCH_EINTR(n
= read(fd
, data
, sizeof(data
)));
82 printk(UM_KERN_ERR
"Couldn't read '%s', errno = %d\n", stat
,
88 n
= sscanf(data
, "%*d " COMM_SCANF
" %*c %d", &parent
);
90 printk(UM_KERN_ERR
"Failed to scan '%s'\n", data
);
95 void os_stop_process(int pid
)
100 void os_kill_process(int pid
, int reap_child
)
104 CATCH_EINTR(waitpid(pid
, NULL
, __WALL
));
107 /* This is here uniquely to have access to the userspace errno, i.e. the one
108 * used by ptrace in case of error.
111 long os_ptrace_ldt(long pid
, long addr
, long data
)
115 ret
= ptrace(PTRACE_LDT
, pid
, addr
, data
);
122 /* Kill off a ptraced child by all means available. kill it normally first,
123 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
124 * which it can't exit directly.
127 void os_kill_ptraced_process(int pid
, int reap_child
)
130 ptrace(PTRACE_KILL
, pid
);
131 ptrace(PTRACE_CONT
, pid
);
133 CATCH_EINTR(waitpid(pid
, NULL
, __WALL
));
136 /* Don't use the glibc version, which caches the result in TLS. It misses some
137 * syscalls, and also breaks with clone(), which does not unshare the TLS.
142 return syscall(__NR_getpid
);
150 int os_map_memory(void *virt
, int fd
, unsigned long long off
, unsigned long len
,
156 prot
= (r
? PROT_READ
: 0) | (w
? PROT_WRITE
: 0) |
159 loc
= mmap64((void *) virt
, len
, prot
, MAP_SHARED
| MAP_FIXED
,
161 if (loc
== MAP_FAILED
)
166 int os_protect_memory(void *addr
, unsigned long len
, int r
, int w
, int x
)
168 int prot
= ((r
? PROT_READ
: 0) | (w
? PROT_WRITE
: 0) |
169 (x
? PROT_EXEC
: 0));
171 if (mprotect(addr
, len
, prot
) < 0)
177 int os_unmap_memory(void *addr
, int len
)
181 err
= munmap(addr
, len
);
188 #define MADV_REMOVE KERNEL_MADV_REMOVE
191 int os_drop_memory(void *addr
, int length
)
195 err
= madvise(addr
, length
, MADV_REMOVE
);
201 int __init
can_drop_memory(void)
206 printk(UM_KERN_INFO
"Checking host MADV_REMOVE support...");
207 fd
= create_mem_file(UM_KERN_PAGE_SIZE
);
209 printk(UM_KERN_ERR
"Creating test memory file failed, "
214 addr
= mmap64(NULL
, UM_KERN_PAGE_SIZE
, PROT_READ
| PROT_WRITE
,
216 if (addr
== MAP_FAILED
) {
217 printk(UM_KERN_ERR
"Mapping test memory file failed, "
218 "err = %d\n", -errno
);
222 if (madvise(addr
, UM_KERN_PAGE_SIZE
, MADV_REMOVE
) != 0) {
223 printk(UM_KERN_ERR
"MADV_REMOVE failed, err = %d\n", -errno
);
231 munmap(addr
, UM_KERN_PAGE_SIZE
);
238 void init_new_thread_signals(void)
240 set_handler(SIGSEGV
, (__sighandler_t
) sig_handler
, SA_ONSTACK
,
241 SIGUSR1
, SIGIO
, SIGWINCH
, SIGVTALRM
, -1);
242 set_handler(SIGTRAP
, (__sighandler_t
) sig_handler
, SA_ONSTACK
,
243 SIGUSR1
, SIGIO
, SIGWINCH
, SIGVTALRM
, -1);
244 set_handler(SIGFPE
, (__sighandler_t
) sig_handler
, SA_ONSTACK
,
245 SIGUSR1
, SIGIO
, SIGWINCH
, SIGVTALRM
, -1);
246 set_handler(SIGILL
, (__sighandler_t
) sig_handler
, SA_ONSTACK
,
247 SIGUSR1
, SIGIO
, SIGWINCH
, SIGVTALRM
, -1);
248 set_handler(SIGBUS
, (__sighandler_t
) sig_handler
, SA_ONSTACK
,
249 SIGUSR1
, SIGIO
, SIGWINCH
, SIGVTALRM
, -1);
250 signal(SIGHUP
, SIG_IGN
);
255 int run_kernel_thread(int (*fn
)(void *), void *arg
, jmp_buf **jmp_ptr
)
261 n
= UML_SETJMP(&buf
);