2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
13 #include <sys/syscall.h>
14 #include "ptrace_user.h"
17 #include "user_util.h"
20 #include "kern_util.h"
22 #include "skas_ptrace.h"
23 #include "kern_constants.h"
25 #define ARBITRARY_ADDR -1
26 #define FAILURE_PID -1
28 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
29 #define COMM_SCANF "%*[^)])"
31 unsigned long os_process_pc(int pid)
33 char proc_stat[STAT_PATH_LEN], buf[256];
37 sprintf(proc_stat, "/proc/%d/stat", pid);
38 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
40 printk("os_process_pc - couldn't open '%s', err = %d\n",
42 return(ARBITRARY_ADDR);
44 err = os_read_file(fd, buf, sizeof(buf));
46 printk("os_process_pc - couldn't read '%s', err = %d\n",
49 return(ARBITRARY_ADDR);
53 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
54 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
55 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
56 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
61 int os_process_parent(int pid)
63 char stat[STAT_PATH_LEN];
67 if(pid == -1) return(-1);
69 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
70 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
72 printk("Couldn't open '%s', err = %d\n", stat, -fd);
76 n = os_read_file(fd, data, sizeof(data));
80 printk("Couldn't read '%s', err = %d\n", stat, -n);
85 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
87 printk("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, 0));
105 /* This is here uniquely to have access to the userspace errno, i.e. the one
106 * used by ptrace in case of error.
109 long os_ptrace_ldt(long pid, long addr, long data)
113 ret = ptrace(PTRACE_LDT, pid, addr, data);
120 /* Kill off a ptraced child by all means available. kill it normally first,
121 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
122 * which it can't exit directly.
125 void os_kill_ptraced_process(int pid, int reap_child)
128 ptrace(PTRACE_KILL, pid);
129 ptrace(PTRACE_CONT, pid);
131 CATCH_EINTR(waitpid(pid, NULL, 0));
134 void os_usr1_process(int pid)
139 /* Don't use the glibc version, which caches the result in TLS. It misses some
140 * syscalls, and also breaks with clone(), which does not unshare the TLS.
145 return(syscall(__NR_getpid));
153 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
159 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
162 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
164 if(loc == MAP_FAILED)
169 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
171 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
172 (x ? PROT_EXEC : 0));
174 if(mprotect(addr, len, prot) < 0)
179 int os_unmap_memory(void *addr, int len)
183 err = munmap(addr, len);
190 #define MADV_REMOVE KERNEL_MADV_REMOVE
193 int os_drop_memory(void *addr, int length)
197 err = madvise(addr, length, MADV_REMOVE);
203 int can_drop_memory(void)
208 printk("Checking host MADV_REMOVE support...");
209 fd = create_mem_file(UM_KERN_PAGE_SIZE);
211 printk("Creating test memory file failed, err = %d\n", -fd);
215 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
217 if(addr == MAP_FAILED){
218 printk("Mapping test memory file failed, err = %d\n", -errno);
222 if(madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0){
223 printk("MADV_REMOVE failed, err = %d\n", -errno);
231 munmap(addr, UM_KERN_PAGE_SIZE);
238 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
240 int flags = 0, pages;
242 if(sig_stack != NULL){
243 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
244 set_sigstack(sig_stack, pages * page_size());
250 sa.sa_handler = usr1_handler;
251 sigemptyset(&sa.sa_mask);
253 sa.sa_restorer = NULL;
254 if(sigaction(SIGUSR1, &sa, NULL) < 0)
255 panic("init_new_thread_stack - sigaction failed - "
256 "errno = %d\n", errno);
260 void init_new_thread_signals(void)
262 set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
263 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
264 set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
265 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
266 set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
267 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
268 set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
269 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
270 set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
271 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
272 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
273 SA_ONSTACK, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM,
275 signal(SIGHUP, SIG_IGN);
280 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
286 n = UML_SETJMP(&buf);