xref: /original-bsd/lib/csu/tahoe/crt0.c (revision d364528e)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)crt0.c	5.4 (Berkeley) 07/16/92";
10 #endif /* not lint */
11 
12 /*
13  *	C start up routine.
14  *	Robert Henry, UCB, 20 Oct 81
15  *
16  *	We make the following (true) assumptions:
17  *	1) When the kernel calls start, it does a jump to location 2,
18  *	and thus avoids the register save mask.  We are NOT called
19  *	with a calls!
20  *	2) The only register variable that we can trust is sp,
21  *	which points to the base of the kernel calling frame.
22  */
23 
24 char **environ = (char **)0;
25 static int fd;
26 
27 extern	unsigned char	etext;
28 extern	unsigned char	eprol asm ("eprol");
29 extern			start() asm("start");
30 
31 /*
32  * Some kluges: store sp at entry in environ, and
33  * install 16 bits of 0 at location 0 (a zero register save mask).
34  * These two hacks remove limits on the use of local
35  * and register variables in start().
36  * The reason for using 'moval (sp),...' is that 'movl sp,...' generates
37  * a privileged instruction trap (argh).
38  * XXX 'addl3 $start,$2,r0; jmp (r0)' should be replaced with
39  * XXX 'jbr start+2' when we convert over to gas.
40  */
41 asm(".text; .word 0; moval (sp),_environ; addl3 $start,$2,r0; jmp (r0)");
42 
43 start()
44 {
45 	struct kframe {
46 		int	kargc;
47 		char	*kargv[1];	/* size depends on kargc */
48 		char	kargstr[1];	/* size varies */
49 		char	kenvstr[1];	/* size varies */
50 	};
51 	register struct kframe *kfp;
52 	register char **targv;
53 	register char **argv;
54 	extern int errno;
55 
56 	kfp = (struct kframe *) environ;
57 	for (argv = targv = &kfp->kargv[0]; *targv++; /* void */)
58 		/* void */ ;
59 	if (targv >= (char **)(*argv))
60 		--targv;
61 	environ = targv;
62 asm("eprol:");
63 
64 #ifdef MCRT0
65 	monstartup(&eprol, &etext);
66 #endif MCRT0
67 	errno = 0;
68 	exit(main(kfp->kargc, argv, environ));
69 }
70 
71 #ifdef MCRT0
72 /*ARGSUSED*/
73 exit(code)
74 	register int code;
75 {
76 	_mcleanup();
77 	_cleanup();
78 	_exit(code);
79 }
80 #endif MCRT0
81 
82 #ifdef CRT0
83 /*
84  * null moncontrol, just in case some routine is compiled for profiling
85  */
86 moncontrol(val)
87 	int val;
88 {
89 
90 }
91 #endif CRT0
92