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