start_up.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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, syscall, 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. while(1){
  171. count++;
  172. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  173. goto fail;
  174. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  175. if(n < 0)
  176. panic("check_ptrace : wait failed, errno = %d", errno);
  177. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  178. panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
  179. "got status = %d", status);
  180. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  181. 0);
  182. if(syscall == __NR_getpid){
  183. if (!count)
  184. panic("check_ptrace : SYSEMU_SINGLESTEP 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. }
  193. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  194. goto fail_stopped;
  195. sysemu_supported = 2;
  196. printk("OK\n");
  197. if ( !force_sysemu_disabled )
  198. set_using_sysemu(sysemu_supported);
  199. return;
  200. fail:
  201. stop_ptraced_child(pid, stack, 1, 0);
  202. fail_stopped:
  203. printk("missing\n");
  204. }
  205. static void __init check_ptrace(void)
  206. {
  207. void *stack;
  208. int pid, syscall, n, status;
  209. printk("Checking that ptrace can change system call numbers...");
  210. pid = start_ptraced_child(&stack);
  211. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  212. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
  213. while(1){
  214. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  215. panic("check_ptrace : ptrace failed, errno = %d",
  216. errno);
  217. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  218. if(n < 0)
  219. panic("check_ptrace : wait failed, errno = %d", errno);
  220. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
  221. panic("check_ptrace : expected SIGTRAP + 0x80, "
  222. "got status = %d", status);
  223. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  224. 0);
  225. if(syscall == __NR_getpid){
  226. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  227. __NR_getppid);
  228. if(n < 0)
  229. panic("check_ptrace : failed to modify system "
  230. "call, errno = %d", errno);
  231. break;
  232. }
  233. }
  234. stop_ptraced_child(pid, stack, 0, 1);
  235. printk("OK\n");
  236. check_sysemu();
  237. }
  238. void os_early_checks(void)
  239. {
  240. check_ptrace();
  241. }
  242. #ifdef UML_CONFIG_MODE_SKAS
  243. static inline void check_skas3_ptrace_support(void)
  244. {
  245. struct ptrace_faultinfo fi;
  246. void *stack;
  247. int pid, n;
  248. printf("Checking for the skas3 patch in the host...");
  249. pid = start_ptraced_child(&stack);
  250. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  251. if (n < 0) {
  252. ptrace_faultinfo = 0;
  253. if(errno == EIO)
  254. printf("not found\n");
  255. else
  256. perror("not found");
  257. }
  258. else {
  259. if (!ptrace_faultinfo)
  260. printf("found but disabled on command line\n");
  261. else
  262. printf("found\n");
  263. }
  264. init_registers(pid);
  265. stop_ptraced_child(pid, stack, 1, 1);
  266. }
  267. int can_do_skas(void)
  268. {
  269. printf("Checking for /proc/mm...");
  270. if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
  271. proc_mm = 0;
  272. printf("not found\n");
  273. }
  274. else {
  275. if (!proc_mm)
  276. printf("found but disabled on command line\n");
  277. else
  278. printf("found\n");
  279. }
  280. check_skas3_ptrace_support();
  281. return 1;
  282. }
  283. #else
  284. int can_do_skas(void)
  285. {
  286. return(0);
  287. }
  288. #endif