Wed May 10 21:00:47 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 <elf.h>
30 #include "set-hooks.h"
31 #include "hurdmalloc.h"         /* XXX */
32 #include "hurdstartup.h"
33
34 mach_port_t *_hurd_init_dtable;
35 mach_msg_type_number_t _hurd_init_dtablesize;
36
37 unsigned int __hurd_threadvar_max;
38 unsigned long int __hurd_threadvar_stack_mask;
39 unsigned long int __hurd_threadvar_stack_offset;
40
41 /* These are set up by _hurdsig_init.  */
42 unsigned long int __hurd_sigthread_stack_base;
43 unsigned long int __hurd_sigthread_stack_end;
44 unsigned long int *__hurd_sigthread_variables;
45
46 extern void __mach_init (void);
47
48 int _hurd_split_args (char *, size_t, char **);
49
50
51 /* Entry point.  This is the first thing in the text segment.
52
53    The exec server started the initial thread in our task with this spot the
54    PC, and a stack that is presumably big enough.  We do basic Mach
55    initialization so mig-generated stubs work, and then do an exec_startup
56    RPC on our bootstrap port, to which the exec server responds with the
57    information passed in the exec call, as well as our original bootstrap
58    port, and the base address and size of the preallocated stack.
59
60    If using cthreads, we are given a new stack by cthreads initialization and
61    deallocate the stack set up by the exec server.  On the new stack we call
62    `start1' (above) to do the rest of the startup work.  Since the stack may
63    disappear out from under us in a machine-dependent way, we use a pile of
64    static variables to communicate the information from exec_startup to start1.
65    This is unfortunate but preferable to machine-dependent frobnication to copy
66    the state from the old stack to the new one.  */
67
68
69 void
70 _hurd_startup (void **argptr, void (*main) (int *data))
71 {
72   error_t err;
73   mach_port_t in_bootstrap;
74   char *args, *env;
75   mach_msg_type_number_t argslen, envlen;
76   struct hurd_startup_data data;
77   char **argv, **envp;
78   int argc, envc;
79   int *argcptr;
80
81   /* Basic Mach initialization, must be done before RPCs can be done.  */
82   __mach_init ();
83
84   if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
85                                      &in_bootstrap))
86     LOSE;
87
88   if (in_bootstrap != MACH_PORT_NULL)
89     {
90       /* Call the exec server on our bootstrap port and
91          get all our standard information from it.  */
92
93       argslen = envlen = 0;
94       data.dtablesize = data.portarraysize = data.intarraysize = 0;
95
96       err = __exec_startup (in_bootstrap,
97                             &data.stack_base, &data.stack_size,
98                             &data.flags, &args, &argslen, &env, &envlen,
99                             &data.dtable, &data.dtablesize,
100                             &data.portarray, &data.portarraysize,
101                             &data.intarray, &data.intarraysize);
102       __mach_port_deallocate (__mach_task_self (), in_bootstrap);
103     }
104
105   if (err || in_bootstrap == MACH_PORT_NULL)
106     {
107 #if 0
108       /* Either we have no bootstrap port, or the RPC to the exec server
109          failed.  Try to snarf the args in the canonical Mach way.
110          Hopefully either they will be on the stack as expected, or the
111          stack will be zeros so we don't crash.  Set all our other
112          variables to have empty information.  */
113
114       ENTRY_SP (argptr);
115       /* SNARF_ARGS (ARGPTR, ARGC, ARGV, ENVP) snarfs the arguments and
116          environment from the stack, assuming they were put there by the
117          microkernel.  */
118 XXX XXX XXX
119       
120       SNARF_ARGS (argptr, argc, argv, envp);
121 #endif
122
123       data.flags = 0;
124       args = env = NULL;
125       argslen = envlen = 0;
126       data.dtable = NULL;
127       data.dtablesize = 0;
128       data.portarray = NULL;
129       data.portarraysize = 0;
130       data.intarray = NULL;
131       data.intarraysize = 0;
132     }
133   else
134     argv = envp = NULL;
135
136
137   /* Turn the block of null-separated strings we were passed for the
138      arguments and environment into vectors of pointers to strings.  */
139
140
141   
142   if (! argv)
143     {
144       /* Count up the arguments so we can allocate ARGV.  */
145       argc = _hurd_split_args (args, argslen, NULL);
146       /* Count up the environment variables so we can allocate ENVP.  */
147       envc = _hurd_split_args (env, envlen, NULL);
148
149       /* There were some arguments.  Allocate space for the vectors of
150          pointers and fill them in.  We allocate the space for the
151          environment pointers immediately after the argv pointers because
152          the ELF ABI will expect it.  */
153       argcptr = __alloca (sizeof (int) +
154                           (argc + 1 + envc + 1) * sizeof (char *) +
155                           sizeof (struct hurd_startup_data));
156       *argcptr = argc;
157       argv = (void *) (argcptr + 1);
158       _hurd_split_args (args, argslen, argv);
159
160       /* There was some environment.  */
161       envp = &argv[argc + 1];
162       _hurd_split_args (env, envlen, envp);
163     }
164
165   {
166     struct hurd_startup_data *d = (void *) &envp[envc + 1];
167
168     /* XXX hardcoded until exec_startup changes */
169 #ifdef PIC
170 #if 0
171     const Elf32_Ehdr *ehdr = (const void *) 0x08000000;
172     vm_address_t phdr = 0x08000000 + ehdr->e_phoff;
173     vm_size_t phdrsz = ehdr->e_phnum * ehdr->e_phentsize;
174     vm_address_t user_entry = ehdr->e_entry;
175 #else
176     vm_address_t phdr = 0;
177     vm_size_t phdrsz = 0;
178 extern void _start();
179     vm_address_t user_entry = (vm_address_t) &_start;
180 #endif
181 #else
182     vm_address_t phdr = 0;
183     vm_size_t phdrsz = 0;
184     vm_address_t user_entry = 0;
185 #endif
186
187     *d = data;
188     _hurd_init_dtable = d->dtable;
189     _hurd_init_dtablesize = d->dtablesize;
190     d->phdr = phdr;
191     d->phdrsz = phdrsz;
192     d->user_entry = user_entry;
193
194     (*main) (argcptr);
195   }
196
197   /* Should never get here.  */
198   LOSE;
199   abort ();
200 }
201
202 /* Split ARGSLEN bytes at ARGS into words, breaking at NUL characters.  If
203    ARGV is not a null pointer, store a pointer to the start of each word in
204    ARGV[n], and null-terminate ARGV.  Return the number of words split.  */
205
206 int
207 _hurd_split_args (char *args, size_t argslen, char **argv)
208 {
209   char *p = args;
210   size_t n = argslen;
211   int argc = 0;
212
213   while (n > 0)
214     {
215       char *end = memchr (p, '\0', n);
216
217       if (argv)
218         argv[argc] = p;
219       ++argc;
220
221       if (end == NULL)
222         /* The last argument is unterminated.  */
223         break;
224
225       n -= end + 1 - p;
226       p = end + 1;
227     }
228
229   if (argv)
230     argv[argc] = NULL;
231   return argc;
232 }