start_up.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. /* Changed only during early boot */
  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. /* Changed only during early boot */
  146. static int force_sysemu_disabled = 0;
  147. static int __init nosysemu_cmd_param(char *str, int* add)
  148. {
  149. force_sysemu_disabled = 1;
  150. return 0;
  151. }
  152. __uml_setup("nosysemu", nosysemu_cmd_param,
  153. "nosysemu\n"
  154. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  155. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  156. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  157. " To make it working, you need a kernel patch for your host, too.\n"
  158. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  159. " information.\n\n");
  160. static void __init check_sysemu(void)
  161. {
  162. void *stack;
  163. int pid, n, status, count=0;
  164. printf("Checking syscall emulation patch for ptrace...");
  165. sysemu_supported = 0;
  166. pid = start_ptraced_child(&stack);
  167. if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  168. goto fail;
  169. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  170. if (n < 0)
  171. panic("check_sysemu : wait failed, errno = %d", errno);
  172. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  173. panic("check_sysemu : expected SIGTRAP, "
  174. "got status = %d", status);
  175. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  176. os_getpid());
  177. if(n < 0)
  178. panic("check_sysemu : failed to modify system "
  179. "call return, errno = %d", errno);
  180. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  181. goto fail_stopped;
  182. sysemu_supported = 1;
  183. printf("OK\n");
  184. set_using_sysemu(!force_sysemu_disabled);
  185. printf("Checking advanced syscall emulation patch for ptrace...");
  186. pid = start_ptraced_child(&stack);
  187. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  188. (void *) PTRACE_O_TRACESYSGOOD) < 0)
  189. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
  190. errno);
  191. while(1){
  192. count++;
  193. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  194. goto fail;
  195. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  196. if(n < 0)
  197. panic("check_ptrace : wait failed, errno = %d", errno);
  198. if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
  199. if (!count)
  200. panic("check_ptrace : SYSEMU_SINGLESTEP "
  201. "doesn't singlestep");
  202. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  203. os_getpid());
  204. if(n < 0)
  205. panic("check_sysemu : failed to modify system "
  206. "call return, errno = %d", errno);
  207. break;
  208. }
  209. else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  210. count++;
  211. else
  212. panic("check_ptrace : expected SIGTRAP or "
  213. "(SIGTRAP|0x80), got status = %d", status);
  214. }
  215. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  216. goto fail_stopped;
  217. sysemu_supported = 2;
  218. printf("OK\n");
  219. if ( !force_sysemu_disabled )
  220. set_using_sysemu(sysemu_supported);
  221. return;
  222. fail:
  223. stop_ptraced_child(pid, stack, 1, 0);
  224. fail_stopped:
  225. printf("missing\n");
  226. }
  227. static void __init check_ptrace(void)
  228. {
  229. void *stack;
  230. int pid, syscall, n, status;
  231. printf("Checking that ptrace can change system call numbers...");
  232. pid = start_ptraced_child(&stack);
  233. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  234. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
  235. while(1){
  236. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  237. panic("check_ptrace : ptrace failed, errno = %d",
  238. errno);
  239. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  240. if(n < 0)
  241. panic("check_ptrace : wait failed, errno = %d", errno);
  242. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
  243. panic("check_ptrace : expected (SIGTRAP|0x80), "
  244. "got status = %d", status);
  245. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  246. 0);
  247. if(syscall == __NR_getpid){
  248. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  249. __NR_getppid);
  250. if(n < 0)
  251. panic("check_ptrace : failed to modify system "
  252. "call, errno = %d", errno);
  253. break;
  254. }
  255. }
  256. stop_ptraced_child(pid, stack, 0, 1);
  257. printf("OK\n");
  258. check_sysemu();
  259. }
  260. extern void check_tmpexec(void);
  261. void os_early_checks(void)
  262. {
  263. check_ptrace();
  264. /* Need to check this early because mmapping happens before the
  265. * kernel is running.
  266. */
  267. check_tmpexec();
  268. }
  269. static int __init noprocmm_cmd_param(char *str, int* add)
  270. {
  271. proc_mm = 0;
  272. return 0;
  273. }
  274. __uml_setup("noprocmm", noprocmm_cmd_param,
  275. "noprocmm\n"
  276. " Turns off usage of /proc/mm, even if host supports it.\n"
  277. " To support /proc/mm, the host needs to be patched using\n"
  278. " the current skas3 patch.\n\n");
  279. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  280. {
  281. ptrace_faultinfo = 0;
  282. return 0;
  283. }
  284. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  285. "noptracefaultinfo\n"
  286. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  287. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  288. " using the current skas3 patch.\n\n");
  289. static int __init noptraceldt_cmd_param(char *str, int* add)
  290. {
  291. ptrace_ldt = 0;
  292. return 0;
  293. }
  294. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  295. "noptraceldt\n"
  296. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  297. " To support PTRACE_LDT, the host needs to be patched using\n"
  298. " the current skas3 patch.\n\n");
  299. #ifdef UML_CONFIG_MODE_SKAS
  300. static inline void check_skas3_ptrace_faultinfo(void)
  301. {
  302. struct ptrace_faultinfo fi;
  303. void *stack;
  304. int pid, n;
  305. printf(" - PTRACE_FAULTINFO...");
  306. pid = start_ptraced_child(&stack);
  307. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  308. if (n < 0) {
  309. ptrace_faultinfo = 0;
  310. if(errno == EIO)
  311. printf("not found\n");
  312. else
  313. perror("not found");
  314. }
  315. else {
  316. if (!ptrace_faultinfo)
  317. printf("found but disabled on command line\n");
  318. else
  319. printf("found\n");
  320. }
  321. init_registers(pid);
  322. stop_ptraced_child(pid, stack, 1, 1);
  323. }
  324. static inline void check_skas3_ptrace_ldt(void)
  325. {
  326. #ifdef PTRACE_LDT
  327. void *stack;
  328. int pid, n;
  329. unsigned char ldtbuf[40];
  330. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  331. .func = 2, /* read default ldt */
  332. .ptr = ldtbuf,
  333. .bytecount = sizeof(ldtbuf)};
  334. printf(" - PTRACE_LDT...");
  335. pid = start_ptraced_child(&stack);
  336. n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  337. if (n < 0) {
  338. if(errno == EIO)
  339. printf("not found\n");
  340. else {
  341. perror("not found");
  342. }
  343. ptrace_ldt = 0;
  344. }
  345. else {
  346. if(ptrace_ldt)
  347. printf("found\n");
  348. else
  349. printf("found, but use is disabled\n");
  350. }
  351. stop_ptraced_child(pid, stack, 1, 1);
  352. #else
  353. /* PTRACE_LDT might be disabled via cmdline option.
  354. * We want to override this, else we might use the stub
  355. * without real need
  356. */
  357. ptrace_ldt = 1;
  358. #endif
  359. }
  360. static inline void check_skas3_proc_mm(void)
  361. {
  362. printf(" - /proc/mm...");
  363. if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
  364. proc_mm = 0;
  365. printf("not found\n");
  366. }
  367. else {
  368. if (!proc_mm)
  369. printf("found but disabled on command line\n");
  370. else
  371. printf("found\n");
  372. }
  373. }
  374. int can_do_skas(void)
  375. {
  376. printf("Checking for the skas3 patch in the host:\n");
  377. check_skas3_proc_mm();
  378. check_skas3_ptrace_faultinfo();
  379. check_skas3_ptrace_ldt();
  380. if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  381. skas_needs_stub = 1;
  382. return 1;
  383. }
  384. #else
  385. int can_do_skas(void)
  386. {
  387. return(0);
  388. }
  389. #endif
  390. int __init parse_iomem(char *str, int *add)
  391. {
  392. struct iomem_region *new;
  393. struct uml_stat buf;
  394. char *file, *driver;
  395. int fd, err, size;
  396. driver = str;
  397. file = strchr(str,',');
  398. if(file == NULL){
  399. printf("parse_iomem : failed to parse iomem\n");
  400. goto out;
  401. }
  402. *file = '\0';
  403. file++;
  404. fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
  405. if(fd < 0){
  406. os_print_error(fd, "parse_iomem - Couldn't open io file");
  407. goto out;
  408. }
  409. err = os_stat_fd(fd, &buf);
  410. if(err < 0){
  411. os_print_error(err, "parse_iomem - cannot stat_fd file");
  412. goto out_close;
  413. }
  414. new = malloc(sizeof(*new));
  415. if(new == NULL){
  416. perror("Couldn't allocate iomem_region struct");
  417. goto out_close;
  418. }
  419. size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  420. *new = ((struct iomem_region) { .next = iomem_regions,
  421. .driver = driver,
  422. .fd = fd,
  423. .size = size,
  424. .phys = 0,
  425. .virt = 0 });
  426. iomem_regions = new;
  427. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  428. return(0);
  429. out_close:
  430. os_close_file(fd);
  431. out:
  432. return(1);
  433. }
  434. /* Changed during early boot */
  435. int pty_output_sigio = 0;
  436. int pty_close_sigio = 0;
  437. /* Used as a flag during SIGIO testing early in boot */
  438. static volatile int got_sigio = 0;
  439. static void __init handler(int sig)
  440. {
  441. got_sigio = 1;
  442. }
  443. struct openpty_arg {
  444. int master;
  445. int slave;
  446. int err;
  447. };
  448. static void openpty_cb(void *arg)
  449. {
  450. struct openpty_arg *info = arg;
  451. info->err = 0;
  452. if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
  453. info->err = -errno;
  454. }
  455. static void __init check_one_sigio(void (*proc)(int, int))
  456. {
  457. struct sigaction old, new;
  458. struct openpty_arg pty = { .master = -1, .slave = -1 };
  459. int master, slave, err;
  460. initial_thread_cb(openpty_cb, &pty);
  461. if(pty.err){
  462. printk("openpty failed, errno = %d\n", -pty.err);
  463. return;
  464. }
  465. master = pty.master;
  466. slave = pty.slave;
  467. if((master == -1) || (slave == -1)){
  468. printk("openpty failed to allocate a pty\n");
  469. return;
  470. }
  471. /* Not now, but complain so we now where we failed. */
  472. err = raw(master);
  473. if (err < 0)
  474. panic("check_sigio : __raw failed, errno = %d\n", -err);
  475. err = os_sigio_async(master, slave);
  476. if(err < 0)
  477. panic("tty_fds : sigio_async failed, err = %d\n", -err);
  478. if(sigaction(SIGIO, NULL, &old) < 0)
  479. panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
  480. new = old;
  481. new.sa_handler = handler;
  482. if(sigaction(SIGIO, &new, NULL) < 0)
  483. panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
  484. got_sigio = 0;
  485. (*proc)(master, slave);
  486. close(master);
  487. close(slave);
  488. if(sigaction(SIGIO, &old, NULL) < 0)
  489. panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
  490. }
  491. static void tty_output(int master, int slave)
  492. {
  493. int n;
  494. char buf[512];
  495. printk("Checking that host ptys support output SIGIO...");
  496. memset(buf, 0, sizeof(buf));
  497. while(os_write_file(master, buf, sizeof(buf)) > 0) ;
  498. if(errno != EAGAIN)
  499. panic("check_sigio : write failed, errno = %d\n", errno);
  500. while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
  501. if(got_sigio){
  502. printk("Yes\n");
  503. pty_output_sigio = 1;
  504. }
  505. else if(n == -EAGAIN) printk("No, enabling workaround\n");
  506. else panic("check_sigio : read failed, err = %d\n", n);
  507. }
  508. static void tty_close(int master, int slave)
  509. {
  510. printk("Checking that host ptys support SIGIO on close...");
  511. close(slave);
  512. if(got_sigio){
  513. printk("Yes\n");
  514. pty_close_sigio = 1;
  515. }
  516. else printk("No, enabling workaround\n");
  517. }
  518. void __init check_sigio(void)
  519. {
  520. if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
  521. (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
  522. printk("No pseudo-terminals available - skipping pty SIGIO "
  523. "check\n");
  524. return;
  525. }
  526. check_one_sigio(tty_output);
  527. check_one_sigio(tty_close);
  528. }
  529. void os_check_bugs(void)
  530. {
  531. check_ptrace();
  532. check_sigio();
  533. }