start_up.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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/ptrace.h>
  16. #include <sys/stat.h>
  17. #include <sys/wait.h>
  18. #include <asm/unistd.h>
  19. #include "init.h"
  20. #include "os.h"
  21. #include "mem_user.h"
  22. #include "ptrace_user.h"
  23. #include "registers.h"
  24. #include "skas.h"
  25. #include "skas_ptrace.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. int ptrace_faultinfo;
  126. static int disable_ptrace_faultinfo;
  127. int ptrace_ldt;
  128. static int disable_ptrace_ldt;
  129. int proc_mm;
  130. static int disable_proc_mm;
  131. int have_switch_mm;
  132. static int disable_switch_mm;
  133. int skas_needs_stub;
  134. static int __init skas0_cmd_param(char *str, int* add)
  135. {
  136. disable_ptrace_faultinfo = 1;
  137. disable_ptrace_ldt = 1;
  138. disable_proc_mm = 1;
  139. disable_switch_mm = 1;
  140. return 0;
  141. }
  142. /* The two __uml_setup would conflict, without this stupid alias. */
  143. static int __init mode_skas0_cmd_param(char *str, int* add)
  144. __attribute__((alias("skas0_cmd_param")));
  145. __uml_setup("skas0", skas0_cmd_param,
  146. "skas0\n"
  147. " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n");
  148. __uml_setup("mode=skas0", mode_skas0_cmd_param,
  149. "mode=skas0\n"
  150. " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
  151. /* Changed only during early boot */
  152. static int force_sysemu_disabled = 0;
  153. static int __init nosysemu_cmd_param(char *str, int* add)
  154. {
  155. force_sysemu_disabled = 1;
  156. return 0;
  157. }
  158. __uml_setup("nosysemu", nosysemu_cmd_param,
  159. "nosysemu\n"
  160. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  161. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  162. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  163. " To make it working, you need a kernel patch for your host, too.\n"
  164. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  165. " information.\n\n");
  166. static void __init check_sysemu(void)
  167. {
  168. unsigned long regs[MAX_REG_NR];
  169. int pid, n, status, count=0;
  170. non_fatal("Checking syscall emulation patch for ptrace...");
  171. sysemu_supported = 0;
  172. pid = start_ptraced_child();
  173. if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  174. goto fail;
  175. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  176. if (n < 0)
  177. fatal_perror("check_sysemu : wait failed");
  178. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  179. fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
  180. status);
  181. if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  182. fatal_perror("check_sysemu : PTRACE_GETREGS failed");
  183. if (PT_SYSCALL_NR(regs) != __NR_getpid) {
  184. non_fatal("check_sysemu got system call number %d, "
  185. "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
  186. goto fail;
  187. }
  188. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
  189. if (n < 0) {
  190. non_fatal("check_sysemu : failed to modify system call "
  191. "return");
  192. goto fail;
  193. }
  194. if (stop_ptraced_child(pid, 0, 0) < 0)
  195. goto fail_stopped;
  196. sysemu_supported = 1;
  197. non_fatal("OK\n");
  198. set_using_sysemu(!force_sysemu_disabled);
  199. non_fatal("Checking advanced syscall emulation patch for ptrace...");
  200. pid = start_ptraced_child();
  201. if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  202. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  203. fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
  204. while (1) {
  205. count++;
  206. if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  207. goto fail;
  208. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  209. if (n < 0)
  210. fatal_perror("check_sysemu: wait failed");
  211. if (WIFSTOPPED(status) &&
  212. (WSTOPSIG(status) == (SIGTRAP|0x80))) {
  213. if (!count) {
  214. non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
  215. "doesn't singlestep");
  216. goto fail;
  217. }
  218. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  219. os_getpid());
  220. if (n < 0)
  221. fatal_perror("check_sysemu : failed to modify "
  222. "system call return");
  223. break;
  224. }
  225. else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  226. count++;
  227. else {
  228. non_fatal("check_sysemu: expected SIGTRAP or "
  229. "(SIGTRAP | 0x80), got status = %d\n",
  230. status);
  231. goto fail;
  232. }
  233. }
  234. if (stop_ptraced_child(pid, 0, 0) < 0)
  235. goto fail_stopped;
  236. sysemu_supported = 2;
  237. non_fatal("OK\n");
  238. if (!force_sysemu_disabled)
  239. set_using_sysemu(sysemu_supported);
  240. return;
  241. fail:
  242. stop_ptraced_child(pid, 1, 0);
  243. fail_stopped:
  244. non_fatal("missing\n");
  245. }
  246. static void __init check_ptrace(void)
  247. {
  248. int pid, syscall, n, status;
  249. non_fatal("Checking that ptrace can change system call numbers...");
  250. pid = start_ptraced_child();
  251. if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  252. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  253. fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
  254. while (1) {
  255. if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  256. fatal_perror("check_ptrace : ptrace failed");
  257. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  258. if (n < 0)
  259. fatal_perror("check_ptrace : wait failed");
  260. if (!WIFSTOPPED(status) ||
  261. (WSTOPSIG(status) != (SIGTRAP | 0x80)))
  262. fatal("check_ptrace : expected (SIGTRAP|0x80), "
  263. "got status = %d", status);
  264. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  265. 0);
  266. if (syscall == __NR_getpid) {
  267. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  268. __NR_getppid);
  269. if (n < 0)
  270. fatal_perror("check_ptrace : failed to modify "
  271. "system call");
  272. break;
  273. }
  274. }
  275. stop_ptraced_child(pid, 0, 1);
  276. non_fatal("OK\n");
  277. check_sysemu();
  278. }
  279. extern void check_tmpexec(void);
  280. static void __init check_coredump_limit(void)
  281. {
  282. struct rlimit lim;
  283. int err = getrlimit(RLIMIT_CORE, &lim);
  284. if (err) {
  285. perror("Getting core dump limit");
  286. return;
  287. }
  288. printf("Core dump limits :\n\tsoft - ");
  289. if (lim.rlim_cur == RLIM_INFINITY)
  290. printf("NONE\n");
  291. else printf("%lu\n", lim.rlim_cur);
  292. printf("\thard - ");
  293. if (lim.rlim_max == RLIM_INFINITY)
  294. printf("NONE\n");
  295. else printf("%lu\n", lim.rlim_max);
  296. }
  297. void __init os_early_checks(void)
  298. {
  299. int pid;
  300. /* Print out the core dump limits early */
  301. check_coredump_limit();
  302. check_ptrace();
  303. /* Need to check this early because mmapping happens before the
  304. * kernel is running.
  305. */
  306. check_tmpexec();
  307. pid = start_ptraced_child();
  308. if (init_registers(pid))
  309. fatal("Failed to initialize default registers");
  310. stop_ptraced_child(pid, 1, 1);
  311. }
  312. static int __init noprocmm_cmd_param(char *str, int* add)
  313. {
  314. disable_proc_mm = 1;
  315. return 0;
  316. }
  317. __uml_setup("noprocmm", noprocmm_cmd_param,
  318. "noprocmm\n"
  319. " Turns off usage of /proc/mm, even if host supports it.\n"
  320. " To support /proc/mm, the host needs to be patched using\n"
  321. " the current skas3 patch.\n\n");
  322. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  323. {
  324. disable_ptrace_faultinfo = 1;
  325. return 0;
  326. }
  327. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  328. "noptracefaultinfo\n"
  329. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  330. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  331. " using the current skas3 patch.\n\n");
  332. static int __init noptraceldt_cmd_param(char *str, int* add)
  333. {
  334. disable_ptrace_ldt = 1;
  335. return 0;
  336. }
  337. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  338. "noptraceldt\n"
  339. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  340. " To support PTRACE_LDT, the host needs to be patched using\n"
  341. " the current skas3 patch.\n\n");
  342. static inline void check_skas3_ptrace_faultinfo(void)
  343. {
  344. struct ptrace_faultinfo fi;
  345. int pid, n;
  346. non_fatal(" - PTRACE_FAULTINFO...");
  347. pid = start_ptraced_child();
  348. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  349. if (n < 0) {
  350. if (errno == EIO)
  351. non_fatal("not found\n");
  352. else
  353. perror("not found");
  354. } else if (disable_ptrace_faultinfo)
  355. non_fatal("found but disabled on command line\n");
  356. else {
  357. ptrace_faultinfo = 1;
  358. non_fatal("found\n");
  359. }
  360. stop_ptraced_child(pid, 1, 1);
  361. }
  362. static inline void check_skas3_ptrace_ldt(void)
  363. {
  364. #ifdef PTRACE_LDT
  365. int pid, n;
  366. unsigned char ldtbuf[40];
  367. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  368. .func = 2, /* read default ldt */
  369. .ptr = ldtbuf,
  370. .bytecount = sizeof(ldtbuf)};
  371. non_fatal(" - PTRACE_LDT...");
  372. pid = start_ptraced_child();
  373. n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  374. if (n < 0) {
  375. if (errno == EIO)
  376. non_fatal("not found\n");
  377. else
  378. perror("not found");
  379. } else if (disable_ptrace_ldt)
  380. non_fatal("found, but use is disabled\n");
  381. else {
  382. ptrace_ldt = 1;
  383. non_fatal("found\n");
  384. }
  385. stop_ptraced_child(pid, 1, 1);
  386. #endif
  387. }
  388. static inline void check_skas3_proc_mm(void)
  389. {
  390. non_fatal(" - /proc/mm...");
  391. if (access("/proc/mm", W_OK) < 0)
  392. perror("not found");
  393. else if (disable_proc_mm)
  394. non_fatal("found but disabled on command line\n");
  395. else {
  396. proc_mm = 1;
  397. non_fatal("found\n");
  398. }
  399. }
  400. void can_do_skas(void)
  401. {
  402. non_fatal("Checking for the skas3 patch in the host:\n");
  403. check_skas3_proc_mm();
  404. check_skas3_ptrace_faultinfo();
  405. check_skas3_ptrace_ldt();
  406. if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  407. skas_needs_stub = 1;
  408. }
  409. int __init parse_iomem(char *str, int *add)
  410. {
  411. struct iomem_region *new;
  412. struct stat64 buf;
  413. char *file, *driver;
  414. int fd, size;
  415. driver = str;
  416. file = strchr(str,',');
  417. if (file == NULL) {
  418. fprintf(stderr, "parse_iomem : failed to parse iomem\n");
  419. goto out;
  420. }
  421. *file = '\0';
  422. file++;
  423. fd = open(file, O_RDWR, 0);
  424. if (fd < 0) {
  425. perror("parse_iomem - Couldn't open io file");
  426. goto out;
  427. }
  428. if (fstat64(fd, &buf) < 0) {
  429. perror("parse_iomem - cannot stat_fd file");
  430. goto out_close;
  431. }
  432. new = malloc(sizeof(*new));
  433. if (new == NULL) {
  434. perror("Couldn't allocate iomem_region struct");
  435. goto out_close;
  436. }
  437. size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  438. *new = ((struct iomem_region) { .next = iomem_regions,
  439. .driver = driver,
  440. .fd = fd,
  441. .size = size,
  442. .phys = 0,
  443. .virt = 0 });
  444. iomem_regions = new;
  445. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  446. return 0;
  447. out_close:
  448. close(fd);
  449. out:
  450. return 1;
  451. }