1 /* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include "hurdstartup.h"
25 #include "set-hooks.h"
26 #include "hurdmalloc.h" /* XXX */
28 extern void __mach_init (void);
29 extern void __libc_init (int, char **, char **);
30 extern void __getopt_clean_environment (void);
31 extern void __libc_global_ctors (void);
33 unsigned int __hurd_threadvar_max;
34 unsigned long int __hurd_threadvar_stack_offset;
35 unsigned long int __hurd_threadvar_stack_mask;
37 int __libc_multiple_libcs = 1;
39 extern int __libc_argc;
40 extern char **__libc_argv;
42 /* We often need the PID. Cache this value. */
45 void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
46 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
48 /* Things that want to be run before _hurd_init or much anything else.
49 Importantly, these are called before anything tries to use malloc. */
50 DEFINE_HOOK (_hurd_preinit_hook, (void));
53 init1 (int argc, char *arg0, ...)
56 char **envp = &argv[argc + 1];
57 struct hurd_startup_data *d;
66 /* If we are the bootstrap task started by the kernel,
67 then after the environment pointers there is no Hurd
68 data block; the argument strings start there. */
69 if ((void *) d != argv[0])
71 _hurd_init_dtable = d->dtable;
72 _hurd_init_dtablesize = d->dtablesize;
75 /* Check if the stack we are now on is different from
76 the one described by _hurd_stack_{base,size}. */
79 const vm_address_t newsp = (vm_address_t) &dummy;
81 if (d->stack_size != 0 && (newsp < d->stack_base ||
82 newsp - d->stack_base > d->stack_size))
83 /* The new stack pointer does not intersect with the
84 stack the exec server set up for us, so free that stack. */
85 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
89 if (__hurd_threadvar_stack_mask == 0)
91 /* We are not using cthreads, so we will have just a single allocated
92 area for the per-thread variables of the main user thread. */
94 __hurd_threadvar_stack_offset
95 = (unsigned long int) malloc (__hurd_threadvar_max *
96 sizeof (unsigned long int));
97 if (__hurd_threadvar_stack_offset == 0)
98 __libc_fatal ("Can't allocate single-threaded per-thread variables.");
99 for (i = 0; i < __hurd_threadvar_max; ++i)
100 ((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0;
103 if ((void *) d != argv[0] && (d->portarray || d->intarray))
104 /* Initialize library data structures, start signal processing, etc. */
105 _hurd_init (d->flags, argv,
106 d->portarray, d->portarraysize,
107 d->intarray, d->intarraysize);
109 __libc_init (argc, argv, __environ);
111 /* This is a hack to make the special getopt in GNU libc working. */
112 __getopt_clean_environment ();
115 __libc_global_ctors ();
123 char **argv = (void *) (data + 1);
124 char **envp = &argv[argc + 1];
125 struct hurd_startup_data *d;
132 /* The user might have defined a value for this, to get more variables.
133 Otherwise it will be zero on startup. We must make sure it is set
134 properly before before cthreads initialization, so cthreads can know
135 how much space to leave for thread variables. */
136 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
137 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
140 /* After possibly switching stacks, call `init1' (above) with the user
141 code as the return address, and the argument data immediately above
142 that on the stack. */
144 if (_cthread_init_routine)
146 /* Initialize cthreads, which will allocate us a new stack to run on. */
147 void *newsp = (*_cthread_init_routine) ();
148 struct hurd_startup_data *od;
150 /* Copy the argdata from the old stack to the new one. */
151 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
152 (char *) d - (char *) data);
154 /* Set up the Hurd startup data block immediately following
155 the argument and environment pointers on the new stack. */
156 od = (newsp + ((char *) d - (char *) data));
157 if ((void *) argv[0] == d)
158 /* We were started up by the kernel with arguments on the stack.
159 There is no Hurd startup data, so zero the block. */
160 memset (od, 0, sizeof *od);
162 /* Copy the Hurd startup data block to the new stack. */
165 /* Push the user code address on the top of the new stack. It will
166 be the return address for `init1'; we will jump there with NEWSP
167 as the stack pointer. */
168 *--(int *) newsp = data[-1];
169 ((void **) data)[-1] = &&switch_stacks;
170 /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
171 by function return. */
172 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
176 /* The argument data is just above the stack frame we will unwind by
177 returning. Mutate our own return address to run the code below. */
178 int usercode = data[-1];
179 ((void **) data)[-1] = &&call_init1;
180 /* Force USERCODE into %eax and &init1 into %ecx, which are not
181 restored by function return. */
182 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
188 /* Our return address was redirected to here, so at this point our stack
189 is unwound and callers' registers restored. Only %ecx and %eax are
190 call-clobbered and thus still have the values we set just above.
191 Fetch from there the new stack pointer we will run on, and jmp to the
192 run-time address of `init1'; when it returns, it will run the user
193 code with the argument data at the top of the stack. */
194 asm volatile ("movl %eax, %esp; jmp *%ecx");
198 /* As in the stack-switching case, at this point our stack is unwound and
199 callers' registers restored, and only %ecx and %eax communicate values
200 from the lines above. In this case we have stashed in %eax the user
201 code return address. Push it on the top of the stack so it acts as
202 init1's return address, and then jump there. */
203 asm volatile ("pushl %eax; jmp *%ecx");
209 /* This function is called to initialize the shared C library.
210 It is called just before the user _start code from i386/elf/start.S,
211 with the stack set up as that code gets it. */
213 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
214 pointer in the dynamic section based solely on that. It is convention
215 for this function to be in the `.init' section, but the symbol name is
216 the only thing that really matters!! */
217 /*void _init (int argc, ...) __attribute__ ((unused, section (".init")));*/
220 _init (int argc, ...)
222 /* Initialize data structures so we can do RPCs. */
225 RUN_HOOK (_hurd_preinit_hook, ());
233 __libc_init_first (int argc __attribute__ ((unused)), ...)
236 void doinit (int *data)
238 /* This function gets called with the argument data at TOS. */
239 void doinit1 (int argc, ...)
244 /* Push the user return address after the argument data, and then
245 jump to `doinit1' (above), so it is as if __libc_init_first's
246 caller had called `doinit1' with the argument data already on the
248 *--data = (&argc)[-1];
249 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
250 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
251 "jmp *%1" : : "r" (data), "r" (&doinit1));
255 /* Initialize data structures so we can do RPCs. */
258 RUN_HOOK (_hurd_preinit_hook, ());
260 _hurd_startup ((void **) &argc, &doinit);
265 /* This function is defined here so that if this file ever gets into
266 ld.so we will get a link error. Having this file silently included
267 in ld.so causes disaster, because the _init definition above will
268 cause ld.so to gain an init function, which is not a cool thing. */