main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. int ret, i, err;
  73. #ifdef UML_CONFIG_CMDLINE_ON_HOST
  74. /* Allocate memory for thread command lines */
  75. if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
  76. char padding[THREAD_NAME_LEN] = {
  77. [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
  78. };
  79. new_argv = malloc((argc + 2) * sizeof(char*));
  80. if(!new_argv) {
  81. perror("Allocating extended argv");
  82. exit(1);
  83. }
  84. new_argv[0] = argv[0];
  85. new_argv[1] = padding;
  86. for(i = 2; i <= argc; i++)
  87. new_argv[i] = argv[i - 1];
  88. new_argv[argc + 1] = NULL;
  89. execvp(new_argv[0], new_argv);
  90. perror("execing with extended args");
  91. exit(1);
  92. }
  93. #endif
  94. linux_prog = argv[0];
  95. set_stklim();
  96. new_argv = malloc((argc + 1) * sizeof(char *));
  97. if(new_argv == NULL){
  98. perror("Mallocing argv");
  99. exit(1);
  100. }
  101. for(i=0;i<argc;i++){
  102. new_argv[i] = strdup(argv[i]);
  103. if(new_argv[i] == NULL){
  104. perror("Mallocing an arg");
  105. exit(1);
  106. }
  107. }
  108. new_argv[argc] = NULL;
  109. set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  110. set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  111. set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  112. scan_elf_aux( envp);
  113. do_uml_initcalls();
  114. ret = linux_main(argc, argv);
  115. /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
  116. * off the profiling time, but UML dies with a SIGPROF just before
  117. * exiting when profiling is active.
  118. */
  119. change_sig(SIGPROF, 0);
  120. /* This signal stuff used to be in the reboot case. However,
  121. * sometimes a SIGVTALRM can come in when we're halting (reproducably
  122. * when writing out gcov information, presumably because that takes
  123. * some time) and cause a segfault.
  124. */
  125. /* stop timers and set SIG*ALRM to be ignored */
  126. disable_timer();
  127. /* disable SIGIO for the fds and set SIGIO to be ignored */
  128. err = deactivate_all_fds();
  129. if(err)
  130. printf("deactivate_all_fds failed, errno = %d\n", -err);
  131. /* Let any pending signals fire now. This ensures
  132. * that they won't be delivered after the exec, when
  133. * they are definitely not expected.
  134. */
  135. unblock_signals();
  136. /* Reboot */
  137. if(ret){
  138. printf("\n");
  139. execvp(new_argv[0], new_argv);
  140. perror("Failed to exec kernel");
  141. ret = 1;
  142. }
  143. printf("\n");
  144. return(uml_exitcode);
  145. }
  146. #define CAN_KMALLOC() \
  147. (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
  148. extern void *__real_malloc(int);
  149. void *__wrap_malloc(int size)
  150. {
  151. void *ret;
  152. if(!CAN_KMALLOC())
  153. return(__real_malloc(size));
  154. else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
  155. ret = um_kmalloc(size);
  156. else ret = um_vmalloc(size);
  157. /* glibc people insist that if malloc fails, errno should be
  158. * set by malloc as well. So we do.
  159. */
  160. if(ret == NULL)
  161. errno = ENOMEM;
  162. return(ret);
  163. }
  164. void *__wrap_calloc(int n, int size)
  165. {
  166. void *ptr = __wrap_malloc(n * size);
  167. if(ptr == NULL) return(NULL);
  168. memset(ptr, 0, n * size);
  169. return(ptr);
  170. }
  171. extern void __real_free(void *);
  172. extern unsigned long high_physmem;
  173. void __wrap_free(void *ptr)
  174. {
  175. unsigned long addr = (unsigned long) ptr;
  176. /* We need to know how the allocation happened, so it can be correctly
  177. * freed. This is done by seeing what region of memory the pointer is
  178. * in -
  179. * physical memory - kmalloc/kfree
  180. * kernel virtual memory - vmalloc/vfree
  181. * anywhere else - malloc/free
  182. * If kmalloc is not yet possible, then either high_physmem and/or
  183. * end_vm are still 0 (as at startup), in which case we call free, or
  184. * we have set them, but anyway addr has not been allocated from those
  185. * areas. So, in both cases __real_free is called.
  186. *
  187. * CAN_KMALLOC is checked because it would be bad to free a buffer
  188. * with kmalloc/vmalloc after they have been turned off during
  189. * shutdown.
  190. * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
  191. * there is a possibility for memory leaks.
  192. */
  193. if((addr >= uml_physmem) && (addr < high_physmem)){
  194. if(CAN_KMALLOC())
  195. kfree(ptr);
  196. }
  197. else if((addr >= start_vm) && (addr < end_vm)){
  198. if(CAN_KMALLOC())
  199. vfree(ptr);
  200. }
  201. else __real_free(ptr);
  202. }