Blame hurd/hurdstartup.c

Packit 6c4009
/* Initial program startup for running under the GNU Hurd.
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/exec_startup.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <elf.h>
Packit 6c4009
#include <set-hooks.h>
Packit 6c4009
#include "hurdstartup.h"
Packit 6c4009
#include <argz.h>
Packit 6c4009
Packit 6c4009
mach_port_t *_hurd_init_dtable;
Packit 6c4009
mach_msg_type_number_t _hurd_init_dtablesize;
Packit 6c4009
Packit 6c4009
extern void __mach_init (void);
Packit 6c4009
Packit 6c4009
/* Entry point.  This is the first thing in the text segment.
Packit 6c4009
Packit 6c4009
   The exec server started the initial thread in our task with this spot the
Packit 6c4009
   PC, and a stack that is presumably big enough.  We do basic Mach
Packit 6c4009
   initialization so mig-generated stubs work, and then do an exec_startup
Packit 6c4009
   RPC on our bootstrap port, to which the exec server responds with the
Packit 6c4009
   information passed in the exec call, as well as our original bootstrap
Packit 6c4009
   port, and the base address and size of the preallocated stack.
Packit 6c4009
Packit 6c4009
   If using cthreads, we are given a new stack by cthreads initialization and
Packit 6c4009
   deallocate the stack set up by the exec server.  On the new stack we call
Packit 6c4009
   `start1' (above) to do the rest of the startup work.  Since the stack may
Packit 6c4009
   disappear out from under us in a machine-dependent way, we use a pile of
Packit 6c4009
   static variables to communicate the information from exec_startup to start1.
Packit 6c4009
   This is unfortunate but preferable to machine-dependent frobnication to copy
Packit 6c4009
   the state from the old stack to the new one.  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_hurd_startup (void **argptr, void (*main) (intptr_t *data))
Packit 6c4009
{
Packit 6c4009
  error_t err;
Packit 6c4009
  mach_port_t in_bootstrap;
Packit 6c4009
  char *args, *env;
Packit 6c4009
  mach_msg_type_number_t argslen, envlen;
Packit 6c4009
  struct hurd_startup_data data;
Packit 6c4009
  char **argv, **envp;
Packit 6c4009
  int argc, envc;
Packit 6c4009
  intptr_t *argcptr;
Packit 6c4009
  vm_address_t addr;
Packit 6c4009
Packit 6c4009
  /* Attempt to map page zero redzoned before we receive any RPC
Packit 6c4009
     data that might get allocated there.  We can ignore errors.  */
Packit 6c4009
  addr = 0;
Packit 6c4009
  __vm_map (__mach_task_self (),
Packit 6c4009
	    &addr, __vm_page_size, 0, 0, MACH_PORT_NULL, 0, 1,
Packit 6c4009
	    VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
Packit 6c4009
Packit 6c4009
  if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
Packit 6c4009
				     &in_bootstrap))
Packit 6c4009
    LOSE;
