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