main.c 6.5 KB

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