main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
  3. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Licensed under the GPL
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <string.h>
  12. #include <sys/resource.h>
  13. #include <as-layout.h>
  14. #include <init.h>
  15. #include <kern_util.h>
  16. #include <os.h>
  17. #include <um_malloc.h>
  18. #define PGD_BOUND (4 * 1024 * 1024)
  19. #define STACKSIZE (8 * 1024 * 1024)
  20. #define THREAD_NAME_LEN (256)
  21. long elf_aux_hwcap;
  22. static void set_stklim(void)
  23. {
  24. struct rlimit lim;
  25. if (getrlimit(RLIMIT_STACK, &lim) < 0) {
  26. perror("getrlimit");
  27. exit(1);
  28. }
  29. if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) {
  30. lim.rlim_cur = STACKSIZE;
  31. if (setrlimit(RLIMIT_STACK, &lim) < 0) {
  32. perror("setrlimit");
  33. exit(1);
  34. }
  35. }
  36. }
  37. static void last_ditch_exit(int sig)
  38. {
  39. uml_cleanup();
  40. exit(1);
  41. }
  42. static void install_fatal_handler(int sig)
  43. {
  44. struct sigaction action;
  45. /* All signals are enabled in this handler ... */
  46. sigemptyset(&action.sa_mask);
  47. /*
  48. * ... including the signal being handled, plus we want the
  49. * handler reset to the default behavior, so that if an exit
  50. * handler is hanging for some reason, the UML will just die
  51. * after this signal is sent a second time.
  52. */
  53. action.sa_flags = SA_RESETHAND | SA_NODEFER;
  54. action.sa_restorer = NULL;
  55. action.sa_handler = last_ditch_exit;
  56. if (sigaction(sig, &action, NULL) < 0) {
  57. os_warn("failed to install handler for signal %d "
  58. "- errno = %d\n", sig, errno);
  59. exit(1);
  60. }
  61. }
  62. #define UML_LIB_PATH ":" OS_LIB_PATH "/uml"
  63. static void setup_env_path(void)
  64. {
  65. char *new_path = NULL;
  66. char *old_path = NULL;
  67. int path_len = 0;
  68. old_path = getenv("PATH");
  69. /*
  70. * if no PATH variable is set or it has an empty value
  71. * just use the default + /usr/lib/uml
  72. */
  73. if (!old_path || (path_len = strlen(old_path)) == 0) {
  74. if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH))
  75. perror("couldn't putenv");
  76. return;
  77. }
  78. /* append /usr/lib/uml to the existing path */
  79. path_len += strlen("PATH=" UML_LIB_PATH) + 1;
  80. new_path = malloc(path_len);
  81. if (!new_path) {
  82. perror("couldn't malloc to set a new PATH");
  83. return;
  84. }
  85. snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
  86. if (putenv(new_path)) {
  87. perror("couldn't putenv to set a new PATH");
  88. free(new_path);
  89. }
  90. }
  91. extern void scan_elf_aux( char **envp);
  92. int __init main(int argc, char **argv, char **envp)
  93. {
  94. char **new_argv;
  95. int ret, i, err;
  96. set_stklim();
  97. setup_env_path();
  98. setsid();
  99. new_argv = malloc((argc + 1) * sizeof(char *));
  100. if (new_argv == NULL) {
  101. perror("Mallocing argv");
  102. exit(1);
  103. }
  104. for (i = 0; i < argc; i++) {
  105. new_argv[i] = strdup(argv[i]);
  106. if (new_argv[i] == NULL) {
  107. perror("Mallocing an arg");
  108. exit(1);
  109. }
  110. }
  111. new_argv[argc] = NULL;
  112. /*
  113. * Allow these signals to bring down a UML if all other
  114. * methods of control fail.
  115. */
  116. install_fatal_handler(SIGINT);
  117. install_fatal_handler(SIGTERM);
  118. #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
  119. scan_elf_aux(envp);
  120. #endif
  121. change_sig(SIGPIPE, 0);
  122. ret = linux_main(argc, argv);
  123. /*
  124. * Disable SIGPROF - I have no idea why libc doesn't do this or turn
  125. * off the profiling time, but UML dies with a SIGPROF just before
  126. * exiting when profiling is active.
  127. */
  128. change_sig(SIGPROF, 0);
  129. /*
  130. * This signal stuff used to be in the reboot case. However,
  131. * sometimes a timer signal can come in when we're halting (reproducably
  132. * when writing out gcov information, presumably because that takes
  133. * some time) and cause a segfault.
  134. */
  135. /* stop timers and set timer signal to be ignored */
  136. os_timer_disable();
  137. /* disable SIGIO for the fds and set SIGIO to be ignored */
  138. err = deactivate_all_fds();
  139. if (err)
  140. os_warn("deactivate_all_fds failed, errno = %d\n", -err);
  141. /*
  142. * Let any pending signals fire now. This ensures
  143. * that they won't be delivered after the exec, when
  144. * they are definitely not expected.
  145. */
  146. unblock_signals();
  147. os_info("\n");
  148. /* Reboot */
  149. if (ret) {
  150. execvp(new_argv[0], new_argv);
  151. perror("Failed to exec kernel");
  152. ret = 1;
  153. }
  154. return uml_exitcode;
  155. }
  156. extern void *__real_malloc(int);
  157. void *__wrap_malloc(int size)
  158. {
  159. void *ret;
  160. if (!kmalloc_ok)
  161. return __real_malloc(size);
  162. else if (size <= UM_KERN_PAGE_SIZE)
  163. /* finding contiguous pages can be hard*/
  164. ret = uml_kmalloc(size, UM_GFP_KERNEL);
  165. else ret = vmalloc(size);
  166. /*
  167. * glibc people insist that if malloc fails, errno should be
  168. * set by malloc as well. So we do.
  169. */
  170. if (ret == NULL)
  171. errno = ENOMEM;
  172. return ret;
  173. }
  174. void *__wrap_calloc(int n, int size)
  175. {
  176. void *ptr = __wrap_malloc(n * size);
  177. if (ptr == NULL)
  178. return NULL;
  179. memset(ptr, 0, n * size);
  180. return ptr;
  181. }
  182. extern void __real_free(void *);
  183. extern unsigned long high_physmem;
  184. void __wrap_free(void *ptr)
  185. {
  186. unsigned long addr = (unsigned long) ptr;
  187. /*
  188. * We need to know how the allocation happened, so it can be correctly
  189. * freed. This is done by seeing what region of memory the pointer is
  190. * in -
  191. * physical memory - kmalloc/kfree
  192. * kernel virtual memory - vmalloc/vfree
  193. * anywhere else - malloc/free
  194. * If kmalloc is not yet possible, then either high_physmem and/or
  195. * end_vm are still 0 (as at startup), in which case we call free, or
  196. * we have set them, but anyway addr has not been allocated from those
  197. * areas. So, in both cases __real_free is called.
  198. *
  199. * CAN_KMALLOC is checked because it would be bad to free a buffer
  200. * with kmalloc/vmalloc after they have been turned off during
  201. * shutdown.
  202. * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
  203. * there is a possibility for memory leaks.
  204. */
  205. if ((addr >= uml_physmem) && (addr < high_physmem)) {
  206. if (kmalloc_ok)
  207. kfree(ptr);
  208. }
  209. else if ((addr >= start_vm) && (addr < end_vm)) {
  210. if (kmalloc_ok)
  211. vfree(ptr);
  212. }
  213. else __real_free(ptr);
  214. }