| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Initial program startup for running under the GNU Hurd. | 
|  | 2 | Copyright (C) 1991-2016 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 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. | 
|  | 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 | Lesser General Public License for more details. | 
|  | 14 |  | 
|  | 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/>.  */ | 
|  | 18 |  | 
|  | 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> | 
|  | 32 |  | 
|  | 33 | mach_port_t *_hurd_init_dtable; | 
|  | 34 | mach_msg_type_number_t _hurd_init_dtablesize; | 
|  | 35 |  | 
|  | 36 | extern void __mach_init (void); | 
|  | 37 |  | 
|  | 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 |  | 
|  | 55 |  | 
|  | 56 | void | 
|  | 57 | _hurd_startup (void **argptr, void (*main) (intptr_t *data)) | 
|  | 58 | { | 
|  | 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; | 
|  | 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); | 
|  | 75 |  | 
|  | 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 |  | 
|  | 85 | argslen = envlen = 0; | 
|  | 86 | data.dtablesize = data.portarraysize = data.intarraysize = 0; | 
|  | 87 |  | 
|  | 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); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS)) | 
|  | 102 | { | 
|  | 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.  */ | 
|  | 108 |  | 
|  | 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; | 
|  | 116 | } | 
|  | 117 | else | 
|  | 118 | { | 
|  | 119 | /* Turn the block of null-separated strings we were passed for the | 
|  | 120 | arguments and environment into vectors of pointers to strings.  */ | 
|  | 121 |  | 
|  | 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); | 
|  | 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.  */ | 
|  | 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); | 
|  | 137 |  | 
|  | 138 | /* There was some environment.  */ | 
|  | 139 | envp = &argv[argc + 1]; | 
|  | 140 | __argz_extract (env, envlen, envp); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 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 | { | 
|  | 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; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | { | 
|  | 179 | struct hurd_startup_data *d = (void *) &envp[envc + 1]; | 
|  | 180 |  | 
|  | 181 | if ((void *) d != argv[0]) | 
|  | 182 | { | 
|  | 183 | *d = data; | 
|  | 184 | _hurd_init_dtable = d->dtable; | 
|  | 185 | _hurd_init_dtablesize = d->dtablesize; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | (*main) (argcptr); | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | /* Should never get here.  */ | 
|  | 192 | LOSE; | 
|  | 193 | abort (); | 
|  | 194 | } |