Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 Jeff Dike (jdike@addtoit.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Paolo 'Blaisorblade' Giarrusso | ae756df | 2005-09-21 18:40:10 +0200 | [diff] [blame^] | 6 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <errno.h> |
| 9 | #include <signal.h> |
Gennady Sharapov | 60d339f | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 10 | #include <setjmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/unistd.h> |
| 12 | #include <sys/mman.h> |
| 13 | #include <sys/wait.h> |
| 14 | #include "ptrace_user.h" |
| 15 | #include "os.h" |
| 16 | #include "user.h" |
| 17 | #include "user_util.h" |
Gennady Sharapov | 60d339f | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 18 | #include "signal_user.h" |
| 19 | #include "process.h" |
| 20 | #include "irq_user.h" |
| 21 | #include "kern_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #define ARBITRARY_ADDR -1 |
| 24 | #define FAILURE_PID -1 |
| 25 | |
| 26 | #define STAT_PATH_LEN sizeof("/proc/#######/stat\0") |
| 27 | #define COMM_SCANF "%*[^)])" |
| 28 | |
| 29 | unsigned long os_process_pc(int pid) |
| 30 | { |
| 31 | char proc_stat[STAT_PATH_LEN], buf[256]; |
| 32 | unsigned long pc; |
| 33 | int fd, err; |
| 34 | |
| 35 | sprintf(proc_stat, "/proc/%d/stat", pid); |
| 36 | fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0); |
| 37 | if(fd < 0){ |
| 38 | printk("os_process_pc - couldn't open '%s', err = %d\n", |
| 39 | proc_stat, -fd); |
| 40 | return(ARBITRARY_ADDR); |
| 41 | } |
| 42 | err = os_read_file(fd, buf, sizeof(buf)); |
| 43 | if(err < 0){ |
| 44 | printk("os_process_pc - couldn't read '%s', err = %d\n", |
| 45 | proc_stat, -err); |
| 46 | os_close_file(fd); |
| 47 | return(ARBITRARY_ADDR); |
| 48 | } |
| 49 | os_close_file(fd); |
| 50 | pc = 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); |
| 55 | } |
| 56 | return(pc); |
| 57 | } |
| 58 | |
| 59 | int os_process_parent(int pid) |
| 60 | { |
| 61 | char stat[STAT_PATH_LEN]; |
| 62 | char data[256]; |
| 63 | int parent, n, fd; |
| 64 | |
| 65 | if(pid == -1) return(-1); |
| 66 | |
| 67 | snprintf(stat, sizeof(stat), "/proc/%d/stat", pid); |
| 68 | fd = os_open_file(stat, of_read(OPENFLAGS()), 0); |
| 69 | if(fd < 0){ |
| 70 | printk("Couldn't open '%s', err = %d\n", stat, -fd); |
| 71 | return(FAILURE_PID); |
| 72 | } |
| 73 | |
| 74 | n = os_read_file(fd, data, sizeof(data)); |
| 75 | os_close_file(fd); |
| 76 | |
| 77 | if(n < 0){ |
| 78 | printk("Couldn't read '%s', err = %d\n", stat, -n); |
| 79 | return(FAILURE_PID); |
| 80 | } |
| 81 | |
| 82 | parent = FAILURE_PID; |
| 83 | n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent); |
| 84 | if(n != 1) |
| 85 | printk("Failed to scan '%s'\n", data); |
| 86 | |
| 87 | return(parent); |
| 88 | } |
| 89 | |
| 90 | void os_stop_process(int pid) |
| 91 | { |
| 92 | kill(pid, SIGSTOP); |
| 93 | } |
| 94 | |
| 95 | void os_kill_process(int pid, int reap_child) |
| 96 | { |
| 97 | kill(pid, SIGKILL); |
| 98 | if(reap_child) |
| 99 | CATCH_EINTR(waitpid(pid, NULL, 0)); |
| 100 | |
| 101 | } |
| 102 | |
| 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. |
| 106 | */ |
| 107 | |
| 108 | void os_kill_ptraced_process(int pid, int reap_child) |
| 109 | { |
| 110 | kill(pid, SIGKILL); |
| 111 | ptrace(PTRACE_KILL, pid); |
| 112 | ptrace(PTRACE_CONT, pid); |
| 113 | if(reap_child) |
| 114 | CATCH_EINTR(waitpid(pid, NULL, 0)); |
| 115 | } |
| 116 | |
| 117 | void os_usr1_process(int pid) |
| 118 | { |
| 119 | kill(pid, SIGUSR1); |
| 120 | } |
| 121 | |
Gennady Sharapov | 60d339f | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 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. |
| 124 | */ |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | inline _syscall0(pid_t, getpid) |
| 127 | |
| 128 | int os_getpid(void) |
| 129 | { |
| 130 | return(getpid()); |
| 131 | } |
| 132 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 133 | int os_getpgrp(void) |
| 134 | { |
| 135 | return getpgrp(); |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len, |
| 139 | int r, int w, int x) |
| 140 | { |
| 141 | void *loc; |
| 142 | int prot; |
| 143 | |
| 144 | prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | |
| 145 | (x ? PROT_EXEC : 0); |
| 146 | |
| 147 | loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED, |
| 148 | fd, off); |
| 149 | if(loc == MAP_FAILED) |
| 150 | return(-errno); |
| 151 | return(0); |
| 152 | } |
| 153 | |
| 154 | int os_protect_memory(void *addr, unsigned long len, int r, int w, int x) |
| 155 | { |
| 156 | int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | |
| 157 | (x ? PROT_EXEC : 0)); |
| 158 | |
| 159 | if(mprotect(addr, len, prot) < 0) |
| 160 | return(-errno); |
| 161 | return(0); |
| 162 | } |
| 163 | |
| 164 | int os_unmap_memory(void *addr, int len) |
| 165 | { |
| 166 | int err; |
| 167 | |
| 168 | err = munmap(addr, len); |
| 169 | if(err < 0) |
| 170 | return(-errno); |
| 171 | return(0); |
| 172 | } |
| 173 | |
Gennady Sharapov | 60d339f | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 174 | void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int)) |
| 175 | { |
| 176 | int flags = 0, pages; |
| 177 | |
| 178 | if(sig_stack != NULL){ |
| 179 | pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER); |
| 180 | set_sigstack(sig_stack, pages * page_size()); |
| 181 | flags = SA_ONSTACK; |
| 182 | } |
| 183 | if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1); |
| 184 | } |
| 185 | |
| 186 | void init_new_thread_signals(int altstack) |
| 187 | { |
| 188 | int flags = altstack ? SA_ONSTACK : 0; |
| 189 | |
| 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); |
| 203 | |
| 204 | init_irq_signals(altstack); |
| 205 | } |
| 206 | |
| 207 | int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr) |
| 208 | { |
| 209 | sigjmp_buf buf; |
| 210 | int n; |
| 211 | |
| 212 | *jmp_ptr = &buf; |
| 213 | n = sigsetjmp(buf, 1); |
| 214 | if(n != 0) |
| 215 | return(n); |
| 216 | (*fn)(arg); |
| 217 | return(0); |
| 218 | } |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | /* |
| 221 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 222 | * Emacs will notice this stuff at the end of the file and automatically |
| 223 | * adjust the settings for this buffer only. This must remain at the end |
| 224 | * of the file. |
| 225 | * --------------------------------------------------------------------------- |
| 226 | * Local variables: |
| 227 | * c-file-style: "linux" |
| 228 | * End: |
| 229 | */ |