home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / os / kludge03.tz / kludge03 / mk74 / user / libmach / crt0.c < prev    next >
C/C++ Source or Header  |  1992-08-29  |  6KB  |  205 lines

  1. /*
  2.  * Mach Operating System
  3.  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation is hereby granted, provided that both the copyright
  8.  * notice and this permission notice appear in all copies of the
  9.  * software, derivative works or modified versions, and any portions
  10.  * thereof, and that both notices appear in supporting documentation.
  11.  *
  12.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15.  *
  16.  * Carnegie Mellon requests users of this software to return to
  17.  *
  18.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  19.  *  School of Computer Science
  20.  *  Carnegie Mellon University
  21.  *  Pittsburgh PA 15213-3890
  22.  *
  23.  * any improvements or extensions that they make and grant Carnegie Mellon
  24.  * the rights to redistribute these changes.
  25.  */
  26. /*
  27.  * HISTORY
  28.  * $Log:    crt0.c,v $
  29.  * Revision 2.7  91/07/31  18:27:51  dbg
  30.  *     Fix for ANSI C.
  31.  *     [91/07/30  17:29:44  dbg]
  32.  *
  33.  * Revision 2.6  91/05/14  17:52:39  mrt
  34.  *     Correcting copyright
  35.  *
  36.  * Revision 2.5  91/02/14  14:17:23  mrt
  37.  *     Added new Mach copyright
  38.  *     [91/02/13  14:16:29  mrt]
  39.  *
  40.  * Revision 2.4  90/11/05  14:35:42  rpd
  41.  *     Removed the definition of exit.
  42.  *     [90/10/30            rpd]
  43.  *
  44.  * Revision 2.3  90/06/02  14:36:49  rpd
  45.  *     Converted to new IPC.
  46.  *     [90/06/02            rpd]
  47.  *
  48.  * Revision 2.2  90/05/03  15:54:13  dbg
  49.  *     Fix include of i386/asm.h
  50.  *     [90/04/30            dbg]
  51.  *     Created.
  52.  *     [90/04/30  15:32:17  dbg]
  53.  *
  54.  */
  55. /*
  56.  * Copyright (c) 1990 The Regents of the University of California.
  57.  * All rights reserved.
  58.  *
  59.  * This code is derived from software contributed to Berkeley by
  60.  * William Jolitz.
  61.  *
  62.  * Redistribution and use in source and binary forms are permitted
  63.  * provided that: (1) source distributions retain this entire copyright
  64.  * notice and comment, and (2) distributions including binaries display
  65.  * the following acknowledgement:  ``This product includes software
  66.  * developed by the University of California, Berkeley and its contributors''
  67.  * in the documentation or other materials provided with the distribution
  68.  * and in all advertising materials mentioning features or use of this
  69.  * software. Neither the name of the University nor the names of its
  70.  * contributors may be used to endorse or promote products derived
  71.  * from this software without specific prior written permission.
  72.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  73.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  74.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  75.  */
  76.  
  77. #ifndef lint
  78. static char sccsid[] = "@(#)crt0.c    5.2 (Berkeley) 5/14/90";
  79. #endif /* not lint */
  80.  
  81. /*
  82.  *    C start up routine.
  83.  *    Robert Henry, UCB, 20 Oct 81
  84.  *
  85.  *    We make the following (true) assumptions:
  86.  *    1) when the kernel calls start, it does a jump to location 2,
  87.  *    and thus avoids the register save mask.  We are NOT called
  88.  *    with a calls!  see sys1.c:setregs().
  89.  *    2) The only register variable that we can trust is sp,
  90.  *    which points to the base of the kernel calling frame.
  91.  *    Do NOT believe the documentation in exec(2) regarding the
  92.  *    values of fp and ap.
  93.  *    3) We can allocate as many register variables as we want,
  94.  *    and don't have to save them for anybody.
  95.  *    4) Because of the ways that asm's work, we can't have
  96.  *    any automatic variables allocated on the stack, because
  97.  *    we must catch the value of sp before any automatics are
  98.  *    allocated.
  99.  */
  100.  
  101. char **environ = (char **)0;
  102. #ifdef paranoid
  103. static int fd;
  104. #endif paranoid
  105.  
  106. #if    MACH
  107. int    (*mach_init_routine)();
  108. int    (*_cthread_init_routine)();
  109. int    (*_cthread_exit_routine)();
  110. int    (*_StrongBox_init_routine)();
  111. int    errno = 0;
  112. int    exit();
  113. #endif    MACH
  114.  
  115. extern    unsigned char    etext;
  116. extern    unsigned char    _eprol;
  117. _start()
  118. {
  119.     struct kframe {
  120.         int    kargc;
  121.         char    *kargv[1];    /* size depends on kargc */
  122.         char    kargstr[1];    /* size varies */
  123.         char    kenvstr[1];    /* size varies */
  124.     };
  125.     /*
  126.      *    ALL REGISTER VARIABLES!!!
  127.      */
  128.     register int r11;        /* needed for init */
  129.     register struct kframe *kfp;
  130.     register char **targv;
  131.     register char **argv;
  132.  
  133.     asm volatile("movel sp,%0" : "=r" (kfp));
  134.  
  135.     for (argv = targv = &kfp->kargv[0]; *targv++; /* void */)
  136.         /* void */ ;
  137.     if (targv >= (char **)(*argv))
  138.         --targv;
  139.     environ = targv;
  140. #if    MACH
  141.     if (mach_init_routine)
  142.         (void) mach_init_routine();
  143. #endif    MACH
  144.  
  145. #ifdef paranoid
  146.     /*
  147.      * The standard I/O library assumes that file descriptors 0, 1, and 2
  148.      * are open. If one of these descriptors is closed prior to the start
  149.      * of the process, I/O gets very confused. To avoid this problem, we
  150.      * insure that the first three file descriptors are open before calling
  151.      * main(). Normally this is undefined, as it adds two unnecessary
  152.      * system calls.
  153.      */
  154.     do    {
  155.         fd = open("/dev/null", 2);
  156.     } while (fd >= 0 && fd < 3);
  157.     close(fd);
  158. #endif paranoid
  159.  
  160. #ifdef MCRT0
  161.     monstartup(&_eprol, &etext);
  162. #endif MCRT0
  163. #if    MACH
  164.     if (_cthread_init_routine) {
  165.         register int new_sp;
  166.         new_sp = (*_cthread_init_routine)();
  167.         if (new_sp) {
  168.         asm volatile("movel %0,sp" : : "r" (new_sp) );
  169.         }
  170.     }
  171.     if (_StrongBox_init_routine) (*_StrongBox_init_routine)();
  172.     (* (_cthread_exit_routine ? _cthread_exit_routine : exit))
  173.         (main(kfp->kargc, argv, targv));
  174. #else    !MACH
  175.     exit(main(kfp->kargc, argv, environ));
  176. #endif    !MACH
  177. }
  178.  
  179. #ifdef MCRT0
  180. /*ARGSUSED*/
  181. exit(code)
  182.     register int code;    /* r11 */
  183. {
  184.     monitor(0);
  185.     _cleanup();
  186.     _exit(code);
  187. }
  188. #endif MCRT0
  189.  
  190. #ifdef CRT0
  191. /*
  192.  * null mcount and moncontrol,
  193.  * just in case some routine is compiled for profiling
  194.  */
  195. moncontrol(val)
  196.     int val;
  197. {
  198.  
  199. }
  200.  
  201. mcount()
  202. {
  203. }
  204. #endif CRT0
  205.