1/*
2 * Copyright 2019, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12#include <sel4runtime.h>
13#include <sel4runtime/start.h>
14
15/*
16 * This function is simply passed a pointer to the inital stack from the
17 * C runtime entrypoint.
18 *
19 * The stack has the following structure:
20 *
21 * * argument count,
22 * * array of argument pointers,
23 * * an empty string,
24 * * array of environment pointers,
25 * * a null terminator,
26 * * array of auxiliary vector entries,
27 * * an 'zero' auxiliary vector, then
28 * * unspecified data.
29 */
30void __sel4_start_c(void const *stack)
31{
32    // First word on the stack is argument count.
33    unsigned long argc = *((unsigned long const *) stack);
34
35    // Second word on the stack is the start of the argument vector.
36    char const *const *argv = &((char const * const *) stack)[1];
37
38    // The environment pointer vector follows after the argv.
39    char const *const *envp = &argv[argc + 1];
40    int envc = 0;
41    while (envp[envc] != SEL4RUNTIME_NULL) {
42        envc++;
43    }
44
45
46    // The auxiliary vector follows the environment pointer vector.
47    auxv_t const *auxv = (void const *)(&envp[envc + 1]);
48
49    __sel4runtime_start_main(main, argc, argv, envp, auxv);
50}
51