main.c 5.9 KB

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