Update to LGPL v2.1.
[jlayton/glibc.git] / sysdeps / mach / hurd / i386 / init-first.c
1 /* Initialization code run first thing by the ELF startup code.  For i386/Hurd.
2    Copyright (C) 1995,96,97,98,99,2000,2001 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 Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the 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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <assert.h>
21 #include <hurd.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <sysdep.h>
26 #include <set-hooks.h>
27 #include "hurdstartup.h"
28 #include "hurdmalloc.h"         /* XXX */
29
30 extern void __mach_init (void);
31 extern void __libc_init (int, char **, char **);
32 extern void __getopt_clean_environment (char **);
33 extern void __libc_global_ctors (void);
34
35 unsigned int __hurd_threadvar_max;
36 unsigned long int __hurd_threadvar_stack_offset;
37 unsigned long int __hurd_threadvar_stack_mask;
38
39 #ifndef SHARED
40 int __libc_enable_secure;
41 #endif
42 int __libc_multiple_libcs = 1;
43
44 extern int __libc_argc;
45 extern char **__libc_argv;
46 extern char **_dl_argv;
47
48 void *(*_cthread_init_routine) (void); /* Returns new SP to use.  */
49 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
50
51 /* Things that want to be run before _hurd_init or much anything else.
52    Importantly, these are called before anything tries to use malloc.  */
53 DEFINE_HOOK (_hurd_preinit_hook, (void));
54
55
56 /* We call this once the Hurd magic is all set up and we are ready to be a
57    Posixoid program.  This does the same things the generic version does.  */
58 static void
59 posixland_init (int argc, char **argv, char **envp)
60 {
61   __libc_argc = argc;
62   __libc_argv = argv;
63   __environ = envp;
64
65   __libc_init (argc, argv, envp);
66
67   /* This is a hack to make the special getopt in GNU libc working.  */
68   __getopt_clean_environment (envp);
69
70 #ifdef SHARED
71   __libc_global_ctors ();
72 #endif
73 }
74
75
76 static void
77 init1 (int argc, char *arg0, ...)
78 {
79   char **argv = &arg0;
80   char **envp = &argv[argc + 1];
81   struct hurd_startup_data *d;
82
83   while (*envp)
84     ++envp;
85   d = (void *) ++envp;
86
87   /* If we are the bootstrap task started by the kernel,
88      then after the environment pointers there is no Hurd
89      data block; the argument strings start there.  */
90   if ((void *) d != argv[0])
91     {
92       _hurd_init_dtable = d->dtable;
93       _hurd_init_dtablesize = d->dtablesize;
94
95       {
96         /* Check if the stack we are now on is different from
97            the one described by _hurd_stack_{base,size}.  */
98
99         char dummy;
100         const vm_address_t newsp = (vm_address_t) &dummy;
101
102         if (d->stack_size != 0 && (newsp < d->stack_base ||
103                                    newsp - d->stack_base > d->stack_size))
104           /* The new stack pointer does not intersect with the
105              stack the exec server set up for us, so free that stack.  */
106           __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
107       }
108     }
109
110   if ((void *) d != argv[0] && (d->portarray || d->intarray))
111     /* Initialize library data structures, start signal processing, etc.  */
112     _hurd_init (d->flags, argv,
113                 d->portarray, d->portarraysize,
114                 d->intarray, d->intarraysize);
115
116 #ifndef SHARED
117   __libc_enable_secure = d->flags & EXEC_SECURE;
118 #endif
119 }
120
121
122 static inline void
123 init (int *data)
124 {
125   int argc = *data;
126   char **argv = (void *) (data + 1);
127   char **envp = &argv[argc + 1];
128   struct hurd_startup_data *d;
129   unsigned long int threadvars[_HURD_THREADVAR_MAX];
130
131   /* Provide temporary storage for thread-specific variables on the startup
132      stack so the cthreads initialization code can use them for malloc et al,
133   or so we can use malloc below for the real threadvars array.  */
134   memset (threadvars, 0, sizeof threadvars);
135   __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
136
137   while (*envp)
138     ++envp;
139   d = (void *) ++envp;
140
141   /* The user might have defined a value for this, to get more variables.
142      Otherwise it will be zero on startup.  We must make sure it is set
143      properly before before cthreads initialization, so cthreads can know
144      how much space to leave for thread variables.  */
145   if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
146     __hurd_threadvar_max = _HURD_THREADVAR_MAX;
147
148
149   /* After possibly switching stacks, call `init1' (above) with the user
150      code as the return address, and the argument data immediately above
151      that on the stack.  */
152
153   if (_cthread_init_routine)
154     {
155       /* Initialize cthreads, which will allocate us a new stack to run on.  */
156       void *newsp = (*_cthread_init_routine) ();
157       struct hurd_startup_data *od;
158
159       void switch_stacks (void);
160
161       /* Copy per-thread variables from that temporary
162          area onto the new cthread stack.  */
163       memcpy (__hurd_threadvar_location_from_sp (0, newsp),
164               threadvars, sizeof threadvars);
165
166       /* Copy the argdata from the old stack to the new one.  */
167       newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
168                       (char *) d - (char *) data);
169
170 #ifdef SHARED
171       /* And readjust the dynamic linker's idea of where the argument
172          vector lives.  */
173       assert (_dl_argv == argv);
174       _dl_argv = (void *) ((int *) newsp + 1);
175 #endif
176
177       /* Set up the Hurd startup data block immediately following
178          the argument and environment pointers on the new stack.  */
179       od = (newsp + ((char *) d - (char *) data));
180       if ((void *) argv[0] == d)
181         /* We were started up by the kernel with arguments on the stack.
182            There is no Hurd startup data, so zero the block.  */
183         memset (od, 0, sizeof *od);
184       else
185         /* Copy the Hurd startup data block to the new stack.  */
186         *od = *d;
187
188       /* Push the user code address on the top of the new stack.  It will
189          be the return address for `init1'; we will jump there with NEWSP
190          as the stack pointer.  */
191       *--(int *) newsp = data[-1];
192       ((void **) data)[-1] = switch_stacks;
193       /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
194          by function return.  */
195       asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
196     }
197   else
198     {
199       /* We are not using cthreads, so we will have just a single allocated
200          area for the per-thread variables of the main user thread.  */
201       unsigned long int *array;
202       unsigned int i;
203       int usercode;
204
205       void call_init1 (void);
206
207       array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
208       if (array == NULL)
209         __libc_fatal ("Can't allocate single-threaded thread variables.");
210
211       /* Copy per-thread variables from the temporary array into the
212          newly malloc'd space.  */
213       memcpy (array, threadvars, sizeof threadvars);
214       __hurd_threadvar_stack_offset = (unsigned long int) array;
215       for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
216         array[i] = 0;
217
218       /* The argument data is just above the stack frame we will unwind by
219          returning.  Mutate our own return address to run the code below.  */
220       usercode = data[-1];
221       ((void **) data)[-1] = call_init1;
222       /* Force USERCODE into %eax and &init1 into %ecx, which are not
223          restored by function return.  */
224       asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
225     }
226 }
227
228 /* These bits of inline assembler used to be located inside `init'.
229    However they were optimized away by gcc 2.95.  */
230
231 /* The return address of `init' above, was redirected to here, so at
232    this point our stack is unwound and callers' registers restored.
233    Only %ecx and %eax are call-clobbered and thus still have the
234    values we set just above.  Fetch from there the new stack pointer
235    we will run on, and jmp to the run-time address of `init1'; when it
236    returns, it will run the user code with the argument data at the
237    top of the stack.  */
238 asm ("
239  switch_stacks:
240   movl %eax, %esp
241   jmp *%ecx
242 ");
243
244 /* As in the stack-switching case, at this point our stack is unwound
245    and callers' registers restored, and only %ecx and %eax communicate
246    values from the lines above.  In this case we have stashed in %eax
247    the user code return address.  Push it on the top of the stack so
248    it acts as init1's return address, and then jump there.  */
249 asm ("
250   call_init1:
251   push %eax
252   jmp *%ecx
253 ");
254
255
256 /* Do the first essential initializations that must precede all else.  */
257 static inline void
258 first_init (void)
259 {
260   /* Initialize data structures so we can do RPCs.  */
261   __mach_init ();
262
263   RUN_HOOK (_hurd_preinit_hook, ());
264 }
265
266 #ifdef SHARED
267 /* This function is called specially by the dynamic linker to do early
268    initialization of the shared C library before normal initializers
269    expecting a Posixoid environment can run.  It gets called with the
270    stack set up just as the user will see it, so it can switch stacks.  */
271
272 void
273 _dl_init_first (int argc, ...)
274 {
275   first_init ();
276
277   init (&argc);
278 }
279 #endif
280
281
282 #ifdef SHARED
283 /* The regular posixland initialization is what goes into libc's
284    normal initializer.  */
285 /* NOTE!  The linker notices the magical name `_init' and sets the DT_INIT
286    pointer in the dynamic section based solely on that.  It is convention
287    for this function to be in the `.init' section, but the symbol name is
288    the only thing that really matters!!  */
289 strong_alias (posixland_init, _init);
290
291 void
292 __libc_init_first (int argc, char **argv, char **envp)
293 {
294   /* Everything was done in the shared library initializer, _init.  */
295 }
296 #else
297 strong_alias (posixland_init, __libc_init_first);
298
299
300 /* XXX This is all a crock and I am not happy with it.
301    This poorly-named function is called by static-start.S,
302    which should not exist at all.  */
303 void
304 _hurd_stack_setup (int argc __attribute__ ((unused)), ...)
305 {
306   void doinit (int *data)
307     {
308       /* This function gets called with the argument data at TOS.  */
309       void doinit1 (int argc, ...)
310         {
311           init (&argc);
312         }
313
314       /* Push the user return address after the argument data, and then
315          jump to `doinit1' (above), so it is as if __libc_init_first's
316          caller had called `doinit1' with the argument data already on the
317          stack.  */
318       *--data = (&argc)[-1];
319       asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack.  */
320                     "movl $0, %%ebp\n" /* Clear outermost frame pointer.  */
321                     "jmp *%1" : : "r" (data), "r" (&doinit1));
322       /* NOTREACHED */
323     }
324
325   first_init ();
326
327   _hurd_startup ((void **) &argc, &doinit);
328 }
329 #endif
330
331
332 /* This function is defined here so that if this file ever gets into
333    ld.so we will get a link error.  Having this file silently included
334    in ld.so causes disaster, because the _init definition above will
335    cause ld.so to gain an init function, which is not a cool thing. */
336
337 void
338 _dl_start (void)
339 {
340   abort ();
341 }