start_up.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stddef.h>
  7. #include <stdarg.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <sched.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <setjmp.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 "signal_user.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 "time_user.h"
  33. #include "init.h"
  34. #include "os.h"
  35. #include "uml-config.h"
  36. #include "choose-mode.h"
  37. #include "mode.h"
  38. #include "tempfile.h"
  39. #include "kern_constants.h"
  40. #ifdef UML_CONFIG_MODE_SKAS
  41. #include "skas.h"
  42. #include "skas_ptrace.h"
  43. #include "registers.h"
  44. #endif
  45. static int ptrace_child(void *arg)
  46. {
  47. int ret;
  48. int pid = os_getpid(), ppid = getppid();
  49. int sc_result;
  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. printk("check_ptrace : child exited with status 2. "
  109. "Serious trouble happening! Try updating your "
  110. "host skas patch!\nDisabling SYSEMU support.");
  111. printk("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. printk("\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 proc_mm = 1;
  126. static int __init skas0_cmd_param(char *str, int* add)
  127. {
  128. ptrace_faultinfo = proc_mm = 0;
  129. return 0;
  130. }
  131. /* The two __uml_setup would conflict, without this stupid alias. */
  132. static int __init mode_skas0_cmd_param(char *str, int* add)
  133. __attribute__((alias("skas0_cmd_param")));
  134. __uml_setup("skas0", skas0_cmd_param,
  135. "skas0\n"
  136. " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
  137. " you specify mode=tt.\n\n");
  138. __uml_setup("mode=skas0", mode_skas0_cmd_param,
  139. "mode=skas0\n"
  140. " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
  141. " specify mode=tt. Note that this was recently added - on \n"
  142. " older kernels you must use simply \"skas0\".\n\n");
  143. static int force_sysemu_disabled = 0;
  144. static int __init nosysemu_cmd_param(char *str, int* add)
  145. {
  146. force_sysemu_disabled = 1;
  147. return 0;
  148. }
  149. __uml_setup("nosysemu", nosysemu_cmd_param,
  150. "nosysemu\n"
  151. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  152. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  153. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  154. " To make it working, you need a kernel patch for your host, too.\n"
  155. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  156. " information.\n\n");
  157. static void __init check_sysemu(void)
  158. {
  159. void *stack;
  160. int pid, n, status, count=0;
  161. printk("Checking syscall emulation patch for ptrace...");
  162. sysemu_supported = 0;
  163. pid = start_ptraced_child(&stack);
  164. if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  165. goto fail;
  166. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  167. if (n < 0)
  168. panic("check_sysemu : wait failed, errno = %d", errno);
  169. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  170. panic("check_sysemu : expected SIGTRAP, "
  171. "got status = %d", status);
  172. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  173. os_getpid());
  174. if(n < 0)
  175. panic("check_sysemu : failed to modify system "
  176. "call return, errno = %d", errno);
  177. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  178. goto fail_stopped;
  179. sysemu_supported = 1;
  180. printk("OK\n");
  181. set_using_sysemu(!force_sysemu_disabled);
  182. printk("Checking advanced syscall emulation patch for ptrace...");
  183. pid = start_ptraced_child(&stack);
  184. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  185. (void *) PTRACE_O_TRACESYSGOOD) < 0)
  186. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
  187. errno);
  188. while(1){
  189. count++;
  190. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  191. goto fail;
  192. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  193. if(n < 0)
  194. panic("check_ptrace : wait failed, errno = %d", errno);
  195. if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
  196. if (!count)
  197. panic("check_ptrace : SYSEMU_SINGLESTEP "
  198. "doesn't singlestep");
  199. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  200. os_getpid());
  201. if(n < 0)
  202. panic("check_sysemu : failed to modify system "
  203. "call return, errno = %d", errno);
  204. break;
  205. }
  206. else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  207. count++;
  208. else
  209. panic("check_ptrace : expected SIGTRAP or "
  210. "(SIGTRAP|0x80), got status = %d", status);
  211. }
  212. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  213. goto fail_stopped;
  214. sysemu_supported = 2;
  215. printk("OK\n");
  216. if ( !force_sysemu_disabled )
  217. set_using_sysemu(sysemu_supported);
  218. return;
  219. fail:
  220. stop_ptraced_child(pid, stack, 1, 0);
  221. fail_stopped:
  222. printk("missing\n");
  223. }
  224. static void __init check_ptrace(void)
  225. {
  226. void *stack;
  227. int pid, syscall, n, status;
  228. printk("Checking that ptrace can change system call numbers...");
  229. pid = start_ptraced_child(&stack);
  230. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  231. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
  232. while(1){
  233. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  234. panic("check_ptrace : ptrace failed, errno = %d",
  235. errno);
  236. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  237. if(n < 0)
  238. panic("check_ptrace : wait failed, errno = %d", errno);
  239. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
  240. panic("check_ptrace : expected (SIGTRAP|0x80), "
  241. "got status = %d", status);
  242. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  243. 0);
  244. if(syscall == __NR_getpid){
  245. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  246. __NR_getppid);
  247. if(n < 0)
  248. panic("check_ptrace : failed to modify system "
  249. "call, errno = %d", errno);
  250. break;
  251. }
  252. }
  253. stop_ptraced_child(pid, stack, 0, 1);
  254. printk("OK\n");
  255. check_sysemu();
  256. }
  257. extern int create_tmp_file(unsigned long len);
  258. static void check_tmpexec(void)
  259. {
  260. void *addr;
  261. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  262. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  263. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  264. printf("Checking PROT_EXEC mmap in /tmp...");
  265. fflush(stdout);
  266. if(addr == MAP_FAILED){
  267. err = errno;
  268. perror("failed");
  269. if(err == EPERM)
  270. printf("/tmp must be not mounted noexec\n");
  271. exit(1);
  272. }
  273. printf("OK\n");
  274. munmap(addr, UM_KERN_PAGE_SIZE);
  275. close(fd);
  276. }
  277. void os_early_checks(void)
  278. {
  279. check_ptrace();
  280. /* Need to check this early because mmapping happens before the
  281. * kernel is running.
  282. */
  283. check_tmpexec();
  284. }
  285. static int __init noprocmm_cmd_param(char *str, int* add)
  286. {
  287. proc_mm = 0;
  288. return 0;
  289. }
  290. __uml_setup("noprocmm", noprocmm_cmd_param,
  291. "noprocmm\n"
  292. " Turns off usage of /proc/mm, even if host supports it.\n"
  293. " To support /proc/mm, the host needs to be patched using\n"
  294. " the current skas3 patch.\n\n");
  295. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  296. {
  297. ptrace_faultinfo = 0;
  298. return 0;
  299. }
  300. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  301. "noptracefaultinfo\n"
  302. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  303. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  304. " using the current skas3 patch.\n\n");
  305. #ifdef UML_CONFIG_MODE_SKAS
  306. static inline void check_skas3_ptrace_support(void)
  307. {
  308. struct ptrace_faultinfo fi;
  309. void *stack;
  310. int pid, n;
  311. printf("Checking for the skas3 patch in the host...");
  312. pid = start_ptraced_child(&stack);
  313. n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
  314. if (n < 0) {
  315. ptrace_faultinfo = 0;
  316. if(errno == EIO)
  317. printf("not found\n");
  318. else
  319. perror("not found");
  320. }
  321. else {
  322. if (!ptrace_faultinfo)
  323. printf("found but disabled on command line\n");
  324. else
  325. printf("found\n");
  326. }
  327. init_registers(pid);
  328. stop_ptraced_child(pid, stack, 1, 1);
  329. }
  330. int can_do_skas(void)
  331. {
  332. printf("Checking for /proc/mm...");
  333. if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
  334. proc_mm = 0;
  335. printf("not found\n");
  336. }
  337. else {
  338. if (!proc_mm)
  339. printf("found but disabled on command line\n");
  340. else
  341. printf("found\n");
  342. }
  343. check_skas3_ptrace_support();
  344. return 1;
  345. }
  346. #else
  347. int can_do_skas(void)
  348. {
  349. return(0);
  350. }
  351. #endif
  352. int have_devanon = 0;
  353. void check_devanon(void)
  354. {
  355. int fd;
  356. printk("Checking for /dev/anon on the host...");
  357. fd = open("/dev/anon", O_RDWR);
  358. if(fd < 0){
  359. printk("Not available (open failed with errno %d)\n", errno);
  360. return;
  361. }
  362. printk("OK\n");
  363. have_devanon = 1;
  364. }
  365. int __init parse_iomem(char *str, int *add)
  366. {
  367. struct iomem_region *new;
  368. struct uml_stat buf;
  369. char *file, *driver;
  370. int fd, err, size;
  371. driver = str;
  372. file = strchr(str,',');
  373. if(file == NULL){
  374. printf("parse_iomem : failed to parse iomem\n");
  375. goto out;
  376. }
  377. *file = '\0';
  378. file++;
  379. fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
  380. if(fd < 0){
  381. os_print_error(fd, "parse_iomem - Couldn't open io file");
  382. goto out;
  383. }
  384. err = os_stat_fd(fd, &buf);
  385. if(err < 0){
  386. os_print_error(err, "parse_iomem - cannot stat_fd file");
  387. goto out_close;
  388. }
  389. new = malloc(sizeof(*new));
  390. if(new == NULL){
  391. perror("Couldn't allocate iomem_region struct");
  392. goto out_close;
  393. }
  394. size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  395. *new = ((struct iomem_region) { .next = iomem_regions,
  396. .driver = driver,
  397. .fd = fd,
  398. .size = size,
  399. .phys = 0,
  400. .virt = 0 });
  401. iomem_regions = new;
  402. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  403. return(0);
  404. out_close:
  405. os_close_file(fd);
  406. out:
  407. return(1);
  408. }