1998-11-22 Roland McGrath <roland@baalperazim.frob.com>

* sysdeps/mach/hurd/i386/init-first.c (init): Provide temporary storage 
for the per-thread variables of the main user thread to make it 
possible to use malloc as soon as _hurd_preinit_hook has been run. 
For cthreads, copy values to new stack from there. 
For non-cthreads, malloc threadvar array here and copy from temp space. 
(init1): No longer initialize threadvars here. 
(doinit1): Made static void at top level. 
(init): Folded into [PIC] _init or [!PIC] doinit1, since GCC cannot 
inline a function that uses dynamic auto arrays.
This commit is contained in:
Roland McGrath 1998-11-22 17:32:14 +00:00
parent b17f830fe1
commit 54509b04ce
1 changed files with 126 additions and 109 deletions

View File

@ -105,20 +105,6 @@ init1 (int argc, char *arg0, ...)
} }
} }
if (__hurd_threadvar_stack_mask == 0)
{
/* We are not using cthreads, so we will have just a single allocated
area for the per-thread variables of the main user thread. */
unsigned long int i;
__hurd_threadvar_stack_offset
= (unsigned long int) malloc (__hurd_threadvar_max *
sizeof (unsigned long int));
if (__hurd_threadvar_stack_offset == 0)
__libc_fatal ("Can't allocate single-threaded per-thread variables.");
for (i = 0; i < __hurd_threadvar_max; ++i)
((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0;
}
if ((void *) d != argv[0] && (d->portarray || d->intarray)) if ((void *) d != argv[0] && (d->portarray || d->intarray))
/* Initialize library data structures, start signal processing, etc. */ /* Initialize library data structures, start signal processing, etc. */
_hurd_init (d->flags, argv, _hurd_init (d->flags, argv,
@ -133,14 +119,48 @@ init1 (int argc, char *arg0, ...)
} }
static inline void #ifdef PIC
init (int *data) /* This function is called to initialize the shared C library.
It is called just before the user _start code from i386/elf/start.S,
with the stack set up as that code gets it. */
/* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
pointer in the dynamic section based solely on that. It is convention
for this function to be in the `.init' section, but the symbol name is
the only thing that really matters!! */
void
_init (int argc, ...)
{ {
int argc = *data; /* Initialize data structures so we can do RPCs. */
__mach_init ();
RUN_HOOK (_hurd_preinit_hook, ());
#else
/* In statically-linked programs, this function is
called from _hurd_stack_setup (below). */
static void
doinit1 (int argc, ...)
{
#endif
/* This block used to be a separate inline function.
But GCC refuses to inline a function that uses alloca
or dynamically-sized auto arrays. */
{
int *const data = &argc;
char **argv = (void *) (data + 1); char **argv = (void *) (data + 1);
char **envp = &argv[argc + 1]; char **envp = &argv[argc + 1];
struct hurd_startup_data *d; struct hurd_startup_data *d;
unsigned long int threadvars[__hurd_threadvar_max];
/* Provide temporary storage for thread-specific variables on the startup
stack so the cthreads initialization code can use them for malloc et al,
or so we can use malloc below for the real threadvars array. */
memset (threadvars, 0, sizeof threadvars);
__hurd_threadvar_stack_offset = (unsigned long int) threadvars;
__environ = envp; __environ = envp;
while (*envp) while (*envp)
++envp; ++envp;
@ -160,10 +180,18 @@ init (int *data)
if (_cthread_init_routine) if (_cthread_init_routine)
{ {
/* Initialize cthreads, which will allocate us a new stack to run on. */ void *newsp;
void *newsp = (*_cthread_init_routine) ();
struct hurd_startup_data *od; struct hurd_startup_data *od;
/* Initialize cthreads, which will allocate us a new
stack to run on. */
newsp = (*_cthread_init_routine) ();
/* Copy per-thread variables from that temporary
area onto the new cthread stack. */
memcpy (__hurd_threadvar_location_from_sp (0, newsp),
threadvars, sizeof threadvars);
/* Copy the argdata from the old stack to the new one. */ /* Copy the argdata from the old stack to the new one. */
newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data, newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
(char *) d - (char *) data); (char *) d - (char *) data);
@ -190,9 +218,23 @@ init (int *data)
} }
else else
{ {
/* We are not using cthreads, so we will have just a single allocated
area for the per-thread variables of the main user thread. */
void *array;
int usercode;
array = malloc (sizeof threadvars);
if (array == NULL)
__libc_fatal ("Can't allocate single-threaded thread variables.");
/* Copy per-thread variables from the temporary array into the
newly malloc'd space. */
memcpy (array, threadvars, sizeof threadvars);
__hurd_threadvar_stack_offset = (unsigned long int) array;
/* The argument data is just above the stack frame we will unwind by /* The argument data is just above the stack frame we will unwind by
returning. Mutate our own return address to run the code below. */ returning. Mutate our own return address to run the code below. */
int usercode = data[-1]; usercode = data[-1];
((void **) data)[-1] = &&call_init1; ((void **) data)[-1] = &&call_init1;
/* Force USERCODE into %eax and &init1 into %ecx, which are not /* Force USERCODE into %eax and &init1 into %ecx, which are not
restored by function return. */ restored by function return. */
@ -202,48 +244,27 @@ init (int *data)
return; return;
switch_stacks: switch_stacks:
/* Our return address was redirected to here, so at this point our stack /* Our return address was redirected to here, so at this point our
is unwound and callers' registers restored. Only %ecx and %eax are stack is unwound and callers' registers restored. Only %ecx and
call-clobbered and thus still have the values we set just above. %eax are call-clobbered and thus still have the values we set just
Fetch from there the new stack pointer we will run on, and jmp to the above. Fetch from there the new stack pointer we will run on, and
run-time address of `init1'; when it returns, it will run the user jmp to the run-time address of `init1'; when it returns, it will run
code with the argument data at the top of the stack. */ the user code with the argument data at the top of the stack. */
asm volatile ("movl %eax, %esp; jmp *%ecx"); asm volatile ("movl %eax, %esp; jmp *%ecx");
/* NOTREACHED */ /* NOTREACHED */
call_init1: call_init1:
/* As in the stack-switching case, at this point our stack is unwound and /* As in the stack-switching case, at this point our stack is unwound
callers' registers restored, and only %ecx and %eax communicate values and callers' registers restored, and only %ecx and %eax communicate
from the lines above. In this case we have stashed in %eax the user values from the lines above. In this case we have stashed in %eax
code return address. Push it on the top of the stack so it acts as the user code return address. Push it on the top of the stack so it
init1's return address, and then jump there. */ acts as init1's return address, and then jump there. */
asm volatile ("pushl %eax; jmp *%ecx"); asm volatile ("pushl %eax; jmp *%ecx");
/* NOTREACHED */ /* NOTREACHED */
}
} }
#ifdef PIC
/* This function is called to initialize the shared C library.
It is called just before the user _start code from i386/elf/start.S,
with the stack set up as that code gets it. */
/* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
pointer in the dynamic section based solely on that. It is convention
for this function to be in the `.init' section, but the symbol name is
the only thing that really matters!! */
void
_init (int argc, ...)
{
/* Initialize data structures so we can do RPCs. */
__mach_init ();
RUN_HOOK (_hurd_preinit_hook, ());
init (&argc);
}
#endif
void void
__libc_init_first (int argc, char **argv, char **envp) __libc_init_first (int argc, char **argv, char **envp)
#ifdef PIC #ifdef PIC
@ -264,10 +285,6 @@ _hurd_stack_setup (int argc __attribute__ ((unused)), ...)
void doinit (int *data) void doinit (int *data)
{ {
/* This function gets called with the argument data at TOS. */ /* This function gets called with the argument data at TOS. */
void doinit1 (int argc, ...)
{
init (&argc);
}
/* Push the user return address after the argument data, and then /* Push the user return address after the argument data, and then
jump to `doinit1' (above), so it is as if __libc_init_first's jump to `doinit1' (above), so it is as if __libc_init_first's