start_up.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "signal_kern.h"
  22. #include "signal_user.h"
  23. #include "sysdep/ptrace.h"
  24. #include "sysdep/sigcontext.h"
  25. #include "irq_user.h"
  26. #include "ptrace_user.h"
  27. #include "time_user.h"
  28. #include "init.h"
  29. #include "os.h"
  30. #include "uml-config.h"
  31. #include "choose-mode.h"
  32. #include "mode.h"
  33. #include "tempfile.h"
  34. #ifdef UML_CONFIG_MODE_SKAS
  35. #include "skas.h"
  36. #include "skas_ptrace.h"
  37. #include "registers.h"
  38. #endif
  39. static int ptrace_child(void *arg)
  40. {
  41. int ret;
  42. int pid = os_getpid(), ppid = getppid();
  43. int sc_result;
  44. if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
  45. perror("ptrace");
  46. os_kill_process(pid, 0);
  47. }
  48. os_stop_process(pid);
  49. /*This syscall will be intercepted by the parent. Don't call more than
  50. * once, please.*/
  51. sc_result = os_getpid();
  52. if (sc_result == pid)
  53. ret = 1; /*Nothing modified by the parent, we are running
  54. normally.*/
  55. else if (sc_result == ppid)
  56. ret = 0; /*Expected in check_ptrace and check_sysemu when they
  57. succeed in modifying the stack frame*/
  58. else
  59. ret = 2; /*Serious trouble! This could be caused by a bug in
  60. host 2.6 SKAS3/2.6 patch before release -V6, together
  61. with a bug in the UML code itself.*/
  62. _exit(ret);
  63. }
  64. static int start_ptraced_child(void **stack_out)
  65. {
  66. void *stack;
  67. unsigned long sp;
  68. int pid, n, status;
  69. stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
  70. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  71. if(stack == MAP_FAILED)
  72. panic("check_ptrace : mmap failed, errno = %d", errno);
  73. sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
  74. pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
  75. if(pid < 0)
  76. panic("start_ptraced_child : clone failed, errno = %d", errno);
  77. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  78. if(n < 0)
  79. panic("check_ptrace : clone failed, errno = %d", errno);
  80. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  81. panic("check_ptrace : expected SIGSTOP, got status = %d",
  82. status);
  83. *stack_out = stack;
  84. return(pid);
  85. }
  86. /* When testing for SYSEMU support, if it is one of the broken versions, we
  87. * must just avoid using sysemu, not panic, but only if SYSEMU features are
  88. * broken.
  89. * So only for SYSEMU features we test mustpanic, while normal host features
  90. * must work anyway!
  91. */
  92. static int stop_ptraced_child(int pid, void *stack, int exitcode,
  93. int mustpanic)
  94. {
  95. int status, n, ret = 0;
  96. if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
  97. panic("check_ptrace : ptrace failed, errno = %d", errno);
  98. CATCH_EINTR(n = waitpid(pid, &status, 0));
  99. if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
  100. int exit_with = WEXITSTATUS(status);
  101. if (exit_with == 2)
  102. printk("check_ptrace : child exited with status 2. "
  103. "Serious trouble happening! Try updating your "
  104. "host skas patch!\nDisabling SYSEMU support.");
  105. printk("check_ptrace : child exited with exitcode %d, while "
  106. "expecting %d; status 0x%x", exit_with,
  107. exitcode, status);
  108. if (mustpanic)
  109. panic("\n");
  110. else
  111. printk("\n");
  112. ret = -1;
  113. }
  114. if(munmap(stack, PAGE_SIZE) < 0)
  115. panic("check_ptrace : munmap failed, errno = %d", errno);
  116. return ret;
  117. }
  118. int ptrace_faultinfo = 1;
  119. int proc_mm = 1;
  120. static int __init skas0_cmd_param(char *str, int* add)
  121. {
  122. ptrace_faultinfo = proc_mm = 0;
  123. return 0;
  124. }
  125. __uml_setup("skas0", skas0_cmd_param,
  126. "skas0\n"
  127. " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
  128. " you specify mode=tt.\n\n");
  129. static int force_sysemu_disabled = 0;
  130. static int __init nosysemu_cmd_param(char *str, int* add)
  131. {
  132. force_sysemu_disabled = 1;
  133. return 0;
  134. }
  135. __uml_setup("nosysemu", nosysemu_cmd_param,
  136. "nosysemu\n"
  137. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  138. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  139. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  140. " To make it working, you need a kernel patch for your host, too.\n"
  141. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  142. " information.\n\n");
  143. static void __init check_sysemu(void)
  144. {
  145. void *stack;
  146. int pid, n, status, count=0;
  147. printk("Checking syscall emulation patch for ptrace...");
  148. sysemu_supported = 0;
  149. pid = start_ptraced_child(&stack);
  150. if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  151. goto fail;
  152. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  153. if (n < 0)
  154. panic("check_sysemu : wait failed, errno = %d", errno);
  155. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  156. panic("check_sysemu : expected SIGTRAP, "
  157. "got status = %d", status);
  158. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  159. os_getpid());
  160. if(n < 0)
  161. panic("check_sysemu : failed to modify system "
  162. "call return, errno = %d", errno);
  163. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  164. goto fail_stopped;
  165. sysemu_supported = 1;
  166. printk("OK\n");
  167. set_using_sysemu(!force_sysemu_disabled);
  168. printk("Checking advanced syscall emulation patch for ptrace...");
  169. pid = start_ptraced_child(&stack);
  170. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  171. (void *) PTRACE_O_TRACESYSGOOD) < 0)
  172. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
  173. errno);
  174. while(1){
  175. count++;
  176. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  177. goto fail;
  178. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  179. if(n < 0)
  180. panic("check_ptrace : wait failed, errno = %d", errno);
  181. if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
  182. if (!count)
  183. panic("check_ptrace : SYSEMU_SINGLESTEP "
  184. "doesn't singlestep");
  185. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  186. os_getpid());
  187. if(n < 0)
  188. panic("check_sysemu : failed to modify system "
  189. "call return, errno = %d", errno);
  190. break;
  191. }
  192. else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  193. count++;
  194. else
  195. panic("check_ptrace : expected SIGTRAP or "
  196. "(SIGTRAP|0x80), got status = %d", status);
  197. }
  198. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  199. goto fail_stopped;
  200. sysemu_supported = 2;
  201. printk("OK\n");
  202. if ( !force_sysemu_disabled )
  203. set_using_sysemu(sysemu_supported);
  204. return;
  205. fail:
  206. stop_ptraced_child(pid, stack, 1, 0);
  207. fail_stopped:
  208. printk("missing\n");
  209. }
  210. static void __init check_ptrace(void)
  211. {
  212. void *stack;
  213. int pid, syscall, n, status;
  214. printk("Checking that ptrace can change system call numbers...");
  215. pid = start_ptraced_child(&stack);
  216. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  217. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
  218. while(1){
  219. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  220. panic("check_ptrace : ptrace failed, errno = %d",
  221. errno);
  222. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  223. if(n < 0)
  224. panic("check_ptrace : wait failed, errno = %d", errno);
  225. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
  226. panic("check_ptrace : expected (SIGTRAP|0x80), "
  227. "got status = %d", status);
  228. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  229. 0);
  230. if(syscall == __NR_getpid){
  231. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  232. __NR_getppid);
  233. if(n < 0)
  234. panic("check_ptrace : failed to modify system "
  235. "call, errno = %d", errno);
  236. break;
  237. }
  238. }
  239. stop_ptraced_child(pid, stack, 0, 1);
  240. printk("OK\n");
  241. check_sysemu();
  242. }
  243. void os_early_checks(void)
  244. {
  245. check_ptrace();
  246. }
  247. static int __init noprocmm_cmd_param(char *str, int* add)
  248. {
  249. proc_mm = 0;
  250. return 0;
  251. }
  252. __uml_setup("noprocmm", noprocmm_cmd_param,
  253. "noprocmm\n"
  254. " Turns off usage of /proc/mm, even if host supports it.\n"
  255. " To support /proc/mm, the host needs to be patched using\n"
  256. " the current skas3 patch.\n\n");
  257. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  258. {
  259. ptrace_faultinfo = 0;
  260. return 0;
  261. }
  262. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  263. "noptracefaultinfo\n"
  264. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  265. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  266. " using the current skas3 patch.\n\n");
  267. #ifdef UML_CONFIG_MODE_SKAS
  268. static inline void check_skas3_ptrace_support(void)
  269. {
  270. struct ptrace_faultinfo fi;
  271. void *stack;
  272. int pid, n;
  273. printf("Checking for the skas3 patch in the host...");
  274. pid = start_ptraced_child(&stack);
  275. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  276. if (n < 0) {
  277. ptrace_faultinfo = 0;
  278. if(errno == EIO)
  279. printf("not found\n");
  280. else
  281. perror("not found");
  282. }
  283. else {
  284. if (!ptrace_faultinfo)
  285. printf("found but disabled on command line\n");
  286. else
  287. printf("found\n");
  288. }
  289. init_registers(pid);
  290. stop_ptraced_child(pid, stack, 1, 1);
  291. }
  292. int can_do_skas(void)
  293. {
  294. printf("Checking for /proc/mm...");
  295. if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
  296. proc_mm = 0;
  297. printf("not found\n");
  298. }
  299. else {
  300. if (!proc_mm)
  301. printf("found but disabled on command line\n");
  302. else
  303. printf("found\n");
  304. }
  305. check_skas3_ptrace_support();
  306. return 1;
  307. }
  308. #else
  309. int can_do_skas(void)
  310. {
  311. return(0);
  312. }
  313. #endif