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.7 (Berkeley) 07/10/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! 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 extern unsigned char etext; 36 extern unsigned char eprol asm ("eprol"); 37 extern start() asm("start"); 38 39 asm(".text; orb #0,d0"); /* 32 bits of zero at location 0 */ 40 41 start() 42 { 43 struct kframe { 44 int kargc; 45 char *kargv[1]; /* size depends on kargc */ 46 char kargstr[1]; /* size varies */ 47 char kenvstr[1]; /* size varies */ 48 }; 49 /* 50 * ALL REGISTER VARIABLES!!! 51 */ 52 register struct kframe *kfp; /* r10 */ 53 register char **targv; 54 register char **argv; 55 extern int errno; 56 extern void _mcleanup(); 57 58 #ifdef lint 59 kfp = 0; 60 initcode = initcode = 0; 61 #else not lint 62 asm("lea a6@(4),%0" : "=r" (kfp)); /* catch it quick */ 63 #endif not lint 64 for (argv = targv = &kfp->kargv[0]; *targv++; /* void */) 65 /* void */ ; 66 if (targv >= (char **)(*argv)) 67 --targv; 68 environ = targv; 69 asm("eprol:"); 70 71 #ifdef paranoid 72 /* 73 * The standard I/O library assumes that file descriptors 0, 1, and 2 74 * are open. If one of these descriptors is closed prior to the start 75 * of the process, I/O gets very confused. To avoid this problem, we 76 * insure that the first three file descriptors are open before calling 77 * main(). Normally this is undefined, as it adds two unnecessary 78 * system calls. 79 */ 80 do { 81 fd = open("/dev/null", 2); 82 } while (fd >= 0 && fd < 3); 83 close(fd); 84 #endif paranoid 85 86 #ifdef MCRT0 87 atexit(_mcleanup); 88 monstartup(&eprol, &etext); 89 #endif MCRT0 90 errno = 0; 91 exit(main(kfp->kargc, argv, environ)); 92 } 93 94 #ifdef CRT0 95 /* 96 * null moncontrol, just in case some routine is compiled for profiling 97 */ 98 moncontrol(val) 99 int val; 100 { 101 102 } 103 #endif /* CRT0 */ 104