2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
10 #include <linux/unistd.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.
143 inline _syscall0(pid_t, getpid)
155 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
161 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
164 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
166 if(loc == MAP_FAILED)
171 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
173 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
174 (x ? PROT_EXEC : 0));
176 if(mprotect(addr, len, prot) < 0)
181 int os_unmap_memory(void *addr, int len)
185 err = munmap(addr, len);
192 #define MADV_REMOVE KERNEL_MADV_REMOVE
195 int os_drop_memory(void *addr, int length)
199 err = madvise(addr, length, MADV_REMOVE);
205 int can_drop_memory(void)
210 printk("Checking host MADV_REMOVE support...");
211 fd = create_mem_file(UM_KERN_PAGE_SIZE);
213 printk("Creating test memory file failed, err = %d\n", -fd);
217 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
219 if(addr == MAP_FAILED){
220 printk("Mapping test memory file failed, err = %d\n", -errno);
224 if(madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0){
225 printk("MADV_REMOVE failed, err = %d\n", -errno);
233 munmap(addr, UM_KERN_PAGE_SIZE);
240 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
242 int flags = 0, pages;
244 if(sig_stack != NULL){
245 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
246 set_sigstack(sig_stack, pages * page_size());
252 sa.sa_handler = usr1_handler;
253 sigemptyset(&sa.sa_mask);
255 sa.sa_restorer = NULL;
256 if(sigaction(SIGUSR1, &sa, NULL) < 0)
257 panic("init_new_thread_stack - sigaction failed - "
258 "errno = %d\n", errno);
262 void init_new_thread_signals(void)
264 set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
265 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
266 set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
267 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
268 set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
269 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
270 set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
271 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
272 set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
273 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
274 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
275 SA_ONSTACK, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM,
277 signal(SIGHUP, SIG_IGN);
282 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
288 n = UML_SETJMP(&buf);