start_up.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 int create_tmp_file(unsigned long long len);
  260. static void check_tmpexec(void)
  261. {
  262. void *addr;
  263. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  264. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  265. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  266. printf("Checking PROT_EXEC mmap in /tmp...");
  267. fflush(stdout);
  268. if(addr == MAP_FAILED){
  269. err = errno;
  270. perror("failed");
  271. if(err == EPERM)
  272. printf("/tmp must be not mounted noexec\n");
  273. exit(1);
  274. }
  275. printf("OK\n");
  276. munmap(addr, UM_KERN_PAGE_SIZE);
  277. close(fd);
  278. }
  279. void os_early_checks(void)
  280. {
  281. check_ptrace();
  282. /* Need to check this early because mmapping happens before the
  283. * kernel is running.
  284. */
  285. check_tmpexec();
  286. }
  287. static int __init noprocmm_cmd_param(char *str, int* add)
  288. {
  289. proc_mm = 0;
  290. return 0;
  291. }
  292. __uml_setup("noprocmm", noprocmm_cmd_param,
  293. "noprocmm\n"
  294. " Turns off usage of /proc/mm, even if host supports it.\n"
  295. " To support /proc/mm, the host needs to be patched using\n"
  296. " the current skas3 patch.\n\n");
  297. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  298. {
  299. ptrace_faultinfo = 0;
  300. return 0;
  301. }
  302. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  303. "noptracefaultinfo\n"
  304. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  305. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  306. " using the current skas3 patch.\n\n");
  307. static int __init noptraceldt_cmd_param(char *str, int* add)
  308. {
  309. ptrace_ldt = 0;
  310. return 0;
  311. }
  312. __uml_setup("noptraceldt", noptraceldt_cmd_param,
  313. "noptraceldt\n"
  314. " Turns off usage of PTRACE_LDT, even if host supports it.\n"
  315. " To support PTRACE_LDT, the host needs to be patched using\n"
  316. " the current skas3 patch.\n\n");
  317. #ifdef UML_CONFIG_MODE_SKAS
  318. static inline void check_skas3_ptrace_faultinfo(void)
  319. {
  320. struct ptrace_faultinfo fi;
  321. void *stack;
  322. int pid, n;
  323. printf(" - PTRACE_FAULTINFO...");
  324. pid = start_ptraced_child(&stack);
  325. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  326. if (n < 0) {
  327. ptrace_faultinfo = 0;
  328. if(errno == EIO)
  329. printf("not found\n");
  330. else
  331. perror("not found");
  332. }
  333. else {
  334. if (!ptrace_faultinfo)
  335. printf("found but disabled on command line\n");
  336. else
  337. printf("found\n");
  338. }
  339. init_registers(pid);
  340. stop_ptraced_child(pid, stack, 1, 1);
  341. }
  342. static inline void check_skas3_ptrace_ldt(void)
  343. {
  344. #ifdef PTRACE_LDT
  345. void *stack;
  346. int pid, n;
  347. unsigned char ldtbuf[40];
  348. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  349. .func = 2, /* read default ldt */
  350. .ptr = ldtbuf,
  351. .bytecount = sizeof(ldtbuf)};
  352. printf(" - PTRACE_LDT...");
  353. pid = start_ptraced_child(&stack);
  354. n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  355. if (n < 0) {
  356. if(errno == EIO)
  357. printf("not found\n");
  358. else {
  359. perror("not found");
  360. }
  361. ptrace_ldt = 0;
  362. }
  363. else {
  364. if(ptrace_ldt)
  365. printf("found\n");
  366. else
  367. printf("found, but use is disabled\n");
  368. }
  369. stop_ptraced_child(pid, stack, 1, 1);
  370. #else
  371. /* PTRACE_LDT might be disabled via cmdline option.
  372. * We want to override this, else we might use the stub
  373. * without real need
  374. */
  375. ptrace_ldt = 1;
  376. #endif
  377. }
  378. static inline void check_skas3_proc_mm(void)
  379. {
  380. printf(" - /proc/mm...");
  381. if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
  382. proc_mm = 0;
  383. printf("not found\n");
  384. }
  385. else {
  386. if (!proc_mm)
  387. printf("found but disabled on command line\n");
  388. else
  389. printf("found\n");
  390. }
  391. }
  392. int can_do_skas(void)
  393. {
  394. printf("Checking for the skas3 patch in the host:\n");
  395. check_skas3_proc_mm();
  396. check_skas3_ptrace_faultinfo();
  397. check_skas3_ptrace_ldt();
  398. if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
  399. skas_needs_stub = 1;
  400. return 1;
  401. }
  402. #else
  403. int can_do_skas(void)
  404. {
  405. return(0);
  406. }
  407. #endif
  408. int __init parse_iomem(char *str, int *add)
  409. {
  410. struct iomem_region *new;
  411. struct uml_stat buf;
  412. char *file, *driver;
  413. int fd, err, size;
  414. driver = str;
  415. file = strchr(str,',');
  416. if(file == NULL){
  417. printf("parse_iomem : failed to parse iomem\n");
  418. goto out;
  419. }
  420. *file = '\0';
  421. file++;
  422. fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
  423. if(fd < 0){
  424. os_print_error(fd, "parse_iomem - Couldn't open io file");
  425. goto out;
  426. }
  427. err = os_stat_fd(fd, &buf);
  428. if(err < 0){
  429. os_print_error(err, "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.ust_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. os_close_file(fd);
  449. out:
  450. return(1);
  451. }
  452. /* Changed during early boot */
  453. int pty_output_sigio = 0;
  454. int pty_close_sigio = 0;
  455. /* Used as a flag during SIGIO testing early in boot */
  456. static volatile int got_sigio = 0;
  457. static void __init handler(int sig)
  458. {
  459. got_sigio = 1;
  460. }
  461. struct openpty_arg {
  462. int master;
  463. int slave;
  464. int err;
  465. };
  466. static void openpty_cb(void *arg)
  467. {
  468. struct openpty_arg *info = arg;
  469. info->err = 0;
  470. if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
  471. info->err = -errno;
  472. }
  473. static void __init check_one_sigio(void (*proc)(int, int))
  474. {
  475. struct sigaction old, new;
  476. struct openpty_arg pty = { .master = -1, .slave = -1 };
  477. int master, slave, err;
  478. initial_thread_cb(openpty_cb, &pty);
  479. if(pty.err){
  480. printk("openpty failed, errno = %d\n", -pty.err);
  481. return;
  482. }
  483. master = pty.master;
  484. slave = pty.slave;
  485. if((master == -1) || (slave == -1)){
  486. printk("openpty failed to allocate a pty\n");
  487. return;
  488. }
  489. /* Not now, but complain so we now where we failed. */
  490. err = raw(master);
  491. if (err < 0)
  492. panic("check_sigio : __raw failed, errno = %d\n", -err);
  493. err = os_sigio_async(master, slave);
  494. if(err < 0)
  495. panic("tty_fds : sigio_async failed, err = %d\n", -err);
  496. if(sigaction(SIGIO, NULL, &old) < 0)
  497. panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
  498. new = old;
  499. new.sa_handler = handler;
  500. if(sigaction(SIGIO, &new, NULL) < 0)
  501. panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
  502. got_sigio = 0;
  503. (*proc)(master, slave);
  504. close(master);
  505. close(slave);
  506. if(sigaction(SIGIO, &old, NULL) < 0)
  507. panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
  508. }
  509. static void tty_output(int master, int slave)
  510. {
  511. int n;
  512. char buf[512];
  513. printk("Checking that host ptys support output SIGIO...");
  514. memset(buf, 0, sizeof(buf));
  515. while(os_write_file(master, buf, sizeof(buf)) > 0) ;
  516. if(errno != EAGAIN)
  517. panic("check_sigio : write failed, errno = %d\n", errno);
  518. while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
  519. if(got_sigio){
  520. printk("Yes\n");
  521. pty_output_sigio = 1;
  522. }
  523. else if(n == -EAGAIN) printk("No, enabling workaround\n");
  524. else panic("check_sigio : read failed, err = %d\n", n);
  525. }
  526. static void tty_close(int master, int slave)
  527. {
  528. printk("Checking that host ptys support SIGIO on close...");
  529. close(slave);
  530. if(got_sigio){
  531. printk("Yes\n");
  532. pty_close_sigio = 1;
  533. }
  534. else printk("No, enabling workaround\n");
  535. }
  536. void __init check_sigio(void)
  537. {
  538. if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
  539. (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
  540. printk("No pseudo-terminals available - skipping pty SIGIO "
  541. "check\n");
  542. return;
  543. }
  544. check_one_sigio(tty_output);
  545. check_one_sigio(tty_close);
  546. }
  547. void os_check_bugs(void)
  548. {
  549. check_ptrace();
  550. check_sigio();
  551. }