start_up.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <pty.h>
  6. #include <stdio.h>
  7. #include <stddef.h>
  8. #include <stdarg.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <sched.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #include <setjmp.h>
  17. #include <sys/time.h>
  18. #include <sys/wait.h>
  19. #include <sys/mman.h>
  20. #include <asm/unistd.h>
  21. #include <asm/page.h>
  22. #include <sys/types.h>
  23. #include "user_util.h"
  24. #include "kern_util.h"
  25. #include "user.h"
  26. #include "signal_kern.h"
  27. #include "sysdep/ptrace.h"
  28. #include "sysdep/sigcontext.h"
  29. #include "irq_user.h"
  30. #include "ptrace_user.h"
  31. #include "mem_user.h"
  32. #include "init.h"
  33. #include "os.h"
  34. #include "uml-config.h"
  35. #include "choose-mode.h"
  36. #include "mode.h"
  37. #include "tempfile.h"
  38. #include "kern_constants.h"
  39. #ifdef UML_CONFIG_MODE_SKAS
  40. #include "skas.h"
  41. #include "skas_ptrace.h"
  42. #include "registers.h"
  43. #endif
  44. static int ptrace_child(void *arg)
  45. {
  46. int ret;
  47. int pid = os_getpid(), ppid = getppid();
  48. int sc_result;
  49. change_sig(SIGWINCH, 0);
  50. if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
  51. perror("ptrace");
  52. os_kill_process(pid, 0);
  53. }
  54. os_stop_process(pid);
  55. /*This syscall will be intercepted by the parent. Don't call more than
  56. * once, please.*/
  57. sc_result = os_getpid();
  58. if (sc_result == pid)
  59. ret = 1; /*Nothing modified by the parent, we are running
  60. normally.*/
  61. else if (sc_result == ppid)
  62. ret = 0; /*Expected in check_ptrace and check_sysemu when they
  63. succeed in modifying the stack frame*/
  64. else
  65. ret = 2; /*Serious trouble! This could be caused by a bug in
  66. host 2.6 SKAS3/2.6 patch before release -V6, together
  67. with a bug in the UML code itself.*/
  68. _exit(ret);
  69. }
  70. static int start_ptraced_child(void **stack_out)
  71. {
  72. void *stack;
  73. unsigned long sp;
  74. int pid, n, status;
  75. stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
  76. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  77. if(stack == MAP_FAILED)
  78. panic("check_ptrace : mmap failed, errno = %d", errno);
  79. sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
  80. pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
  81. if(pid < 0)
  82. panic("start_ptraced_child : clone failed, errno = %d", errno);
  83. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  84. if(n < 0)
  85. panic("check_ptrace : clone failed, errno = %d", errno);
  86. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  87. panic("check_ptrace : expected SIGSTOP, got status = %d",
  88. status);
  89. *stack_out = stack;
  90. return(pid);
  91. }
  92. /* When testing for SYSEMU support, if it is one of the broken versions, we
  93. * must just avoid using sysemu, not panic, but only if SYSEMU features are
  94. * broken.
  95. * So only for SYSEMU features we test mustpanic, while normal host features
  96. * must work anyway!
  97. */
  98. static int stop_ptraced_child(int pid, void *stack, int exitcode,
  99. int mustpanic)
  100. {
  101. int status, n, ret = 0;
  102. if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
  103. panic("check_ptrace : ptrace failed, errno = %d", errno);
  104. CATCH_EINTR(n = waitpid(pid, &status, 0));
  105. if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
  106. int exit_with = WEXITSTATUS(status);
  107. if (exit_with == 2)
  108. printf("check_ptrace : child exited with status 2. "
  109. "Serious trouble happening! Try updating your "
  110. "host skas patch!\nDisabling SYSEMU support.");
  111. printf("check_ptrace : child exited with exitcode %d, while "
  112. "expecting %d; status 0x%x", exit_with,
  113. exitcode, status);
  114. if (mustpanic)
  115. panic("\n");
  116. else
  117. printf("\n");
  118. ret = -1;
  119. }
  120. if(munmap(stack, PAGE_SIZE) < 0)
  121. panic("check_ptrace : munmap failed, errno = %d", errno);
  122. return ret;
  123. }
  124. int ptrace_faultinfo = 1;
  125. int ptrace_ldt = 1;
  126. int proc_mm = 1;
  127. int skas_needs_stub = 0;
  128. static int __init skas0_cmd_param(char *str, int* add)
  129. {
  130. ptrace_faultinfo = proc_mm = 0;
  131. return 0;
  132. }
  133. /* The two __uml_setup would conflict, without this stupid alias. */
  134. static int __init mode_skas0_cmd_param(char *str, int* add)
  135. __attribute__((alias("skas0_cmd_param")));
  136. __uml_setup("skas0", skas0_cmd_param,
  137. "skas0\n"
  138. " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
  139. " you specify mode=tt.\n\n");
  140. __uml_setup("mode=skas0", mode_skas0_cmd_param,
  141. "mode=skas0\n"
  142. " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
  143. " specify mode=tt. Note that this was recently added - on \n"
  144. " older kernels you must use simply \"skas0\".\n\n");
  145. static int force_sysemu_disabled = 0;
  146. static int __init nosysemu_cmd_param(char *str, int* add)
  147. {
  148. force_sysemu_disabled = 1;
  149. return 0;
  150. }
  151. __uml_setup("nosysemu", nosysemu_cmd_param,
  152. "nosysemu\n"
  153. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  154. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  155. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  156. " To make it working, you need a kernel patch for your host, too.\n"
  157. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  158. " information.\n\n");
  159. static void __init check_sysemu(void)
  160. {
  161. void *stack;
  162. int pid, n, status, count=0;
  163. printf("Checking syscall emulation patch for ptrace...");
  164. sysemu_supported = 0;
  165. pid = start_ptraced_child(&stack);
  166. if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  167. goto fail;
  168. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  169. if (n < 0)
  170. panic("check_sysemu : wait failed, errno = %d", errno);
  171. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  172. panic("check_sysemu : expected SIGTRAP, "
  173. "got status = %d", status);
  174. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  175. os_getpid());
  176. if(n < 0)
  177. panic("check_sysemu : failed to modify system "
  178. "call return, errno = %d", errno);
  179. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  180. goto fail_stopped;
  181. sysemu_supported = 1;
  182. printf("OK\n");
  183. set_using_sysemu(!force_sysemu_disabled);
  184. printf("Checking advanced syscall emulation patch for ptrace...");
  185. pid = start_ptraced_child(&stack);
  186. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  187. (void *) PTRACE_O_TRACESYSGOOD) < 0)
  188. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
  189. errno);
  190. while(1){
  191. count++;
  192. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  193. goto fail;
  194. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  195. if(n < 0)
  196. panic("check_ptrace : wait failed, errno = %d", errno);
  197. if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
  198. if (!count)
  199. panic("check_ptrace : SYSEMU_SINGLESTEP "
  200. "doesn't singlestep");
  201. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  202. os_getpid());
  203. if(n < 0)
  204. panic("check_sysemu : failed to modify system "
  205. "call return, errno = %d", errno);
  206. break;
  207. }
  208. else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  209. count++;
  210. else
  211. panic("check_ptrace : expected SIGTRAP or "
  212. "(SIGTRAP|0x80), got status = %d", status);
  213. }
  214. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  215. goto fail_stopped;
  216. sysemu_supported = 2;
  217. printf("OK\n");
  218. if ( !force_sysemu_disabled )
  219. set_using_sysemu(sysemu_supported);
  220. return;
  221. fail:
  222. stop_ptraced_child(pid, stack, 1, 0);
  223. fail_stopped:
  224. printf("missing\n");
  225. }
  226. static void __init check_ptrace(void)
  227. {
  228. void *stack;
  229. int pid, syscall, n, status;
  230. printf("Checking that ptrace can change system call numbers...");
  231. pid = start_ptraced_child(&stack);
  232. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  233. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
  234. while(1){
  235. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  236. panic("check_ptrace : ptrace failed, errno = %d",
  237. errno);
  238. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  239. if(n < 0)
  240. panic("check_ptrace : wait failed, errno = %d", errno);
  241. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
  242. panic("check_ptrace : expected (SIGTRAP|0x80), "
  243. "got status = %d", status);
  244. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  245. 0);
  246. if(syscall == __NR_getpid){
  247. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  248. __NR_getppid);
  249. if(n < 0)
  250. panic("check_ptrace : failed to modify system "
  251. "call, errno = %d", errno);
  252. break;
  253. }
  254. }
  255. stop_ptraced_child(pid, stack, 0, 1);
  256. printf("OK\n");
  257. check_sysemu();
  258. }
  259. extern void check_tmpexec(void);
  260. void os_early_checks(void)
  261. {
  262. check_ptrace();
  263. /* Need to check this early because mmapping happens before the
  264. * kernel is running.
  265. */
  266. check_tmpexec();
  267. }
  268. static int __init noprocmm_cmd_param(char *str, int* add)
  269. {
  270. proc_mm = 0;
  271. return 0;
  272. }
  273. __uml_setup("noprocmm", noprocmm_cmd_param,
  274. "noprocmm\n"
  275. " Turns off usage of /proc/mm, even if host supports it.\n"
  276. " To support /proc/mm, the host needs to be patched using\n"
  277. " the current skas3 patch.\n\n");
  278. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  279. {
  280. ptrace_faultinfo = 0;
  281. return 0;
  282. }
  283. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  284. "noptracefaultinfo\n"
  285. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  286. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  287. " using the current skas3 patch.\n\n");
  288. static int __init noptraceldt_cmd_param(char *str, int* add)
  289. {
  290. ptrace_ldt = 0;
  291. return 0;
  292. }
  293. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  294. "noptraceldt\n"
  295. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  296. " To support PTRACE_LDT, the host needs to be patched using\n"
  297. " the current skas3 patch.\n\n");
  298. #ifdef UML_CONFIG_MODE_SKAS
  299. static inline void check_skas3_ptrace_faultinfo(void)
  300. {
  301. struct ptrace_faultinfo fi;
  302. void *stack;
  303. int pid, n;
  304. printf(" - PTRACE_FAULTINFO...");
  305. pid = start_ptraced_child(&stack);
  306. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  307. if (n < 0) {
  308. ptrace_faultinfo = 0;
  309. if(errno == EIO)
  310. printf("not found\n");
  311. else
  312. perror("not found");
  313. }
  314. else {
  315. if (!ptrace_faultinfo)
  316. printf("found but disabled on command line\n");
  317. else
  318. printf("found\n");
  319. }
  320. init_registers(pid);
  321. stop_ptraced_child(pid, stack, 1, 1);
  322. }
  323. static inline void check_skas3_ptrace_ldt(void)
  324. {
  325. #ifdef PTRACE_LDT
  326. void *stack;
  327. int pid, n;
  328. unsigned char ldtbuf[40];
  329. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  330. .func = 2, /* read default ldt */
  331. .ptr = ldtbuf,
  332. .bytecount = sizeof(ldtbuf)};
  333. printf(" - PTRACE_LDT...");
  334. pid = start_ptraced_child(&stack);
  335. n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  336. if (n < 0) {
  337. if(errno == EIO)
  338. printf("not found\n");
  339. else {
  340. perror("not found");
  341. }
  342. ptrace_ldt = 0;
  343. }
  344. else {
  345. if(ptrace_ldt)
  346. printf("found\n");
  347. else
  348. printf("found, but use is disabled\n");
  349. }
  350. stop_ptraced_child(pid, stack, 1, 1);
  351. #else
  352. /* PTRACE_LDT might be disabled via cmdline option.
  353. * We want to override this, else we might use the stub
  354. * without real need
  355. */
  356. ptrace_ldt = 1;
  357. #endif
  358. }
  359. static inline void check_skas3_proc_mm(void)
  360. {
  361. printf(" - /proc/mm...");
  362. if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
  363. proc_mm = 0;
  364. printf("not found\n");
  365. }
  366. else {
  367. if (!proc_mm)
  368. printf("found but disabled on command line\n");
  369. else
  370. printf("found\n");
  371. }
  372. }
  373. int can_do_skas(void)
  374. {
  375. printf("Checking for the skas3 patch in the host:\n");
  376. check_skas3_proc_mm();
  377. check_skas3_ptrace_faultinfo();
  378. check_skas3_ptrace_ldt();
  379. if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  380. skas_needs_stub = 1;
  381. return 1;
  382. }
  383. #else
  384. int can_do_skas(void)
  385. {
  386. return(0);
  387. }
  388. #endif
  389. int __init parse_iomem(char *str, int *add)
  390. {
  391. struct iomem_region *new;
  392. struct uml_stat buf;
  393. char *file, *driver;
  394. int fd, err, size;
  395. driver = str;
  396. file = strchr(str,',');
  397. if(file == NULL){
  398. printf("parse_iomem : failed to parse iomem\n");
  399. goto out;
  400. }
  401. *file = '\0';
  402. file++;
  403. fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
  404. if(fd < 0){
  405. os_print_error(fd, "parse_iomem - Couldn't open io file");
  406. goto out;
  407. }
  408. err = os_stat_fd(fd, &buf);
  409. if(err < 0){
  410. os_print_error(err, "parse_iomem - cannot stat_fd file");
  411. goto out_close;
  412. }
  413. new = malloc(sizeof(*new));
  414. if(new == NULL){
  415. perror("Couldn't allocate iomem_region struct");
  416. goto out_close;
  417. }
  418. size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  419. *new = ((struct iomem_region) { .next = iomem_regions,
  420. .driver = driver,
  421. .fd = fd,
  422. .size = size,
  423. .phys = 0,
  424. .virt = 0 });
  425. iomem_regions = new;
  426. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  427. return(0);
  428. out_close:
  429. os_close_file(fd);
  430. out:
  431. return(1);
  432. }
  433. /* Changed during early boot */
  434. int pty_output_sigio = 0;
  435. int pty_close_sigio = 0;
  436. /* Used as a flag during SIGIO testing early in boot */
  437. static volatile int got_sigio = 0;
  438. static void __init handler(int sig)
  439. {
  440. got_sigio = 1;
  441. }
  442. struct openpty_arg {
  443. int master;
  444. int slave;
  445. int err;
  446. };
  447. static void openpty_cb(void *arg)
  448. {
  449. struct openpty_arg *info = arg;
  450. info->err = 0;
  451. if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
  452. info->err = -errno;
  453. }
  454. static void __init check_one_sigio(void (*proc)(int, int))
  455. {
  456. struct sigaction old, new;
  457. struct openpty_arg pty = { .master = -1, .slave = -1 };
  458. int master, slave, err;
  459. initial_thread_cb(openpty_cb, &pty);
  460. if(pty.err){
  461. printk("openpty failed, errno = %d\n", -pty.err);
  462. return;
  463. }
  464. master = pty.master;
  465. slave = pty.slave;
  466. if((master == -1) || (slave == -1)){
  467. printk("openpty failed to allocate a pty\n");
  468. return;
  469. }
  470. /* Not now, but complain so we now where we failed. */
  471. err = raw(master);
  472. if (err < 0)
  473. panic("check_sigio : __raw failed, errno = %d\n", -err);
  474. err = os_sigio_async(master, slave);
  475. if(err < 0)
  476. panic("tty_fds : sigio_async failed, err = %d\n", -err);
  477. if(sigaction(SIGIO, NULL, &old) < 0)
  478. panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
  479. new = old;
  480. new.sa_handler = handler;
  481. if(sigaction(SIGIO, &new, NULL) < 0)
  482. panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
  483. got_sigio = 0;
  484. (*proc)(master, slave);
  485. close(master);
  486. close(slave);
  487. if(sigaction(SIGIO, &old, NULL) < 0)
  488. panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
  489. }
  490. static void tty_output(int master, int slave)
  491. {
  492. int n;
  493. char buf[512];
  494. printk("Checking that host ptys support output SIGIO...");
  495. memset(buf, 0, sizeof(buf));
  496. while(os_write_file(master, buf, sizeof(buf)) > 0) ;
  497. if(errno != EAGAIN)
  498. panic("check_sigio : write failed, errno = %d\n", errno);
  499. while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
  500. if(got_sigio){
  501. printk("Yes\n");
  502. pty_output_sigio = 1;
  503. }
  504. else if(n == -EAGAIN) printk("No, enabling workaround\n");
  505. else panic("check_sigio : read failed, err = %d\n", n);
  506. }
  507. static void tty_close(int master, int slave)
  508. {
  509. printk("Checking that host ptys support SIGIO on close...");
  510. close(slave);
  511. if(got_sigio){
  512. printk("Yes\n");
  513. pty_close_sigio = 1;
  514. }
  515. else printk("No, enabling workaround\n");
  516. }
  517. void __init check_sigio(void)
  518. {
  519. if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
  520. (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
  521. printk("No pseudo-terminals available - skipping pty SIGIO "
  522. "check\n");
  523. return;
  524. }
  525. check_one_sigio(tty_output);
  526. check_one_sigio(tty_close);
  527. }
  528. void os_check_bugs(void)
  529. {
  530. check_ptrace();
  531. check_sigio();
  532. }