main.c 6.3 KB

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