2 * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
11 #include <linux/unistd.h>
14 #include "ptrace_user.h"
17 #include "user_util.h"
20 #include "kern_util.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];
35 sprintf(proc_stat, "/proc/%d/stat", pid);
36 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
38 printk("os_process_pc - couldn't open '%s', err = %d\n",
40 return(ARBITRARY_ADDR);
42 err = os_read_file(fd, buf, sizeof(buf));
44 printk("os_process_pc - couldn't read '%s', err = %d\n",
47 return(ARBITRARY_ADDR);
51 if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
52 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
53 "%*d %*d %*d %*d %*d %lu", &pc) != 1){
54 printk("os_process_pc - couldn't find pc in '%s'\n", buf);
59 int os_process_parent(int pid)
61 char stat[STAT_PATH_LEN];
65 if(pid == -1) return(-1);
67 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
68 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
70 printk("Couldn't open '%s', err = %d\n", stat, -fd);
74 n = os_read_file(fd, data, sizeof(data));
78 printk("Couldn't read '%s', err = %d\n", stat, -n);
83 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
85 printk("Failed to scan '%s'\n", data);
90 void os_stop_process(int pid)
95 void os_kill_process(int pid, int reap_child)
99 CATCH_EINTR(waitpid(pid, NULL, 0));
103 /* Kill off a ptraced child by all means available. kill it normally first,
104 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
105 * which it can't exit directly.
108 void os_kill_ptraced_process(int pid, int reap_child)
111 ptrace(PTRACE_KILL, pid);
112 ptrace(PTRACE_CONT, pid);
114 CATCH_EINTR(waitpid(pid, NULL, 0));
117 void os_usr1_process(int pid)
122 /* Don't use the glibc version, which caches the result in TLS. It misses some
123 * syscalls, and also breaks with clone(), which does not unshare the TLS.
126 inline _syscall0(pid_t, getpid)
138 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
144 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
147 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
149 if(loc == MAP_FAILED)
154 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
156 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
157 (x ? PROT_EXEC : 0));
159 if(mprotect(addr, len, prot) < 0)
164 int os_unmap_memory(void *addr, int len)
168 err = munmap(addr, len);
174 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
176 int flags = 0, pages;
178 if(sig_stack != NULL){
179 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
180 set_sigstack(sig_stack, pages * page_size());
183 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
186 void init_new_thread_signals(int altstack)
188 int flags = altstack ? SA_ONSTACK : 0;
190 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
191 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
192 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
193 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
194 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
195 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
196 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
197 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
198 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
199 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
200 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
201 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
202 signal(SIGHUP, SIG_IGN);
204 init_irq_signals(altstack);
207 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
213 n = UML_SIGSETJMP(&buf, enable);