main.c 6.7 KB

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