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"
22 #define ARBITRARY_ADDR -1
23 #define FAILURE_PID -1
25 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
26 #define COMM_SCANF "%*[^)])"
28 unsigned long os_process_pc(int pid)
30 char proc_stat[STAT_PATH_LEN], buf[256];
34 sprintf(proc_stat, "/proc/%d/stat", pid);
35 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0);
37 printk("os_process_pc - couldn't open '%s', err = %d\n",
39 return(ARBITRARY_ADDR);
41 err = os_read_file(fd, buf, sizeof(buf));
43 printk("os_process_pc - couldn't read '%s', err = %d\n",
46 return(ARBITRARY_ADDR);
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("os_process_pc - couldn't find pc in '%s'\n", buf);
58 int os_process_parent(int pid)
60 char stat[STAT_PATH_LEN];
64 if(pid == -1) return(-1);
66 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
67 fd = os_open_file(stat, of_read(OPENFLAGS()), 0);
69 printk("Couldn't open '%s', err = %d\n", stat, -fd);
73 n = os_read_file(fd, data, sizeof(data));
77 printk("Couldn't read '%s', err = %d\n", stat, -n);
82 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
84 printk("Failed to scan '%s'\n", data);
89 void os_stop_process(int pid)
94 void os_kill_process(int pid, int reap_child)
98 CATCH_EINTR(waitpid(pid, NULL, 0));
102 /* Kill off a ptraced child by all means available. kill it normally first,
103 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
104 * which it can't exit directly.
107 void os_kill_ptraced_process(int pid, int reap_child)
110 ptrace(PTRACE_KILL, pid);
111 ptrace(PTRACE_CONT, pid);
113 CATCH_EINTR(waitpid(pid, NULL, 0));
116 void os_usr1_process(int pid)
121 /* Don't use the glibc version, which caches the result in TLS. It misses some
122 * syscalls, and also breaks with clone(), which does not unshare the TLS.
125 inline _syscall0(pid_t, getpid)
137 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
143 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
146 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
148 if(loc == MAP_FAILED)
153 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
155 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
156 (x ? PROT_EXEC : 0));
158 if(mprotect(addr, len, prot) < 0)
163 int os_unmap_memory(void *addr, int len)
167 err = munmap(addr, len);
173 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
175 int flags = 0, pages;
177 if(sig_stack != NULL){
178 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
179 set_sigstack(sig_stack, pages * page_size());
182 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
185 void init_new_thread_signals(int altstack)
187 int flags = altstack ? SA_ONSTACK : 0;
189 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
190 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
191 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
192 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
193 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
194 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
195 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
196 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
197 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
198 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
199 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
200 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
201 signal(SIGHUP, SIG_IGN);
203 init_irq_signals(altstack);
206 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
212 n = sigsetjmp(buf, 1);
220 * Overrides for Emacs so that we follow Linus's tabbing style.
221 * Emacs will notice this stuff at the end of the file and automatically
222 * adjust the settings for this buffer only. This must remain at the end
224 * ---------------------------------------------------------------------------
226 * c-file-style: "linux"