start_up.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 <asm/unistd.h>
  18. #include "init.h"
  19. #include "os.h"
  20. #include "mem_user.h"
  21. #include "ptrace_user.h"
  22. #include "registers.h"
  23. #include "skas.h"
  24. #include "skas_ptrace.h"
  25. static void ptrace_child(void)
  26. {
  27. int ret;
  28. /* Calling os_getpid because some libcs cached getpid incorrectly */
  29. int pid = os_getpid(), ppid = getppid();
  30. int sc_result;
  31. if (change_sig(SIGWINCH, 0) < 0 ||
  32. ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
  33. perror("ptrace");
  34. kill(pid, SIGKILL);
  35. }
  36. kill(pid, SIGSTOP);
  37. /*
  38. * This syscall will be intercepted by the parent. Don't call more than
  39. * once, please.
  40. */
  41. sc_result = os_getpid();
  42. if (sc_result == pid)
  43. /* Nothing modified by the parent, we are running normally. */
  44. ret = 1;
  45. else if (sc_result == ppid)
  46. /*
  47. * Expected in check_ptrace and check_sysemu when they succeed
  48. * in modifying the stack frame
  49. */
  50. ret = 0;
  51. else
  52. /* Serious trouble! This could be caused by a bug in host 2.6
  53. * SKAS3/2.6 patch before release -V6, together with a bug in
  54. * the UML code itself.
  55. */
  56. ret = 2;
  57. exit(ret);
  58. }
  59. static void fatal_perror(const char *str)
  60. {
  61. perror(str);
  62. exit(1);
  63. }
  64. static void fatal(char *fmt, ...)
  65. {
  66. va_list list;
  67. va_start(list, fmt);
  68. vfprintf(stderr, fmt, list);
  69. va_end(list);
  70. exit(1);
  71. }
  72. static void non_fatal(char *fmt, ...)
  73. {
  74. va_list list;
  75. va_start(list, fmt);
  76. vfprintf(stderr, fmt, list);
  77. va_end(list);
  78. }
  79. static int start_ptraced_child(void)
  80. {
  81. int pid, n, status;
  82. pid = fork();
  83. if (pid == 0)
  84. ptrace_child();
  85. else if (pid < 0)
  86. fatal_perror("start_ptraced_child : fork failed");
  87. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  88. if (n < 0)
  89. fatal_perror("check_ptrace : waitpid failed");
  90. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  91. fatal("check_ptrace : expected SIGSTOP, got status = %d",
  92. status);
  93. return pid;
  94. }
  95. /* When testing for SYSEMU support, if it is one of the broken versions, we
  96. * must just avoid using sysemu, not panic, but only if SYSEMU features are
  97. * broken.
  98. * So only for SYSEMU features we test mustpanic, while normal host features
  99. * must work anyway!
  100. */
  101. static int stop_ptraced_child(int pid, int exitcode, int mustexit)
  102. {
  103. int status, n, ret = 0;
  104. if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
  105. perror("stop_ptraced_child : ptrace failed");
  106. return -1;
  107. }
  108. CATCH_EINTR(n = waitpid(pid, &status, 0));
  109. if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
  110. int exit_with = WEXITSTATUS(status);
  111. if (exit_with == 2)
  112. non_fatal("check_ptrace : child exited with status 2. "
  113. "\nDisabling SYSEMU support.\n");
  114. non_fatal("check_ptrace : child exited with exitcode %d, while "
  115. "expecting %d; status 0x%x\n", exit_with,
  116. exitcode, status);
  117. if (mustexit)
  118. exit(1);
  119. ret = -1;
  120. }
  121. return ret;
  122. }
  123. /* Changed only during early boot */
  124. int ptrace_faultinfo;
  125. static int disable_ptrace_faultinfo;
  126. int ptrace_ldt;
  127. static int disable_ptrace_ldt;
  128. int proc_mm;
  129. static int disable_proc_mm;
  130. int have_switch_mm;
  131. static int disable_switch_mm;
  132. int skas_needs_stub;
  133. static int __init skas0_cmd_param(char *str, int* add)
  134. {
  135. disable_ptrace_faultinfo = 1;
  136. disable_ptrace_ldt = 1;
  137. disable_proc_mm = 1;
  138. disable_switch_mm = 1;
  139. return 0;
  140. }
  141. /* The two __uml_setup would conflict, without this stupid alias. */
  142. static int __init mode_skas0_cmd_param(char *str, int* add)
  143. __attribute__((alias("skas0_cmd_param")));
  144. __uml_setup("skas0", skas0_cmd_param,
  145. "skas0\n"
  146. " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n");
  147. __uml_setup("mode=skas0", mode_skas0_cmd_param,
  148. "mode=skas0\n"
  149. " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
  150. /* Changed only during early boot */
  151. static int force_sysemu_disabled = 0;
  152. static int __init nosysemu_cmd_param(char *str, int* add)
  153. {
  154. force_sysemu_disabled = 1;
  155. return 0;
  156. }
  157. __uml_setup("nosysemu", nosysemu_cmd_param,
  158. "nosysemu\n"
  159. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  160. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  161. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  162. " To make it working, you need a kernel patch for your host, too.\n"
  163. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  164. " information.\n\n");
  165. static void __init check_sysemu(void)
  166. {
  167. unsigned long regs[MAX_REG_NR];
  168. int pid, n, status, count=0;
  169. non_fatal("Checking syscall emulation patch for ptrace...");
  170. sysemu_supported = 0;
  171. pid = start_ptraced_child();
  172. if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  173. goto fail;
  174. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  175. if (n < 0)
  176. fatal_perror("check_sysemu : wait failed");
  177. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  178. fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
  179. status);
  180. if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  181. fatal_perror("check_sysemu : PTRACE_GETREGS failed");
  182. if (PT_SYSCALL_NR(regs) != __NR_getpid) {
  183. non_fatal("check_sysemu got system call number %d, "
  184. "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
  185. goto fail;
  186. }
  187. n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
  188. if (n < 0) {
  189. non_fatal("check_sysemu : failed to modify system call "
  190. "return");
  191. goto fail;
  192. }
  193. if (stop_ptraced_child(pid, 0, 0) < 0)
  194. goto fail_stopped;
  195. sysemu_supported = 1;
  196. non_fatal("OK\n");
  197. set_using_sysemu(!force_sysemu_disabled);
  198. non_fatal("Checking advanced syscall emulation patch for ptrace...");
  199. pid = start_ptraced_child();
  200. if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  201. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  202. fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
  203. while (1) {
  204. count++;
  205. if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  206. goto fail;
  207. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  208. if (n < 0)
  209. fatal_perror("check_sysemu: wait failed");
  210. if (WIFSTOPPED(status) &&
  211. (WSTOPSIG(status) == (SIGTRAP|0x80))) {
  212. if (!count) {
  213. non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
  214. "doesn't singlestep");
  215. goto fail;
  216. }
  217. n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
  218. os_getpid());
  219. if (n < 0)
  220. fatal_perror("check_sysemu : failed to modify "
  221. "system call return");
  222. break;
  223. }
  224. else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  225. count++;
  226. else {
  227. non_fatal("check_sysemu: expected SIGTRAP or "
  228. "(SIGTRAP | 0x80), got status = %d\n",
  229. status);
  230. goto fail;
  231. }
  232. }
  233. if (stop_ptraced_child(pid, 0, 0) < 0)
  234. goto fail_stopped;
  235. sysemu_supported = 2;
  236. non_fatal("OK\n");
  237. if (!force_sysemu_disabled)
  238. set_using_sysemu(sysemu_supported);
  239. return;
  240. fail:
  241. stop_ptraced_child(pid, 1, 0);
  242. fail_stopped:
  243. non_fatal("missing\n");
  244. }
  245. static void __init check_ptrace(void)
  246. {
  247. int pid, syscall, n, status;
  248. non_fatal("Checking that ptrace can change system call numbers...");
  249. pid = start_ptraced_child();
  250. if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  251. (void *) PTRACE_O_TRACESYSGOOD) < 0))
  252. fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
  253. while (1) {
  254. if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  255. fatal_perror("check_ptrace : ptrace failed");
  256. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  257. if (n < 0)
  258. fatal_perror("check_ptrace : wait failed");
  259. if (!WIFSTOPPED(status) ||
  260. (WSTOPSIG(status) != (SIGTRAP | 0x80)))
  261. fatal("check_ptrace : expected (SIGTRAP|0x80), "
  262. "got status = %d", status);
  263. syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
  264. 0);
  265. if (syscall == __NR_getpid) {
  266. n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
  267. __NR_getppid);
  268. if (n < 0)
  269. fatal_perror("check_ptrace : failed to modify "
  270. "system call");
  271. break;
  272. }
  273. }
  274. stop_ptraced_child(pid, 0, 1);
  275. non_fatal("OK\n");
  276. check_sysemu();
  277. }
  278. extern void check_tmpexec(void);
  279. static void __init check_coredump_limit(void)
  280. {
  281. struct rlimit lim;
  282. int err = getrlimit(RLIMIT_CORE, &lim);
  283. if (err) {
  284. perror("Getting core dump limit");
  285. return;
  286. }
  287. printf("Core dump limits :\n\tsoft - ");
  288. if (lim.rlim_cur == RLIM_INFINITY)
  289. printf("NONE\n");
  290. else printf("%lu\n", lim.rlim_cur);
  291. printf("\thard - ");
  292. if (lim.rlim_max == RLIM_INFINITY)
  293. printf("NONE\n");
  294. else printf("%lu\n", lim.rlim_max);
  295. }
  296. void __init os_early_checks(void)
  297. {
  298. int pid;
  299. /* Print out the core dump limits early */
  300. check_coredump_limit();
  301. check_ptrace();
  302. /* Need to check this early because mmapping happens before the
  303. * kernel is running.
  304. */
  305. check_tmpexec();
  306. pid = start_ptraced_child();
  307. if (init_registers(pid))
  308. fatal("Failed to initialize default registers");
  309. stop_ptraced_child(pid, 1, 1);
  310. }
  311. static int __init noprocmm_cmd_param(char *str, int* add)
  312. {
  313. disable_proc_mm = 1;
  314. return 0;
  315. }
  316. __uml_setup("noprocmm", noprocmm_cmd_param,
  317. "noprocmm\n"
  318. " Turns off usage of /proc/mm, even if host supports it.\n"
  319. " To support /proc/mm, the host needs to be patched using\n"
  320. " the current skas3 patch.\n\n");
  321. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  322. {
  323. disable_ptrace_faultinfo = 1;
  324. return 0;
  325. }
  326. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  327. "noptracefaultinfo\n"
  328. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  329. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  330. " using the current skas3 patch.\n\n");
  331. static int __init noptraceldt_cmd_param(char *str, int* add)
  332. {
  333. disable_ptrace_ldt = 1;
  334. return 0;
  335. }
  336. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  337. "noptraceldt\n"
  338. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  339. " To support PTRACE_LDT, the host needs to be patched using\n"
  340. " the current skas3 patch.\n\n");
  341. static inline void check_skas3_ptrace_faultinfo(void)
  342. {
  343. struct ptrace_faultinfo fi;
  344. int pid, n;
  345. non_fatal(" - PTRACE_FAULTINFO...");
  346. pid = start_ptraced_child();
  347. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  348. if (n < 0) {
  349. if (errno == EIO)
  350. non_fatal("not found\n");
  351. else
  352. perror("not found");
  353. } else if (disable_ptrace_faultinfo)
  354. non_fatal("found but disabled on command line\n");
  355. else {
  356. ptrace_faultinfo = 1;
  357. non_fatal("found\n");
  358. }
  359. stop_ptraced_child(pid, 1, 1);
  360. }
  361. static inline void check_skas3_ptrace_ldt(void)
  362. {
  363. #ifdef PTRACE_LDT
  364. int pid, n;
  365. unsigned char ldtbuf[40];
  366. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  367. .func = 2, /* read default ldt */
  368. .ptr = ldtbuf,
  369. .bytecount = sizeof(ldtbuf)};
  370. non_fatal(" - PTRACE_LDT...");
  371. pid = start_ptraced_child();
  372. n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  373. if (n < 0) {
  374. if (errno == EIO)
  375. non_fatal("not found\n");
  376. else
  377. perror("not found");
  378. } else if (disable_ptrace_ldt)
  379. non_fatal("found, but use is disabled\n");
  380. else {
  381. ptrace_ldt = 1;
  382. non_fatal("found\n");
  383. }
  384. stop_ptraced_child(pid, 1, 1);
  385. #endif
  386. }
  387. static inline void check_skas3_proc_mm(void)
  388. {
  389. non_fatal(" - /proc/mm...");
  390. if (access("/proc/mm", W_OK) < 0)
  391. perror("not found");
  392. else if (disable_proc_mm)
  393. non_fatal("found but disabled on command line\n");
  394. else {
  395. proc_mm = 1;
  396. non_fatal("found\n");
  397. }
  398. }
  399. void can_do_skas(void)
  400. {
  401. non_fatal("Checking for the skas3 patch in the host:\n");
  402. check_skas3_proc_mm();
  403. check_skas3_ptrace_faultinfo();
  404. check_skas3_ptrace_ldt();
  405. if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  406. skas_needs_stub = 1;
  407. }
  408. int __init parse_iomem(char *str, int *add)
  409. {
  410. struct iomem_region *new;
  411. struct stat64 buf;
  412. char *file, *driver;
  413. int fd, size;
  414. driver = str;
  415. file = strchr(str,',');
  416. if (file == NULL) {
  417. fprintf(stderr, "parse_iomem : failed to parse iomem\n");
  418. goto out;
  419. }
  420. *file = '\0';
  421. file++;
  422. fd = open(file, O_RDWR, 0);
  423. if (fd < 0) {
  424. perror("parse_iomem - Couldn't open io file");
  425. goto out;
  426. }
  427. if (fstat64(fd, &buf) < 0) {
  428. perror("parse_iomem - cannot stat_fd file");
  429. goto out_close;
  430. }
  431. new = malloc(sizeof(*new));
  432. if (new == NULL) {
  433. perror("Couldn't allocate iomem_region struct");
  434. goto out_close;
  435. }
  436. size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  437. *new = ((struct iomem_region) { .next = iomem_regions,
  438. .driver = driver,
  439. .fd = fd,
  440. .size = size,
  441. .phys = 0,
  442. .virt = 0 });
  443. iomem_regions = new;
  444. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  445. return 0;
  446. out_close:
  447. close(fd);
  448. out:
  449. return 1;
  450. }