Rely on HP_TIMING_AVAIL implies HP_SMALL_TIMING_AVAIL
[glibc.git] / hurd / hurdstartup.c
blob01c9e145663119ff2749c1629f88a6302aa0e997
1 /* Initial program startup for running under the GNU Hurd.
2 Copyright (C) 1991-2014 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/>. */
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <hurd.h>
24 #include <hurd/exec_startup.h>
25 #include <sysdep.h>
26 #include <hurd/threadvar.h>
27 #include <unistd.h>
28 #include <elf.h>
29 #include <set-hooks.h>
30 #include "hurdstartup.h"
31 #include <argz.h>
33 mach_port_t *_hurd_init_dtable;
34 mach_msg_type_number_t _hurd_init_dtablesize;
36 extern void __mach_init (void);
38 /* Entry point. This is the first thing in the text segment.
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.
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. */
56 void
57 _hurd_startup (void **argptr, void (*main) (intptr_t *data))
59 error_t err;
60 mach_port_t in_bootstrap;
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;
66 intptr_t *argcptr;
67 vm_address_t addr;
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);
76 if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
77 &in_bootstrap))
78 LOSE;
80 if (in_bootstrap != MACH_PORT_NULL)
82 /* Call the exec server on our bootstrap port and
83 get all our standard information from it. */
85 argslen = envlen = 0;
86 data.dtablesize = data.portarraysize = data.intarraysize = 0;
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);
98 __mach_port_deallocate (__mach_task_self (), in_bootstrap);
101 if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
103 /* Either we have no bootstrap port, or the RPC to the exec server
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.
106 Hopefully either they will be on the stack as expected, or the
107 stack will be zeros so we don't crash. */
109 argcptr = (intptr_t *) argptr;
110 argc = argcptr[0];
111 argv = (char **) &argcptr[1];
112 envp = &argv[argc + 1];
113 envc = 0;
114 while (envp[envc])
115 ++envc;
117 else
119 /* Turn the block of null-separated strings we were passed for the
120 arguments and environment into vectors of pointers to strings. */
122 /* Count up the arguments so we can allocate ARGV. */
123 argc = __argz_count (args, argslen);
124 /* Count up the environment variables so we can allocate ENVP. */
125 envc = __argz_count (env, envlen);
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. */
131 argcptr = __alloca (sizeof (intptr_t) +
132 (argc + 1 + envc + 1) * sizeof (char *) +
133 sizeof (struct hurd_startup_data));
134 *argcptr = argc;
135 argv = (void *) (argcptr + 1);
136 __argz_extract (args, argslen, argv);
138 /* There was some environment. */
139 envp = &argv[argc + 1];
140 __argz_extract (env, envlen, envp);
143 if (err || in_bootstrap == MACH_PORT_NULL)
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. */
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;
158 else if ((void *) &envp[envc + 1] == argv[0])
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
165 intptr_t count;
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;
179 struct hurd_startup_data *d = (void *) &envp[envc + 1];
181 if ((void *) d != argv[0])
183 *d = data;
184 _hurd_init_dtable = d->dtable;
185 _hurd_init_dtablesize = d->dtablesize;
188 (*main) (argcptr);
191 /* Should never get here. */
192 LOSE;
193 abort ();