1 /* 2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 3 * Licensed under the GPL 4 */ 5 6 #include <stdio.h> 7 #include <unistd.h> 8 #include <errno.h> 9 #include <signal.h> 10 #include <sys/mman.h> 11 #include <sys/ptrace.h> 12 #include <sys/wait.h> 13 #include <asm/unistd.h> 14 #include "init.h" 15 #include "kern_constants.h" 16 #include "longjmp.h" 17 #include "os.h" 18 #include "process.h" 19 #include "skas_ptrace.h" 20 #include "user.h" 21 22 #define ARBITRARY_ADDR -1 23 #define FAILURE_PID -1 24 25 #define STAT_PATH_LEN sizeof("/proc/#######/stat\0") 26 #define COMM_SCANF "%*[^)])" 27 28 unsigned long os_process_pc(int pid) 29 { 30 char proc_stat[STAT_PATH_LEN], buf[256]; 31 unsigned long pc; 32 int fd, err; 33 34 sprintf(proc_stat, "/proc/%d/stat", pid); 35 fd = os_open_file(proc_stat, of_read(OPENFLAGS()), 0); 36 if (fd < 0) { 37 printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', " 38 "err = %d\n", proc_stat, -fd); 39 return ARBITRARY_ADDR; 40 } 41 CATCH_EINTR(err = read(fd, buf, sizeof(buf))); 42 if (err < 0) { 43 printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', " 44 "err = %d\n", proc_stat, errno); 45 os_close_file(fd); 46 return ARBITRARY_ADDR; 47 } 48 os_close_file(fd); 49 pc = 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(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n", 54 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) 66 return -1; 67 68 snprintf(stat, sizeof(stat), "/proc/%d/stat", pid); 69 fd = os_open_file(stat, of_read(OPENFLAGS()), 0); 70 if (fd < 0) { 71 printk(UM_KERN_ERR "Couldn't open '%s', err = %d\n", stat, -fd); 72 return FAILURE_PID; 73 } 74 75 CATCH_EINTR(n = read(fd, data, sizeof(data))); 76 os_close_file(fd); 77 78 if (n < 0) { 79 printk(UM_KERN_ERR "Couldn't read '%s', err = %d\n", stat, 80 errno); 81 return FAILURE_PID; 82 } 83 84 parent = FAILURE_PID; 85 n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent); 86 if (n != 1) 87 printk(UM_KERN_ERR "Failed to scan '%s'\n", data); 88 89 return parent; 90 } 91 92 void os_stop_process(int pid) 93 { 94 kill(pid, SIGSTOP); 95 } 96 97 void os_kill_process(int pid, int reap_child) 98 { 99 kill(pid, SIGKILL); 100 if (reap_child) 101 CATCH_EINTR(waitpid(pid, NULL, 0)); 102 } 103 104 /* This is here uniquely to have access to the userspace errno, i.e. the one 105 * used by ptrace in case of error. 106 */ 107 108 long os_ptrace_ldt(long pid, long addr, long data) 109 { 110 int ret; 111 112 ret = ptrace(PTRACE_LDT, pid, addr, data); 113 114 if (ret < 0) 115 return -errno; 116 return ret; 117 } 118 119 /* Kill off a ptraced child by all means available. kill it normally first, 120 * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from 121 * which it can't exit directly. 122 */ 123 124 void os_kill_ptraced_process(int pid, int reap_child) 125 { 126 kill(pid, SIGKILL); 127 ptrace(PTRACE_KILL, pid); 128 ptrace(PTRACE_CONT, pid); 129 if (reap_child) 130 CATCH_EINTR(waitpid(pid, NULL, 0)); 131 } 132 133 /* Don't use the glibc version, which caches the result in TLS. It misses some 134 * syscalls, and also breaks with clone(), which does not unshare the TLS. 135 */ 136 137 int os_getpid(void) 138 { 139 return syscall(__NR_getpid); 140 } 141 142 int os_getpgrp(void) 143 { 144 return getpgrp(); 145 } 146 147 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len, 148 int r, int w, int x) 149 { 150 void *loc; 151 int prot; 152 153 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | 154 (x ? PROT_EXEC : 0); 155 156 loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED, 157 fd, off); 158 if (loc == MAP_FAILED) 159 return -errno; 160 return 0; 161 } 162 163 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x) 164 { 165 int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | 166 (x ? PROT_EXEC : 0)); 167 168 if (mprotect(addr, len, prot) < 0) 169 return -errno; 170 171 return 0; 172 } 173 174 int os_unmap_memory(void *addr, int len) 175 { 176 int err; 177 178 err = munmap(addr, len); 179 if (err < 0) 180 return -errno; 181 return 0; 182 } 183 184 #ifndef MADV_REMOVE 185 #define MADV_REMOVE KERNEL_MADV_REMOVE 186 #endif 187 188 int os_drop_memory(void *addr, int length) 189 { 190 int err; 191 192 err = madvise(addr, length, MADV_REMOVE); 193 if (err < 0) 194 err = -errno; 195 return err; 196 } 197 198 int __init can_drop_memory(void) 199 { 200 void *addr; 201 int fd, ok = 0; 202 203 printk(UM_KERN_INFO "Checking host MADV_REMOVE support..."); 204 fd = create_mem_file(UM_KERN_PAGE_SIZE); 205 if (fd < 0) { 206 printk(UM_KERN_ERR "Creating test memory file failed, " 207 "err = %d\n", -fd); 208 goto out; 209 } 210 211 addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE, 212 MAP_SHARED, fd, 0); 213 if (addr == MAP_FAILED) { 214 printk(UM_KERN_ERR "Mapping test memory file failed, " 215 "err = %d\n", -errno); 216 goto out_close; 217 } 218 219 if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) { 220 printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno); 221 goto out_unmap; 222 } 223 224 printk("OK\n"); 225 ok = 1; 226 227 out_unmap: 228 munmap(addr, UM_KERN_PAGE_SIZE); 229 out_close: 230 close(fd); 231 out: 232 return ok; 233 } 234 235 void init_new_thread_signals(void) 236 { 237 set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK, 238 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); 239 set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK, 240 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); 241 set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK, 242 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); 243 set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK, 244 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); 245 set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK, 246 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1); 247 signal(SIGHUP, SIG_IGN); 248 249 init_irq_signals(1); 250 } 251 252 int run_kernel_thread(int (*fn)(void *), void *arg, jmp_buf **jmp_ptr) 253 { 254 jmp_buf buf; 255 int n; 256 257 *jmp_ptr = &buf; 258 n = UML_SETJMP(&buf); 259 if (n != 0) 260 return n; 261 (*fn)(arg); 262 return 0; 263 } 264