main.c 6.0 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 "irq_user.h"
  27. #include "time_user.h"
  28. #include "os.h"
  29. /* Set in set_stklim, which is called from main and __wrap_malloc.
  30. * __wrap_malloc only calls it if main hasn't started.
  31. */
  32. unsigned long stacksizelim;
  33. /* Set in main */
  34. char *linux_prog;
  35. #define PGD_BOUND (4 * 1024 * 1024)
  36. #define STACKSIZE (8 * 1024 * 1024)
  37. #define THREAD_NAME_LEN (256)
  38. static void set_stklim(void)
  39. {
  40. struct rlimit lim;
  41. if(getrlimit(RLIMIT_STACK, &lim) < 0){
  42. perror("getrlimit");
  43. exit(1);
  44. }
  45. if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
  46. lim.rlim_cur = STACKSIZE;
  47. if(setrlimit(RLIMIT_STACK, &lim) < 0){
  48. perror("setrlimit");
  49. exit(1);
  50. }
  51. }
  52. stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
  53. }
  54. static __init void do_uml_initcalls(void)
  55. {
  56. initcall_t *call;
  57. call = &__uml_initcall_start;
  58. while (call < &__uml_initcall_end){;
  59. (*call)();
  60. call++;
  61. }
  62. }
  63. static void last_ditch_exit(int sig)
  64. {
  65. CHOOSE_MODE(kmalloc_ok = 0, (void) 0);
  66. signal(SIGINT, SIG_DFL);
  67. signal(SIGTERM, SIG_DFL);
  68. signal(SIGHUP, SIG_DFL);
  69. uml_cleanup();
  70. exit(1);
  71. }
  72. extern int uml_exitcode;
  73. extern void scan_elf_aux( char **envp);
  74. int main(int argc, char **argv, char **envp)
  75. {
  76. char **new_argv;
  77. sigset_t mask;
  78. int ret, i;
  79. /* Enable all signals except SIGIO - in some environments, we can
  80. * enter with some signals blocked
  81. */
  82. sigemptyset(&mask);
  83. sigaddset(&mask, SIGIO);
  84. if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
  85. perror("sigprocmask");
  86. exit(1);
  87. }
  88. #ifdef UML_CONFIG_MODE_TT
  89. /* Allocate memory for thread command lines */
  90. if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
  91. char padding[THREAD_NAME_LEN] = {
  92. [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
  93. };
  94. new_argv = malloc((argc + 2) * sizeof(char*));
  95. if(!new_argv) {
  96. perror("Allocating extended argv");
  97. exit(1);
  98. }
  99. new_argv[0] = argv[0];
  100. new_argv[1] = padding;
  101. for(i = 2; i <= argc; i++)
  102. new_argv[i] = argv[i - 1];
  103. new_argv[argc + 1] = NULL;
  104. execvp(new_argv[0], new_argv);
  105. perror("execing with extended args");
  106. exit(1);
  107. }
  108. #endif
  109. linux_prog = argv[0];
  110. set_stklim();
  111. new_argv = malloc((argc + 1) * sizeof(char *));
  112. if(new_argv == NULL){
  113. perror("Mallocing argv");
  114. exit(1);
  115. }
  116. for(i=0;i<argc;i++){
  117. new_argv[i] = strdup(argv[i]);
  118. if(new_argv[i] == NULL){
  119. perror("Mallocing an arg");
  120. exit(1);
  121. }
  122. }
  123. new_argv[argc] = NULL;
  124. set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  125. set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  126. set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
  127. scan_elf_aux( envp);
  128. do_uml_initcalls();
  129. ret = linux_main(argc, argv);
  130. /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
  131. * off the profiling time, but UML dies with a SIGPROF just before
  132. * exiting when profiling is active.
  133. */
  134. change_sig(SIGPROF, 0);
  135. /* Reboot */
  136. if(ret){
  137. int err;
  138. printf("\n");
  139. /* stop timers and set SIG*ALRM to be ignored */
  140. disable_timer();
  141. /* disable SIGIO for the fds and set SIGIO to be ignored */
  142. err = deactivate_all_fds();
  143. if(err)
  144. printf("deactivate_all_fds failed, errno = %d\n",
  145. -err);
  146. /* Let any pending signals fire now. This ensures
  147. * that they won't be delivered after the exec, when
  148. * they are definitely not expected.
  149. */
  150. unblock_signals();
  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. */