Thu Sep 21 00:03:53 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / sysdeps / mach / hurd / i386 / init-first.c
blobba39a70d23d9a523a856d2b293395c261bb8430d
1 /* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2 Copyright (C) 1995 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <hurd.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
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 __libc_global_ctors (void);
32 void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
33 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
36 /* Things that want to be run before _hurd_init or much anything else.
37 Importantly, these are called before anything tries to use malloc. */
38 DEFINE_HOOK (_hurd_preinit_hook, (void));
41 static void
42 init1 (int argc, char *arg0, ...)
44 char **argv = &arg0;
45 char **envp = &argv[argc + 1];
46 struct hurd_startup_data *d;
48 __environ = envp;
49 while (*envp)
50 ++envp;
51 d = (void *) ++envp;
53 /* If we are the bootstrap task started by the kernel,
54 then after the environment pointers there is no Hurd
55 data block; the argument strings start there. */
56 if ((void *) d != argv[0])
58 _hurd_init_dtable = d->dtable;
59 _hurd_init_dtablesize = d->dtablesize;
62 /* Check if the stack we are now on is different from
63 the one described by _hurd_stack_{base,size}. */
65 char dummy;
66 const vm_address_t newsp = (vm_address_t) &dummy;
68 if (d->stack_size != 0 && (newsp < d->stack_base ||
69 newsp - d->stack_base > d->stack_size))
70 /* The new stack pointer does not intersect with the
71 stack the exec server set up for us, so free that stack. */
72 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
76 if (__hurd_threadvar_stack_mask == 0)
78 /* We are not using cthreads, so we will have just a single allocated
79 area for the per-thread variables of the main user thread. */
80 unsigned long int i;
81 __hurd_threadvar_stack_offset
82 = (unsigned long int) malloc (__hurd_threadvar_max *
83 sizeof (unsigned long int));
84 if (__hurd_threadvar_stack_offset == 0)
85 __libc_fatal ("Can't allocate single-threaded per-thread variables.");
86 for (i = 0; i < __hurd_threadvar_max; ++i)
87 ((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0;
90 if ((void *) d != argv[0] && (d->portarray || d->intarray))
91 /* Initialize library data structures, start signal processing, etc. */
92 _hurd_init (d->flags, argv,
93 d->portarray, d->portarraysize,
94 d->intarray, d->intarraysize);
96 __libc_init (argc, argv, __environ);
97 #ifdef PIC
98 __libc_global_ctors ();
99 #endif
102 static void
103 init (int *data)
105 int argc = *data;
106 char **argv = (void *) (data + 1);
107 char **envp = &argv[argc + 1];
108 struct hurd_startup_data *d;
110 __environ = envp;
111 while (*envp)
112 ++envp;
113 d = (void *) ++envp;
115 /* The user might have defined a value for this, to get more variables.
116 Otherwise it will be zero on startup. We must make sure it is set
117 properly before before cthreads initialization, so cthreads can know
118 how much space to leave for thread variables. */
119 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
120 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
123 /* After possibly switching stacks, call `init1' (above) with the user
124 code as the return address, and the argument data immediately above
125 that on the stack. */
127 if (_cthread_init_routine)
129 /* Initialize cthreads, which will allocate us a new stack to run on. */
130 void *newsp = (*_cthread_init_routine) ();
131 struct hurd_startup_data *od;
133 /* Copy the argdata from the old stack to the new one. */
134 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
135 (char *) d - (char *) data);
137 /* Set up the Hurd startup data block immediately following
138 the argument and environment pointers on the new stack. */
139 od = (newsp + ((char *) d - (char *) data));
140 if ((void *) argv[0] == d)
141 /* We were started up by the kernel with arguments on the stack.
142 There is no Hurd startup data, so zero the block. */
143 memset (od, 0, sizeof *od);
144 else
145 /* Copy the Hurd startup data block to the new stack. */
146 *od = *d;
148 /* Push the user code address on the top of the new stack. It will
149 be the return address for `init1'; we will jump there with NEWSP
150 as the stack pointer. */
151 *--(int *) newsp = data[-1];
152 ((void **) data)[-1] = &&switch_stacks;
153 /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
154 by function return. */
155 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
157 else
159 /* The argument data is just above the stack frame we will unwind by
160 returning. Mutate our own return address to run the code below. */
161 int usercode = data[-1];
162 ((void **) data)[-1] = &&call_init1;
163 /* Force USERCODE into %eax and &init1 into %ecx, which are not
164 restored by function return. */
165 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
168 return;
170 switch_stacks:
171 /* Our return address was redirected to here, so at this point our stack
172 is unwound and callers' registers restored. Only %ecx and %eax are
173 call-clobbered and thus still have the values we set just above.
174 Fetch from there the new stack pointer we will run on, and jmp to the
175 run-time address of `init1'; when it returns, it will run the user
176 code with the argument data at the top of the stack. */
177 asm volatile ("movl %eax, %esp; jmp *%ecx");
178 /* NOTREACHED */
180 call_init1:
181 /* As in the stack-switching case, at this point our stack is unwound and
182 callers' registers restored, and only %ecx and %eax communicate values
183 from the lines above. In this case we have stashed in %eax the user
184 code return address. Push it on the top of the stack so it acts as
185 init1's return address, and then jump there. */
186 asm volatile ("pushl %eax; jmp *%ecx");
187 /* NOTREACHED */
191 #ifdef PIC
192 /* This function is called to initialize the shared C library.
193 It is called just before the user _start code from i386/elf/start.S,
194 with the stack set up as that code gets it. */
196 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
197 pointer in the dynamic section based solely on that. It is convention
198 for this function to be in the `.init' section, but the symbol name is
199 the only thing that really matters!! */
200 /*void _init (int argc, ...) __attribute__ ((unused, section (".init")));*/
202 void
203 _init (int argc, ...)
205 /* Initialize data structures so we can do RPCs. */
206 __mach_init ();
208 RUN_HOOK (_hurd_preinit_hook, ());
210 init (&argc);
212 #endif
215 void
216 __libc_init_first (int argc __attribute__ ((unused)), ...)
218 #ifndef PIC
219 void doinit (int *data)
221 /* This function gets called with the argument data at TOS. */
222 void doinit1 (int argc, ...)
224 init (&argc);
227 /* Push the user return address after the argument data, and then
228 jump to `doinit1' (above), so it is as if __libc_init_first's
229 caller had called `doinit1' with the argument data already on the
230 stack. */
231 *--data = (&argc)[-1];
232 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
233 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
234 "jmp *%1" : : "r" (data), "r" (&doinit1));
235 /* NOTREACHED */
238 /* Initialize data structures so we can do RPCs. */
239 __mach_init ();
241 RUN_HOOK (_hurd_preinit_hook, ());
243 _hurd_startup ((void **) &argc, &doinit);
244 #endif