xref: /original-bsd/lib/csu/vax/crt0.c (revision ba762ddc)
1 /*-
2  * Copyright (c) 1982 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)crt0.c	5.5 (Berkeley) 04/12/91";
10 #endif /* LIBC_SCCS and 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!  see sys1.c:setregs().
20  *	2) The only register variable that we can trust is sp,
21  *	which points to the base of the kernel calling frame.
22  *	Do NOT believe the documentation in exec(2) regarding the
23  *	values of fp and ap.
24  *	3) We can allocate as many register variables as we want,
25  *	and don't have to save them for anybody.
26  *	4) Because of the ways that asm's work, we can't have
27  *	any automatic variables allocated on the stack, because
28  *	we must catch the value of sp before any automatics are
29  *	allocated.
30  */
31 
32 char **environ = (char **)0;
33 static int fd;
34 
35 asm("#define _start start");
36 asm("#define _eprol eprol");
37 extern	unsigned char	etext;
38 extern	unsigned char	eprol;
39 start()
40 {
41 	struct kframe {
42 		int	kargc;
43 		char	*kargv[1];	/* size depends on kargc */
44 		char	kargstr[1];	/* size varies */
45 		char	kenvstr[1];	/* size varies */
46 	};
47 	/*
48 	 *	ALL REGISTER VARIABLES!!!
49 	 */
50 	register int r11;		/* needed for init */
51 	register struct kframe *kfp;	/* r10 */
52 	register char **targv;
53 	register char **argv;
54 	extern int errno;
55 
56 #ifdef lint
57 	kfp = 0;
58 	initcode = initcode = 0;
59 #else not lint
60 	asm("	movl	sp,r10");	/* catch it quick */
61 #endif not lint
62 	for (argv = targv = &kfp->kargv[0]; *targv++; /* void */)
63 		/* void */ ;
64 	if (targv >= (char **)(*argv))
65 		--targv;
66 	environ = targv;
67 asm("eprol:");
68 
69 #ifdef paranoid
70 	/*
71 	 * The standard I/O library assumes that file descriptors 0, 1, and 2
72 	 * are open. If one of these descriptors is closed prior to the start
73 	 * of the process, I/O gets very confused. To avoid this problem, we
74 	 * insure that the first three file descriptors are open before calling
75 	 * main(). Normally this is undefined, as it adds two unnecessary
76 	 * system calls.
77 	 */
78 	do	{
79 		fd = open("/dev/null", 2);
80 	} while (fd >= 0 && fd < 3);
81 	close(fd);
82 #endif paranoid
83 
84 #ifdef MCRT0
85 	monstartup(&eprol, &etext);
86 #endif MCRT0
87 	errno = 0;
88 	exit(main(kfp->kargc, argv, environ));
89 }
90 asm("#undef _start");
91 asm("#undef _eprol");
92 
93 #ifdef MCRT0
94 /*ARGSUSED*/
95 exit(code)
96 	register int code;	/* r11 */
97 {
98 	monitor(0);
99 	_cleanup();
100 	_exit(code);
101 }
102 #endif MCRT0
103 
104 #ifdef CRT0
105 /*
106  * null mcount and moncontrol,
107  * just in case some routine is compiled for profiling
108  */
109 moncontrol(val)
110 	int val;
111 {
112 
113 }
114 asm("	.globl	mcount");
115 asm("mcount:	rsb");
116 #endif CRT0
117