/arch/um/kernel/process.c

https://bitbucket.org/evzijst/gittest · C · 423 lines · 345 code · 61 blank · 17 comment · 70 complexity · 9d45d1437f7bc2b4157ea3938e27199c MD5 · raw file

  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <sched.h>
  9. #include <errno.h>
  10. #include <stdarg.h>
  11. #include <stdlib.h>
  12. #include <setjmp.h>
  13. #include <sys/time.h>
  14. #include <sys/wait.h>
  15. #include <sys/mman.h>
  16. #include <asm/unistd.h>
  17. #include <asm/page.h>
  18. #include "user_util.h"
  19. #include "kern_util.h"
  20. #include "user.h"
  21. #include "process.h"
  22. #include "signal_kern.h"
  23. #include "signal_user.h"
  24. #include "sysdep/ptrace.h"
  25. #include "sysdep/sigcontext.h"
  26. #include "irq_user.h"
  27. #include "ptrace_user.h"
  28. #include "time_user.h"
  29. #include "init.h"
  30. #include "os.h"
  31. #include "uml-config.h"
  32. #include "ptrace_user.h"
  33. #include "choose-mode.h"
  34. #include "mode.h"
  35. #ifdef UML_CONFIG_MODE_SKAS
  36. #include "skas.h"
  37. #include "skas_ptrace.h"
  38. #include "registers.h"
  39. #endif
  40. void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
  41. {
  42. int flags = 0, pages;
  43. if(sig_stack != NULL){
  44. pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
  45. set_sigstack(sig_stack, pages * page_size());
  46. flags = SA_ONSTACK;
  47. }
  48. if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
  49. }
  50. void init_new_thread_signals(int altstack)
  51. {
  52. int flags = altstack ? SA_ONSTACK : 0;
  53. set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
  54. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  55. set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
  56. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  57. set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
  58. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  59. set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
  60. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  61. set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
  62. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  63. set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags,
  64. SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  65. set_handler(SIGUSR2, (__sighandler_t) sig_handler,
  66. flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
  67. signal(SIGHUP, SIG_IGN);
  68. init_irq_signals(altstack);
  69. }
  70. struct tramp {
  71. int (*tramp)(void *);
  72. void *tramp_data;
  73. unsigned long temp_stack;
  74. int flags;
  75. int pid;
  76. };
  77. /* See above for why sigkill is here */
  78. int sigkill = SIGKILL;
  79. int outer_tramp(void *arg)
  80. {
  81. struct tramp *t;
  82. int sig = sigkill;
  83. t = arg;
  84. t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
  85. t->flags, t->tramp_data);
  86. if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
  87. kill(os_getpid(), sig);
  88. _exit(0);
  89. }
  90. int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
  91. int clone_flags, int (*tramp)(void *))
  92. {
  93. struct tramp arg;
  94. unsigned long sp;
  95. int new_pid, status, err;
  96. /* The trampoline will run on the temporary stack */
  97. sp = stack_sp(temp_stack);
  98. clone_flags |= CLONE_FILES | SIGCHLD;
  99. arg.tramp = tramp;
  100. arg.tramp_data = thread_arg;
  101. arg.temp_stack = temp_stack;
  102. arg.flags = clone_flags;
  103. /* Start the process and wait for it to kill itself */
  104. new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
  105. if(new_pid < 0)
  106. return(new_pid);
  107. CATCH_EINTR(err = waitpid(new_pid, &status, 0));
  108. if(err < 0)
  109. panic("Waiting for outer trampoline failed - errno = %d",
  110. errno);
  111. if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
  112. panic("outer trampoline didn't exit with SIGKILL, "
  113. "status = %d", status);
  114. return(arg.pid);
  115. }
  116. static int ptrace_child(void *arg)
  117. {
  118. int ret;
  119. int pid = os_getpid(), ppid = getppid();
  120. int sc_result;
  121. if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
  122. perror("ptrace");
  123. os_kill_process(pid, 0);
  124. }
  125. os_stop_process(pid);
  126. /*This syscall will be intercepted by the parent. Don't call more than
  127. * once, please.*/
  128. sc_result = os_getpid();
  129. if (sc_result == pid)
  130. ret = 1; /*Nothing modified by the parent, we are running
  131. normally.*/
  132. else if (sc_result == ppid)
  133. ret = 0; /*Expected in check_ptrace and check_sysemu when they
  134. succeed in modifying the stack frame*/
  135. else
  136. ret = 2; /*Serious trouble! This could be caused by a bug in
  137. host 2.6 SKAS3/2.6 patch before release -V6, together
  138. with a bug in the UML code itself.*/
  139. _exit(ret);
  140. }
  141. static int start_ptraced_child(void **stack_out)
  142. {
  143. void *stack;
  144. unsigned long sp;
  145. int pid, n, status;
  146. stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
  147. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  148. if(stack == MAP_FAILED)
  149. panic("check_ptrace : mmap failed, errno = %d", errno);
  150. sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
  151. pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
  152. if(pid < 0)
  153. panic("check_ptrace : clone failed, errno = %d", errno);
  154. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  155. if(n < 0)
  156. panic("check_ptrace : wait failed, errno = %d", errno);
  157. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  158. panic("check_ptrace : expected SIGSTOP, got status = %d",
  159. status);
  160. *stack_out = stack;
  161. return(pid);
  162. }
  163. /* When testing for SYSEMU support, if it is one of the broken versions, we must
  164. * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
  165. * So only for SYSEMU features we test mustpanic, while normal host features
  166. * must work anyway!*/
  167. static int stop_ptraced_child(int pid, void *stack, int exitcode, int mustpanic)
  168. {
  169. int status, n, ret = 0;
  170. if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
  171. panic("check_ptrace : ptrace failed, errno = %d", errno);
  172. CATCH_EINTR(n = waitpid(pid, &status, 0));
  173. if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
  174. int exit_with = WEXITSTATUS(status);
  175. if (exit_with == 2)
  176. printk("check_ptrace : child exited with status 2. "
  177. "Serious trouble happening! Try updating your "
  178. "host skas patch!\nDisabling SYSEMU support.");
  179. printk("check_ptrace : child exited with exitcode %d, while "
  180. "expecting %d; status 0x%x", exit_with,
  181. exitcode, status);
  182. if (mustpanic)
  183. panic("\n");
  184. else
  185. printk("\n");
  186. ret = -1;
  187. }
  188. if(munmap(stack, PAGE_SIZE) < 0)
  189. panic("check_ptrace : munmap failed, errno = %d", errno);
  190. return ret;
  191. }
  192. static int force_sysemu_disabled = 0;
  193. static int __init nosysemu_cmd_param(char *str, int* add)
  194. {
  195. force_sysemu_disabled = 1;
  196. return 0;
  197. }
  198. __uml_setup("nosysemu", nosysemu_cmd_param,
  199. "nosysemu\n"
  200. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  201. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  202. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  203. " To make it working, you need a kernel patch for your host, too.\n"
  204. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
  205. static void __init check_sysemu(void)
  206. {
  207. void *stack;
  208. int pid, syscall, n, status, count=0;
  209. printk("Checking syscall emulation patch for ptrace...");
  210. sysemu_supported = 0;
  211. pid = start_ptraced_child(&stack);
  212. if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  213. goto fail;
  214. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  215. if (n < 0)
  216. panic("check_sysemu : wait failed, errno = %d", errno);
  217. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  218. panic("check_sysemu : expected SIGTRAP, "
  219. "got status = %d", status);
  220. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  221. os_getpid());
  222. if(n < 0)
  223. panic("check_sysemu : failed to modify system "
  224. "call return, errno = %d", errno);
  225. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  226. goto fail_stopped;
  227. sysemu_supported = 1;
  228. printk("OK\n");
  229. set_using_sysemu(!force_sysemu_disabled);
  230. printk("Checking advanced syscall emulation patch for ptrace...");
  231. pid = start_ptraced_child(&stack);
  232. while(1){
  233. count++;
  234. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  235. goto fail;
  236. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  237. if(n < 0)
  238. panic("check_ptrace : wait failed, errno = %d", errno);
  239. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  240. panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
  241. "got status = %d", status);
  242. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  243. 0);
  244. if(syscall == __NR_getpid){
  245. if (!count)
  246. panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
  247. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  248. os_getpid());
  249. if(n < 0)
  250. panic("check_sysemu : failed to modify system "
  251. "call return, errno = %d", errno);
  252. break;
  253. }
  254. }
  255. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  256. goto fail_stopped;
  257. sysemu_supported = 2;
  258. printk("OK\n");
  259. if ( !force_sysemu_disabled )
  260. set_using_sysemu(sysemu_supported);
  261. return;
  262. fail:
  263. stop_ptraced_child(pid, stack, 1, 0);
  264. fail_stopped:
  265. printk("missing\n");
  266. }
  267. void __init check_ptrace(void)
  268. {
  269. void *stack;
  270. int pid, syscall, n, status;
  271. printk("Checking that ptrace can change system call numbers...");
  272. pid = start_ptraced_child(&stack);
  273. if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  274. panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
  275. while(1){
  276. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  277. panic("check_ptrace : ptrace failed, errno = %d",
  278. errno);
  279. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  280. if(n < 0)
  281. panic("check_ptrace : wait failed, errno = %d", errno);
  282. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
  283. panic("check_ptrace : expected SIGTRAP + 0x80, "
  284. "got status = %d", status);
  285. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  286. 0);
  287. if(syscall == __NR_getpid){
  288. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  289. __NR_getppid);
  290. if(n < 0)
  291. panic("check_ptrace : failed to modify system "
  292. "call, errno = %d", errno);
  293. break;
  294. }
  295. }
  296. stop_ptraced_child(pid, stack, 0, 1);
  297. printk("OK\n");
  298. check_sysemu();
  299. }
  300. int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
  301. {
  302. sigjmp_buf buf;
  303. int n;
  304. *jmp_ptr = &buf;
  305. n = sigsetjmp(buf, 1);
  306. if(n != 0)
  307. return(n);
  308. (*fn)(arg);
  309. return(0);
  310. }
  311. void forward_pending_sigio(int target)
  312. {
  313. sigset_t sigs;
  314. if(sigpending(&sigs))
  315. panic("forward_pending_sigio : sigpending failed");
  316. if(sigismember(&sigs, SIGIO))
  317. kill(target, SIGIO);
  318. }
  319. #ifdef UML_CONFIG_MODE_SKAS
  320. static inline int check_skas3_ptrace_support(void)
  321. {
  322. struct ptrace_faultinfo fi;
  323. void *stack;
  324. int pid, n, ret = 1;
  325. printf("Checking for the skas3 patch in the host...");
  326. pid = start_ptraced_child(&stack);
  327. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  328. if (n < 0) {
  329. if(errno == EIO)
  330. printf("not found\n");
  331. else {
  332. perror("not found");
  333. }
  334. ret = 0;
  335. } else {
  336. printf("found\n");
  337. }
  338. init_registers(pid);
  339. stop_ptraced_child(pid, stack, 1, 1);
  340. return(ret);
  341. }
  342. int can_do_skas(void)
  343. {
  344. int ret = 1;
  345. printf("Checking for /proc/mm...");
  346. if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
  347. printf("not found\n");
  348. ret = 0;
  349. goto out;
  350. } else {
  351. printf("found\n");
  352. }
  353. ret = check_skas3_ptrace_support();
  354. out:
  355. return ret;
  356. }
  357. #else
  358. int can_do_skas(void)
  359. {
  360. return(0);
  361. }
  362. #endif