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 | 60d339f6 | 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> |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 14 | #include <sys/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "ptrace_user.h" |
| 16 | #include "os.h" |
| 17 | #include "user.h" |
| 18 | #include "user_util.h" |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 19 | #include "process.h" |
| 20 | #include "irq_user.h" |
| 21 | #include "kern_util.h" |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 22 | #include "longjmp.h" |
Paolo 'Blaisorblade' Giarrusso | 07f4e2c | 2006-02-24 13:03:55 -0800 | [diff] [blame] | 23 | #include "skas_ptrace.h" |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 24 | #include "kern_constants.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #define ARBITRARY_ADDR -1 |
| 27 | #define FAILURE_PID -1 |
| 28 | |
| 29 | #define STAT_PATH_LEN sizeof("/proc/#######/stat\0") |
| 30 | #define COMM_SCANF "%*[^)])" |
| 31 | |
| 32 | unsigned long os_process_pc(int pid) |
| 33 | { |
| 34 | char proc_stat[STAT_PATH_LEN], buf[256]; |
| 35 | unsigned long pc; |
| 36 | int fd, err; |
| 37 | |
| 38 | sprintf(proc_stat, "/proc/%d/stat", pid); |
| 39 | fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0); |
| 40 | if(fd < 0){ |
| 41 | printk("os_process_pc - couldn't open '%s', err = %d\n", |
| 42 | proc_stat, -fd); |
| 43 | return(ARBITRARY_ADDR); |
| 44 | } |
| 45 | err = os_read_file(fd, buf, sizeof(buf)); |
| 46 | if(err < 0){ |
| 47 | printk("os_process_pc - couldn't read '%s', err = %d\n", |
| 48 | proc_stat, -err); |
| 49 | os_close_file(fd); |
| 50 | return(ARBITRARY_ADDR); |
| 51 | } |
| 52 | os_close_file(fd); |
| 53 | pc = ARBITRARY_ADDR; |
| 54 | if(sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d " |
| 55 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " |
| 56 | "%*d %*d %*d %*d %*d %lu", &pc) != 1){ |
| 57 | printk("os_process_pc - couldn't find pc in '%s'\n", buf); |
| 58 | } |
| 59 | return(pc); |
| 60 | } |
| 61 | |
| 62 | int os_process_parent(int pid) |
| 63 | { |
| 64 | char stat[STAT_PATH_LEN]; |
| 65 | char data[256]; |
| 66 | int parent, n, fd; |
| 67 | |
| 68 | if(pid == -1) return(-1); |
| 69 | |
| 70 | snprintf(stat, sizeof(stat), "/proc/%d/stat", pid); |
| 71 | fd = os_open_file(stat, of_read(OPENFLAGS()), 0); |
| 72 | if(fd < 0){ |
| 73 | printk("Couldn't open '%s', err = %d\n", stat, -fd); |
| 74 | return(FAILURE_PID); |
| 75 | } |
| 76 | |
| 77 | n = os_read_file(fd, data, sizeof(data)); |
| 78 | os_close_file(fd); |
| 79 | |
| 80 | if(n < 0){ |
| 81 | printk("Couldn't read '%s', err = %d\n", stat, -n); |
| 82 | return(FAILURE_PID); |
| 83 | } |
| 84 | |
| 85 | parent = FAILURE_PID; |
| 86 | n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent); |
| 87 | if(n != 1) |
| 88 | printk("Failed to scan '%s'\n", data); |
| 89 | |
| 90 | return(parent); |
| 91 | } |
| 92 | |
| 93 | void os_stop_process(int pid) |
| 94 | { |
| 95 | kill(pid, SIGSTOP); |
| 96 | } |
| 97 | |
| 98 | void os_kill_process(int pid, int reap_child) |
| 99 | { |
| 100 | kill(pid, SIGKILL); |
| 101 | if(reap_child) |
| 102 | CATCH_EINTR(waitpid(pid, NULL, 0)); |
| 103 | |
| 104 | } |
| 105 | |
Paolo 'Blaisorblade' Giarrusso | 07f4e2c | 2006-02-24 13:03:55 -0800 | [diff] [blame] | 106 | /* This is here uniquely to have access to the userspace errno, i.e. the one |
| 107 | * used by ptrace in case of error. |
| 108 | */ |
| 109 | |
| 110 | long os_ptrace_ldt(long pid, long addr, long data) |
| 111 | { |
| 112 | int ret; |
| 113 | |
| 114 | ret = ptrace(PTRACE_LDT, pid, addr, data); |
| 115 | |
| 116 | if (ret < 0) |
| 117 | return -errno; |
| 118 | return ret; |
| 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* Kill off a ptraced child by all means available. kill it normally first, |
| 122 | * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from |
| 123 | * which it can't exit directly. |
| 124 | */ |
| 125 | |
| 126 | void os_kill_ptraced_process(int pid, int reap_child) |
| 127 | { |
| 128 | kill(pid, SIGKILL); |
| 129 | ptrace(PTRACE_KILL, pid); |
| 130 | ptrace(PTRACE_CONT, pid); |
| 131 | if(reap_child) |
| 132 | CATCH_EINTR(waitpid(pid, NULL, 0)); |
| 133 | } |
| 134 | |
| 135 | void os_usr1_process(int pid) |
| 136 | { |
| 137 | kill(pid, SIGUSR1); |
| 138 | } |
| 139 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 140 | /* Don't use the glibc version, which caches the result in TLS. It misses some |
| 141 | * syscalls, and also breaks with clone(), which does not unshare the TLS. |
| 142 | */ |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | inline _syscall0(pid_t, getpid) |
| 145 | |
| 146 | int os_getpid(void) |
| 147 | { |
| 148 | return(getpid()); |
| 149 | } |
| 150 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 151 | int os_getpgrp(void) |
| 152 | { |
| 153 | return getpgrp(); |
| 154 | } |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len, |
| 157 | int r, int w, int x) |
| 158 | { |
| 159 | void *loc; |
| 160 | int prot; |
| 161 | |
| 162 | prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | |
| 163 | (x ? PROT_EXEC : 0); |
| 164 | |
| 165 | loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED, |
| 166 | fd, off); |
| 167 | if(loc == MAP_FAILED) |
| 168 | return(-errno); |
| 169 | return(0); |
| 170 | } |
| 171 | |
| 172 | int os_protect_memory(void *addr, unsigned long len, int r, int w, int x) |
| 173 | { |
| 174 | int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | |
| 175 | (x ? PROT_EXEC : 0)); |
| 176 | |
| 177 | if(mprotect(addr, len, prot) < 0) |
| 178 | return(-errno); |
| 179 | return(0); |
| 180 | } |
| 181 | |
| 182 | int os_unmap_memory(void *addr, int len) |
| 183 | { |
| 184 | int err; |
| 185 | |
| 186 | err = munmap(addr, len); |
| 187 | if(err < 0) |
| 188 | return(-errno); |
| 189 | return(0); |
| 190 | } |
| 191 | |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 192 | #ifndef MADV_REMOVE |
Jeff Dike | b73781c | 2006-04-18 22:20:24 -0700 | [diff] [blame] | 193 | #define MADV_REMOVE KERNEL_MADV_REMOVE |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 194 | #endif |
| 195 | |
| 196 | int os_drop_memory(void *addr, int length) |
| 197 | { |
| 198 | int err; |
| 199 | |
| 200 | err = madvise(addr, length, MADV_REMOVE); |
| 201 | if(err < 0) |
| 202 | err = -errno; |
| 203 | return err; |
| 204 | } |
| 205 | |
| 206 | int can_drop_memory(void) |
| 207 | { |
| 208 | void *addr; |
Jeff Dike | e3104f5 | 2006-05-01 12:15:58 -0700 | [diff] [blame] | 209 | int fd, ok = 0; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 210 | |
| 211 | printk("Checking host MADV_REMOVE support..."); |
| 212 | fd = create_mem_file(UM_KERN_PAGE_SIZE); |
| 213 | if(fd < 0){ |
| 214 | printk("Creating test memory file failed, err = %d\n", -fd); |
Jeff Dike | e3104f5 | 2006-05-01 12:15:58 -0700 | [diff] [blame] | 215 | goto out; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE, |
Jeff Dike | b73781c | 2006-04-18 22:20:24 -0700 | [diff] [blame] | 219 | MAP_SHARED, fd, 0); |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 220 | if(addr == MAP_FAILED){ |
| 221 | printk("Mapping test memory file failed, err = %d\n", -errno); |
Jeff Dike | e3104f5 | 2006-05-01 12:15:58 -0700 | [diff] [blame] | 222 | goto out_close; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if(madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0){ |
| 226 | printk("MADV_REMOVE failed, err = %d\n", -errno); |
Jeff Dike | e3104f5 | 2006-05-01 12:15:58 -0700 | [diff] [blame] | 227 | goto out_unmap; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | printk("OK\n"); |
Jeff Dike | e3104f5 | 2006-05-01 12:15:58 -0700 | [diff] [blame] | 231 | ok = 1; |
| 232 | |
| 233 | out_unmap: |
| 234 | munmap(addr, UM_KERN_PAGE_SIZE); |
| 235 | out_close: |
| 236 | close(fd); |
| 237 | out: |
| 238 | return ok; |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 241 | void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int)) |
| 242 | { |
| 243 | int flags = 0, pages; |
| 244 | |
| 245 | if(sig_stack != NULL){ |
| 246 | pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER); |
| 247 | set_sigstack(sig_stack, pages * page_size()); |
| 248 | flags = SA_ONSTACK; |
| 249 | } |
| 250 | if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1); |
| 251 | } |
| 252 | |
| 253 | void init_new_thread_signals(int altstack) |
| 254 | { |
| 255 | int flags = altstack ? SA_ONSTACK : 0; |
| 256 | |
| 257 | set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags, |
| 258 | SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); |
| 259 | set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags, |
| 260 | SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); |
| 261 | set_handler(SIGFPE, (__sighandler_t) sig_handler, flags, |
| 262 | SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); |
| 263 | set_handler(SIGILL, (__sighandler_t) sig_handler, flags, |
| 264 | SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); |
| 265 | set_handler(SIGBUS, (__sighandler_t) sig_handler, flags, |
| 266 | SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); |
| 267 | set_handler(SIGUSR2, (__sighandler_t) sig_handler, |
| 268 | flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); |
| 269 | signal(SIGHUP, SIG_IGN); |
| 270 | |
| 271 | init_irq_signals(altstack); |
| 272 | } |
| 273 | |
| 274 | int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr) |
| 275 | { |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 276 | jmp_buf buf; |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 277 | int n, enable; |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 278 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 279 | *jmp_ptr = &buf; |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 280 | n = UML_SETJMP(&buf, enable); |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 281 | if(n != 0) |
| 282 | return(n); |
| 283 | (*fn)(arg); |
| 284 | return(0); |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 285 | } |