start_up.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. __uml_setup("skas0", skas0_cmd_param,
  132. "skas0\n"
  133. " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
  134. " you specify mode=tt.\n\n");
  135. static int force_sysemu_disabled = 0;
  136. static int __init nosysemu_cmd_param(char *str, int* add)
  137. {
  138. force_sysemu_disabled = 1;
  139. return 0;
  140. }
  141. __uml_setup("nosysemu", nosysemu_cmd_param,
  142. "nosysemu\n"
  143. " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
  144. " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
  145. " behaviour of ptrace() and helps reducing host context switch rate.\n"
  146. " To make it working, you need a kernel patch for your host, too.\n"
  147. " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
  148. " information.\n\n");
  149. static void __init check_sysemu(void)
  150. {
  151. void *stack;
  152. int pid, n, status, count=0;
  153. printk("Checking syscall emulation patch for ptrace...");
  154. sysemu_supported = 0;
  155. pid = start_ptraced_child(&stack);
  156. if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
  157. goto fail;
  158. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  159. if (n < 0)
  160. panic("check_sysemu : wait failed, errno = %d", errno);
  161. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
  162. panic("check_sysemu : expected SIGTRAP, "
  163. "got status = %d", status);
  164. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  165. os_getpid());
  166. if(n < 0)
  167. panic("check_sysemu : failed to modify system "
  168. "call return, errno = %d", errno);
  169. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  170. goto fail_stopped;
  171. sysemu_supported = 1;
  172. printk("OK\n");
  173. set_using_sysemu(!force_sysemu_disabled);
  174. printk("Checking advanced syscall emulation patch for ptrace...");
  175. pid = start_ptraced_child(&stack);
  176. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
  177. (void *) PTRACE_O_TRACESYSGOOD) < 0)
  178. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
  179. errno);
  180. while(1){
  181. count++;
  182. if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
  183. goto fail;
  184. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  185. if(n < 0)
  186. panic("check_ptrace : wait failed, errno = %d", errno);
  187. if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
  188. if (!count)
  189. panic("check_ptrace : SYSEMU_SINGLESTEP "
  190. "doesn't singlestep");
  191. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
  192. os_getpid());
  193. if(n < 0)
  194. panic("check_sysemu : failed to modify system "
  195. "call return, errno = %d", errno);
  196. break;
  197. }
  198. else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
  199. count++;
  200. else
  201. panic("check_ptrace : expected SIGTRAP or "
  202. "(SIGTRAP|0x80), got status = %d", status);
  203. }
  204. if (stop_ptraced_child(pid, stack, 0, 0) < 0)
  205. goto fail_stopped;
  206. sysemu_supported = 2;
  207. printk("OK\n");
  208. if ( !force_sysemu_disabled )
  209. set_using_sysemu(sysemu_supported);
  210. return;
  211. fail:
  212. stop_ptraced_child(pid, stack, 1, 0);
  213. fail_stopped:
  214. printk("missing\n");
  215. }
  216. static void __init check_ptrace(void)
  217. {
  218. void *stack;
  219. int pid, syscall, n, status;
  220. printk("Checking that ptrace can change system call numbers...");
  221. pid = start_ptraced_child(&stack);
  222. if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  223. panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
  224. while(1){
  225. if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
  226. panic("check_ptrace : ptrace failed, errno = %d",
  227. errno);
  228. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  229. if(n < 0)
  230. panic("check_ptrace : wait failed, errno = %d", errno);
  231. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
  232. panic("check_ptrace : expected (SIGTRAP|0x80), "
  233. "got status = %d", status);
  234. syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
  235. 0);
  236. if(syscall == __NR_getpid){
  237. n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
  238. __NR_getppid);
  239. if(n < 0)
  240. panic("check_ptrace : failed to modify system "
  241. "call, errno = %d", errno);
  242. break;
  243. }
  244. }
  245. stop_ptraced_child(pid, stack, 0, 1);
  246. printk("OK\n");
  247. check_sysemu();
  248. }
  249. extern int create_tmp_file(unsigned long len);
  250. static void check_tmpexec(void)
  251. {
  252. void *addr;
  253. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  254. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  255. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  256. printf("Checking PROT_EXEC mmap in /tmp...");
  257. fflush(stdout);
  258. if(addr == MAP_FAILED){
  259. err = errno;
  260. perror("failed");
  261. if(err == EPERM)
  262. printf("/tmp must be not mounted noexec\n");
  263. exit(1);
  264. }
  265. printf("OK\n");
  266. munmap(addr, UM_KERN_PAGE_SIZE);
  267. close(fd);
  268. }
  269. void os_early_checks(void)
  270. {
  271. check_ptrace();
  272. /* Need to check this early because mmapping happens before the
  273. * kernel is running.
  274. */
  275. check_tmpexec();
  276. }
  277. static int __init noprocmm_cmd_param(char *str, int* add)
  278. {
  279. proc_mm = 0;
  280. return 0;
  281. }
  282. __uml_setup("noprocmm", noprocmm_cmd_param,
  283. "noprocmm\n"
  284. " Turns off usage of /proc/mm, even if host supports it.\n"
  285. " To support /proc/mm, the host needs to be patched using\n"
  286. " the current skas3 patch.\n\n");
  287. static int __init noptracefaultinfo_cmd_param(char *str, int* add)
  288. {
  289. ptrace_faultinfo = 0;
  290. return 0;
  291. }
  292. __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
  293. "noptracefaultinfo\n"
  294. " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
  295. " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
  296. " using the current skas3 patch.\n\n");
  297. #ifdef UML_CONFIG_MODE_SKAS
  298. static inline void check_skas3_ptrace_support(void)
  299. {
  300. struct ptrace_faultinfo fi;
  301. void *stack;
  302. int pid, n;
  303. printf("Checking for the skas3 patch in the host...");
  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. int can_do_skas(void)
  323. {
  324. printf("Checking for /proc/mm...");
  325. if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
  326. proc_mm = 0;
  327. printf("not found\n");
  328. }
  329. else {
  330. if (!proc_mm)
  331. printf("found but disabled on command line\n");
  332. else
  333. printf("found\n");
  334. }
  335. check_skas3_ptrace_support();
  336. return 1;
  337. }
  338. #else
  339. int can_do_skas(void)
  340. {
  341. return(0);
  342. }
  343. #endif
  344. int have_devanon = 0;
  345. void check_devanon(void)
  346. {
  347. int fd;
  348. printk("Checking for /dev/anon on the host...");
  349. fd = open("/dev/anon", O_RDWR);
  350. if(fd < 0){
  351. printk("Not available (open failed with errno %d)\n", errno);
  352. return;
  353. }
  354. printk("OK\n");
  355. have_devanon = 1;
  356. }
  357. int __init parse_iomem(char *str, int *add)
  358. {
  359. struct iomem_region *new;
  360. struct uml_stat buf;
  361. char *file, *driver;
  362. int fd, err, size;
  363. driver = str;
  364. file = strchr(str,',');
  365. if(file == NULL){
  366. printf("parse_iomem : failed to parse iomem\n");
  367. goto out;
  368. }
  369. *file = '\0';
  370. file++;
  371. fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
  372. if(fd < 0){
  373. os_print_error(fd, "parse_iomem - Couldn't open io file");
  374. goto out;
  375. }
  376. err = os_stat_fd(fd, &buf);
  377. if(err < 0){
  378. os_print_error(err, "parse_iomem - cannot stat_fd file");
  379. goto out_close;
  380. }
  381. new = malloc(sizeof(*new));
  382. if(new == NULL){
  383. perror("Couldn't allocate iomem_region struct");
  384. goto out_close;
  385. }
  386. size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  387. *new = ((struct iomem_region) { .next = iomem_regions,
  388. .driver = driver,
  389. .fd = fd,
  390. .size = size,
  391. .phys = 0,
  392. .virt = 0 });
  393. iomem_regions = new;
  394. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  395. return(0);
  396. out_close:
  397. os_close_file(fd);
  398. out:
  399. return(1);
  400. }