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