]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/hurdstartup.c
Update to 2.1.x development version
[thirdparty/glibc.git] / hurd / hurdstartup.c
CommitLineData
11872325 1/* Initial program startup for running under the GNU Hurd.
c84142e8
UD
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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. */
11872325
RM
19
20#include <errno.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <hurd.h>
f44f9c33 25#include <hurd/exec_startup.h>
11872325
RM
26#include <sysdep.h>
27#include <hurd/threadvar.h>
28#include <unistd.h>
99b306dc 29#include <elf.h>
11872325
RM
30#include "set-hooks.h"
31#include "hurdmalloc.h" /* XXX */
99b306dc 32#include "hurdstartup.h"
75cd5204 33#include <argz.h>
11872325
RM
34
35mach_port_t *_hurd_init_dtable;
36mach_msg_type_number_t _hurd_init_dtablesize;
37
38unsigned int __hurd_threadvar_max;
39unsigned long int __hurd_threadvar_stack_mask;
40unsigned long int __hurd_threadvar_stack_offset;
41
42/* These are set up by _hurdsig_init. */
43unsigned long int __hurd_sigthread_stack_base;
44unsigned long int __hurd_sigthread_stack_end;
45unsigned long int *__hurd_sigthread_variables;
46
11872325 47extern void __mach_init (void);
11872325 48
11872325
RM
49/* Entry point. This is the first thing in the text segment.
50
51 The exec server started the initial thread in our task with this spot the
52 PC, and a stack that is presumably big enough. We do basic Mach
53 initialization so mig-generated stubs work, and then do an exec_startup
54 RPC on our bootstrap port, to which the exec server responds with the
55 information passed in the exec call, as well as our original bootstrap
56 port, and the base address and size of the preallocated stack.
57
58 If using cthreads, we are given a new stack by cthreads initialization and
59 deallocate the stack set up by the exec server. On the new stack we call
60 `start1' (above) to do the rest of the startup work. Since the stack may
61 disappear out from under us in a machine-dependent way, we use a pile of
62 static variables to communicate the information from exec_startup to start1.
63 This is unfortunate but preferable to machine-dependent frobnication to copy
64 the state from the old stack to the new one. */
65
99b306dc 66
11872325 67void
99b306dc 68_hurd_startup (void **argptr, void (*main) (int *data))
11872325
RM
69{
70 error_t err;
71 mach_port_t in_bootstrap;
99b306dc
RM
72 char *args, *env;
73 mach_msg_type_number_t argslen, envlen;
74 struct hurd_startup_data data;
75 char **argv, **envp;
76 int argc, envc;
77 int *argcptr;
68dbb3a6
UD
78 vm_address_t addr;
79
80 /* Attempt to map page zero redzoned before we receive any RPC
81 data that might get allocated there. We can ignore errors. */
82 addr = 0;
83 __vm_map (__mach_task_self (),
84 &addr, __vm_page_size, 0, 0, MACH_PORT_NULL, 0, 1,
85 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
11872325 86
11872325
RM
87 if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
88 &in_bootstrap))
89 LOSE;
90
91 if (in_bootstrap != MACH_PORT_NULL)
92 {
93 /* Call the exec server on our bootstrap port and
94 get all our standard information from it. */
95
99b306dc
RM
96 argslen = envlen = 0;
97 data.dtablesize = data.portarraysize = data.intarraysize = 0;
11872325 98
f44f9c33
RM
99 err = __exec_startup_get_info (in_bootstrap,
100 &data.user_entry,
101 &data.phdr, &data.phdrsz,
102 &data.stack_base, &data.stack_size,
103 &data.flags,
104 &args, &argslen,
105 &env, &envlen,
106 &data.dtable, &data.dtablesize,
107 &data.portarray, &data.portarraysize,
108 &data.intarray, &data.intarraysize);
11872325
RM
109 __mach_port_deallocate (__mach_task_self (), in_bootstrap);
110 }
111
60092701 112 if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
11872325
RM
113 {
114 /* Either we have no bootstrap port, or the RPC to the exec server
60092701
RM
115 failed, or whoever started us up passed the flag saying args are
116 on the stack. Try to snarf the args in the canonical Mach way.
11872325 117 Hopefully either they will be on the stack as expected, or the
60092701 118 stack will be zeros so we don't crash. */
11872325 119
a2fe9c76
RM
120 argcptr = (int *) argptr;
121 argc = argcptr[0];
122 argv = (char **) &argcptr[1];
123 envp = &argv[argc + 1];
124 envc = 0;
125 while (envp[envc])
126 ++envc;
11872325
RM
127 }
128 else
11872325 129 {
60092701
RM
130 /* Turn the block of null-separated strings we were passed for the
131 arguments and environment into vectors of pointers to strings. */
132
99b306dc 133 /* Count up the arguments so we can allocate ARGV. */
75cd5204 134 argc = __argz_count (args, argslen);
99b306dc 135 /* Count up the environment variables so we can allocate ENVP. */
75cd5204 136 envc = __argz_count (env, envlen);
99b306dc
RM
137
138 /* There were some arguments. Allocate space for the vectors of
139 pointers and fill them in. We allocate the space for the
140 environment pointers immediately after the argv pointers because
141 the ELF ABI will expect it. */
142 argcptr = __alloca (sizeof (int) +
143 (argc + 1 + envc + 1) * sizeof (char *) +
144 sizeof (struct hurd_startup_data));
145 *argcptr = argc;
146 argv = (void *) (argcptr + 1);
395565f0 147 __argz_extract (args, argslen, argv);
99b306dc
RM
148
149 /* There was some environment. */
150 envp = &argv[argc + 1];
395565f0 151 __argz_extract (env, envlen, envp);
11872325
RM
152 }
153
60092701
RM
154 if (err || in_bootstrap == MACH_PORT_NULL)
155 {
156 /* Either we have no bootstrap port, or the RPC to the exec server
157 failed. Set all our other variables to have empty information. */
158
159 data.flags = 0;
160 args = env = NULL;
161 argslen = envlen = 0;
162 data.dtable = NULL;
163 data.dtablesize = 0;
164 data.portarray = NULL;
165 data.portarraysize = 0;
166 data.intarray = NULL;
167 data.intarraysize = 0;
168 }
169 else if ((void *) &envp[envc + 1] == argv[0])
170 {
171 /* The arguments arrived on the stack from the kernel, but our
172 protocol requires some space after them for a `struct
173 hurd_startup_data'. Move them. */
174 struct
175 {
176 int count;
177 char *argv[argc + 1];
178 char *envp[envc + 1];
179 struct hurd_startup_data data;
180 } *args = alloca (sizeof *args);
181 if ((void *) &args[1] == (void *) argcptr)
182 args = alloca (-((char *) &args->data - (char *) args));
183 memmove (args, argcptr, (char *) &args->data - (char *) args);
184 argcptr = (void *) args;
185 argv = args->argv;
186 envp = args->envp;
187 }
188
99b306dc
RM
189 {
190 struct hurd_startup_data *d = (void *) &envp[envc + 1];
191
a2fe9c76
RM
192 if ((void *) d != argv[0])
193 {
194 *d = data;
195 _hurd_init_dtable = d->dtable;
196 _hurd_init_dtablesize = d->dtablesize;
a2fe9c76 197 }
99b306dc
RM
198
199 (*main) (argcptr);
200 }
11872325
RM
201
202 /* Should never get here. */
203 LOSE;
99b306dc 204 abort ();
11872325 205}