]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/i386/init-first.c
Wed Sep 20 18:02:03 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[thirdparty/glibc.git] / sysdeps / mach / hurd / i386 / init-first.c
CommitLineData
99b306dc
RM
1/* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2Copyright (C) 1995 Free Software Foundation, Inc.
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, USA. */
19
20#include <hurd.h>
21#include <stdio.h>
22#include <unistd.h>
a2fe9c76 23#include <string.h>
99b306dc
RM
24#include "hurdstartup.h"
25#include "set-hooks.h"
26#include "hurdmalloc.h" /* XXX */
27
28extern void __mach_init (void);
29extern void __libc_init (int, char **, char **);
30
31void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
32void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
33
34
35/* Things that want to be run before _hurd_init or much anything else.
36 Importantly, these are called before anything tries to use malloc. */
37DEFINE_HOOK (_hurd_preinit_hook, (void));
38
39
40static void
41init1 (int argc, char *arg0, ...)
42{
43 char **argv = &arg0;
44 char **envp = &argv[argc + 1];
45 struct hurd_startup_data *d;
46
47 __environ = envp;
48 while (*envp)
49 ++envp;
50 d = (void *) ++envp;
51
52 /* If we are the bootstrap task started by the kernel,
53 then after the environment pointers there is no Hurd
54 data block; the argument strings start there. */
55 if ((void *) d != argv[0])
56 {
57 _hurd_init_dtable = d->dtable;
58 _hurd_init_dtablesize = d->dtablesize;
59
60 {
61 /* Check if the stack we are now on is different from
62 the one described by _hurd_stack_{base,size}. */
63
64 char dummy;
65 const vm_address_t newsp = (vm_address_t) &dummy;
66
67 if (d->stack_size != 0 && (newsp < d->stack_base ||
68 newsp - d->stack_base > d->stack_size))
69 /* The new stack pointer does not intersect with the
70 stack the exec server set up for us, so free that stack. */
71 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
72 }
73 }
74
75 if (__hurd_threadvar_stack_mask == 0)
76 {
77 /* We are not using cthreads, so we will have just a single allocated
78 area for the per-thread variables of the main user thread. */
79 unsigned long int i;
80 __hurd_threadvar_stack_offset
81 = (unsigned long int) malloc (__hurd_threadvar_max *
82 sizeof (unsigned long int));
83 if (__hurd_threadvar_stack_offset == 0)
84 __libc_fatal ("Can't allocate single-threaded per-thread variables.");
85 for (i = 0; i < __hurd_threadvar_max; ++i)
86 ((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0;
87 }
88
89 if ((void *) d != argv[0] && (d->portarray || d->intarray))
90 /* Initialize library data structures, start signal processing, etc. */
91 _hurd_init (d->flags, argv,
92 d->portarray, d->portarraysize,
93 d->intarray, d->intarraysize);
94
95 __libc_init (argc, argv, __environ);
96}
97
a1a9d215 98static void
d819080c 99init (int *data)
99b306dc
RM
100{
101 int argc = *data;
102 char **argv = (void *) (data + 1);
103 char **envp = &argv[argc + 1];
104 struct hurd_startup_data *d;
105
106 __environ = envp;
107 while (*envp)
108 ++envp;
109 d = (void *) ++envp;
110
111 /* The user might have defined a value for this, to get more variables.
112 Otherwise it will be zero on startup. We must make sure it is set
113 properly before before cthreads initialization, so cthreads can know
114 how much space to leave for thread variables. */
115 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
116 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
117
a1a9d215
RM
118
119 /* After possibly switching stacks, call `init1' (above) with the user
120 code as the return address, and the argument data immediately above
121 that on the stack. */
122
99b306dc
RM
123 if (_cthread_init_routine)
124 {
125 /* Initialize cthreads, which will allocate us a new stack to run on. */
126 void *newsp = (*_cthread_init_routine) ();
a2fe9c76
RM
127 struct hurd_startup_data *od;
128
99b306dc
RM
129 /* Copy the argdata from the old stack to the new one. */
130 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
a2fe9c76
RM
131 (char *) d - (char *) data);
132
133 /* Set up the Hurd startup data block immediately following
134 the argument and environment pointers on the new stack. */
135 od = (newsp + ((char *) d - (char *) data));
136 if ((void *) argv[0] == d)
137 /* We were started up by the kernel with arguments on the stack.
138 There is no Hurd startup data, so zero the block. */
139 memset (od, 0, sizeof *od);
140 else
141 /* Copy the Hurd startup data block to the new stack. */
142 *od = *d;
143
a1a9d215
RM
144 /* Push the user code address on the top of the new stack. It will
145 be the return address for `init1'; we will jump there with NEWSP
146 as the stack pointer. */
d819080c
RM
147 *--(int *) newsp = data[-1];
148 ((void **) data)[-1] = &&switch_stacks;
a1a9d215 149 /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
d819080c
RM
150 by function return. */
151 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
152 }
a1a9d215
RM
153 else
154 {
d819080c
RM
155 /* The argument data is just above the stack frame we will unwind by
156 returning. Mutate our own return address to run the code below. */
157 int usercode = data[-1];
158 ((void **) data)[-1] = &&call_init1;
159 /* Force USERCODE into %eax and &init1 into %ecx, which are not
160 restored by function return. */
161 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
99b306dc 162 }
d819080c
RM
163
164 return;
165
166 switch_stacks:
167 /* Our return address was redirected to here, so at this point our stack
168 is unwound and callers' registers restored. Only %ecx and %eax are
169 call-clobbered and thus still have the values we set just above.
170 Fetch from there the new stack pointer we will run on, and jmp to the
171 run-time address of `init1'; when it returns, it will run the user
172 code with the argument data at the top of the stack. */
173 asm volatile ("movl %eax, %esp; jmp *%ecx");
174 /* NOTREACHED */
175
176 call_init1:
177 /* As in the stack-switching case, at this point our stack is unwound and
178 callers' registers restored, and only %ecx and %eax communicate values
179 from the lines above. In this case we have stashed in %eax the user
180 code return address. Push it on the top of the stack so it acts as
181 init1's return address, and then jump there. */
182 asm volatile ("pushl %eax; jmp *%ecx");
183 /* NOTREACHED */
99b306dc
RM
184}
185
186
187#ifdef PIC
188/* This function is called to initialize the shared C library.
189 It is called just before the user _start code from i386/elf/start.S,
190 with the stack set up as that code gets it. */
191
a1a9d215
RM
192/* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
193 pointer in the dynamic section based solely on that. It is convention
194 for this function to be in the `.init' section, but the symbol name is
195 the only thing that really matters!! */
196/*void _init (int argc, ...) __attribute__ ((unused, section (".init")));*/
99b306dc 197
a1a9d215
RM
198void
199_init (int argc, ...)
99b306dc
RM
200{
201 /* Initialize data structures so we can do RPCs. */
202 __mach_init ();
203
204 RUN_HOOK (_hurd_preinit_hook, ());
205
d819080c 206 init (&argc);
99b306dc
RM
207}
208#endif
209
210
211void
a1a9d215 212__libc_init_first (int argc __attribute__ ((unused)), ...)
99b306dc
RM
213{
214#ifndef PIC
215 void doinit (int *data)
216 {
d819080c
RM
217 /* This function gets called with the argument data at TOS. */
218 void doinit1 (int argc, ...)
219 {
220 init (&argc);
221 }
222
223 /* Push the user return address after the argument data, and then
224 jump to `doinit1' (above), so it is as if __libc_init_first's
225 caller had called `doinit1' with the argument data already on the
226 stack. */
227 *--data = (&argc)[-1];
228 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
229 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
230 "jmp *%1" : : "r" (data), "r" (&doinit1));
231 /* NOTREACHED */
99b306dc
RM
232 }
233
234 /* Initialize data structures so we can do RPCs. */
235 __mach_init ();
236
237 RUN_HOOK (_hurd_preinit_hook, ());
238
239 _hurd_startup ((void **) &argc, &doinit);
240#endif
241}