start_up.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdarg.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <sched.h>
  12. #include <signal.h>
  13. #include <string.h>
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16. #include <sys/wait.h>
  17. #include <sys/time.h>
  18. #include <sys/resource.h>
  19. #include <asm/unistd.h>
  20. #include <init.h>
  21. #include <os.h>
  22. #include <mem_user.h>
  23. #include <ptrace_user.h>
  24. #include <registers.h>
  25. #include <skas.h>
  26. static void ptrace_child(void)
  27. {
  28. int ret;
  29. /* Calling os_getpid because some libcs cached getpid incorrectly */
  30. int pid = os_getpid(), ppid = getppid();
  31. int sc_result;
  32. if (change_sig(SIGWINCH, 0) < 0 ||
  33. ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
  34. perror("ptrace");
  35. kill(pid, SIGKILL);
  36. }
  37. kill(pid, SIGSTOP);
  38. /*
  39. * This syscall will be intercepted by the parent. Don't call more than
  40. * once, please.
  41. */
  42. sc_result = os_getpid();
  43. if (sc_result == pid)
  44. /* Nothing modified by the parent, we are running normally. */
  45. ret = 1;
  46. else if (sc_result == ppid)
  47. /*
  48. * Expected in check_ptrace and check_sysemu when they succeed
  49. * in modifying the stack frame
  50. */
  51. ret = 0;
  52. else
  53. /* Serious trouble! This could be caused by a bug in host 2.6
  54. * SKAS3/2.6 patch before release -V6, together with a bug in
  55. * the UML code itself.
  56. */
  57. ret = 2;
  58. exit(ret);
  59. }
  60. static void fatal_perror(const char *str)
  61. {
  62. perror(str);
  63. exit(1);
  64. }
  65. static void fatal(char *fmt, ...)
  66. {
  67. va_list list;
  68. va_start(list, fmt);
  69. vfprintf(stderr, fmt, list);
  70. va_end(list);
  71. exit(1);
  72. }
  73. static void non_fatal(char *fmt, ...)
  74. {
  75. va_list list;
  76. va_start(list, fmt);
  77. vfprintf(stderr, fmt, list);
  78. va_end(list);
  79. }
  80. static int start_ptraced_child(void)
  81. {
  82. int pid, n, status;
  83. pid = fork();
  84. if (pid == 0)
  85. ptrace_child();
  86. else if (pid < 0)
  87. fatal_perror("start_ptraced_child : fork failed");
  88. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  89. if (n < 0)
  90. fatal_perror("check_ptrace : waitpid failed");
  91. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  92. fatal("check_ptrace : expected SIGSTOP, got status = %d",
  93. status);
  94. return pid;
  95. }
  96. /* When testing for SYSEMU support, if it is one of the broken versions, we
  97. * must just avoid using sysemu, not panic, but only if SYSEMU features are
  98. * broken.
  99. * So only for SYSEMU features we test mustpanic, while normal host features
  100. * must work anyway!
  101. */
  102. static int stop_ptraced_child(int pid, int exitcode, int mustexit)
  103. {
  104. int status, n, ret = 0;
  105. if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
  106. perror("stop_ptraced_child : ptrace failed");
  107. return -1;
  108. }
  109. CATCH_EINTR(n = waitpid(pid, &status, 0));
  110. if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
  111. int exit_with = WEXITSTATUS(status);
  112. if (exit_with == 2)
  113. non_fatal("check_ptrace : child exited with status 2. "
  114. "\nDisabling SYSEMU support.\n");
  115. non_fatal("check_ptrace : child exited with exitcode %d, while "
  116. "expecting %d; status 0x%x\n", exit_with,
  117. exitcode, status);
  118. if (mustexit)
  119. exit(1);
  120. ret = -1;
  121. }
  122. return ret;
  123. }
  124. /* Changed only during early boot */
  125. static int force_sysemu_disabled = 0;
  126. static int __init nosysemu_cmd_param(char *str, int* add)
  127. {
  128. force_sysemu_disabled = 1;
  129. return 0;
  130. }
  131. __uml_setup("nosysemu", nosysemu_cmd_param,
  132. "nosysemu\n"
  133. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  134. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  135. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  136. " To make it working, you need a kernel patch for your host, too.\n"
  137. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  138. " information.\n\n");
  139. static void __init check_sysemu(void)
  140. {
  141. unsigned long regs[MAX_REG_NR];
  142. int pid, n, status, count=0;
  143. non_fatal("Checking syscall emulation patch for ptrace...");
  144. sysemu_supported = 0;
  145. pid = start_ptraced_child();
  146. if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  147. goto fail;
  148. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  149. if (n < 0)
  150. fatal_perror("check_sysemu : wait failed");
  151. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  152. fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
  153. status);
  154. if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  155. fatal_perror("check_sysemu : PTRACE_GETREGS failed");
  156. if (PT_SYSCALL_NR(regs) != __NR_getpid) {
  157. non_fatal("check_sysemu got system call number %d, "
  158. "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
  159. goto fail;
  160. }
  161. n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
  162. if (n < 0) {
  163. non_fatal("check_sysemu : failed to modify system call "
  164. "return");
  165. goto fail;
  166. }
  167. if (stop_ptraced_child(pid, 0, 0) < 0)
  168. goto fail_stopped;
  169. sysemu_supported = 1;
  170. non_fatal("OK\n");
  171. set_using_sysemu(!force_sysemu_disabled);
  172. non_fatal("Checking advanced syscall emulation patch for ptrace...");
  173. pid = start_ptraced_child();
  174. if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  175. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  176. fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
  177. while (1) {
  178. count++;
  179. if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  180. goto fail;
  181. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  182. if (n < 0)
  183. fatal_perror("check_sysemu: wait failed");
  184. if (WIFSTOPPED(status) &&
  185. (WSTOPSIG(status) == (SIGTRAP|0x80))) {
  186. if (!count) {
  187. non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
  188. "doesn't singlestep");
  189. goto fail;
  190. }
  191. n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
  192. os_getpid());
  193. if (n < 0)
  194. fatal_perror("check_sysemu : failed to modify "
  195. "system call return");
  196. break;
  197. }
  198. else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  199. count++;
  200. else {
  201. non_fatal("check_sysemu: expected SIGTRAP or "
  202. "(SIGTRAP | 0x80), got status = %d\n",
  203. status);
  204. goto fail;
  205. }
  206. }
  207. if (stop_ptraced_child(pid, 0, 0) < 0)
  208. goto fail_stopped;
  209. sysemu_supported = 2;
  210. non_fatal("OK\n");
  211. if (!force_sysemu_disabled)
  212. set_using_sysemu(sysemu_supported);
  213. return;
  214. fail:
  215. stop_ptraced_child(pid, 1, 0);
  216. fail_stopped:
  217. non_fatal("missing\n");
  218. }
  219. static void __init check_ptrace(void)
  220. {
  221. int pid, syscall, n, status;
  222. non_fatal("Checking that ptrace can change system call numbers...");
  223. pid = start_ptraced_child();
  224. if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  225. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  226. fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
  227. while (1) {
  228. if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  229. fatal_perror("check_ptrace : ptrace failed");
  230. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  231. if (n < 0)
  232. fatal_perror("check_ptrace : wait failed");
  233. if (!WIFSTOPPED(status) ||
  234. (WSTOPSIG(status) != (SIGTRAP | 0x80)))
  235. fatal("check_ptrace : expected (SIGTRAP|0x80), "
  236. "got status = %d", status);
  237. syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
  238. 0);
  239. if (syscall == __NR_getpid) {
  240. n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
  241. __NR_getppid);
  242. if (n < 0)
  243. fatal_perror("check_ptrace : failed to modify "
  244. "system call");
  245. break;
  246. }
  247. }
  248. stop_ptraced_child(pid, 0, 1);
  249. non_fatal("OK\n");
  250. check_sysemu();
  251. }
  252. extern void check_tmpexec(void);
  253. static void __init check_coredump_limit(void)
  254. {
  255. struct rlimit lim;
  256. int err = getrlimit(RLIMIT_CORE, &lim);
  257. if (err) {
  258. perror("Getting core dump limit");
  259. return;
  260. }
  261. printf("Core dump limits :\n\tsoft - ");
  262. if (lim.rlim_cur == RLIM_INFINITY)
  263. printf("NONE\n");
  264. else printf("%lu\n", lim.rlim_cur);
  265. printf("\thard - ");
  266. if (lim.rlim_max == RLIM_INFINITY)
  267. printf("NONE\n");
  268. else printf("%lu\n", lim.rlim_max);
  269. }
  270. void __init os_early_checks(void)
  271. {
  272. int pid;
  273. /* Print out the core dump limits early */
  274. check_coredump_limit();
  275. check_ptrace();
  276. /* Need to check this early because mmapping happens before the
  277. * kernel is running.
  278. */
  279. check_tmpexec();
  280. pid = start_ptraced_child();
  281. if (init_registers(pid))
  282. fatal("Failed to initialize default registers");
  283. stop_ptraced_child(pid, 1, 1);
  284. }
  285. int __init parse_iomem(char *str, int *add)
  286. {
  287. struct iomem_region *new;
  288. struct stat64 buf;
  289. char *file, *driver;
  290. int fd, size;
  291. driver = str;
  292. file = strchr(str,',');
  293. if (file == NULL) {
  294. fprintf(stderr, "parse_iomem : failed to parse iomem\n");
  295. goto out;
  296. }
  297. *file = '\0';
  298. file++;
  299. fd = open(file, O_RDWR, 0);
  300. if (fd < 0) {
  301. perror("parse_iomem - Couldn't open io file");
  302. goto out;
  303. }
  304. if (fstat64(fd, &buf) < 0) {
  305. perror("parse_iomem - cannot stat_fd file");
  306. goto out_close;
  307. }
  308. new = malloc(sizeof(*new));
  309. if (new == NULL) {
  310. perror("Couldn't allocate iomem_region struct");
  311. goto out_close;
  312. }
  313. size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  314. *new = ((struct iomem_region) { .next = iomem_regions,
  315. .driver = driver,
  316. .fd = fd,
  317. .size = size,
  318. .phys = 0,
  319. .virt = 0 });
  320. iomem_regions = new;
  321. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  322. return 0;
  323. out_close:
  324. close(fd);
  325. out:
  326. return 1;
  327. }