[3eddaff] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Martin Decky
|
---|
[3eddaff] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[b2951e2] | 27 | */
|
---|
[a46da63] | 28 |
|
---|
| 29 | /** @addtogroup lc Libc
|
---|
[433131d] | 30 | * @brief HelenOS C library
|
---|
[a46da63] | 31 | * @{
|
---|
| 32 | * @}
|
---|
| 33 | */
|
---|
[433131d] | 34 |
|
---|
[a46da63] | 35 | /** @addtogroup libc generic
|
---|
| 36 | * @ingroup lc
|
---|
[b2951e2] | 37 | * @{
|
---|
| 38 | */
|
---|
[433131d] | 39 |
|
---|
[b2951e2] | 40 | /** @file
|
---|
[433131d] | 41 | */
|
---|
[3eddaff] | 42 |
|
---|
[153c7a29] | 43 | #include <errno.h>
|
---|
[3eddaff] | 44 | #include <libc.h>
|
---|
[47b7006] | 45 | #include <stdlib.h>
|
---|
[fa23560] | 46 | #include <tls.h>
|
---|
[bc1f1c2] | 47 | #include <fibril.h>
|
---|
[47b7006] | 48 | #include <task.h>
|
---|
[c98e6ee] | 49 | #include <loader/pcb.h>
|
---|
[5126f80] | 50 | #include <vfs/vfs.h>
|
---|
| 51 | #include <vfs/inbox.h>
|
---|
[e26a4633] | 52 | #include "private/libc.h"
|
---|
[fc5f7a8] | 53 | #include "private/async.h"
|
---|
[47b7006] | 54 | #include "private/malloc.h"
|
---|
| 55 | #include "private/io.h"
|
---|
[d54b303] | 56 |
|
---|
| 57 | #ifdef FUTEX_UPGRADABLE
|
---|
| 58 | #include <rcu.h>
|
---|
| 59 | #endif
|
---|
[07d960a] | 60 |
|
---|
[7fb3f1c] | 61 | #ifdef CONFIG_RTLD
|
---|
[8a1fb09] | 62 | #include <rtld/rtld.h>
|
---|
[7fb3f1c] | 63 | #endif
|
---|
[1ea99cc] | 64 |
|
---|
[d54b303] | 65 |
|
---|
[47b7006] | 66 | static bool env_setup = false;
|
---|
[350514c] | 67 |
|
---|
[c98e6ee] | 68 | void __main(void *pcb_ptr)
|
---|
[a46da63] | 69 | {
|
---|
[153c7a29] | 70 | /* Initialize user task run-time environment */
|
---|
| 71 | __malloc_init();
|
---|
| 72 |
|
---|
[91e4567] | 73 | /* Save the PCB pointer */
|
---|
| 74 | __pcb = (pcb_t *) pcb_ptr;
|
---|
| 75 |
|
---|
| 76 | #ifdef CONFIG_RTLD
|
---|
| 77 | if (__pcb != NULL && __pcb->rtld_runtime != NULL) {
|
---|
| 78 | runtime_env = (rtld_t *) __pcb->rtld_runtime;
|
---|
[153c7a29] | 79 | } else {
|
---|
| 80 | if (rtld_init_static() != EOK)
|
---|
| 81 | abort();
|
---|
[91e4567] | 82 | }
|
---|
| 83 | #endif
|
---|
[47b7006] | 84 |
|
---|
[433131d] | 85 | fibril_t *fibril = fibril_setup();
|
---|
[47b7006] | 86 | if (fibril == NULL)
|
---|
| 87 | abort();
|
---|
| 88 |
|
---|
[433131d] | 89 | __tcb_set(fibril->tcb);
|
---|
[663c5a6] | 90 |
|
---|
[433131d] | 91 |
|
---|
[d54b303] | 92 | #ifdef FUTEX_UPGRADABLE
|
---|
| 93 | rcu_register_fibril();
|
---|
| 94 | #endif
|
---|
| 95 |
|
---|
| 96 | __async_init();
|
---|
[9a3b469] | 97 |
|
---|
[47b7006] | 98 | /* The basic run-time environment is setup */
|
---|
| 99 | env_setup = true;
|
---|
| 100 |
|
---|
[433131d] | 101 | int argc;
|
---|
| 102 | char **argv;
|
---|
| 103 |
|
---|
[47b7006] | 104 | /*
|
---|
| 105 | * Get command line arguments and initialize
|
---|
| 106 | * standard input and output
|
---|
| 107 | */
|
---|
[c98e6ee] | 108 | if (__pcb == NULL) {
|
---|
| 109 | argc = 0;
|
---|
| 110 | argv = NULL;
|
---|
[bb9ec2d] | 111 | __stdio_init();
|
---|
[c98e6ee] | 112 | } else {
|
---|
| 113 | argc = __pcb->argc;
|
---|
| 114 | argv = __pcb->argv;
|
---|
[bb9ec2d] | 115 | __inbox_init(__pcb->inbox, __pcb->inbox_entries);
|
---|
| 116 | __stdio_init();
|
---|
[5126f80] | 117 | vfs_root_set(inbox_get("root"));
|
---|
[7591b27d] | 118 | (void) chdir(__pcb->cwd);
|
---|
[c98e6ee] | 119 | }
|
---|
[433131d] | 120 |
|
---|
[47b7006] | 121 | /*
|
---|
| 122 | * Run main() and set task return value
|
---|
| 123 | * according the result
|
---|
| 124 | */
|
---|
| 125 | int retval = main(argc, argv);
|
---|
| 126 | exit(retval);
|
---|
| 127 | }
|
---|
[7114d83] | 128 |
|
---|
[47b7006] | 129 | void exit(int status)
|
---|
| 130 | {
|
---|
| 131 | if (env_setup) {
|
---|
| 132 | __stdio_done();
|
---|
| 133 | task_retval(status);
|
---|
[4d11204] | 134 | fibril_teardown(__tcb_get()->fibril_data, false);
|
---|
[47b7006] | 135 | }
|
---|
| 136 |
|
---|
| 137 | __SYSCALL1(SYS_TASK_EXIT, false);
|
---|
| 138 |
|
---|
| 139 | /* Unreachable */
|
---|
| 140 | while (1);
|
---|
[3eddaff] | 141 | }
|
---|
| 142 |
|
---|
[47b7006] | 143 | void abort(void)
|
---|
[a46da63] | 144 | {
|
---|
[47b7006] | 145 | __SYSCALL1(SYS_TASK_EXIT, true);
|
---|
| 146 |
|
---|
| 147 | /* Unreachable */
|
---|
| 148 | while (1);
|
---|
[3eddaff] | 149 | }
|
---|
[b2951e2] | 150 |
|
---|
[a46da63] | 151 | /** @}
|
---|
[b2951e2] | 152 | */
|
---|