main.c 6.3 KB

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