process.c 11 KB

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