main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
  4. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  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. #include "internal.h"
  19. #define PGD_BOUND (4 * 1024 * 1024)
  20. #define STACKSIZE (8 * 1024 * 1024)
  21. #define THREAD_NAME_LEN (256)
  22. long elf_aux_hwcap;
  23. static void set_stklim(void)
  24. {
  25. struct rlimit lim;
  26. if (getrlimit(RLIMIT_STACK, &lim) < 0) {
  27. perror("getrlimit");
  28. exit(1);
  29. }
  30. if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) {
  31. lim.rlim_cur = STACKSIZE;
  32. if (setrlimit(RLIMIT_STACK, &lim) < 0) {
  33. perror("setrlimit");
  34. exit(1);
  35. }
  36. }
  37. }
  38. static void last_ditch_exit(int sig)
  39. {
  40. uml_cleanup();
  41. exit(1);
  42. }
  43. static void install_fatal_handler(int sig)
  44. {
  45. struct sigaction action;
  46. /* All signals are enabled in this handler ... */
  47. sigemptyset(&action.sa_mask);
  48. /*
  49. * ... including the signal being handled, plus we want the
  50. * handler reset to the default behavior, so that if an exit
  51. * handler is hanging for some reason, the UML will just die
  52. * after this signal is sent a second time.
  53. */
  54. action.sa_flags = SA_RESETHAND | SA_NODEFER;
  55. action.sa_restorer = NULL;
  56. action.sa_handler = last_ditch_exit;
  57. if (sigaction(sig, &action, NULL) < 0) {
  58. os_warn("failed to install handler for signal %d "
  59. "- errno = %d\n", sig, errno);
  60. exit(1);
  61. }
  62. }
  63. #define UML_LIB_PATH ":" OS_LIB_PATH "/uml"
  64. static void setup_env_path(void)
  65. {
  66. char *new_path = NULL;
  67. char *old_path = NULL;
  68. int path_len = 0;
  69. old_path = getenv("PATH");
  70. /*
  71. * if no PATH variable is set or it has an empty value
  72. * just use the default + /usr/lib/uml
  73. */
  74. if (!old_path || (path_len = strlen(old_path)) == 0) {
  75. if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH))
  76. perror("couldn't putenv");
  77. return;
  78. }
  79. /* append /usr/lib/uml to the existing path */
  80. path_len += strlen("PATH=" UML_LIB_PATH) + 1;
  81. new_path = malloc(path_len);
  82. if (!new_path) {
  83. perror("couldn't malloc to set a new PATH");
  84. return;
  85. }
  86. snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
  87. if (putenv(new_path)) {
  88. perror("couldn't putenv to set a new PATH");
  89. free(new_path);
  90. }
  91. }
  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. /* workaround for -Wmissing-prototypes warnings */
  158. void *__wrap_malloc(int size);
  159. void *__wrap_calloc(int n, int size);
  160. void __wrap_free(void *ptr);
  161. void *__wrap_malloc(int size)
  162. {
  163. void *ret;
  164. if (!kmalloc_ok)
  165. return __real_malloc(size);
  166. else if (size <= UM_KERN_PAGE_SIZE)
  167. /* finding contiguous pages can be hard*/
  168. ret = uml_kmalloc(size, UM_GFP_KERNEL);
  169. else ret = vmalloc(size);
  170. /*
  171. * glibc people insist that if malloc fails, errno should be
  172. * set by malloc as well. So we do.
  173. */
  174. if (ret == NULL)
  175. errno = ENOMEM;
  176. return ret;
  177. }
  178. void *__wrap_calloc(int n, int size)
  179. {
  180. void *ptr = __wrap_malloc(n * size);
  181. if (ptr == NULL)
  182. return NULL;
  183. memset(ptr, 0, n * size);
  184. return ptr;
  185. }
  186. extern void __real_free(void *);
  187. extern unsigned long high_physmem;
  188. void __wrap_free(void *ptr)
  189. {
  190. unsigned long addr = (unsigned long) ptr;
  191. /*
  192. * We need to know how the allocation happened, so it can be correctly
  193. * freed. This is done by seeing what region of memory the pointer is
  194. * in -
  195. * physical memory - kmalloc/kfree
  196. * kernel virtual memory - vmalloc/vfree
  197. * anywhere else - malloc/free
  198. * If kmalloc is not yet possible, then either high_physmem and/or
  199. * end_vm are still 0 (as at startup), in which case we call free, or
  200. * we have set them, but anyway addr has not been allocated from those
  201. * areas. So, in both cases __real_free is called.
  202. *
  203. * CAN_KMALLOC is checked because it would be bad to free a buffer
  204. * with kmalloc/vmalloc after they have been turned off during
  205. * shutdown.
  206. * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
  207. * there is a possibility for memory leaks.
  208. */
  209. if ((addr >= uml_physmem) && (addr < high_physmem)) {
  210. if (kmalloc_ok)
  211. kfree(ptr);
  212. }
  213. else if ((addr >= start_vm) && (addr < end_vm)) {
  214. if (kmalloc_ok)
  215. vfree(ptr);
  216. }
  217. else __real_free(ptr);
  218. }