Sat Apr 22 14:48:03 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[platform/upstream/glibc.git] / hurd / hurdstartup.c
1 /* Initial program startup for running under the GNU Hurd.
2 Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB.  If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.  */
19
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <hurd.h>
25 #include <hurd/exec.h>
26 #include <sysdep.h>
27 #include <hurd/threadvar.h>
28 #include <unistd.h>
29 #include "set-hooks.h"
30 #include "hurdmalloc.h"         /* XXX */
31
32 mach_port_t *_hurd_init_dtable;
33 mach_msg_type_number_t _hurd_init_dtablesize;
34
35 unsigned int __hurd_threadvar_max;
36 unsigned long int __hurd_threadvar_stack_mask;
37 unsigned long int __hurd_threadvar_stack_offset;
38
39 /* These are set up by _hurdsig_init.  */
40 unsigned long int __hurd_sigthread_stack_base;
41 unsigned long int __hurd_sigthread_stack_end;
42 unsigned long int *__hurd_sigthread_variables;
43
44 vm_address_t _hurd_stack_base;
45 vm_size_t _hurd_stack_size;
46
47 /* Things that want to be run before _hurd_init or much anything else.
48    Importantly, these are called before anything tries to use malloc.  */
49 DEFINE_HOOK (_hurd_preinit_hook, (void));
50
51 extern void __mach_init (void);
52 extern void __libc_init (int argc, char **argv, char **envp);
53
54 void *(*_cthread_init_routine) (void); /* Returns new SP to use.  */
55 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
56
57 int _hurd_split_args (char *, size_t, char **);
58
59 /* These communicate values from _hurd_startup to start1,
60    where we cannot use the stack for anything.  */
61 struct info
62   {
63     char *args, *env;
64     mach_port_t *portarray;
65     int *intarray;
66     mach_msg_type_number_t argslen, envlen, portarraysize, intarraysize;
67     int flags;
68     char **argv, **envp;
69     int argc;
70     void (*hurd_main) (int, char **, char **,
71                        mach_port_t *, mach_msg_type_number_t,
72                        int *, mach_msg_type_number_t);
73   };
74
75 static void start1 (struct info *) __attribute__ ((__noreturn__));
76
77
78 /* Entry point.  This is the first thing in the text segment.
79
80    The exec server started the initial thread in our task with this spot the
81    PC, and a stack that is presumably big enough.  We do basic Mach
82    initialization so mig-generated stubs work, and then do an exec_startup
83    RPC on our bootstrap port, to which the exec server responds with the
84    information passed in the exec call, as well as our original bootstrap
85    port, and the base address and size of the preallocated stack.
86
87    If using cthreads, we are given a new stack by cthreads initialization and
88    deallocate the stack set up by the exec server.  On the new stack we call
89    `start1' (above) to do the rest of the startup work.  Since the stack may
90    disappear out from under us in a machine-dependent way, we use a pile of
91    static variables to communicate the information from exec_startup to start1.
92    This is unfortunate but preferable to machine-dependent frobnication to copy
93    the state from the old stack to the new one.  */
94
95 void
96 _hurd_startup (void **argptr,
97                void (*main) (int, char **, char **,
98                              mach_port_t *, mach_msg_type_number_t,
99                              int *, mach_msg_type_number_t))
100 {
101   error_t err;
102   mach_port_t in_bootstrap;
103   struct info i;
104
105   /* Basic Mach initialization, must be done before RPCs can be done.  */
106   __mach_init ();
107
108   /* Run things that want to do initialization as soon as possible.  We do
109      this before exec_startup so that no out of line data arrives and
110      clutters up the address space before brk initialization.  */
111
112   RUN_HOOK (_hurd_preinit_hook, ());
113
114   if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
115                                      &in_bootstrap))
116     LOSE;
117
118   if (in_bootstrap != MACH_PORT_NULL)
119     {
120       /* Call the exec server on our bootstrap port and
121          get all our standard information from it.  */
122
123       i.argslen = i.envlen = 0;
124       _hurd_init_dtablesize = i.portarraysize = i.intarraysize = 0;
125
126       err = __exec_startup (in_bootstrap,
127                             &_hurd_stack_base, &_hurd_stack_size,
128                             &i.flags,
129                             &i.args, &i.argslen, &i.env, &i.envlen,
130                             &_hurd_init_dtable, &_hurd_init_dtablesize,
131                             &i.portarray, &i.portarraysize,
132                             &i.intarray, &i.intarraysize);
133       __mach_port_deallocate (__mach_task_self (), in_bootstrap);
134     }
135
136   if (err || in_bootstrap == MACH_PORT_NULL)
137     {
138       /* Either we have no bootstrap port, or the RPC to the exec server
139          failed.  Try to snarf the args in the canonical Mach way.
140          Hopefully either they will be on the stack as expected, or the
141          stack will be zeros so we don't crash.  Set all our other
142          variables to have empty information.  */
143
144       /* SNARF_ARGS (ARGPTR, ARGC, ARGV, ENVP) snarfs the arguments and
145          environment from the stack, assuming they were put there by the
146          microkernel.  */
147       SNARF_ARGS (argptr, i.argc, i.argv, i.envp);
148
149       i.flags = 0;
150       i.args = i.env = NULL;
151       i.argslen = i.envlen = 0;
152       _hurd_init_dtable = NULL;
153       _hurd_init_dtablesize = 0;
154       i.portarray = NULL;
155       i.portarraysize = 0;
156       i.intarray = NULL;
157       i.intarraysize = 0;
158     }
159   else
160     i.argv = i.envp = NULL;
161
162   i.hurd_main = main;
163
164   /* The user might have defined a value for this, to get more variables.
165      Otherwise it will be zero on startup.  We must make sure it is set
166      properly before before cthreads initialization, so cthreads can know
167      how much space to leave for thread variables.  */
168   if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
169     __hurd_threadvar_max = _HURD_THREADVAR_MAX;
170
171   /* Do cthreads initialization and switch to the cthread stack.  */
172
173   if (_cthread_init_routine != NULL)
174     CALL_WITH_SP (start1, i, (*_cthread_init_routine) ());
175   else
176     start1 (&i);
177
178   /* Should never get here.  */
179   LOSE;
180 }
181
182
183 static void
184 start1 (struct info *info)
185 {
186   register int envc = 0;
187
188   {
189     /* Check if the stack we are now on is different from
190        the one described by _hurd_stack_{base,size}.  */
191
192     char dummy;
193     const vm_address_t newsp = (vm_address_t) &dummy;
194
195     if (_hurd_stack_size != 0 && (newsp < _hurd_stack_base ||
196                                   newsp - _hurd_stack_base > _hurd_stack_size))
197       /* The new stack pointer does not intersect with the
198          stack the exec server set up for us, so free that stack.  */
199       __vm_deallocate (__mach_task_self (),
200                        _hurd_stack_base, _hurd_stack_size);
201   }
202
203   if (__hurd_threadvar_stack_mask == 0)
204     {
205       /* We are not using cthreads, so we will have just a single allocated
206          area for the per-thread variables of the main user thread.  */
207       unsigned long int i;
208       __hurd_threadvar_stack_offset
209         = (unsigned long int) malloc (__hurd_threadvar_max *
210                                       sizeof (unsigned long int));
211       if (__hurd_threadvar_stack_offset == 0)
212         __libc_fatal ("Can't allocate single-threaded per-thread variables.");
213       for (i = 0; i < __hurd_threadvar_max; ++i)
214         ((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0;
215     }
216
217
218   /* Turn the block of null-separated strings we were passed for the
219      arguments and environment into vectors of pointers to strings.  */
220
221   if (! info->argv)
222     {
223       if (info->args)
224         /* Count up the arguments so we can allocate ARGV.  */
225         info->argc = _hurd_split_args (args, argslen, NULL);
226       if (! info->args || info->argc == 0)
227         {
228           /* No arguments passed; set argv to { NULL }.  */
229           info->argc = 0;
230           info->args = NULL;
231           info->argv = (char **) &info->args;
232         }
233     }
234
235   if (! info->envp)
236     {
237       if (info->env)
238         /* Count up the environment variables so we can allocate ENVP.  */
239         envc = _hurd_split_args (info->env, info->envlen, NULL);
240       if (! info->env || envc == 0)
241         {
242           /* No environment passed; set __environ to { NULL }.  */
243           info->env = NULL;
244           info->envp = (char **) &env;
245         }
246     }
247
248   if (! info->argv)
249     {
250       /* There were some arguments.
251          Allocate space for the vectors of pointers and fill them in.  */
252       info->argv = __alloca ((info->argc + 1) * sizeof (char *));
253       _hurd_split_args (info->args, info->argslen, info->argv);
254     }
255   
256   if (! info->envp)
257     {
258       /* There was some environment.
259          Allocate space for the vectors of pointers and fill them in.  */
260       info->envp = __alloca ((envc + 1) * sizeof (char *));
261       _hurd_split_args (info->env, info->envlen, info->envp);
262     }
263
264   (*info->hurd_main) (info->argc, info->argv, info->envp,
265                       info->portarray, info->portarraysize,
266                       info->intarray, info->intarraysize);
267
268   /* Should never get here.  */
269   LOSE;
270 }
271
272 /* Split ARGSLEN bytes at ARGS into words, breaking at NUL characters.  If
273    ARGV is not a null pointer, store a pointer to the start of each word in
274    ARGV[n], and null-terminate ARGV.  Return the number of words split.  */
275
276 int
277 _hurd_split_args (char *args, size_t argslen, char **argv)
278 {
279   char *p = args;
280   size_t n = argslen;
281   int argc = 0;
282
283   while (n > 0)
284     {
285       char *end = memchr (p, '\0', n);
286
287       if (argv)
288         argv[argc] = p;
289       ++argc;
290
291       if (end == NULL)
292         /* The last argument is unterminated.  */
293         break;
294
295       n -= end + 1 - p;
296       p = end + 1;
297     }
298
299   if (argv)
300     argv[argc] = NULL;
301   return argc;
302 }