hurdstartup.c
上传用户:xiaoji1011
上传日期:2020-02-08
资源大小:19981k
文件大小:6k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* Initial program startup for running under the GNU Hurd.
  2.    Copyright (C) 1991,92,93,94,95,96,97,98,2002 Free Software Foundation, Inc.
  3.    This file is part of the GNU C Library.
  4.    The GNU C Library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Lesser General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2.1 of the License, or (at your option) any later version.
  8.    The GNU C Library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Lesser General Public License for more details.
  12.    You should have received a copy of the GNU Lesser General Public
  13.    License along with the GNU C Library; if not, write to the Free
  14.    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15.    02111-1307 USA.  */
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <hurd.h>
  21. #include <hurd/exec_startup.h>
  22. #include <sysdep.h>
  23. #include <hurd/threadvar.h>
  24. #include <unistd.h>
  25. #include <elf.h>
  26. #include <set-hooks.h>
  27. #include "hurdstartup.h"
  28. #include <argz.h>
  29. mach_port_t *_hurd_init_dtable;
  30. mach_msg_type_number_t _hurd_init_dtablesize;
  31. extern void __mach_init (void);
  32. /* Entry point.  This is the first thing in the text segment.
  33.    The exec server started the initial thread in our task with this spot the
  34.    PC, and a stack that is presumably big enough.  We do basic Mach
  35.    initialization so mig-generated stubs work, and then do an exec_startup
  36.    RPC on our bootstrap port, to which the exec server responds with the
  37.    information passed in the exec call, as well as our original bootstrap
  38.    port, and the base address and size of the preallocated stack.
  39.    If using cthreads, we are given a new stack by cthreads initialization and
  40.    deallocate the stack set up by the exec server.  On the new stack we call
  41.    `start1' (above) to do the rest of the startup work.  Since the stack may
  42.    disappear out from under us in a machine-dependent way, we use a pile of
  43.    static variables to communicate the information from exec_startup to start1.
  44.    This is unfortunate but preferable to machine-dependent frobnication to copy
  45.    the state from the old stack to the new one.  */
  46. void
  47. _hurd_startup (void **argptr, void (*main) (intptr_t *data))
  48. {
  49.   error_t err;
  50.   mach_port_t in_bootstrap;
  51.   char *args, *env;
  52.   mach_msg_type_number_t argslen, envlen;
  53.   struct hurd_startup_data data;
  54.   char **argv, **envp;
  55.   int argc, envc;
  56.   intptr_t *argcptr;
  57.   vm_address_t addr;
  58.   /* Attempt to map page zero redzoned before we receive any RPC
  59.      data that might get allocated there.  We can ignore errors.  */
  60.   addr = 0;
  61.   __vm_map (__mach_task_self (),
  62.     &addr, __vm_page_size, 0, 0, MACH_PORT_NULL, 0, 1,
  63.     VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
  64.   if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
  65.      &in_bootstrap))
  66.     LOSE;
  67.   if (in_bootstrap != MACH_PORT_NULL)
  68.     {
  69.       /* Call the exec server on our bootstrap port and
  70.  get all our standard information from it.  */
  71.       argslen = envlen = 0;
  72.       data.dtablesize = data.portarraysize = data.intarraysize = 0;
  73.       err = __exec_startup_get_info (in_bootstrap,
  74.      &data.user_entry,
  75.      &data.phdr, &data.phdrsz,
  76.      &data.stack_base, &data.stack_size,
  77.      &data.flags,
  78.      &args, &argslen,
  79.      &env, &envlen,
  80.      &data.dtable, &data.dtablesize,
  81.      &data.portarray, &data.portarraysize,
  82.      &data.intarray, &data.intarraysize);
  83.       __mach_port_deallocate (__mach_task_self (), in_bootstrap);
  84.     }
  85.   if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
  86.     {
  87.       /* Either we have no bootstrap port, or the RPC to the exec server
  88.  failed, or whoever started us up passed the flag saying args are
  89.  on the stack.  Try to snarf the args in the canonical Mach way.
  90.  Hopefully either they will be on the stack as expected, or the
  91.  stack will be zeros so we don't crash.  */
  92.       argcptr = (intptr_t *) argptr;
  93.       argc = argcptr[0];
  94.       argv = (char **) &argcptr[1];
  95.       envp = &argv[argc + 1];
  96.       envc = 0;
  97.       while (envp[envc])
  98. ++envc;
  99.     }
  100.   else
  101.     {
  102.       /* Turn the block of null-separated strings we were passed for the
  103.  arguments and environment into vectors of pointers to strings.  */
  104.       /* Count up the arguments so we can allocate ARGV.  */
  105.       argc = __argz_count (args, argslen);
  106.       /* Count up the environment variables so we can allocate ENVP.  */
  107.       envc = __argz_count (env, envlen);
  108.       /* There were some arguments.  Allocate space for the vectors of
  109.  pointers and fill them in.  We allocate the space for the
  110.  environment pointers immediately after the argv pointers because
  111.  the ELF ABI will expect it.  */
  112.       argcptr = __alloca (sizeof (intptr_t) +
  113.   (argc + 1 + envc + 1) * sizeof (char *) +
  114.   sizeof (struct hurd_startup_data));
  115.       *argcptr = argc;
  116.       argv = (void *) (argcptr + 1);
  117.       __argz_extract (args, argslen, argv);
  118.       /* There was some environment.  */
  119.       envp = &argv[argc + 1];
  120.       __argz_extract (env, envlen, envp);
  121.     }
  122.   if (err || in_bootstrap == MACH_PORT_NULL)
  123.     {
  124.       /* Either we have no bootstrap port, or the RPC to the exec server
  125.  failed.  Set all our other variables to have empty information.  */
  126.       data.flags = 0;
  127.       args = env = NULL;
  128.       argslen = envlen = 0;
  129.       data.dtable = NULL;
  130.       data.dtablesize = 0;
  131.       data.portarray = NULL;
  132.       data.portarraysize = 0;
  133.       data.intarray = NULL;
  134.       data.intarraysize = 0;
  135.     }
  136.   else if ((void *) &envp[envc + 1] == argv[0])
  137.     {
  138.       /* The arguments arrived on the stack from the kernel, but our
  139.  protocol requires some space after them for a `struct
  140.  hurd_startup_data'.  Move them.  */
  141.       struct
  142. {
  143.   intptr_t count;
  144.   char *argv[argc + 1];
  145.   char *envp[envc + 1];
  146.   struct hurd_startup_data data;
  147. } *args = alloca (sizeof *args);
  148.       if ((void *) &args[1] == (void *) argcptr)
  149. args = alloca (-((char *) &args->data - (char *) args));
  150.       memmove (args, argcptr, (char *) &args->data - (char *) args);
  151.       argcptr = (void *) args;
  152.       argv = args->argv;
  153.       envp = args->envp;
  154.     }
  155.   {
  156.     struct hurd_startup_data *d = (void *) &envp[envc + 1];
  157.     if ((void *) d != argv[0])
  158.       {
  159. *d = data;
  160. _hurd_init_dtable = d->dtable;
  161. _hurd_init_dtablesize = d->dtablesize;
  162.       }
  163.     (*main) (argcptr);
  164.   }
  165.   /* Should never get here.  */
  166.   LOSE;
  167.   abort ();
  168. }