start_up.c 15 KB

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