main.c 5.8 KB

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