process.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <signal.h>
  9. #include <fcntl.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. #define ARBITRARY_ADDR -1
  22. #define FAILURE_PID -1
  23. #define STAT_PATH_LEN sizeof("/proc/#######/stat\0")
  24. #define COMM_SCANF "%*[^)])"
  25. unsigned long os_process_pc(int pid)
  26. {
  27. char proc_stat[STAT_PATH_LEN], buf[256];
  28. unsigned long pc = ARBITRARY_ADDR;
  29. int fd, err;
  30. sprintf(proc_stat, "/proc/%d/stat", pid);
  31. fd = open(proc_stat, O_RDONLY, 0);
  32. if (fd < 0) {
  33. printk(UM_KERN_ERR "os_process_pc - couldn't open '%s', "
  34. "errno = %d\n", proc_stat, errno);
  35. goto out;
  36. }
  37. CATCH_EINTR(err = read(fd, buf, sizeof(buf)));
  38. if (err < 0) {
  39. printk(UM_KERN_ERR "os_process_pc - couldn't read '%s', "
  40. "err = %d\n", proc_stat, errno);
  41. goto out_close;
  42. }
  43. os_close_file(fd);
  44. pc = ARBITRARY_ADDR;
  45. if (sscanf(buf, "%*d " COMM_SCANF " %*c %*d %*d %*d %*d %*d %*d %*d "
  46. "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
  47. "%*d %*d %*d %*d %*d %lu", &pc) != 1)
  48. printk(UM_KERN_ERR "os_process_pc - couldn't find pc in '%s'\n",
  49. buf);
  50. out_close:
  51. close(fd);
  52. out:
  53. return pc;
  54. }
  55. int os_process_parent(int pid)
  56. {
  57. char stat[STAT_PATH_LEN];
  58. char data[256];
  59. int parent = FAILURE_PID, n, fd;
  60. if (pid == -1)
  61. return parent;
  62. snprintf(stat, sizeof(stat), "/proc/%d/stat", pid);
  63. fd = open(stat, O_RDONLY, 0);
  64. if (fd < 0) {
  65. printk(UM_KERN_ERR "Couldn't open '%s', errno = %d\n", stat,
  66. errno);
  67. return parent;
  68. }
  69. CATCH_EINTR(n = read(fd, data, sizeof(data)));
  70. close(fd);
  71. if (n < 0) {
  72. printk(UM_KERN_ERR "Couldn't read '%s', errno = %d\n", stat,
  73. errno);
  74. return parent;
  75. }
  76. parent = FAILURE_PID;
  77. n = sscanf(data, "%*d " COMM_SCANF " %*c %d", &parent);
  78. if (n != 1)
  79. printk(UM_KERN_ERR "Failed to scan '%s'\n", data);
  80. return parent;
  81. }
  82. void os_stop_process(int pid)
  83. {
  84. kill(pid, SIGSTOP);
  85. }
  86. void os_kill_process(int pid, int reap_child)
  87. {
  88. kill(pid, SIGKILL);
  89. if (reap_child)
  90. CATCH_EINTR(waitpid(pid, NULL, __WALL));
  91. }
  92. /* This is here uniquely to have access to the userspace errno, i.e. the one
  93. * used by ptrace in case of error.
  94. */
  95. long os_ptrace_ldt(long pid, long addr, long data)
  96. {
  97. int ret;
  98. ret = ptrace(PTRACE_LDT, pid, addr, data);
  99. if (ret < 0)
  100. return -errno;
  101. return ret;
  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. void os_kill_ptraced_process(int pid, int reap_child)
  108. {
  109. kill(pid, SIGKILL);
  110. ptrace(PTRACE_KILL, pid);
  111. ptrace(PTRACE_CONT, pid);
  112. if (reap_child)
  113. CATCH_EINTR(waitpid(pid, NULL, __WALL));
  114. }
  115. /* Don't use the glibc version, which caches the result in TLS. It misses some
  116. * syscalls, and also breaks with clone(), which does not unshare the TLS.
  117. */
  118. int os_getpid(void)
  119. {
  120. return syscall(__NR_getpid);
  121. }
  122. int os_getpgrp(void)
  123. {
  124. return getpgrp();
  125. }
  126. int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
  127. int r, int w, int x)
  128. {
  129. void *loc;
  130. int prot;
  131. prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  132. (x ? PROT_EXEC : 0);
  133. loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
  134. fd, off);
  135. if (loc == MAP_FAILED)
  136. return -errno;
  137. return 0;
  138. }
  139. int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
  140. {
  141. int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  142. (x ? PROT_EXEC : 0));
  143. if (mprotect(addr, len, prot) < 0)
  144. return -errno;
  145. return 0;
  146. }
  147. int os_unmap_memory(void *addr, int len)
  148. {
  149. int err;
  150. err = munmap(addr, len);
  151. if (err < 0)
  152. return -errno;
  153. return 0;
  154. }
  155. #ifndef MADV_REMOVE
  156. #define MADV_REMOVE KERNEL_MADV_REMOVE
  157. #endif
  158. int os_drop_memory(void *addr, int length)
  159. {
  160. int err;
  161. err = madvise(addr, length, MADV_REMOVE);
  162. if (err < 0)
  163. err = -errno;
  164. return err;
  165. }
  166. int __init can_drop_memory(void)
  167. {
  168. void *addr;
  169. int fd, ok = 0;
  170. printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
  171. fd = create_mem_file(UM_KERN_PAGE_SIZE);
  172. if (fd < 0) {
  173. printk(UM_KERN_ERR "Creating test memory file failed, "
  174. "err = %d\n", -fd);
  175. goto out;
  176. }
  177. addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
  178. MAP_SHARED, fd, 0);
  179. if (addr == MAP_FAILED) {
  180. printk(UM_KERN_ERR "Mapping test memory file failed, "
  181. "err = %d\n", -errno);
  182. goto out_close;
  183. }
  184. if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
  185. printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
  186. goto out_unmap;
  187. }
  188. printk(UM_KERN_CONT "OK\n");
  189. ok = 1;
  190. out_unmap:
  191. munmap(addr, UM_KERN_PAGE_SIZE);
  192. out_close:
  193. close(fd);
  194. out:
  195. return ok;
  196. }
  197. void init_new_thread_signals(void)
  198. {
  199. set_handler(SIGSEGV, (__sighandler_t) sig_handler, SA_ONSTACK,
  200. SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  201. set_handler(SIGTRAP, (__sighandler_t) sig_handler, SA_ONSTACK,
  202. SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  203. set_handler(SIGFPE, (__sighandler_t) sig_handler, SA_ONSTACK,
  204. SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  205. set_handler(SIGILL, (__sighandler_t) sig_handler, SA_ONSTACK,
  206. SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  207. set_handler(SIGBUS, (__sighandler_t) sig_handler, SA_ONSTACK,
  208. SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1);
  209. signal(SIGHUP, SIG_IGN);
  210. set_handler(SIGIO, (__sighandler_t) sig_handler,
  211. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, SIGALRM,
  212. SIGVTALRM, -1);
  213. signal(SIGWINCH, SIG_IGN);
  214. signal(SIGTERM, SIG_DFL);
  215. }
  216. int run_kernel_thread(int (*fn)(void *), void *arg, jmp_buf **jmp_ptr)
  217. {
  218. jmp_buf buf;
  219. int n;
  220. *jmp_ptr = &buf;
  221. n = UML_SETJMP(&buf);
  222. if (n != 0)
  223. return n;
  224. (*fn)(arg);
  225. return 0;
  226. }