process.c 11 KB

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