1 /* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2 Copyright (C) 1995, 1996, 1997, 1998 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. */
25 #include <set-hooks.h>
26 #include "hurdstartup.h"
27 #include "hurdmalloc.h" /* XXX */
29 extern void __mach_init (void);
30 extern void __libc_init_secure (void);
31 extern void __libc_init (int, char **, char **);
32 extern void __getopt_clean_environment (char **);
33 extern void __libc_global_ctors (void);
35 unsigned int __hurd_threadvar_max;
36 unsigned long int __hurd_threadvar_stack_offset;
37 unsigned long int __hurd_threadvar_stack_mask;
39 int __libc_multiple_libcs = 1;
41 extern int __libc_argc;
42 extern char **__libc_argv;
44 /* We often need the PID. Cache this value. */
45 pid_t __libc_pid = 0xf00baa;
47 void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
48 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
50 /* Things that want to be run before _hurd_init or much anything else.
51 Importantly, these are called before anything tries to use malloc. */
52 DEFINE_HOOK (_hurd_preinit_hook, (void));
55 init1 (int argc, char *arg0, ...)
58 char **envp = &argv[argc + 1];
59 struct hurd_startup_data *d;
69 /* If we are the bootstrap task started by the kernel,
70 then after the environment pointers there is no Hurd
71 data block; the argument strings start there. */
72 if ((void *) d != argv[0])
74 _hurd_init_dtable = d->dtable;
75 _hurd_init_dtablesize = d->dtablesize;
78 /* Check if the stack we are now on is different from
79 the one described by _hurd_stack_{base,size}. */
82 const vm_address_t newsp = (vm_address_t) &dummy;
84 if (d->stack_size != 0 && (newsp < d->stack_base ||
85 newsp - d->stack_base > d->stack_size))
86 /* The new stack pointer does not intersect with the
87 stack the exec server set up for us, so free that stack. */
88 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
92 if (__hurd_threadvar_stack_mask == 0)
94 /* We are not using cthreads, so we will have just a single allocated
95 area for the per-thread variables of the main user thread. */
97 __hurd_threadvar_stack_offset
98 = (unsigned long int) malloc (__hurd_threadvar_max *
99 sizeof (unsigned long int));
100 if (__hurd_threadvar_stack_offset == 0)
101 __libc_fatal ("Can't allocate single-threaded per-thread variables.");
102 for (i = 0; i < __hurd_threadvar_max; ++i)
103 ((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0;
106 if ((void *) d != argv[0] && (d->portarray || d->intarray))
107 /* Initialize library data structures, start signal processing, etc. */
108 _hurd_init (d->flags, argv,
109 d->portarray, d->portarraysize,
110 d->intarray, d->intarraysize);
113 __libc_init_secure ();
116 __libc_init (argc, argv, __environ);
118 /* This is a hack to make the special getopt in GNU libc working. */
119 __getopt_clean_environment (envp);
122 __libc_global_ctors ();
130 char **argv = (void *) (data + 1);
131 char **envp = &argv[argc + 1];
132 struct hurd_startup_data *d;
139 /* The user might have defined a value for this, to get more variables.
140 Otherwise it will be zero on startup. We must make sure it is set
141 properly before before cthreads initialization, so cthreads can know
142 how much space to leave for thread variables. */
143 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
144 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
147 /* After possibly switching stacks, call `init1' (above) with the user
148 code as the return address, and the argument data immediately above
149 that on the stack. */
151 if (_cthread_init_routine)
153 /* Initialize cthreads, which will allocate us a new stack to run on. */
154 void *newsp = (*_cthread_init_routine) ();
155 struct hurd_startup_data *od;
157 /* Copy the argdata from the old stack to the new one. */
158 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
159 (char *) d - (char *) data);
161 /* Set up the Hurd startup data block immediately following
162 the argument and environment pointers on the new stack. */
163 od = (newsp + ((char *) d - (char *) data));
164 if ((void *) argv[0] == d)
165 /* We were started up by the kernel with arguments on the stack.
166 There is no Hurd startup data, so zero the block. */
167 memset (od, 0, sizeof *od);
169 /* Copy the Hurd startup data block to the new stack. */
172 /* Push the user code address on the top of the new stack. It will
173 be the return address for `init1'; we will jump there with NEWSP
174 as the stack pointer. */
175 *--(int *) newsp = data[-1];
176 ((void **) data)[-1] = &&switch_stacks;
177 /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
178 by function return. */
179 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
183 /* The argument data is just above the stack frame we will unwind by
184 returning. Mutate our own return address to run the code below. */
185 int usercode = data[-1];
186 ((void **) data)[-1] = &&call_init1;
187 /* Force USERCODE into %eax and &init1 into %ecx, which are not
188 restored by function return. */
189 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
195 /* Our return address was redirected to here, so at this point our stack
196 is unwound and callers' registers restored. Only %ecx and %eax are
197 call-clobbered and thus still have the values we set just above.
198 Fetch from there the new stack pointer we will run on, and jmp to the
199 run-time address of `init1'; when it returns, it will run the user
200 code with the argument data at the top of the stack. */
201 asm volatile ("movl %eax, %esp; jmp *%ecx");
205 /* As in the stack-switching case, at this point our stack is unwound and
206 callers' registers restored, and only %ecx and %eax communicate values
207 from the lines above. In this case we have stashed in %eax the user
208 code return address. Push it on the top of the stack so it acts as
209 init1's return address, and then jump there. */
210 asm volatile ("pushl %eax; jmp *%ecx");
216 /* This function is called to initialize the shared C library.
217 It is called just before the user _start code from i386/elf/start.S,
218 with the stack set up as that code gets it. */
220 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
221 pointer in the dynamic section based solely on that. It is convention
222 for this function to be in the `.init' section, but the symbol name is
223 the only thing that really matters!! */
225 _init (int argc, ...)
227 /* Initialize data structures so we can do RPCs. */
230 RUN_HOOK (_hurd_preinit_hook, ());
238 __libc_init_first (int argc __attribute__ ((unused)), ...)
244 _hurd_stack_setup (int argc __attribute__ ((unused)), ...)
247 void doinit (int *data)
249 /* This function gets called with the argument data at TOS. */
250 void doinit1 (int argc, ...)
255 /* Push the user return address after the argument data, and then
256 jump to `doinit1' (above), so it is as if __libc_init_first's
257 caller had called `doinit1' with the argument data already on the
259 *--data = (&argc)[-1];
260 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
261 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
262 "jmp *%1" : : "r" (data), "r" (&doinit1));
266 /* Initialize data structures so we can do RPCs. */
269 RUN_HOOK (_hurd_preinit_hook, ());
271 _hurd_startup ((void **) &argc, &doinit);
276 /* This function is defined here so that if this file ever gets into
277 ld.so we will get a link error. Having this file silently included
278 in ld.so causes disaster, because the _init definition above will
279 cause ld.so to gain an init function, which is not a cool thing. */