1 /* Initialization code run first thing by the ELF startup code. For Mips/Hurd.
2 Copyright (C) 1996,1997,1998,2000,01,02,03 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 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.
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.
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
24 #include "hurdstartup.h"
25 #include "set-hooks.h"
26 #include "hurdmalloc.h" /* XXX */
28 extern void __mach_init (void);
29 extern void __init_misc (int, char **, char **);
30 #ifdef USE_NONOPTION_FLAGS
31 extern void __getopt_clean_environment (char **);
34 extern void _dl_non_dynamic_init (void) internal_function
;
36 extern void __libc_global_ctors (void);
38 unsigned int __hurd_threadvar_max
;
39 unsigned long int __hurd_threadvar_stack_offset
;
40 unsigned long int __hurd_threadvar_stack_mask
;
42 int __libc_multiple_libcs attribute_hidden
= 1;
44 int __libc_argc attribute_hidden
;
45 char **__libc_argv attribute_hidden
;
47 void *(*_cthread_init_routine
) (void); /* Returns new SP to use. */
48 void (*_cthread_exit_routine
) (int status
) __attribute__ ((__noreturn__
));
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));
56 init1 (int argc
, char *arg0
, ...)
59 char **envp
= &argv
[argc
+ 1];
60 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 _dl_non_dynamic_init ();
115 __init_misc (argc
, argv
, __environ
);
117 #ifdef USE_NONOPTION_FLAGS
118 /* This is a hack to make the special getopt in GNU libc working. */
119 __getopt_clean_environment (envp
);
123 __libc_global_ctors ();
133 char **argv
= (void *) (data
+ 1);
134 char **envp
= &argv
[argc
+ 1];
135 struct hurd_startup_data
*d
;
142 /* The user might have defined a value for this, to get more variables.
143 Otherwise it will be zero on startup. We must make sure it is set
144 properly before before cthreads initialization, so cthreads can know
145 how much space to leave for thread variables. */
146 if (__hurd_threadvar_max
< _HURD_THREADVAR_MAX
)
147 __hurd_threadvar_max
= _HURD_THREADVAR_MAX
;
150 /* After possibly switching stacks, call `init1' (above) with the user
151 code as the return address, and the argument data immediately above
152 that on the stack. */
154 if (_cthread_init_routine
)
156 /* Initialize cthreads, which will allocate us a new stack to run on. */
157 void *newsp
= (*_cthread_init_routine
) ();
158 struct hurd_startup_data
*od
;
160 /* Copy the argdata from the old stack to the new one. */
161 newsp
= memcpy (newsp
- ((char *) &d
[1] - (char *) data
), data
,
162 (char *) d
- (char *) data
);
164 /* Set up the Hurd startup data block immediately following
165 the argument and environment pointers on the new stack. */
166 od
= (newsp
+ ((char *) d
- (char *) data
));
167 if ((void *) argv
[0] == d
)
168 /* We were started up by the kernel with arguments on the stack.
169 There is no Hurd startup data, so zero the block. */
170 memset (od
, 0, sizeof *od
);
172 /* Copy the Hurd startup data block to the new stack. */
175 /* Push the user code address on the top of the new stack. It will
176 be the return address for `init1'; we will jump there with NEWSP
177 as the stack pointer. */
181 /* The argument data is just above the stack frame we will unwind by
183 return (void *) data
;
189 /* This function is called to initialize the shared C library.
190 It is called just before the user _start code from mips/elf/start.S,
191 with the stack set up as that code gets it. */
193 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
194 pointer in the dynamic section based solely on that. It is convention
195 for this function to be in the `.init' section, but the symbol name is
196 the only thing that really matters!! */
197 /*void _init (int argc, ...) __attribute__ ((unused, section (".init")));*/
201 .section .init,\"ax\",@progbits\n\
204 .type _init,@function\n\
216 move $16, $29 # Save the old stack pointer to s0 ($16)\n\
217 daddu $4, $29, 4*8\n\
219 # Restore saved registers from the old stack.\n\
223 move $29, $2 # set new sp to SP\n\
236 .section .init,\"ax\",@progbits\n\
239 .type _init,@function\n\
251 move $16, $29 # Save the old stack pointer to s0 ($16)\n\
254 # Restore saved registers from the old stack.\n\
258 move $29, $2 # set new sp to SP\n\
274 /* Initialize data structures so we can do RPCs. */
277 RUN_HOOK (_hurd_preinit_hook
, ());
282 void __libc_init_first (int argc
, ...)
288 /* An assembler code wrapping c function __init. */
301 move $29, $2 # set new sp to SP\n\
322 move $29, $2 # set new sp to SP\n\
333 /* An assembler code wrapping c function ___libc_init_first.
334 ___libc_init_first does an RPC call to flush cache to put doinit
335 function on the stack, so we should call __mach_init first in
341 .globl __libc_init_first\n\
342 __libc_init_first:\n\
350 j ___libc_init_first\n\
356 .globl __libc_init_first\n\
357 __libc_init_first:\n\
365 j ___libc_init_first\n\
370 ___libc_init_first (int return_addr
, int argc
, ...)
372 void doinit (int *data
)
375 /* This function gets called with the argument data at TOS. */
376 void doinit1 (int argc
, ...)
381 extern void init (int *data
);
383 /* Push the user return address after the argument data, and then
384 jump to `doinit1' (above), so it is as if __libc_init_first's
385 caller had called `init' with the argument data already on the
387 *--data
= return_addr
;
390 asm volatile ("ld $31, 0(%0)\n" /* Load the original return address. */
391 "daddu $29, %0, 8\n" /* Switch to new outermost stack. */
393 "jr %1" : : "r" (data
), "r" (&init
));
395 asm volatile ("lw $31, 0(%0)\n" /* Load the original return address. */
396 "addu $29, %0, 4\n" /* Switch to new outermost stack. */
398 "jr %1" : : "r" (data
), "r" (&init
));
404 /* Initialize data structures so we can do RPCs. */
408 RUN_HOOK (_hurd_preinit_hook
, ());
410 _hurd_startup ((void **) &argc
, &doinit
);
412 (void) &___libc_init_first
;