2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
10 #include <linux/unistd.h>
14 #include <sys/syscall.h>
15 #include "ptrace_user.h"
18 #include "user_util.h"
21 #include "kern_util.h"
23 #include "skas_ptrace.h"
24 #include "kern_constants.h"
26 #define ARBITRARY_ADDR -1
27 #define FAILURE_PID -1
29 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
30 #define COMM_SCANF "%*[^)])"
32 unsigned long os_process_pc(int pid)
34 char proc_stat[STAT_PATH_LEN], buf[256];
38 sprintf(proc_stat, "/proc/%d/stat", pid);
39 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
41 printk("os_process_pc - couldn't open '%s', err = %d\n",
43 return(ARBITRARY_ADDR);
45 err = os_read_file(fd, buf, sizeof(buf));
47 printk("os_process_pc - couldn't read '%s', err = %d\n",
50 return(ARBITRARY_ADDR);
54 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
55 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
56 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
57 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
62 int os_process_parent(int pid)
64 char stat[STAT_PATH_LEN];
68 if(pid == -1) return(-1);
70 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
71 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
73 printk("Couldn't open '%s', err = %d\n", stat, -fd);
77 n = os_read_file(fd, data, sizeof(data));
81 printk("Couldn't read '%s', err = %d\n", stat, -n);
86 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
88 printk("Failed to scan '%s'\n", data);
93 void os_stop_process(int pid)
98 void os_kill_process(int pid, int reap_child)
102 CATCH_EINTR(waitpid(pid, NULL, 0));
106 /* This is here uniquely to have access to the userspace errno, i.e. the one
107 * used by ptrace in case of error.
110 long os_ptrace_ldt(long pid, long addr, long data)
114 ret = ptrace(PTRACE_LDT, pid, addr, data);
121 /* Kill off a ptraced child by all means available. kill it normally first,
122 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
123 * which it can't exit directly.
126 void os_kill_ptraced_process(int pid, int reap_child)
129 ptrace(PTRACE_KILL, pid);
130 ptrace(PTRACE_CONT, pid);
132 CATCH_EINTR(waitpid(pid, NULL, 0));
135 void os_usr1_process(int pid)
140 /* Don't use the glibc version, which caches the result in TLS. It misses some
141 * syscalls, and also breaks with clone(), which does not unshare the TLS.
146 return(syscall(__NR_getpid));
154 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
160 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
163 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
165 if(loc == MAP_FAILED)
170 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
172 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
173 (x ? PROT_EXEC : 0));
175 if(mprotect(addr, len, prot) < 0)
180 int os_unmap_memory(void *addr, int len)
184 err = munmap(addr, len);
191 #define MADV_REMOVE KERNEL_MADV_REMOVE
194 int os_drop_memory(void *addr, int length)
198 err = madvise(addr, length, MADV_REMOVE);
204 int can_drop_memory(void)
209 printk("Checking host MADV_REMOVE support...");
210 fd = create_mem_file(UM_KERN_PAGE_SIZE);
212 printk("Creating test memory file failed, err = %d\n", -fd);
216 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
218 if(addr == MAP_FAILED){
219 printk("Mapping test memory file failed, err = %d\n", -errno);
223 if(madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0){
224 printk("MADV_REMOVE failed, err = %d\n", -errno);
232 munmap(addr, UM_KERN_PAGE_SIZE);
239 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
241 int flags = 0, pages;
243 if(sig_stack != NULL){
244 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
245 set_sigstack(sig_stack, pages * page_size());
251 sa.sa_handler = usr1_handler;
252 sigemptyset(&sa.sa_mask);
254 sa.sa_restorer = NULL;
255 if(sigaction(SIGUSR1, &sa, NULL) < 0)
256 panic("init_new_thread_stack - sigaction failed - "
257 "errno = %d\n", errno);
261 void init_new_thread_signals(void)
263 set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
264 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
265 set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
266 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
267 set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
268 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
269 set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
270 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
271 set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
272 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
273 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
274 SA_ONSTACK, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM,
276 signal(SIGHUP, SIG_IGN);
281 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
287 n = UML_SETJMP(&buf);