1 /* Initial program startup for running under the GNU Hurd.
2 Copyright (C) 1991, 1992, 1993, 1994, 1995 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 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.
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.
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
25 #include <hurd/exec.h>
27 #include <hurd/threadvar.h>
30 #include "set-hooks.h"
31 #include "hurdmalloc.h" /* XXX */
32 #include "hurdstartup.h"
34 mach_port_t *_hurd_init_dtable;
35 mach_msg_type_number_t _hurd_init_dtablesize;
37 unsigned int __hurd_threadvar_max;
38 unsigned long int __hurd_threadvar_stack_mask;
39 unsigned long int __hurd_threadvar_stack_offset;
41 /* These are set up by _hurdsig_init. */
42 unsigned long int __hurd_sigthread_stack_base;
43 unsigned long int __hurd_sigthread_stack_end;
44 unsigned long int *__hurd_sigthread_variables;
46 extern void __mach_init (void);
48 int _hurd_split_args (char *, size_t, char **);
51 /* Entry point. This is the first thing in the text segment.
53 The exec server started the initial thread in our task with this spot the
54 PC, and a stack that is presumably big enough. We do basic Mach
55 initialization so mig-generated stubs work, and then do an exec_startup
56 RPC on our bootstrap port, to which the exec server responds with the
57 information passed in the exec call, as well as our original bootstrap
58 port, and the base address and size of the preallocated stack.
60 If using cthreads, we are given a new stack by cthreads initialization and
61 deallocate the stack set up by the exec server. On the new stack we call
62 `start1' (above) to do the rest of the startup work. Since the stack may
63 disappear out from under us in a machine-dependent way, we use a pile of
64 static variables to communicate the information from exec_startup to start1.
65 This is unfortunate but preferable to machine-dependent frobnication to copy
66 the state from the old stack to the new one. */
70 _hurd_startup (void **argptr, void (*main) (int *data))
73 mach_port_t in_bootstrap;
75 mach_msg_type_number_t argslen, envlen;
76 struct hurd_startup_data data;
81 if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
85 if (in_bootstrap != MACH_PORT_NULL)
87 /* Call the exec server on our bootstrap port and
88 get all our standard information from it. */
91 data.dtablesize = data.portarraysize = data.intarraysize = 0;
93 err = __exec_startup (in_bootstrap,
94 &data.stack_base, &data.stack_size,
95 &data.flags, &args, &argslen, &env, &envlen,
96 &data.dtable, &data.dtablesize,
97 &data.portarray, &data.portarraysize,
98 &data.intarray, &data.intarraysize);
99 __mach_port_deallocate (__mach_task_self (), in_bootstrap);
102 if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
104 /* Either we have no bootstrap port, or the RPC to the exec server
105 failed, or whoever started us up passed the flag saying args are
106 on the stack. Try to snarf the args in the canonical Mach way.
107 Hopefully either they will be on the stack as expected, or the
108 stack will be zeros so we don't crash. */
110 argcptr = (int *) argptr;
112 argv = (char **) &argcptr[1];
113 envp = &argv[argc + 1];
120 /* Turn the block of null-separated strings we were passed for the
121 arguments and environment into vectors of pointers to strings. */
123 /* Count up the arguments so we can allocate ARGV. */
124 argc = _hurd_split_args (args, argslen, NULL);
125 /* Count up the environment variables so we can allocate ENVP. */
126 envc = _hurd_split_args (env, envlen, NULL);
128 /* There were some arguments. Allocate space for the vectors of
129 pointers and fill them in. We allocate the space for the
130 environment pointers immediately after the argv pointers because
131 the ELF ABI will expect it. */
132 argcptr = __alloca (sizeof (int) +
133 (argc + 1 + envc + 1) * sizeof (char *) +
134 sizeof (struct hurd_startup_data));
136 argv = (void *) (argcptr + 1);
137 _hurd_split_args (args, argslen, argv);
139 /* There was some environment. */
140 envp = &argv[argc + 1];
141 _hurd_split_args (env, envlen, envp);
144 if (err || in_bootstrap == MACH_PORT_NULL)
146 /* Either we have no bootstrap port, or the RPC to the exec server
147 failed. Set all our other variables to have empty information. */
151 argslen = envlen = 0;
154 data.portarray = NULL;
155 data.portarraysize = 0;
156 data.intarray = NULL;
157 data.intarraysize = 0;
159 else if ((void *) &envp[envc + 1] == argv[0])
161 /* The arguments arrived on the stack from the kernel, but our
162 protocol requires some space after them for a `struct
163 hurd_startup_data'. Move them. */
167 char *argv[argc + 1];
168 char *envp[envc + 1];
169 struct hurd_startup_data data;
170 } *args = alloca (sizeof *args);
171 if ((void *) &args[1] == (void *) argcptr)
172 args = alloca (-((char *) &args->data - (char *) args));
173 memmove (args, argcptr, (char *) &args->data - (char *) args);
174 argcptr = (void *) args;
180 struct hurd_startup_data *d = (void *) &envp[envc + 1];
182 if ((void *) d != argv[0])
185 _hurd_init_dtable = d->dtable;
186 _hurd_init_dtablesize = d->dtablesize;
188 /* XXX hardcoded kludge until exec_startup changes */
190 extern void _start();
191 vm_address_t page = 0;
193 if (__vm_read (__mach_task_self (),
194 0x08000000, __vm_page_size, &page, &size) == 0)
196 const Elf32_Ehdr *ehdr = (const void *) 0x08000000;
197 d->phdr = 0x08000000 + ehdr->e_phoff;
198 d->phdrsz = ehdr->e_phnum * ehdr->e_phentsize;
199 d->user_entry = ehdr->e_entry;
200 __vm_deallocate (__mach_task_self (), page, size);
203 d->user_entry = (Elf32_Addr) &_start;
210 /* Should never get here. */
215 /* Split ARGSLEN bytes at ARGS into words, breaking at NUL characters. If
216 ARGV is not a null pointer, store a pointer to the start of each word in
217 ARGV[n], and null-terminate ARGV. Return the number of words split. */
220 _hurd_split_args (char *args, size_t argslen, char **argv)
228 char *end = memchr (p, '\0', n);
235 /* The last argument is unterminated. */