Packit 6c4009
Packit 6c4009
  if (in_bootstrap != MACH_PORT_NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Call the exec server on our bootstrap port and
Packit 6c4009
	 get all our standard information from it.  */
Packit 6c4009
Packit 6c4009
      argslen = envlen = 0;
Packit 6c4009
      data.dtablesize = data.portarraysize = data.intarraysize = 0;
Packit 6c4009
Packit 6c4009
      err = __exec_startup_get_info (in_bootstrap,
Packit 6c4009
				     &data.user_entry,
Packit 6c4009
				     &data.phdr, &data.phdrsz,
Packit 6c4009
				     &data.stack_base, &data.stack_size,
Packit 6c4009
				     &data.flags,
Packit 6c4009
				     &args, &argslen,
Packit 6c4009
				     &env, &envlen,
Packit 6c4009
				     &data.dtable, &data.dtablesize,
Packit 6c4009
				     &data.portarray, &data.portarraysize,
Packit 6c4009
				     &data.intarray, &data.intarraysize);
Packit 6c4009
      __mach_port_deallocate (__mach_task_self (), in_bootstrap);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
Packit 6c4009
    {
Packit 6c4009
      /* Either we have no bootstrap port, or the RPC to the exec server
Packit 6c4009
	 failed, or whoever started us up passed the flag saying args are
Packit 6c4009
	 on the stack.  Try to snarf the args in the canonical Mach way.
Packit 6c4009
	 Hopefully either they will be on the stack as expected, or the
Packit 6c4009
	 stack will be zeros so we don't crash.  */
Packit 6c4009
Packit 6c4009
      argcptr = (intptr_t *) argptr;
Packit 6c4009
      argc = argcptr[0];
Packit 6c4009
      argv = (char **) &argcptr[1];
Packit 6c4009
      envp = &argv[argc + 1];
Packit 6c4009
      envc = 0;
Packit 6c4009
      while (envp[envc])
Packit 6c4009
	++envc;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Turn the block of null-separated strings we were passed for the
Packit 6c4009
	 arguments and environment into vectors of pointers to strings.  */
Packit 6c4009
Packit 6c4009
      /* Count up the arguments so we can allocate ARGV.  */
Packit 6c4009
      argc = __argz_count (args, argslen);
Packit 6c4009
      /* Count up the environment variables so we can allocate ENVP.  */
Packit 6c4009
      envc = __argz_count (env, envlen);
Packit 6c4009
Packit 6c4009
      /* There were some arguments.  Allocate space for the vectors of
Packit 6c4009
	 pointers and fill them in.  We allocate the space for the
Packit 6c4009
	 environment pointers immediately after the argv pointers because
Packit 6c4009
	 the ELF ABI will expect it.  */
Packit 6c4009
      argcptr = __alloca (sizeof (intptr_t) +
Packit 6c4009
			  (argc + 1 + envc + 1) * sizeof (char *) +
Packit 6c4009
			  sizeof (struct hurd_startup_data));
Packit 6c4009
      *argcptr = argc;
Packit 6c4009
      argv = (void *) (argcptr + 1);
Packit 6c4009
      __argz_extract (args, argslen, argv);
Packit 6c4009
Packit 6c4009
      /* There was some environment.  */
Packit 6c4009
      envp = &argv[argc + 1];
Packit 6c4009
      __argz_extract (env, envlen, envp);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (err || in_bootstrap == MACH_PORT_NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Either we have no bootstrap port, or the RPC to the exec server
Packit 6c4009
	 failed.  Set all our other variables to have empty information.  */
Packit 6c4009
Packit 6c4009
      data.flags = 0;
Packit 6c4009
      args = env = NULL;
Packit 6c4009
      argslen = envlen = 0;
Packit 6c4009
      data.dtable = NULL;
Packit 6c4009
      data.dtablesize = 0;
Packit 6c4009
      data.portarray = NULL;
Packit 6c4009
      data.portarraysize = 0;
Packit 6c4009
      data.intarray = NULL;
Packit 6c4009
      data.intarraysize = 0;
Packit 6c4009
    }
Packit 6c4009
  else if ((void *) &envp[envc + 1] == argv[0])
Packit 6c4009
    {
Packit 6c4009
      /* The arguments arrived on the stack from the kernel, but our
Packit 6c4009
	 protocol requires some space after them for a `struct
Packit 6c4009
	 hurd_startup_data'.  Move them.  */
Packit 6c4009
      struct
Packit 6c4009
	{
Packit 6c4009
	  intptr_t count;
Packit 6c4009
	  char *argv[argc + 1];
Packit 6c4009
	  char *envp[envc + 1];
Packit 6c4009
	  struct hurd_startup_data data;
Packit 6c4009
	} *args = alloca (sizeof *args);
Packit 6c4009
      if ((void *) &args[1] == (void *) argcptr)
Packit 6c4009
	args = alloca (-((char *) &args->data - (char *) args));
Packit 6c4009
      memmove (args, argcptr, (char *) &args->data - (char *) args);
Packit 6c4009
      argcptr = (void *) args;
Packit 6c4009
      argv = args->argv;
Packit 6c4009
      envp = args->envp;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    struct hurd_startup_data *d = (void *) &envp[envc + 1];
Packit 6c4009
Packit 6c4009
    if ((void *) d != argv[0])
Packit 6c4009
      {
Packit 6c4009
	*d = data;
Packit 6c4009
	_hurd_init_dtable = d->dtable;
Packit 6c4009
	_hurd_init_dtablesize = d->dtablesize;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    (*main) (argcptr);
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Should never get here.  */
Packit 6c4009
  LOSE;
Packit 6c4009
  abort ();
Packit 6c4009
}