main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <signal.h>
  10. #include <string.h>
  11. #include <sys/resource.h>
  12. #include "as-layout.h"
  13. #include "init.h"
  14. #include "kern_constants.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. new_argv = malloc((argc + 1) * sizeof(char *));
  108. if (new_argv == NULL) {
  109. perror("Mallocing argv");
  110. exit(1);
  111. }
  112. for (i = 0; i < argc; i++) {
  113. new_argv[i] = strdup(argv[i]);
  114. if (new_argv[i] == NULL) {
  115. perror("Mallocing an arg");
  116. exit(1);
  117. }
  118. }
  119. new_argv[argc] = NULL;
  120. /*
  121. * Allow these signals to bring down a UML if all other
  122. * methods of control fail.
  123. */
  124. install_fatal_handler(SIGINT);
  125. install_fatal_handler(SIGTERM);
  126. #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
  127. scan_elf_aux(envp);
  128. #endif
  129. do_uml_initcalls();
  130. ret = linux_main(argc, argv);
  131. /*
  132. * Disable SIGPROF - I have no idea why libc doesn't do this or turn
  133. * off the profiling time, but UML dies with a SIGPROF just before
  134. * exiting when profiling is active.
  135. */
  136. change_sig(SIGPROF, 0);
  137. /*
  138. * This signal stuff used to be in the reboot case. However,
  139. * sometimes a SIGVTALRM can come in when we're halting (reproducably
  140. * when writing out gcov information, presumably because that takes
  141. * some time) and cause a segfault.
  142. */
  143. /* stop timers and set SIGVTALRM to be ignored */
  144. disable_timer();
  145. /* disable SIGIO for the fds and set SIGIO to be ignored */
  146. err = deactivate_all_fds();
  147. if (err)
  148. printf("deactivate_all_fds failed, errno = %d\n", -err);
  149. /*
  150. * Let any pending signals fire now. This ensures
  151. * that they won't be delivered after the exec, when
  152. * they are definitely not expected.
  153. */
  154. unblock_signals();
  155. /* Reboot */
  156. if (ret) {
  157. printf("\n");
  158. execvp(new_argv[0], new_argv);
  159. perror("Failed to exec kernel");
  160. ret = 1;
  161. }
  162. printf("\n");
  163. return uml_exitcode;
  164. }
  165. extern void *__real_malloc(int);
  166. void *__wrap_malloc(int size)
  167. {
  168. void *ret;
  169. if (!kmalloc_ok)
  170. return __real_malloc(size);
  171. else if (size <= UM_KERN_PAGE_SIZE)
  172. /* finding contiguous pages can be hard*/
  173. ret = uml_kmalloc(size, UM_GFP_KERNEL);
  174. else ret = vmalloc(size);
  175. /*
  176. * glibc people insist that if malloc fails, errno should be
  177. * set by malloc as well. So we do.
  178. */
  179. if (ret == NULL)
  180. errno = ENOMEM;
  181. return ret;
  182. }
  183. void *__wrap_calloc(int n, int size)
  184. {
  185. void *ptr = __wrap_malloc(n * size);
  186. if (ptr == NULL)
  187. return NULL;
  188. memset(ptr, 0, n * size);
  189. return ptr;
  190. }
  191. extern void __real_free(void *);
  192. extern unsigned long high_physmem;
  193. void __wrap_free(void *ptr)
  194. {
  195. unsigned long addr = (unsigned long) ptr;
  196. /*
  197. * We need to know how the allocation happened, so it can be correctly
  198. * freed. This is done by seeing what region of memory the pointer is
  199. * in -
  200. * physical memory - kmalloc/kfree
  201. * kernel virtual memory - vmalloc/vfree
  202. * anywhere else - malloc/free
  203. * If kmalloc is not yet possible, then either high_physmem and/or
  204. * end_vm are still 0 (as at startup), in which case we call free, or
  205. * we have set them, but anyway addr has not been allocated from those
  206. * areas. So, in both cases __real_free is called.
  207. *
  208. * CAN_KMALLOC is checked because it would be bad to free a buffer
  209. * with kmalloc/vmalloc after they have been turned off during
  210. * shutdown.
  211. * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
  212. * there is a possibility for memory leaks.
  213. */
  214. if ((addr >= uml_physmem) && (addr < high_physmem)) {
  215. if (kmalloc_ok)
  216. kfree(ptr);
  217. }
  218. else if ((addr >= start_vm) && (addr < end_vm)) {
  219. if (kmalloc_ok)
  220. vfree(ptr);
  221. }
  222. else __real_free(ptr);
  223. }