start_up.c 15 KB

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