main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /* Set in set_stklim, which is called from main and __wrap_malloc.
  26. * __wrap_malloc only calls it if main hasn't started.
  27. */
  28. unsigned long stacksizelim;
  29. /* Set in main */
  30. char *linux_prog;
  31. #define PGD_BOUND (4 * 1024 * 1024)
  32. #define STACKSIZE (8 * 1024 * 1024)
  33. #define THREAD_NAME_LEN (256)
  34. static void set_stklim(void)
  35. {
  36. struct rlimit lim;
  37. if(getrlimit(RLIMIT_STACK, &lim) < 0){
  38. perror("getrlimit");
  39. exit(1);
  40. }
  41. if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
  42. lim.rlim_cur = STACKSIZE;
  43. if(setrlimit(RLIMIT_STACK, &lim) < 0){
  44. perror("setrlimit");
  45. exit(1);
  46. }
  47. }
  48. stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
  49. }
  50. static __init void do_uml_initcalls(void)
  51. {
  52. initcall_t *call;
  53. call = &__uml_initcall_start;
  54. while (call < &__uml_initcall_end){;
  55. (*call)();
  56. call++;
  57. }
  58. }
  59. static void last_ditch_exit(int sig)
  60. {
  61. signal(SIGINT, SIG_DFL);
  62. signal(SIGTERM, SIG_DFL);
  63. signal(SIGHUP, SIG_DFL);
  64. uml_cleanup();
  65. exit(1);
  66. }
  67. extern int uml_exitcode;
  68. extern void scan_elf_aux( char **envp);
  69. int main(int argc, char **argv, char **envp)
  70. {
  71. char **new_argv;
  72. sigset_t mask;
  73. int ret, i, err;
  74. /* Enable all signals except SIGIO - in some environments, we can
  75. * enter with some signals blocked
  76. */
  77. sigemptyset(&mask);
  78. sigaddset(&mask, SIGIO);
  79. if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
  80. perror("sigprocmask");
  81. exit(1);
  82. }
  83. #ifdef UML_CONFIG_CMDLINE_ON_HOST
  84. /* Allocate memory for thread command lines */
  85. if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
  86. char padding[THREAD_NAME_LEN] = {
  87. [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
  88. };
  89. new_argv = malloc((argc + 2) * sizeof(char*));
  90. if(!new_argv) {
  91. perror("Allocating extended argv");
  92. exit(1);
  93. }
  94. new_argv[0] = argv[0];
  95. new_argv[1] = padding;
  96. for(i = 2; i <= argc; i++)
  97. new_argv[i] = argv[i - 1];
  98. new_argv[argc + 1] = NULL;
  99. execvp(new_argv[0], new_argv);
  100. perror("execing with extended args");
  101. exit(1);
  102. }
  103. #endif
  104. linux_prog = argv[0];
  105. set_stklim();
  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. set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  120. set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  121. set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  122. scan_elf_aux( envp);
  123. do_uml_initcalls();
  124. ret = linux_main(argc, argv);
  125. /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
  126. * off the profiling time, but UML dies with a SIGPROF just before
  127. * exiting when profiling is active.
  128. */
  129. change_sig(SIGPROF, 0);
  130. /* This signal stuff used to be in the reboot case. However,
  131. * sometimes a SIGVTALRM 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 SIG*ALRM to be ignored */
  136. disable_timer();
  137. /* disable SIGIO for the fds and set SIGIO to be ignored */
  138. err = deactivate_all_fds();
  139. if(err)
  140. printf("deactivate_all_fds failed, errno = %d\n", -err);
  141. /* Let any pending signals fire now. This ensures
  142. * that they won't be delivered after the exec, when
  143. * they are definitely not expected.
  144. */
  145. unblock_signals();
  146. /* Reboot */
  147. if(ret){
  148. printf("\n");
  149. execvp(new_argv[0], new_argv);
  150. perror("Failed to exec kernel");
  151. ret = 1;
  152. }
  153. printf("\n");
  154. return(uml_exitcode);
  155. }
  156. #define CAN_KMALLOC() \
  157. (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
  158. extern void *__real_malloc(int);
  159. void *__wrap_malloc(int size)
  160. {
  161. void *ret;
  162. if(!CAN_KMALLOC())
  163. return(__real_malloc(size));
  164. else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
  165. ret = um_kmalloc(size);
  166. else ret = um_vmalloc(size);
  167. /* glibc people insist that if malloc fails, errno should be
  168. * set by malloc as well. So we do.
  169. */
  170. if(ret == NULL)
  171. errno = ENOMEM;
  172. return(ret);
  173. }
  174. void *__wrap_calloc(int n, int size)
  175. {
  176. void *ptr = __wrap_malloc(n * size);
  177. if(ptr == NULL) return(NULL);
  178. memset(ptr, 0, n * size);
  179. return(ptr);
  180. }
  181. extern void __real_free(void *);
  182. extern unsigned long high_physmem;
  183. void __wrap_free(void *ptr)
  184. {
  185. unsigned long addr = (unsigned long) ptr;
  186. /* We need to know how the allocation happened, so it can be correctly
  187. * freed. This is done by seeing what region of memory the pointer is
  188. * in -
  189. * physical memory - kmalloc/kfree
  190. * kernel virtual memory - vmalloc/vfree
  191. * anywhere else - malloc/free
  192. * If kmalloc is not yet possible, then either high_physmem and/or
  193. * end_vm are still 0 (as at startup), in which case we call free, or
  194. * we have set them, but anyway addr has not been allocated from those
  195. * areas. So, in both cases __real_free is called.
  196. *
  197. * CAN_KMALLOC is checked because it would be bad to free a buffer
  198. * with kmalloc/vmalloc after they have been turned off during
  199. * shutdown.
  200. * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
  201. * there is a possibility for memory leaks.
  202. */
  203. if((addr >= uml_physmem) && (addr < high_physmem)){
  204. if(CAN_KMALLOC())
  205. kfree(ptr);
  206. }
  207. else if((addr >= start_vm) && (addr < end_vm)){
  208. if(CAN_KMALLOC())
  209. vfree(ptr);
  210. }
  211. else __real_free(ptr);
  212. }