1 /* Initial program startup for running under the GNU Hurd.
2 Copyright (C) 1991-2019 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, see
17 <http://www.gnu.org/licenses/>. */
24 #include <hurd/exec_startup.h>
28 #include <set-hooks.h>
29 #include "hurdstartup.h"
32 mach_port_t *_hurd_init_dtable;
33 mach_msg_type_number_t _hurd_init_dtablesize;
35 extern void __mach_init (void);
37 /* Entry point. This is the first thing in the text segment.
39 The exec server started the initial thread in our task with this spot the
40 PC, and a stack that is presumably big enough. We do basic Mach
41 initialization so mig-generated stubs work, and then do an exec_startup
42 RPC on our bootstrap port, to which the exec server responds with the
43 information passed in the exec call, as well as our original bootstrap
44 port, and the base address and size of the preallocated stack.
46 If using cthreads, we are given a new stack by cthreads initialization and
47 deallocate the stack set up by the exec server. On the new stack we call
48 `start1' (above) to do the rest of the startup work. Since the stack may
49 disappear out from under us in a machine-dependent way, we use a pile of
50 static variables to communicate the information from exec_startup to start1.
51 This is unfortunate but preferable to machine-dependent frobnication to copy
52 the state from the old stack to the new one. */
56 _hurd_startup (void **argptr, void (*main) (intptr_t *data))
59 mach_port_t in_bootstrap;
61 mach_msg_type_number_t argslen, envlen;
62 struct hurd_startup_data data;
68 /* Attempt to map page zero redzoned before we receive any RPC
69 data that might get allocated there. We can ignore errors. */
71 __vm_map (__mach_task_self (),
72 &addr, __vm_page_size, 0, 0, MACH_PORT_NULL, 0, 1,
73 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
75 if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
79 if (in_bootstrap != MACH_PORT_NULL)
81 /* Call the exec server on our bootstrap port and
82 get all our standard information from it. */
85 data.dtablesize = data.portarraysize = data.intarraysize = 0;
87 err = __exec_startup_get_info (in_bootstrap,
89 &data.phdr, &data.phdrsz,
90 &data.stack_base, &data.stack_size,
94 &data.dtable, &data.dtablesize,
95 &data.portarray, &data.portarraysize,
96 &data.intarray, &data.intarraysize);
97 __mach_port_deallocate (__mach_task_self (), in_bootstrap);
100 if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
102 /* Either we have no bootstrap port, or the RPC to the exec server
103 failed, or whoever started us up passed the flag saying args are
104 on the stack. Try to snarf the args in the canonical Mach way.
105 Hopefully either they will be on the stack as expected, or the
106 stack will be zeros so we don't crash. */
108 argcptr = (intptr_t *) argptr;
110 argv = (char **) &argcptr[1];
111 envp = &argv[argc + 1];
118 /* Turn the block of null-separated strings we were passed for the
119 arguments and environment into vectors of pointers to strings. */
121 /* Count up the arguments so we can allocate ARGV. */
122 argc = __argz_count (args, argslen);
123 /* Count up the environment variables so we can allocate ENVP. */
124 envc = __argz_count (env, envlen);
126 /* There were some arguments. Allocate space for the vectors of
127 pointers and fill them in. We allocate the space for the
128 environment pointers immediately after the argv pointers because
129 the ELF ABI will expect it. */
130 argcptr = __alloca (sizeof (intptr_t) +
131 (argc + 1 + envc + 1) * sizeof (char *) +
132 sizeof (struct hurd_startup_data));
134 argv = (void *) (argcptr + 1);
135 __argz_extract (args, argslen, argv);
137 /* There was some environment. */
138 envp = &argv[argc + 1];
139 __argz_extract (env, envlen, envp);
142 if (err || in_bootstrap == MACH_PORT_NULL)
144 /* Either we have no bootstrap port, or the RPC to the exec server
145 failed. Set all our other variables to have empty information. */
149 argslen = envlen = 0;
152 data.portarray = NULL;
153 data.portarraysize = 0;
154 data.intarray = NULL;
155 data.intarraysize = 0;
157 else if ((void *) &envp[envc + 1] == argv[0])
159 /* The arguments arrived on the stack from the kernel, but our
160 protocol requires some space after them for a `struct
161 hurd_startup_data'. Move them. */
165 char *argv[argc + 1];
166 char *envp[envc + 1];
167 struct hurd_startup_data data;
168 } *args = alloca (sizeof *args);
169 if ((void *) &args[1] == (void *) argcptr)
170 args = alloca (-((char *) &args->data - (char *) args));
171 memmove (args, argcptr, (char *) &args->data - (char *) args);
172 argcptr = (void *) args;
178 struct hurd_startup_data *d = (void *) &envp[envc + 1];
180 if ((void *) d != argv[0])
183 _hurd_init_dtable = d->dtable;
184 _hurd_init_dtablesize = d->dtablesize;
190 /* Should never get here. */