xref: /openbmc/linux/arch/um/os-Linux/main.c (revision f2183125)
152c653b3SJeff Dike /*
252c653b3SJeff Dike  * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
352c653b3SJeff Dike  * Licensed under the GPL
452c653b3SJeff Dike  */
552c653b3SJeff Dike 
652c653b3SJeff Dike #include <unistd.h>
752c653b3SJeff Dike #include <stdio.h>
852c653b3SJeff Dike #include <stdlib.h>
952c653b3SJeff Dike #include <string.h>
1052c653b3SJeff Dike #include <signal.h>
1152c653b3SJeff Dike #include <errno.h>
1252c653b3SJeff Dike #include <sys/resource.h>
1352c653b3SJeff Dike #include <sys/mman.h>
1452c653b3SJeff Dike #include <sys/user.h>
1552c653b3SJeff Dike #include <asm/page.h>
1652c653b3SJeff Dike #include "user_util.h"
1752c653b3SJeff Dike #include "kern_util.h"
1852c653b3SJeff Dike #include "mem_user.h"
1952c653b3SJeff Dike #include "irq_user.h"
2052c653b3SJeff Dike #include "user.h"
2152c653b3SJeff Dike #include "init.h"
2252c653b3SJeff Dike #include "mode.h"
2352c653b3SJeff Dike #include "choose-mode.h"
2452c653b3SJeff Dike #include "uml-config.h"
2552c653b3SJeff Dike #include "os.h"
2652c653b3SJeff Dike 
2752c653b3SJeff Dike /* Set in set_stklim, which is called from main and __wrap_malloc.
2852c653b3SJeff Dike  * __wrap_malloc only calls it if main hasn't started.
2952c653b3SJeff Dike  */
3052c653b3SJeff Dike unsigned long stacksizelim;
3152c653b3SJeff Dike 
3252c653b3SJeff Dike /* Set in main */
3352c653b3SJeff Dike char *linux_prog;
3452c653b3SJeff Dike 
3552c653b3SJeff Dike #define PGD_BOUND (4 * 1024 * 1024)
3652c653b3SJeff Dike #define STACKSIZE (8 * 1024 * 1024)
3752c653b3SJeff Dike #define THREAD_NAME_LEN (256)
3852c653b3SJeff Dike 
3952c653b3SJeff Dike static void set_stklim(void)
4052c653b3SJeff Dike {
4152c653b3SJeff Dike 	struct rlimit lim;
4252c653b3SJeff Dike 
4352c653b3SJeff Dike 	if(getrlimit(RLIMIT_STACK, &lim) < 0){
4452c653b3SJeff Dike 		perror("getrlimit");
4552c653b3SJeff Dike 		exit(1);
4652c653b3SJeff Dike 	}
4752c653b3SJeff Dike 	if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
4852c653b3SJeff Dike 		lim.rlim_cur = STACKSIZE;
4952c653b3SJeff Dike 		if(setrlimit(RLIMIT_STACK, &lim) < 0){
5052c653b3SJeff Dike 			perror("setrlimit");
5152c653b3SJeff Dike 			exit(1);
5252c653b3SJeff Dike 		}
5352c653b3SJeff Dike 	}
5452c653b3SJeff Dike 	stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
5552c653b3SJeff Dike }
5652c653b3SJeff Dike 
5752c653b3SJeff Dike static __init void do_uml_initcalls(void)
5852c653b3SJeff Dike {
5952c653b3SJeff Dike 	initcall_t *call;
6052c653b3SJeff Dike 
6152c653b3SJeff Dike 	call = &__uml_initcall_start;
62f2183125SJeff Dike 	while (call < &__uml_initcall_end){
6352c653b3SJeff Dike 		(*call)();
6452c653b3SJeff Dike 		call++;
6552c653b3SJeff Dike 	}
6652c653b3SJeff Dike }
6752c653b3SJeff Dike 
6852c653b3SJeff Dike static void last_ditch_exit(int sig)
6952c653b3SJeff Dike {
7052c653b3SJeff Dike 	signal(SIGINT, SIG_DFL);
7152c653b3SJeff Dike 	signal(SIGTERM, SIG_DFL);
7252c653b3SJeff Dike 	signal(SIGHUP, SIG_DFL);
7352c653b3SJeff Dike 	uml_cleanup();
7452c653b3SJeff Dike 	exit(1);
7552c653b3SJeff Dike }
7652c653b3SJeff Dike 
77cb98cdcdSMattia Dongili #define UML_LIB_PATH	":/usr/lib/uml"
78cb98cdcdSMattia Dongili 
79cb98cdcdSMattia Dongili static void setup_env_path(void)
80cb98cdcdSMattia Dongili {
81cb98cdcdSMattia Dongili 	char *new_path = NULL;
82cb98cdcdSMattia Dongili 	char *old_path = NULL;
83cb98cdcdSMattia Dongili 	int path_len = 0;
84cb98cdcdSMattia Dongili 
85cb98cdcdSMattia Dongili 	old_path = getenv("PATH");
86cb98cdcdSMattia Dongili 	/* if no PATH variable is set or it has an empty value
87cb98cdcdSMattia Dongili 	 * just use the default + /usr/lib/uml
88cb98cdcdSMattia Dongili 	 */
89cb98cdcdSMattia Dongili 	if (!old_path || (path_len = strlen(old_path)) == 0) {
90cb98cdcdSMattia Dongili 		putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
91cb98cdcdSMattia Dongili 		return;
92cb98cdcdSMattia Dongili 	}
93cb98cdcdSMattia Dongili 
94cb98cdcdSMattia Dongili 	/* append /usr/lib/uml to the existing path */
95cb98cdcdSMattia Dongili 	path_len += strlen("PATH=" UML_LIB_PATH) + 1;
96cb98cdcdSMattia Dongili 	new_path = malloc(path_len);
97cb98cdcdSMattia Dongili 	if (!new_path) {
98cb98cdcdSMattia Dongili 		perror("coudn't malloc to set a new PATH");
99cb98cdcdSMattia Dongili 		return;
100cb98cdcdSMattia Dongili 	}
101cb98cdcdSMattia Dongili 	snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
102cb98cdcdSMattia Dongili 	putenv(new_path);
103cb98cdcdSMattia Dongili }
104cb98cdcdSMattia Dongili 
10552c653b3SJeff Dike extern int uml_exitcode;
10652c653b3SJeff Dike 
10752c653b3SJeff Dike extern void scan_elf_aux( char **envp);
10852c653b3SJeff Dike 
10952c653b3SJeff Dike int main(int argc, char **argv, char **envp)
11052c653b3SJeff Dike {
11152c653b3SJeff Dike 	char **new_argv;
11252c653b3SJeff Dike 	int ret, i, err;
11352c653b3SJeff Dike 
11452c653b3SJeff Dike #ifdef UML_CONFIG_CMDLINE_ON_HOST
11552c653b3SJeff Dike 	/* Allocate memory for thread command lines */
11652c653b3SJeff Dike 	if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
11752c653b3SJeff Dike 
11852c653b3SJeff Dike 		char padding[THREAD_NAME_LEN] = {
11952c653b3SJeff Dike 			[ 0 ...  THREAD_NAME_LEN - 2] = ' ', '\0'
12052c653b3SJeff Dike 		};
12152c653b3SJeff Dike 
12252c653b3SJeff Dike 		new_argv = malloc((argc + 2) * sizeof(char*));
12352c653b3SJeff Dike 		if(!new_argv) {
12452c653b3SJeff Dike 			perror("Allocating extended argv");
12552c653b3SJeff Dike 			exit(1);
12652c653b3SJeff Dike 		}
12752c653b3SJeff Dike 
12852c653b3SJeff Dike 		new_argv[0] = argv[0];
12952c653b3SJeff Dike 		new_argv[1] = padding;
13052c653b3SJeff Dike 
13152c653b3SJeff Dike 		for(i = 2; i <= argc; i++)
13252c653b3SJeff Dike 			new_argv[i] = argv[i - 1];
13352c653b3SJeff Dike 		new_argv[argc + 1] = NULL;
13452c653b3SJeff Dike 
13552c653b3SJeff Dike 		execvp(new_argv[0], new_argv);
13652c653b3SJeff Dike 		perror("execing with extended args");
13752c653b3SJeff Dike 		exit(1);
13852c653b3SJeff Dike 	}
13952c653b3SJeff Dike #endif
14052c653b3SJeff Dike 
14152c653b3SJeff Dike 	linux_prog = argv[0];
14252c653b3SJeff Dike 
14352c653b3SJeff Dike 	set_stklim();
14452c653b3SJeff Dike 
145cb98cdcdSMattia Dongili 	setup_env_path();
146cb98cdcdSMattia Dongili 
14752c653b3SJeff Dike 	new_argv = malloc((argc + 1) * sizeof(char *));
14852c653b3SJeff Dike 	if(new_argv == NULL){
14952c653b3SJeff Dike 		perror("Mallocing argv");
15052c653b3SJeff Dike 		exit(1);
15152c653b3SJeff Dike 	}
15252c653b3SJeff Dike 	for(i=0;i<argc;i++){
15352c653b3SJeff Dike 		new_argv[i] = strdup(argv[i]);
15452c653b3SJeff Dike 		if(new_argv[i] == NULL){
15552c653b3SJeff Dike 			perror("Mallocing an arg");
15652c653b3SJeff Dike 			exit(1);
15752c653b3SJeff Dike 		}
15852c653b3SJeff Dike 	}
15952c653b3SJeff Dike 	new_argv[argc] = NULL;
16052c653b3SJeff Dike 
16152c653b3SJeff Dike 	set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
16252c653b3SJeff Dike 	set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
16352c653b3SJeff Dike 	set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
16452c653b3SJeff Dike 
16552c653b3SJeff Dike 	scan_elf_aux( envp);
16652c653b3SJeff Dike 
16752c653b3SJeff Dike 	do_uml_initcalls();
16852c653b3SJeff Dike 	ret = linux_main(argc, argv);
16952c653b3SJeff Dike 
17052c653b3SJeff Dike 	/* Disable SIGPROF - I have no idea why libc doesn't do this or turn
17152c653b3SJeff Dike 	 * off the profiling time, but UML dies with a SIGPROF just before
17252c653b3SJeff Dike 	 * exiting when profiling is active.
17352c653b3SJeff Dike 	 */
17452c653b3SJeff Dike 	change_sig(SIGPROF, 0);
17552c653b3SJeff Dike 
17652c653b3SJeff Dike 	/* This signal stuff used to be in the reboot case.  However,
17752c653b3SJeff Dike 	 * sometimes a SIGVTALRM can come in when we're halting (reproducably
17852c653b3SJeff Dike 	 * when writing out gcov information, presumably because that takes
17952c653b3SJeff Dike 	 * some time) and cause a segfault.
18052c653b3SJeff Dike 	 */
18152c653b3SJeff Dike 
18252c653b3SJeff Dike 	/* stop timers and set SIG*ALRM to be ignored */
18352c653b3SJeff Dike 	disable_timer();
18452c653b3SJeff Dike 
18552c653b3SJeff Dike 	/* disable SIGIO for the fds and set SIGIO to be ignored */
18652c653b3SJeff Dike 	err = deactivate_all_fds();
18752c653b3SJeff Dike 	if(err)
18852c653b3SJeff Dike 		printf("deactivate_all_fds failed, errno = %d\n", -err);
18952c653b3SJeff Dike 
19052c653b3SJeff Dike 	/* Let any pending signals fire now.  This ensures
19152c653b3SJeff Dike 	 * that they won't be delivered after the exec, when
19252c653b3SJeff Dike 	 * they are definitely not expected.
19352c653b3SJeff Dike 	 */
19452c653b3SJeff Dike 	unblock_signals();
19552c653b3SJeff Dike 
19652c653b3SJeff Dike 	/* Reboot */
19752c653b3SJeff Dike 	if(ret){
19852c653b3SJeff Dike 		printf("\n");
19952c653b3SJeff Dike 		execvp(new_argv[0], new_argv);
20052c653b3SJeff Dike 		perror("Failed to exec kernel");
20152c653b3SJeff Dike 		ret = 1;
20252c653b3SJeff Dike 	}
20352c653b3SJeff Dike 	printf("\n");
20452c653b3SJeff Dike 	return(uml_exitcode);
20552c653b3SJeff Dike }
20652c653b3SJeff Dike 
20752c653b3SJeff Dike #define CAN_KMALLOC() \
20852c653b3SJeff Dike 	(kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
20952c653b3SJeff Dike 
21052c653b3SJeff Dike extern void *__real_malloc(int);
21152c653b3SJeff Dike 
21252c653b3SJeff Dike void *__wrap_malloc(int size)
21352c653b3SJeff Dike {
21452c653b3SJeff Dike 	void *ret;
21552c653b3SJeff Dike 
21652c653b3SJeff Dike 	if(!CAN_KMALLOC())
21752c653b3SJeff Dike 		return(__real_malloc(size));
21852c653b3SJeff Dike 	else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
21952c653b3SJeff Dike 		ret = um_kmalloc(size);
22052c653b3SJeff Dike 	else ret = um_vmalloc(size);
22152c653b3SJeff Dike 
22252c653b3SJeff Dike 	/* glibc people insist that if malloc fails, errno should be
22352c653b3SJeff Dike 	 * set by malloc as well. So we do.
22452c653b3SJeff Dike 	 */
22552c653b3SJeff Dike 	if(ret == NULL)
22652c653b3SJeff Dike 		errno = ENOMEM;
22752c653b3SJeff Dike 
22852c653b3SJeff Dike 	return(ret);
22952c653b3SJeff Dike }
23052c653b3SJeff Dike 
23152c653b3SJeff Dike void *__wrap_calloc(int n, int size)
23252c653b3SJeff Dike {
23352c653b3SJeff Dike 	void *ptr = __wrap_malloc(n * size);
23452c653b3SJeff Dike 
23552c653b3SJeff Dike 	if(ptr == NULL) return(NULL);
23652c653b3SJeff Dike 	memset(ptr, 0, n * size);
23752c653b3SJeff Dike 	return(ptr);
23852c653b3SJeff Dike }
23952c653b3SJeff Dike 
24052c653b3SJeff Dike extern void __real_free(void *);
24152c653b3SJeff Dike 
24252c653b3SJeff Dike extern unsigned long high_physmem;
24352c653b3SJeff Dike 
24452c653b3SJeff Dike void __wrap_free(void *ptr)
24552c653b3SJeff Dike {
24652c653b3SJeff Dike 	unsigned long addr = (unsigned long) ptr;
24752c653b3SJeff Dike 
24852c653b3SJeff Dike 	/* We need to know how the allocation happened, so it can be correctly
24952c653b3SJeff Dike 	 * freed.  This is done by seeing what region of memory the pointer is
25052c653b3SJeff Dike 	 * in -
25152c653b3SJeff Dike 	 * 	physical memory - kmalloc/kfree
25252c653b3SJeff Dike 	 *	kernel virtual memory - vmalloc/vfree
25352c653b3SJeff Dike 	 * 	anywhere else - malloc/free
25452c653b3SJeff Dike 	 * If kmalloc is not yet possible, then either high_physmem and/or
25552c653b3SJeff Dike 	 * end_vm are still 0 (as at startup), in which case we call free, or
25652c653b3SJeff Dike 	 * we have set them, but anyway addr has not been allocated from those
25752c653b3SJeff Dike 	 * areas. So, in both cases __real_free is called.
25852c653b3SJeff Dike 	 *
25952c653b3SJeff Dike 	 * CAN_KMALLOC is checked because it would be bad to free a buffer
26052c653b3SJeff Dike 	 * with kmalloc/vmalloc after they have been turned off during
26152c653b3SJeff Dike 	 * shutdown.
26252c653b3SJeff Dike 	 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
26352c653b3SJeff Dike 	 * there is a possibility for memory leaks.
26452c653b3SJeff Dike 	 */
26552c653b3SJeff Dike 
26652c653b3SJeff Dike 	if((addr >= uml_physmem) && (addr < high_physmem)){
26752c653b3SJeff Dike 		if(CAN_KMALLOC())
26852c653b3SJeff Dike 			kfree(ptr);
26952c653b3SJeff Dike 	}
27052c653b3SJeff Dike 	else if((addr >= start_vm) && (addr < end_vm)){
27152c653b3SJeff Dike 		if(CAN_KMALLOC())
27252c653b3SJeff Dike 			vfree(ptr);
27352c653b3SJeff Dike 	}
27452c653b3SJeff Dike 	else __real_free(ptr);
27552c653b3SJeff Dike }
276